Keep timer deadlines between calls to uev_run()

Timers never expire when the event loop is run one iteration at a time
and something else keeps it busy, e.g., when polled from another event
loop:

    uev_run(ctx, UEV_ONCE | UEV_NONBLOCK);

Every call to uev_run() starts all timers over from their full timeout.

Start dormant timers on the first call only.  A caller that wants a
timer restarted for each call must now use uev_timer_set().

Closes #36
This commit is contained in:
omrzk authored and Joachim Wiberg committed 2026-11-16 15:08:41 +01:00
1 parent 354cdcaf7a
commit 09d9b6ba04
6 files changed
+53 -10

No files matched your search

+3
View File
@@ -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` served the first event. If `flags` is set to `UEV_ONCE | UEV_NONBLOCK`
the event loop returns immediately if no event is available. 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) if (result < 0)
errx(result, "Unrecoverable event loop error, error %d", result); errx(result, "Unrecoverable event loop error, error %d", result);
+2 -2
View File
@@ -68,8 +68,8 @@ static void msec2tspec(int msec, struct timespec *ts)
* period and restarted. * period and restarted.
* *
* A timer is automatically started if the event loop is already * A timer is automatically started if the event loop is already
* running, otherwise it is kept on hold until triggered by calling * running, otherwise it is kept on hold until triggered by the first
* uev_run(). * call to uev_run().
* *
* @see uev_timer_set * @see uev_timer_set
* @return POSIX OK(0) or non-zero with @p errno set on error. * @return POSIX OK(0) or non-zero with @p errno set on error.
+12 -8
View File
@@ -294,6 +294,9 @@ int uev_exit(uev_ctx_t *ctx)
* loop will return immediately if no event is pending, useful when run * loop will return immediately if no event is pending, useful when run
* inside another event loop. * 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 * @return POSIX OK(0) upon successful termination of the event loop, or
* non-zero on error. * non-zero on error.
*/ */
@@ -310,15 +313,16 @@ int uev_run(uev_ctx_t *ctx, int flags)
if (flags & UEV_NONBLOCK) if (flags & UEV_NONBLOCK)
timeout = 0; timeout = 0;
/* Start the event loop */ /* Start the event loop and all dormant timers, first call only */
ctx->running = 1; if (!ctx->running) {
ctx->running = 1;
/* Start all dormant timers */ _UEV_FOREACH(w, ctx->watchers) {
_UEV_FOREACH(w, ctx->watchers) { if (UEV_CRON_TYPE == w->type)
if (UEV_CRON_TYPE == w->type) uev_cron_set(w, w->u.c.when, w->u.c.interval);
uev_cron_set(w, w->u.c.when, w->u.c.interval); if (UEV_TIMER_TYPE == w->type)
if (UEV_TIMER_TYPE == w->type) uev_timer_set(w, w->u.t.timeout, w->u.t.period);
uev_timer_set(w, w->u.t.timeout, w->u.t.period); }
} }
while (ctx->running && ctx->watchers) { while (ctx->running && ctx->watchers) {
+1
View File
@@ -7,6 +7,7 @@ cronrun
cronstart cronstart
error error
io io
resume
signal signal
stdin stdin
timer timer
+1
View File
@@ -12,6 +12,7 @@ TESTS += cronrun
TESTS += cronstart TESTS += cronstart
TESTS += error TESTS += error
TESTS += io TESTS += io
TESTS += resume
TESTS += signal TESTS += signal
TESTS += stdin TESTS += stdin
TESTS += timer TESTS += timer
+34
View File
@@ -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:
*/