commit e56ec2386970fbe2a24a7ff22d1e6fe3cc42e20a
parent 228958d476ae1e79cf988989a05ce4031c4ff10c
Author: xplshn <anto@xplshn.com.ar>
Date: Tue, 24 Nov 2026 14:53:24 -0300
timer: one shot timers behind a backend
Diffstat:
4 files changed, 202 insertions(+), 2 deletions(-)
diff --git a/ninqu.rules b/ninqu.rules
@@ -52,6 +52,7 @@
"reboot_linux.c" "reboot_other.c"
"session_linux.c" "session_other.c"
"sysinfo_linux.c" "sysinfo_other.c"
+ "timer_linux.c" "timer_other.c"
"tty_linux.c" "tty_other.c")
(dep $(MATCH) FEATURES_H)
(after NOFORK_LIST_H)
@@ -89,11 +90,13 @@
"shared/libutil/session_other.c"
"shared/libutil/sysinfo_linux.c"
"shared/libutil/sysinfo_other.c"
+ "shared/libutil/timer_linux.c"
+ "shared/libutil/timer_other.c"
"shared/libutil/tty_linux.c"
"shared/libutil/tty_other.c")
(dep $(MATCH) "shared/diskutil.h" "shared/filesystem.h" "shared/net.h" "shared/passwd.h"
- "shared/proc.h" "shared/reboot.h" "shared/session.h" "shared/sysinfo.h" "shared/util.h"
- FEATURES_H)
+ "shared/proc.h" "shared/reboot.h" "shared/session.h" "shared/sysinfo.h"
+ "shared/timer.h" "shared/util.h" FEATURES_H)
(after NOFORK_LIST_H)
(out "shared/libutil/$(BASESTEM).o")
(exec $(CC) $(CPPFLAGS_LIBUTIL) -Ishared -Ishared/libutil $(CFLAGS) -c -o $(OUT) $(MATCH)))
diff --git a/shared/libutil/timer_linux.c b/shared/libutil/timer_linux.c
@@ -0,0 +1,74 @@
+/* see LICENSE file for copyright and license details */
+#include "../timer.h"
+
+#if defined(__linux__) && !defined(ARUU_FORCE_OTHER)
+#include <errno.h>
+#include <poll.h>
+#include <stdint.h>
+#include <sys/timerfd.h>
+#include <unistd.h>
+
+static int timers[TIMER_MAX];
+static int ntimers;
+
+int
+timer_init(int n)
+{
+ if (n > TIMER_MAX) {
+ errno = EINVAL;
+ return -1;
+ }
+ for (ntimers = 0; ntimers < n; ntimers++) {
+ timers[ntimers] = timerfd_create(CLOCK_BOOTTIME, TFD_CLOEXEC);
+ if (timers[ntimers] == -1)
+ return -1;
+ }
+ return 0;
+}
+
+int
+timer_set(int i, const struct timespec *ts)
+{
+ struct itimerspec its = {0};
+
+ its.it_value = *ts;
+ return timerfd_settime(timers[i], 0, &its, NULL);
+}
+
+int
+timer_left(int i, struct timespec *ts)
+{
+ struct itimerspec its;
+
+ if (timerfd_gettime(timers[i], &its) < 0)
+ return -1;
+ *ts = its.it_value;
+ return 0;
+}
+
+int
+timer_wait(int fd)
+{
+ struct pollfd pfd[TIMER_MAX + 1];
+ uint64_t n;
+ int i, fired = 0;
+
+ pfd[0].fd = fd;
+ pfd[0].events = POLLIN;
+ for (i = 0; i < ntimers; i++) {
+ pfd[i + 1].fd = timers[i];
+ pfd[i + 1].events = POLLIN;
+ }
+ if (poll(pfd, ntimers + 1, -1) == -1)
+ return -1;
+ if (pfd[0].revents)
+ return 0;
+ for (i = 0; i < ntimers; i++) {
+ if (pfd[i + 1].revents) {
+ fired |= 1 << i;
+ read(timers[i], &n, sizeof(n));
+ }
+ }
+ return fired;
+}
+#endif
diff --git a/shared/libutil/timer_other.c b/shared/libutil/timer_other.c
@@ -0,0 +1,104 @@
+/* see LICENSE file for copyright and license details */
+#include "../timer.h"
+
+/* deadlines on the monotonic clock posix has, which may stop while the
+ * machine sleeps */
+#if !defined(__linux__) || defined(ARUU_FORCE_OTHER)
+#include <errno.h>
+#include <poll.h>
+#include <string.h>
+
+static struct timespec deadline[TIMER_MAX]; /* 0 0 for disarmed */
+static int ntimers;
+
+static int
+armed(int i)
+{
+ return deadline[i].tv_sec || deadline[i].tv_nsec;
+}
+
+static long long
+nsec(const struct timespec *ts)
+{
+ return ts->tv_sec * 1000000000LL + ts->tv_nsec;
+}
+
+int
+timer_init(int n)
+{
+ if (n > TIMER_MAX) {
+ errno = EINVAL;
+ return -1;
+ }
+ ntimers = n;
+ memset(deadline, 0, sizeof(deadline));
+ return 0;
+}
+
+int
+timer_set(int i, const struct timespec *ts)
+{
+ struct timespec now;
+ long long t;
+
+ if (!ts->tv_sec && !ts->tv_nsec) {
+ memset(&deadline[i], 0, sizeof(deadline[i]));
+ return 0;
+ }
+ if (clock_gettime(CLOCK_MONOTONIC, &now) < 0)
+ return -1;
+ t = nsec(&now) + nsec(ts);
+ deadline[i].tv_sec = t / 1000000000LL;
+ deadline[i].tv_nsec = t % 1000000000LL;
+ return 0;
+}
+
+int
+timer_left(int i, struct timespec *ts)
+{
+ struct timespec now;
+ long long t = 0;
+
+ if (clock_gettime(CLOCK_MONOTONIC, &now) < 0)
+ return -1;
+ if (armed(i) && (t = nsec(&deadline[i]) - nsec(&now)) < 1)
+ t = 1;
+ ts->tv_sec = t / 1000000000LL;
+ ts->tv_nsec = t % 1000000000LL;
+ return 0;
+}
+
+int
+timer_wait(int fd)
+{
+ struct pollfd pfd = {.fd = fd, .events = POLLIN};
+ struct timespec now;
+ long long next, left;
+ int i, ms, fired;
+
+ for (;;) {
+ if (clock_gettime(CLOCK_MONOTONIC, &now) < 0)
+ return -1;
+ fired = 0;
+ next = -1;
+ for (i = 0; i < ntimers; i++) {
+ if (!armed(i))
+ continue;
+ left = nsec(&deadline[i]) - nsec(&now);
+ if (left <= 0) {
+ fired |= 1 << i;
+ memset(&deadline[i], 0, sizeof(deadline[i]));
+ } else if (next < 0 || left < next) {
+ next = left;
+ }
+ }
+ if (fired)
+ return fired;
+ ms = next < 0 ? -1 : (int)((next + 999999) / 1000000);
+ if (poll(&pfd, 1, ms) == -1)
+ return -1;
+ if (pfd.revents)
+ return 0;
+ }
+}
+#endif
diff --git a/shared/timer.h b/shared/timer.h
@@ -0,0 +1,19 @@
+/* see LICENSE file for copyright and license details */
+#ifndef ARUU_TIMER_H
+#define ARUU_TIMER_H
+
+#include <time.h>
+
+/* one shot timers on a clock that keeps counting while the machine
+ * sleeps, waited on together with a file descriptor. a backend per kernel */
+#define TIMER_MAX 8
+
+int timer_init(int n);
+/* a zero ts disarms timer i */
+int timer_set(int i, const struct timespec *ts);
+int timer_left(int i, struct timespec *ts);
+/* 0 once fd is readable, else the mask of the timers that fired, 1 << i.
+ * -1 with errno set on error */
+int timer_wait(int fd);
+
+#endif