From 397247d5675580c9fa8169cbec161ea749f8d000 Mon Sep 17 00:00:00 2001 From: NefelibateLG <3453510037@qq.com> Date: Wed, 4 Nov 2026 22:30:42 +0800 Subject: [PATCH] Run cron watcher with start time zero when the event loop starts A cron watcher with start time zero, documented to mean 'now', never runs: uev_cron_init(ctx, w, cb, NULL, 0, 3600); Zero is passed as is to the timerfd, where it disarms the timer. Replace it with the current time when the event loop is running. This also goes for uev_cron_set(), where zero no longer disarms the watcher, use uev_cron_stop() for that. The event loop used a start time of zero to mark an at job as done. Stop the watcher there instead and keep its time, so restarting an at job that was interrupted by a clock change does not run it at once. Closes #30 --- src/cron.c | 14 +++++++++----- src/uev.c | 4 +--- test/.gitignore | 1 + test/Makefile.am | 1 + test/cronnow.c | 32 ++++++++++++++++++++++++++++++++ 5 files changed, 44 insertions(+), 8 deletions(-) create mode 100644 test/cronnow.c diff --git a/src/cron.c b/src/cron.c index bc01149..6616fcd 100644 --- a/src/cron.c +++ b/src/cron.c @@ -25,6 +25,7 @@ #include #include /* memset() */ #include +#include /* time() */ #include /* close(), read() */ #include "uev.h" @@ -92,9 +93,12 @@ int uev_cron_init(uev_ctx_t *ctx, uev_t *w, uev_cb_t *cb, void *arg, time_t when /** * Reset an at/cron job watcher * @param w Watcher to reset - * @param when First point in time to call @p cb + * @param when First point in time to call @p cb, zero for 'now' * @param interval For an at job this is zero, for cron the offset interval * + * Note, unlike uev_timer_set() a @p when of zero does not disarm the + * watcher, use uev_cron_stop() for that. + * * @return POSIX OK(0) or non-zero with @p errno set on error. */ int uev_cron_set(uev_t *w, time_t when, time_t interval) @@ -110,12 +114,12 @@ int uev_cron_set(uev_t *w, time_t when, time_t interval) return -1; } + /* Zero is 'now', when the event loop is started */ + if (!when && w->ctx->running) + when = time(NULL); + /* Handle stopped timers */ if (w->fd < 0) { - /* Timer already stopped */ - if (!when && !interval) - return 0; - if (uev_cron_init(w->ctx, w, (uev_cb_t *)w->cb, w->arg, when, interval)) return -1; } diff --git a/src/uev.c b/src/uev.c index 7e2af19..6fb4733 100644 --- a/src/uev.c +++ b/src/uev.c @@ -420,11 +420,9 @@ int uev_run(uev_ctx_t *ctx, int flags) } if (!w->u.c.interval) - w->u.c.when = 0; + uev_timer_stop(w); else w->u.c.when += w->u.c.interval; - if (!w->u.c.when) - uev_timer_stop(w); break; case UEV_EVENT_TYPE: diff --git a/test/.gitignore b/test/.gitignore index 725c86d..b6e5d2f 100644 --- a/test/.gitignore +++ b/test/.gitignore @@ -3,6 +3,7 @@ active api complete +cronnow cronrun cronstart error diff --git a/test/Makefile.am b/test/Makefile.am index 15c37d1..d6d65c1 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -8,6 +8,7 @@ TESTS = TESTS += active TESTS += api TESTS += complete +TESTS += cronnow TESTS += cronrun TESTS += cronstart TESTS += error diff --git a/test/cronnow.c b/test/cronnow.c new file mode 100644 index 0000000..c8ccef8 --- /dev/null +++ b/test/cronnow.c @@ -0,0 +1,32 @@ +/* Verifies that a cron watcher with start time zero runs now */ +#include "check.h" + +static int fired; + +static void cb(uev_t *w, void *arg, int events) +{ + fired++; + uev_exit(w->ctx); +} + +int main(void) +{ + uev_ctx_t ctx; + uev_t w; + + fail_unless(uev_init(&ctx) == 0); + fail_unless(uev_cron_init(&ctx, &w, cb, NULL, 0, 0) == 0); + + alarm(2); + fail_unless(uev_run(&ctx, 0) == 0); + fail_unless(fired == 1); + + return 0; +} + +/** + * Local Variables: + * indent-tabs-mode: t + * c-file-style: "linux" + * End: + */