From 66c5606e34bd290954b8a918028ede344bccc27a Mon Sep 17 00:00:00 2001 From: Joachim Nilsson Date: Sun, 4 Aug 2013 17:26:33 +0200 Subject: [PATCH] Add support for UEV_ONCE and UEV_NONBLOCK run flags to uev_run() Signed-off-by: Joachim Nilsson --- main.c | 18 ++++++++++++++---- test.c | 2 +- uev.h | 15 +++++++++------ 3 files changed, 24 insertions(+), 11 deletions(-) diff --git a/main.c b/main.c index eeae857..d4b5ff3 100644 --- a/main.c +++ b/main.c @@ -31,6 +31,8 @@ #include "uev.h" +#define UNUSED(arg) arg __attribute__ ((unused)) + /* Private to libuev, 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) { @@ -160,13 +162,14 @@ int uev_exit(uev_ctx_t *ctx) /** * Start the event loop - * @param ctx A valid libuev context + * @param ctx A valid libuev context + * @param flags A mask of %UEV_ONCE and %UEV_NONBLOCK, or zero * * @return POSIX OK(0) upon successful termination of the event loop, or non-zero on error. */ -int uev_run(uev_ctx_t *ctx) +int uev_run(uev_ctx_t *ctx, int flags) { - int result = 0; + int result = 0, timeout = -1; uev_t *w; if (!ctx || ctx->fd < 0) { @@ -174,6 +177,9 @@ int uev_run(uev_ctx_t *ctx) return -1; } + if (flags & UEV_NONBLOCK) + timeout = 0; + /* Start the event loop */ ctx->running = 1; @@ -187,7 +193,7 @@ int uev_run(uev_ctx_t *ctx) int i, nfds; struct epoll_event events[UEV_MAX_EVENTS]; - while ((nfds = epoll_wait(ctx->fd, events, UEV_MAX_EVENTS, -1)) < 0) { + while ((nfds = epoll_wait(ctx->fd, events, UEV_MAX_EVENTS, timeout)) < 0) { if (!ctx->running) break; @@ -230,11 +236,15 @@ int uev_run(uev_ctx_t *ctx) uev_timer_stop(w); } } + + if ((flags & UEV_ONCE) || LIST_EMPTY(&ctx->watchers)) + break; } return result; } + /** * Local Variables: * version-control: t diff --git a/test.c b/test.c index 0553533..ed4f6e0 100644 --- a/test.c +++ b/test.c @@ -136,7 +136,7 @@ int main(void) uev_timer_init(&ctx, &periodic, periodic_task, NULL, 200, 200); /* Start event loop */ - uev_run(&ctx); + uev_run(&ctx, 0); fprintf(stderr, "Period is %d must be 10: %s\n", period, 10 == period ? "OK" : "ERROR!"); if (10 != period) diff --git a/uev.h b/uev.h index 5733d59..3d85b02 100644 --- a/uev.h +++ b/uev.h @@ -31,12 +31,16 @@ #include /* Max. number of simulateneous events */ -#define UEV_MAX_EVENTS 10 +#define UEV_MAX_EVENTS 10 /* I/O events, for signals and timers revents will always be UEV_READ */ -#define UEV_NONE 0 -#define UEV_READ EPOLLIN -#define UEV_WRITE EPOLLOUT +#define UEV_NONE 0 +#define UEV_READ EPOLLIN +#define UEV_WRITE EPOLLOUT + +/* Run flags */ +#define UEV_ONCE 1 +#define UEV_NONBLOCK 2 /* I/O, timer, or signal watcher */ typedef enum { @@ -89,8 +93,7 @@ int uev_watcher_stop (uev_t *w); /* Public interface */ int uev_init (uev_ctx_t *ctx); int uev_exit (uev_ctx_t *ctx); - -int uev_run (uev_ctx_t *ctx); +int uev_run (uev_ctx_t *ctx, int flags); int uev_io_init (uev_ctx_t *ctx, uev_t *w, uev_cb_t *cb, void *arg, int fd, int events); int uev_io_set (uev_t *w, int fd, int events);