From b1de7658411af159b6b79f694a6a5a3504b378a6 Mon Sep 17 00:00:00 2001 From: Joachim Nilsson Date: Tue, 23 Jul 2013 02:25:46 +0200 Subject: [PATCH] Further cleanup, rename uev_io_t --> uev_watcher_t Signed-off-by: Joachim Nilsson --- io.c | 4 ++-- main.c | 29 ++++++++++++++--------------- test.c | 12 ++++++------ timer.c | 8 ++++---- uev.h | 53 +++++++++++++++++++++++++++-------------------------- 5 files changed, 53 insertions(+), 53 deletions(-) diff --git a/io.c b/io.c index e6abb2f..adffb58 100644 --- a/io.c +++ b/io.c @@ -36,7 +36,7 @@ * * @return The new I/O watcher, or %NULL if invalid pointers or out or memory. */ -uev_io_t *uev_io_create(uev_t *ctx, uev_cb_t *handler, void *data, int fd, uev_dir_t dir) +uev_watcher_t *uev_io_create(uev_t *ctx, uev_cb_t *handler, void *data, int fd, uev_dir_t dir) { return uev_watcher_create(ctx, UEV_FILE_TYPE, fd, dir, handler, data); } @@ -48,7 +48,7 @@ uev_io_t *uev_io_create(uev_t *ctx, uev_cb_t *handler, void *data, int fd, uev_d * * @return POSIX OK(0) or non-zero with @param errno set. */ -int uev_io_delete(uev_t *ctx, uev_io_t *w) +int uev_io_delete(uev_t *ctx, uev_watcher_t *w) { return uev_watcher_delete(ctx, w); } diff --git a/main.c b/main.c index 05f8713..7896996 100644 --- a/main.c +++ b/main.c @@ -31,25 +31,24 @@ #include "uev.h" /* Private to libuev, do not use directly! */ -uev_io_t *uev_watcher_create(uev_t *ctx, uev_type_t type, int fd, uev_dir_t dir, uev_cb_t *handler, void *data) +uev_watcher_t *uev_watcher_create(uev_t *ctx, uev_type_t type, int fd, uev_dir_t dir, uev_cb_t *cb, void *arg) { - uev_io_t *w; + uev_watcher_t *w; struct epoll_event ev; - if (!ctx || !handler) { + if (!ctx || !cb) { errno = EINVAL; return NULL; } - w = (uev_io_t *)calloc(1, sizeof(*w)); + w = (uev_watcher_t *)calloc(1, sizeof(*w)); if (!w) return NULL; - w->fd = fd; - w->dir = dir; - w->type = type; - w->handler = (void *)handler; - w->data = data; + w->fd = fd; + w->type = type; + w->cb = (void *)cb; + w->arg = arg; ev.events = dir == UEV_DIR_OUTBOUND ? EPOLLOUT : EPOLLIN; ev.data.ptr = w; @@ -64,7 +63,7 @@ uev_io_t *uev_watcher_create(uev_t *ctx, uev_type_t type, int fd, uev_dir_t dir, } /* Private to libuev, do not use directly! */ -int uev_watcher_delete(uev_t *ctx, uev_io_t *w) +int uev_watcher_delete(uev_t *ctx, uev_watcher_t *w) { if (!ctx || !w) { errno = EINVAL; @@ -114,7 +113,7 @@ uev_t *uev_ctx_create(void) void uev_ctx_delete(uev_t *ctx) { while (!LIST_EMPTY(&ctx->watchers)) { - uev_io_t *w = LIST_FIRST(&ctx->watchers); + uev_watcher_t *w = LIST_FIRST(&ctx->watchers); if (UEV_TIMER_TYPE == w->type) uev_timer_delete(ctx, w); @@ -135,7 +134,7 @@ void uev_ctx_delete(uev_t *ctx) int uev_run(uev_t *ctx) { int result = 0; - uev_io_t *w; + uev_watcher_t *w; if (!ctx) { errno = EINVAL; @@ -165,10 +164,10 @@ int uev_run(uev_t *ctx) } for (i = 0; i < nfds; i++) { - w = (uev_io_t *)events[i].data.ptr; + w = (uev_watcher_t *)events[i].data.ptr; - if (w->handler) - w->handler((struct uev *)ctx, w, w->data); + if (w->cb) + w->cb((struct uev *)ctx, w, w->arg); if (UEV_TIMER_TYPE == w->type) { uint64_t exp; diff --git a/test.c b/test.c index 68ab72f..d38612b 100644 --- a/test.c +++ b/test.c @@ -31,16 +31,16 @@ static int in, out; static int period = 0; -static uev_io_t *timer = NULL; +static uev_watcher_t *timer = NULL; -static void lifetime_cb(uev_t *ctx, uev_io_t *w __attribute__ ((unused)), void *data) +static void lifetime_cb(uev_t *ctx, uev_watcher_t *w __attribute__ ((unused)), void *data) { fprintf(stderr, "\nLifetime exceeded %p\n", data); uev_exit(ctx); } /* The pipe watchdog, if it triggers we haven't received data in time. */ -static void timeout_cb(uev_t *ctx, uev_io_t *w, void *data) +static void timeout_cb(uev_t *ctx, uev_watcher_t *w, void *data) { timer = NULL; fprintf(stderr, "\nTimeout exceeded %p\n", data); @@ -49,12 +49,12 @@ static void timeout_cb(uev_t *ctx, uev_io_t *w, void *data) uev_exit(ctx); } -static void periodic_task(uev_t *ctx __attribute__ ((unused)), uev_io_t *w __attribute__ ((unused)), void *data __attribute__ ((unused))) +static void periodic_task(uev_t *ctx __attribute__ ((unused)), uev_watcher_t *w __attribute__ ((unused)), void *data __attribute__ ((unused))) { fprintf(stderr, "|"); } -static void pipe_read_cb(uev_t *ctx, uev_io_t *w __attribute__ ((unused)), void *data __attribute__ ((unused))) +static void pipe_read_cb(uev_t *ctx, uev_watcher_t *w __attribute__ ((unused)), void *data __attribute__ ((unused))) { int cnt; char msg[50]; @@ -68,7 +68,7 @@ static void pipe_read_cb(uev_t *ctx, uev_io_t *w __attribute__ ((unused)), void fprintf(stderr, "%.*s.%d ", cnt, msg, cnt); } -static void pipe_write_cb(uev_t *ctx, uev_io_t *w __attribute__ ((unused)), void *data) +static void pipe_write_cb(uev_t *ctx, uev_watcher_t *w __attribute__ ((unused)), void *data) { int cnt = (int)(intptr_t)data; char *msg = "TESTING"; diff --git a/timer.c b/timer.c index 716f4a1..39b437f 100644 --- a/timer.c +++ b/timer.c @@ -58,10 +58,10 @@ static struct timespec msec2tspec(int msec) * * @return The new timer, or %NULL if invalid pointers or out or memory. */ -uev_io_t *uev_timer_create(uev_t *ctx, uev_cb_t *handler, void *data, int timeout, int period) +uev_watcher_t *uev_timer_create(uev_t *ctx, uev_cb_t *handler, void *data, int timeout, int period) { int fd; - uev_io_t *w; + uev_watcher_t *w; fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK); if (fd < 0) @@ -88,7 +88,7 @@ uev_io_t *uev_timer_create(uev_t *ctx, uev_cb_t *handler, void *data, int timeou * * @return POSIX OK(0) or non-zero with @param errno set. */ -int uev_timer_set(uev_t *ctx, uev_io_t *w, int timeout, int period) +int uev_timer_set(uev_t *ctx, uev_watcher_t *w, int timeout, int period) { struct itimerspec time; @@ -116,7 +116,7 @@ int uev_timer_set(uev_t *ctx, uev_io_t *w, int timeout, int period) * * @return POSIX OK(0) or non-zero with @param errno set. */ -int uev_timer_delete(uev_t *ctx, uev_io_t *w) +int uev_timer_delete(uev_t *ctx, uev_watcher_t *w) { if (!ctx || !w) { errno = EINVAL; diff --git a/uev.h b/uev.h index df6a47c..4260c20 100644 --- a/uev.h +++ b/uev.h @@ -47,48 +47,49 @@ typedef enum { } uev_type_t; /* I/O event watcher */ -typedef struct uev_io { - LIST_ENTRY(uev_io) link; +typedef struct uev_watcher { + LIST_ENTRY(uev_watcher) link; /* For queue.h linked list */ - uev_type_t type; + /* Common to all watchers */ + uev_type_t type; /* I/O, timer, or signal */ + int fd; /* File descriptor */ - int fd; ///< File descriptor - uev_dir_t dir; ///< Direction: in or out + /* Watcher callback with optional argument */ + void (*cb)(struct uev *, struct uev_watcher *, void *); + void *arg; - int timeout; ///< Timeout in milliseconds - int period; ///< Period time, in milliseconds - - void (*handler)(struct uev *, struct uev_io *, void *); - void *data; -} uev_io_t; + /* Timer watchers */ + int timeout; /* Timeout in milliseconds */ + int period; /* Period time, in milliseconds */ +} uev_watcher_t; /* Main libuev context type */ typedef struct { - int running; - int efd; /* For epoll() */ - LIST_HEAD(, uev_io) watchers; + int running; + int efd; /* For epoll() */ + LIST_HEAD(, uev_watcher) watchers; } uev_t; /* Generic callback for watchers */ -typedef void (uev_cb_t) (uev_t *ctx, uev_io_t *w, void *data); +typedef void (uev_cb_t) (uev_t *ctx, uev_watcher_t *w, void *data); /* Private methods, do not use directly! */ -uev_io_t *uev_watcher_create(uev_t *ctx, uev_type_t type, int fd, uev_dir_t dir, uev_cb_t *handler, void *data); -int uev_watcher_delete(uev_t *ctx, uev_io_t *w); +uev_watcher_t *uev_watcher_create(uev_t *ctx, uev_type_t type, int fd, uev_dir_t dir, uev_cb_t *cb, void *data); +int uev_watcher_delete(uev_t *ctx, uev_watcher_t *w); /* Public interface */ -uev_io_t *uev_io_create (uev_t *ctx, uev_cb_t *cb, void *data, int fd, uev_dir_t dir); -int uev_io_delete (uev_t *ctx, uev_io_t *w); +uev_watcher_t *uev_io_create (uev_t *ctx, uev_cb_t *cb, void *data, int fd, uev_dir_t dir); +int uev_io_delete (uev_t *ctx, uev_watcher_t *w); -int uev_timer_set (uev_t *ctx, uev_io_t *w, int timeout, int period); -uev_io_t *uev_timer_create (uev_t *ctx, uev_cb_t *cb, void *data, int timeout, int period); -int uev_timer_delete (uev_t *ctx, uev_io_t *w); +int uev_timer_set (uev_t *ctx, uev_watcher_t *w, int timeout, int period); +uev_watcher_t *uev_timer_create (uev_t *ctx, uev_cb_t *cb, void *data, int timeout, int period); +int uev_timer_delete (uev_t *ctx, uev_watcher_t *w); -uev_t *uev_ctx_create (void); -void uev_ctx_delete (uev_t *uev); +uev_t *uev_ctx_create (void); +void uev_ctx_delete (uev_t *uev); -int uev_run (uev_t *ctx); -void uev_exit (uev_t *ctx); +int uev_run (uev_t *ctx); +void uev_exit (uev_t *ctx); #endif /* LIBUEV_UEV_H_ */