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
This commit is contained in:
NefelibateLG authored and Joachim Wiberg committed 2026-11-16 15:08:42 +01:00
1 parent 28643fa599
commit 397247d567
5 files changed
+44 -8

No files matched your search

+9 -5
View File
@@ -25,6 +25,7 @@
#include <errno.h>
#include <string.h> /* memset() */
#include <sys/timerfd.h>
#include <time.h> /* time() */
#include <unistd.h> /* 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;
}