From ab8c70d501b0bb01c4a2a7840b24fec89c84286b Mon Sep 17 00:00:00 2001 From: Joachim Nilsson Date: Sat, 20 Jul 2013 22:48:52 +0200 Subject: [PATCH] Slight reorder and renaming to clarify, also update README. --- README.md | 6 +-- libuev.c | 147 +++++++++++++++++++++++++++--------------------------- libuev.h | 14 +++--- test.c | 4 +- 4 files changed, 86 insertions(+), 85 deletions(-) diff --git a/README.md b/README.md index 6d70a8b..436ffa0 100644 --- a/README.md +++ b/README.md @@ -14,9 +14,9 @@ The C API --------- The typical application will do the following steps -1. Create the event context with lueCtxtCreate() -2. Register initial callbacks with lueAddInput, lueAddOutput and lueAddTimer -3. Enter the event loop +1. Create the event context with uev_ctx_create() +2. Register callbacks with uev_io_create() and uev_timer_create() +3. Enter the event loop with uev_run() Building -------- diff --git a/libuev.c b/libuev.c index 5e321c7..7e463ed 100644 --- a/libuev.c +++ b/libuev.c @@ -161,71 +161,6 @@ int uev_timer_delete(uev_t *ctx, uev_timer_t *w) return 0; } -/** - * Create an application context - */ -uev_t *uev_create(void) -{ - uev_t *ctx; - - ctx = (uev_t *)malloc(sizeof(*ctx)); - if (!ctx) - return NULL; - - lListInit(&ctx->io_list); - lListInit(&ctx->timer_list); - lListInit(&ctx->io_delist); - lListInit(&ctx->timer_delist); - - ctx->base_time = times(NULL); - ctx->next_time = 0; - ctx->use_next = false; - - ctx->exiting = false; - - if (0 == clock_tick) - clock_tick = sysconf(_SC_CLK_TCK); - - return ctx; -} - -/** - * Destroy an application context - */ -void uev_delete(uev_t *ctx) -{ - lListPurge(&ctx->io_list); - lListPurge(&ctx->timer_list); - free(ctx); -} - -/** - * Drive the timeouts ourself (from now on until an increment of -1) - * This is intended for use in testing frameworks - * Caveat emptor: Currently, interactions with the scheduler (agent) API is undefined. - * Not for use in uevs that use the agent scheduler - * @param ctx UEVlication context object - * @param msec Ms to advance. Use -1 to return to realtime - */ -void uev_run_tick(uev_t *ctx, int msec) -{ - // Obviously we are executing a callback right now (arent we always) - // The time leap is taken into account before falling back into poll() below - if (msec >= 0) { - if (!ctx->use_next) { - ctx->use_next = true; - ctx->next_time = times(NULL) + msec; - } else { - ctx->next_time += msec; - } - } else if (msec == -1) { - /* Go back to realtime */ - ctx->use_next = false; - ctx->base_time += times(NULL) - ctx->next_time; - ctx->next_time = 0; - } -} - /** * Process pending events */ @@ -319,14 +254,6 @@ static int do_run_pending(uev_t *ctx, int immediate) return timeout; } -/** - * Process pending events - */ -int uev_run_pending(uev_t *ctx) -{ - return do_run_pending(ctx, 1); -} - /** * Run the application */ @@ -343,6 +270,41 @@ void uev_run(uev_t *ctx) ctx->exiting = false; } +/** + * Drive the timeouts ourself (from now on until an increment of -1) + * This is intended for use in testing frameworks + * Caveat emptor: Currently, interactions with the scheduler (agent) API is undefined. + * Not for use in uevs that use the agent scheduler + * @param ctx UEVlication context object + * @param msec Ms to advance. Use -1 to return to realtime + */ +void uev_run_tick(uev_t *ctx, int msec) +{ + // Obviously we are executing a callback right now (arent we always) + // The time leap is taken into account before falling back into poll() below + if (msec >= 0) { + if (!ctx->use_next) { + ctx->use_next = true; + ctx->next_time = times(NULL) + msec; + } else { + ctx->next_time += msec; + } + } else if (msec == -1) { + /* Go back to realtime */ + ctx->use_next = false; + ctx->base_time += times(NULL) - ctx->next_time; + ctx->next_time = 0; + } +} + +/** + * Process pending events + */ +int uev_run_pending(uev_t *ctx) +{ + return do_run_pending(ctx, 1); +} + /** * Terminate the application */ @@ -351,6 +313,45 @@ void uev_exit(uev_t *ctx) ctx->exiting = true; } + +/** + * Create an application context + */ +uev_t *uev_ctx_create(void) +{ + uev_t *ctx; + + ctx = (uev_t *)malloc(sizeof(*ctx)); + if (!ctx) + return NULL; + + lListInit(&ctx->io_list); + lListInit(&ctx->timer_list); + lListInit(&ctx->io_delist); + lListInit(&ctx->timer_delist); + + ctx->base_time = times(NULL); + ctx->next_time = 0; + ctx->use_next = false; + + ctx->exiting = false; + + if (0 == clock_tick) + clock_tick = sysconf(_SC_CLK_TCK); + + return ctx; +} + +/** + * Destroy an application context + */ +void uev_ctx_delete(uev_t *ctx) +{ + lListPurge(&ctx->io_list); + lListPurge(&ctx->timer_list); + free(ctx); +} + /** * Local Variables: * version-control: t diff --git a/libuev.h b/libuev.h index f6f7f20..070f983 100644 --- a/libuev.h +++ b/libuev.h @@ -28,6 +28,7 @@ #include "llist.h" +/* Forward declarations due to dependencys, don't try this at home. */ struct uev; struct uev_io; struct uev_timer; @@ -38,7 +39,7 @@ typedef enum { UEV_FHOUT = 1 } uev_dir_t; -/* I/O event type */ +/* I/O event watcher */ typedef struct uev_io_rem { LListField(struct uev_io_rem); struct uev_io *ent; @@ -56,7 +57,7 @@ typedef struct uev_io { void *data; ///< Callback optional parameter } uev_io_t; -/* Timer event type */ +/* Timer event watcher */ typedef struct uev_timer { LListField(struct uev_timer); ///< Linked list elements @@ -102,15 +103,14 @@ int uev_io_delete (uev_t *ctx, uev_io_t *io); uev_timer_t *uev_timer_create (uev_t *ctx, int msec, uev_timer_cb_t handler, void *data); int uev_timer_delete (uev_t *ctx, uev_timer_t *hdl); -uev_t *uev_create (void); -void uev_delete (uev_t *uev); - +void uev_run (uev_t *ctx); void uev_run_tick (uev_t *ctx, int msec); int uev_run_pending (uev_t *ctx); - -void uev_run (uev_t *ctx); void uev_exit (uev_t *ctx); +uev_t *uev_ctx_create (void); +void uev_ctx_delete (uev_t *uev); + #endif /* LIBUEV_H_ */ /** diff --git a/test.c b/test.c index 57e4bd6..4cc572b 100644 --- a/test.c +++ b/test.c @@ -89,7 +89,7 @@ static int pipe_write_cb(uev_t *ctx, uev_timer_t *handle, void *data) int main(void) { int fd[2]; - uev_t *ctx = uev_create(); + uev_t *ctx = uev_ctx_create(); /* Total program execution time */ uev_timer_create(ctx, 4000, lifetime_cb, (void *)(intptr_t)2); @@ -111,7 +111,7 @@ int main(void) uev_run(ctx); /* Tear down event context */ - uev_delete(ctx); + uev_ctx_delete(ctx); fprintf(stderr, "Period is %d must be 10: %s\n", period, 10 == period ? "OK" : "ERROR!"); if (10 != period)