API change -- Remove first ctx argument and add event mask to callback

There really does not exist a good reason for us sending a `uev_ctx_t *`
pointer as the first argument to event callbacks.  If the callback needs
to reference the context, it can find the ctx pointer in the `uev_t *w`.

This patch removes the first argument from all callbacks and also fixes
a slight bug in that the event loop never returned the actual event that
triggered the callback!

Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
This commit is contained in:
Joachim Nilsson committed 2015-06-09 13:10:20 +02:00
1 parent 9e7f6865fc
commit 1f9558059a
6 files changed
+30 -24

No files matched your search

+7 -6
View File
@@ -61,15 +61,16 @@ details, see [this article][4] at [lwn.net](http://lwn.net).
API API
--- ---
Here is the interface to [libuEv]. It handles three different types of The C interface to [libuEv] is very simple. It handles three different
events: I/O (files, sockets, message queues, etc.), timers, and signals. types of events: I/O (files, sockets, message queues, etc.), timers, and
signals. With a slight caveat on signals
```C ```C
/* Callback example, arg is passed from watcher's *_init() /* Callback example, arg is passed from watcher's *_init()
* w->fd holds the file descriptor, events is set by libuEv * w->fd holds the file descriptor, events is set by libuEv
* to indicate if any of UEV_READ and/or UEV_WRITE is ready. * to indicate if any of UEV_READ and/or UEV_WRITE is ready.
*/ */
void callback (uev_ctx_t *ctx, uev_t *w, void *arg, int events); void callback (uev_t *w, void *arg, int events);
/* Event loop functions, notice use of flags! */ /* Event loop functions, notice use of flags! */
int uev_init (uev_ctx_t *ctx); int uev_init (uev_ctx_t *ctx);
@@ -111,10 +112,10 @@ file descriptor to monitor, and an event callback function, by calling
the event type's `_init()` function with the `uev_ctx_t` context. the event type's `_init()` function with the `uev_ctx_t` context.
```C ```C
void cleanup_exit(uev_ctx_t *ctx, uev_t *w, void *arg, int events) void cleanup_exit(uev_t *w, void *arg, int events)
{ {
/* Graceful exit, with optional cleanup ... */ /* Graceful exit, with optional cleanup ... */
uev_exit(ctx); uev_exit(w->ctx);
} }
uev_t termw; uev_t termw;
@@ -186,7 +187,7 @@ struct js_event {
uint8_t number; /* axis/button number */ uint8_t number; /* axis/button number */
} e; } e;
static void joystick_cb(uev_ctx_t *ctx, uev_t *w, void *arg, int events) static void joystick_cb(uev_t *w, void *arg, int events)
{ {
read (w->fd, &e, sizeof(e)); read (w->fd, &e, sizeof(e));
+1 -1
View File
@@ -40,7 +40,7 @@ struct js_event {
uint8_t number; /* axis/button number */ uint8_t number; /* axis/button number */
} e; } e;
static void joystick_cb(uev_ctx_t *UNUSED(ctx), uev_t *w, void *UNUSED(arg), int UNUSED(events)) static void joystick_cb(uev_t *w, void *UNUSED(arg), int UNUSED(events))
{ {
if (read (w->fd, &e, sizeof(e)) < 0) if (read (w->fd, &e, sizeof(e)) < 0)
errx(errno, "Failed reading joystick event"); errx(errno, "Failed reading joystick event");
+10 -6
View File
@@ -69,13 +69,13 @@ static int callback(arg_t *arg, int event, void *foo)
return 42; return 42;
} }
static void sigsegv_cb(uev_ctx_t *UNUSED(ctx), uev_t *UNUSED(w), void *UNUSED(arg), int UNUSED(events)) static void sigsegv_cb(uev_t *UNUSED(w), void *UNUSED(arg), int UNUSED(events))
{ {
warnx("PID %d caused segfault.", getpid()); warnx("PID %d caused segfault.", getpid());
exit(-1); exit(-1);
} }
static void sigchld_cb(uev_ctx_t *UNUSED(ctx), uev_t *UNUSED(w), void *UNUSED(arg), int UNUSED(events)) static void sigchld_cb(uev_t *UNUSED(w), void *UNUSED(arg), int UNUSED(events))
{ {
pid_t pid = waitpid(-1, NULL, WNOHANG); pid_t pid = waitpid(-1, NULL, WNOHANG);
@@ -83,7 +83,7 @@ static void sigchld_cb(uev_ctx_t *UNUSED(ctx), uev_t *UNUSED(w), void *UNUSED(ar
warnx("PID %d exited, bye.", pid); warnx("PID %d exited, bye.", pid);
} }
static void work_cb(uev_ctx_t *UNUSED(ctx), uev_t *UNUSED(w), void *arg, int UNUSED(events)) static void work_cb(uev_t *UNUSED(w), void *arg, int UNUSED(events))
{ {
int status = 0; int status = 0;
pid_t pid; pid_t pid;
@@ -102,15 +102,17 @@ static void work_cb(uev_ctx_t *UNUSED(ctx), uev_t *UNUSED(w), void *arg, int UNU
if (WIFEXITED(status)) if (WIFEXITED(status))
printf("Child exited normally => %d\n", WEXITSTATUS(status)); printf("Child exited normally => %d\n", WEXITSTATUS(status));
else if (WCOREDUMP(status)) else if (WCOREDUMP(status))
printf("Child crashed!\n"); printf("Child crashed! %s\n", DO_SEGFAULT
? "As expected, everything is OK."
: "This should not happen!");
else else
printf("Child did not exit normally!\n"); printf("Child did not exit normally!\n");
} }
static void exit_cb(uev_ctx_t *ctx, uev_t *UNUSED(w), void *UNUSED(arg), int UNUSED(events)) static void exit_cb(uev_t *w, void *UNUSED(arg), int UNUSED(events))
{ {
printf("Process deadline reached, exiting!\n"); printf("Process deadline reached, exiting!\n");
uev_exit(ctx); uev_exit(w->ctx);
} }
int main(void) int main(void)
@@ -120,6 +122,8 @@ int main(void)
/* Initialize libuEv */ /* Initialize libuEv */
uev_init(&ctx); uev_init(&ctx);
/* Setup callbacks */
uev_signal_init(&ctx, &sigsegv_watcher, sigsegv_cb, NULL, SIGSEGV); uev_signal_init(&ctx, &sigsegv_watcher, sigsegv_cb, NULL, SIGSEGV);
uev_signal_init(&ctx, &sigchld_watcher, sigchld_cb, NULL, SIGCHLD); uev_signal_init(&ctx, &sigchld_watcher, sigchld_cb, NULL, SIGCHLD);
uev_timer_init(&ctx, &timeout_watcher, work_cb, &arg, 400, 0); uev_timer_init(&ctx, &timeout_watcher, work_cb, &arg, 400, 0);
+8 -8
View File
@@ -40,33 +40,33 @@ static int in, out;
static int period = 0; static int period = 0;
static uev_t *watchdog; static uev_t *watchdog;
static void lifetime_cb(uev_ctx_t *ctx, uev_t UNUSED(*w), void *arg, int UNUSED(events)) static void lifetime_cb(uev_t *w, void *arg, int UNUSED(events))
{ {
fprintf(stderr, "\nLifetime exceeded, program completed successfully! (arg:%p)\n", arg); fprintf(stderr, "\nLifetime exceeded, program completed successfully! (arg:%p)\n", arg);
uev_exit(ctx); uev_exit(w->ctx);
} }
/* The pipe watchdog, if it triggers we haven't received data in time. */ /* The pipe watchdog, if it triggers we haven't received data in time. */
static void timeout_cb(uev_ctx_t *ctx, uev_t *UNUSED(w), void *arg, int UNUSED(events)) static void timeout_cb(uev_t *w, void *arg, int UNUSED(events))
{ {
watchdog = NULL; watchdog = NULL;
fprintf(stderr, "\nTimeout exceeded %p\n", arg); fprintf(stderr, "\nTimeout exceeded %p\n", arg);
// uev_timer_stop(w); <-- No need to stop timers with period=0 :) // uev_timer_stop(w); <-- No need to stop timers with period=0 :)
uev_exit(ctx); uev_exit(w->ctx);
} }
static void periodic_task(uev_ctx_t UNUSED(*ctx), uev_t UNUSED(*w), void UNUSED(*arg), int UNUSED(events)) static void periodic_task(uev_t UNUSED(*w), void UNUSED(*arg), int UNUSED(events))
{ {
fprintf(stderr, "|"); fprintf(stderr, "|");
} }
static void signal_cb(uev_ctx_t *UNUSED(ctx), uev_t *w, void *UNUSED(arg), int UNUSED(events)) static void signal_cb(uev_t *w, void *UNUSED(arg), int UNUSED(events))
{ {
fprintf(stderr, w->signo == SIGINT ? "^Cv" : "^\v"); fprintf(stderr, w->signo == SIGINT ? "^Cv" : "^\v");
} }
static void pipe_read_cb(uev_ctx_t *UNUSED(ctx), uev_t UNUSED(*w), void UNUSED(*arg), int UNUSED(events)) static void pipe_read_cb(uev_t UNUSED(*w), void UNUSED(*arg), int UNUSED(events))
{ {
int cnt; int cnt;
char msg[50]; char msg[50];
@@ -80,7 +80,7 @@ static void pipe_read_cb(uev_ctx_t *UNUSED(ctx), uev_t UNUSED(*w), void UNUSED(*
fprintf(stderr, "%.*s.%d ", cnt, msg, cnt); fprintf(stderr, "%.*s.%d ", cnt, msg, cnt);
} }
static void pipe_write_cb(uev_ctx_t UNUSED(*ctx), uev_t *w, void *arg, int UNUSED(events)) static void pipe_write_cb(uev_t *w, void *arg, int UNUSED(events))
{ {
my_t *my = arg; my_t *my = arg;
char *msg = "TESTING"; char *msg = "TESTING";
+1 -1
View File
@@ -297,7 +297,7 @@ int uev_run(uev_ctx_t *ctx, int flags)
} }
if (w->cb) if (w->cb)
w->cb((struct uev *)ctx, w, w->arg); w->cb(w, w->arg, events[i].events & UEV_EVENT_MASK);
if (UEV_TIMER_TYPE == w->type) { if (UEV_TIMER_TYPE == w->type) {
if (!w->timeout) if (!w->timeout)
+3 -2
View File
@@ -38,6 +38,7 @@
#define UEV_NONE 0 #define UEV_NONE 0
#define UEV_READ EPOLLIN #define UEV_READ EPOLLIN
#define UEV_WRITE EPOLLOUT #define UEV_WRITE EPOLLOUT
#define UEV_EVENT_MASK (UEV_READ | UEV_WRITE)
/* Run flags */ /* Run flags */
#define UEV_ONCE 1 #define UEV_ONCE 1
@@ -73,7 +74,7 @@ typedef struct uev {
int events; int events;
/* Watcher callback with optional argument */ /* Watcher callback with optional argument */
void (*cb)(struct uev *, struct uev *, void *); void (*cb)(struct uev *, void *, int);
void *arg; void *arg;
/* Timer watchers, time in milliseconds */ /* Timer watchers, time in milliseconds */
@@ -85,7 +86,7 @@ typedef struct uev {
} uev_t; } uev_t;
/* Generic callback for watchers */ /* Generic callback for watchers */
typedef void (uev_cb_t)(uev_ctx_t *ctx, uev_t *w, void *arg, int events); typedef void (uev_cb_t)(uev_t *w, void *arg, int events);
/* Private methods, do not use directly! */ /* Private methods, do not use directly! */
int uev_watcher_init (uev_ctx_t *ctx, uev_t *w, uev_type_t type, uev_cb_t *cb, void *arg, int fd, int events); int uev_watcher_init (uev_ctx_t *ctx, uev_t *w, uev_type_t type, uev_cb_t *cb, void *arg, int fd, int events);