mirror of
https://github.com/troglobit/libuev.git
synced 2026-11-28 21:08:45 +00:00
Add support for UEV_ONCE and UEV_NONBLOCK run flags to uev_run()
Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
This commit is contained in:
1 parent
d6e0146803
commit
66c5606e34
3 files changed
+24
-11
No files matched your search
@@ -31,6 +31,8 @@
|
|||||||
|
|
||||||
#include "uev.h"
|
#include "uev.h"
|
||||||
|
|
||||||
|
#define UNUSED(arg) arg __attribute__ ((unused))
|
||||||
|
|
||||||
/* Private to libuev, do not use directly! */
|
/* 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)
|
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
|
* 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.
|
* @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;
|
uev_t *w;
|
||||||
|
|
||||||
if (!ctx || ctx->fd < 0) {
|
if (!ctx || ctx->fd < 0) {
|
||||||
@@ -174,6 +177,9 @@ int uev_run(uev_ctx_t *ctx)
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (flags & UEV_NONBLOCK)
|
||||||
|
timeout = 0;
|
||||||
|
|
||||||
/* Start the event loop */
|
/* Start the event loop */
|
||||||
ctx->running = 1;
|
ctx->running = 1;
|
||||||
|
|
||||||
@@ -187,7 +193,7 @@ int uev_run(uev_ctx_t *ctx)
|
|||||||
int i, nfds;
|
int i, nfds;
|
||||||
struct epoll_event events[UEV_MAX_EVENTS];
|
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)
|
if (!ctx->running)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@@ -230,11 +236,15 @@ int uev_run(uev_ctx_t *ctx)
|
|||||||
uev_timer_stop(w);
|
uev_timer_stop(w);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ((flags & UEV_ONCE) || LIST_EMPTY(&ctx->watchers))
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Local Variables:
|
* Local Variables:
|
||||||
* version-control: t
|
* version-control: t
|
||||||
|
|||||||
@@ -136,7 +136,7 @@ int main(void)
|
|||||||
uev_timer_init(&ctx, &periodic, periodic_task, NULL, 200, 200);
|
uev_timer_init(&ctx, &periodic, periodic_task, NULL, 200, 200);
|
||||||
|
|
||||||
/* Start event loop */
|
/* 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!");
|
fprintf(stderr, "Period is %d must be 10: %s\n", period, 10 == period ? "OK" : "ERROR!");
|
||||||
if (10 != period)
|
if (10 != period)
|
||||||
|
|||||||
@@ -31,12 +31,16 @@
|
|||||||
#include <sys/queue.h>
|
#include <sys/queue.h>
|
||||||
|
|
||||||
/* Max. number of simulateneous events */
|
/* 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 */
|
/* I/O events, for signals and timers revents will always be UEV_READ */
|
||||||
#define UEV_NONE 0
|
#define UEV_NONE 0
|
||||||
#define UEV_READ EPOLLIN
|
#define UEV_READ EPOLLIN
|
||||||
#define UEV_WRITE EPOLLOUT
|
#define UEV_WRITE EPOLLOUT
|
||||||
|
|
||||||
|
/* Run flags */
|
||||||
|
#define UEV_ONCE 1
|
||||||
|
#define UEV_NONBLOCK 2
|
||||||
|
|
||||||
/* I/O, timer, or signal watcher */
|
/* I/O, timer, or signal watcher */
|
||||||
typedef enum {
|
typedef enum {
|
||||||
@@ -89,8 +93,7 @@ int uev_watcher_stop (uev_t *w);
|
|||||||
/* Public interface */
|
/* Public interface */
|
||||||
int uev_init (uev_ctx_t *ctx);
|
int uev_init (uev_ctx_t *ctx);
|
||||||
int uev_exit (uev_ctx_t *ctx);
|
int uev_exit (uev_ctx_t *ctx);
|
||||||
|
int uev_run (uev_ctx_t *ctx, int flags);
|
||||||
int uev_run (uev_ctx_t *ctx);
|
|
||||||
|
|
||||||
int uev_io_init (uev_ctx_t *ctx, uev_t *w, uev_cb_t *cb, void *arg, int fd, int events);
|
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);
|
int uev_io_set (uev_t *w, int fd, int events);
|
||||||
|
|||||||
Reference in new issue
Block a user