diff --git a/doc/API.md b/doc/API.md index 61f8c75..c024c66 100644 --- a/doc/API.md +++ b/doc/API.md @@ -171,6 +171,9 @@ With `flags` set to `UEV_ONCE` the event loop returns as soon as it has served the first event. If `flags` is set to `UEV_ONCE | UEV_NONBLOCK` the event loop returns immediately if no event is available. +Timers are started by the first call to `uev_run()` and keep their +deadline between calls. Use `uev_timer_set()` to restart a timer. + ``` if (result < 0) errx(result, "Unrecoverable event loop error, error %d", result); diff --git a/src/timer.c b/src/timer.c index dd15d31..7b07a48 100644 --- a/src/timer.c +++ b/src/timer.c @@ -68,8 +68,8 @@ static void msec2tspec(int msec, struct timespec *ts) * period and restarted. * * A timer is automatically started if the event loop is already - * running, otherwise it is kept on hold until triggered by calling - * uev_run(). + * running, otherwise it is kept on hold until triggered by the first + * call to uev_run(). * * @see uev_timer_set * @return POSIX OK(0) or non-zero with @p errno set on error. diff --git a/src/uev.c b/src/uev.c index b75cdc4..fbdbc7b 100644 --- a/src/uev.c +++ b/src/uev.c @@ -294,6 +294,9 @@ int uev_exit(uev_ctx_t *ctx) * loop will return immediately if no event is pending, useful when run * inside another event loop. * + * Timers are started by the first call and keep their deadline between + * calls. Use uev_timer_set() to restart a timer. + * * @return POSIX OK(0) upon successful termination of the event loop, or * non-zero on error. */ @@ -310,15 +313,16 @@ int uev_run(uev_ctx_t *ctx, int flags) if (flags & UEV_NONBLOCK) timeout = 0; - /* Start the event loop */ - ctx->running = 1; + /* Start the event loop and all dormant timers, first call only */ + if (!ctx->running) { + ctx->running = 1; - /* Start all dormant timers */ - _UEV_FOREACH(w, ctx->watchers) { - if (UEV_CRON_TYPE == w->type) - uev_cron_set(w, w->u.c.when, w->u.c.interval); - if (UEV_TIMER_TYPE == w->type) - uev_timer_set(w, w->u.t.timeout, w->u.t.period); + _UEV_FOREACH(w, ctx->watchers) { + if (UEV_CRON_TYPE == w->type) + uev_cron_set(w, w->u.c.when, w->u.c.interval); + if (UEV_TIMER_TYPE == w->type) + uev_timer_set(w, w->u.t.timeout, w->u.t.period); + } } while (ctx->running && ctx->watchers) { diff --git a/test/.gitignore b/test/.gitignore index db5b9b0..efe7bc9 100644 --- a/test/.gitignore +++ b/test/.gitignore @@ -7,6 +7,7 @@ cronrun cronstart error io +resume signal stdin timer diff --git a/test/Makefile.am b/test/Makefile.am index 137850b..d001ac1 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -12,6 +12,7 @@ TESTS += cronrun TESTS += cronstart TESTS += error TESTS += io +TESTS += resume TESTS += signal TESTS += stdin TESTS += timer diff --git a/test/resume.c b/test/resume.c new file mode 100644 index 0000000..343c6f9 --- /dev/null +++ b/test/resume.c @@ -0,0 +1,34 @@ +/* Verifies that timers keep their deadline between calls to uev_run() */ +#include "check.h" + +static int fired; + +static void cb(uev_t *w, void *arg, int events) +{ + fired++; +} + +int main(void) +{ + uev_ctx_t ctx; + uev_t w; + int i; + + fail_unless(uev_init(&ctx) == 0); + fail_unless(uev_timer_init(&ctx, &w, cb, NULL, 50, 0) == 0); + + for (i = 0; i < 15; i++) { + fail_unless(uev_run(&ctx, UEV_ONCE | UEV_NONBLOCK) == 0); + usleep(10000); + } + fail_unless(fired == 1); + + return uev_exit(&ctx); +} + +/** + * Local Variables: + * indent-tabs-mode: t + * c-file-style: "linux" + * End: + */