From 82fa6480de7a85d0ced0701ab7c8825e31b90770 Mon Sep 17 00:00:00 2001 From: Jo-Philipp Wich Date: Sun, 15 Oct 2023 00:17:36 +0200 Subject: uloop: add support for interval timers So far, the only way to implement periodic interval timers was to use one-shot uloop_timeout timers which are rearmed within their completion callback immediately on expiration. While simple, this approach is not very precise and interval lengths will slowly drift over time, due to callback execution overhead, scheduling granularity etc. In order to make uloop provide stable and precise interval timer capabilities, this commit introduces a new `uloop_interval` structure along with the new related `uloop_interval_set()`, `uloop_interval_cancel()` and `uloop_interval_remaining()` api functions. Periodic timers are implemented using the timerfd facility an Linux and kqueue EVFILT_TIMER events on macOS/BSD. The Lua binding has been updated to include support for the new timer type as well. Signed-off-by: Jo-Philipp Wich --- uloop-epoll.c | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) (limited to 'uloop-epoll.c') diff --git a/uloop-epoll.c b/uloop-epoll.c index 70e45e4..cf5733f 100644 --- a/uloop-epoll.c +++ b/uloop-epoll.c @@ -104,3 +104,83 @@ static int uloop_fetch_events(int timeout) return nfds; } + +static void dispatch_timer(struct uloop_fd *u, unsigned int events) +{ + if (!(events & ULOOP_READ)) + return; + + uint64_t fired; + + if (read(u->fd, &fired, sizeof(fired)) != sizeof(fired)) + return; + + struct uloop_interval *tm = container_of(u, struct uloop_interval, private.ufd); + + tm->expirations += fired; + tm->cb(tm); +} + +static int timer_register(struct uloop_interval *tm, unsigned int msecs) +{ + if (!tm->private.ufd.registered) { + int fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC|TFD_NONBLOCK); + + if (fd == -1) + return -1; + + tm->private.ufd.fd = fd; + tm->private.ufd.cb = dispatch_timer; + } + + struct itimerspec spec = { + .it_value = { + .tv_sec = msecs / 1000, + .tv_nsec = (msecs % 1000) * 1000000 + }, + .it_interval = { + .tv_sec = msecs / 1000, + .tv_nsec = (msecs % 1000) * 1000000 + } + }; + + if (timerfd_settime(tm->private.ufd.fd, 0, &spec, NULL) == -1) + goto err; + + if (uloop_fd_add(&tm->private.ufd, ULOOP_READ) == -1) + goto err; + + return 0; + +err: + uloop_fd_delete(&tm->private.ufd); + close(tm->private.ufd.fd); + memset(&tm->private.ufd, 0, sizeof(tm->private.ufd)); + + return -1; +} + +static int timer_remove(struct uloop_interval *tm) +{ + int ret = __uloop_fd_delete(&tm->private.ufd); + + if (ret == 0) { + close(tm->private.ufd.fd); + memset(&tm->private.ufd, 0, sizeof(tm->private.ufd)); + } + + return ret; +} + +static int64_t timer_next(struct uloop_interval *tm) +{ + struct itimerspec spec; + + if (!tm->private.ufd.registered) + return -1; + + if (timerfd_gettime(tm->private.ufd.fd, &spec) == -1) + return -1; + + return spec.it_value.tv_sec * 1000 + spec.it_value.tv_nsec / 1000000; +} -- cgit v1.2.3