From e76a0fe1e2832c5f3c621dac9aa177c795bee7bd Mon Sep 17 00:00:00 2001 From: pantonshire Date: Sat, 27 Dec 2025 13:34:57 +0000 Subject: [PATCH] epoll event loop fd + timer --- src/eventloop_epoll.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/eventloop_epoll.c b/src/eventloop_epoll.c index 2bbb0fd..2d15458 100644 --- a/src/eventloop_epoll.c +++ b/src/eventloop_epoll.c @@ -2,9 +2,11 @@ #include #include #include +#include #include "eventloop.h" #include "eventloop_platform.h" +#include "io.h" int owd_platform_el_init(struct owd_eventloop *el) { if ((el->epoll_fd = epoll_create1(EPOLL_CLOEXEC)) < 0) @@ -117,7 +119,30 @@ int owd_platform_el_add_timer( uint64_t millis, bool oneshot) { + FD_SCOPED(tfd); + struct itimerspec spec; + struct timespec ts; + int res; + + ts.tv_sec = (long)(millis / 1000); + ts.tv_nsec = (long)((millis % 1000) * 1000000); + + memset(&spec, 0, sizeof(spec)); + spec.it_value = ts; + if (!oneshot) + spec.it_interval = ts; + + if ((tfd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC)) < 0) + return -1; + if (timerfd_settime(tfd, 0, &spec, NULL)) + return -1; + + if ((res = el_epoll_add(el, e, tfd, OWD_EVENT_SOURCE_TIMER, EPOLLIN, true))) + return res; + + FD_RELEASE(tfd); + return 0; } int owd_platform_el_add_fd( @@ -126,5 +151,13 @@ int owd_platform_el_add_fd( int fd, uint32_t flags) { + uint32_t events; + + events = 0; + if (flags & OWD_EVENT_READABLE) + events |= EPOLLIN; + if (flags & OWD_EVENT_WRITABLE) + events |= EPOLLOUT; + return el_epoll_add(el, e, fd, OWD_EVENT_SOURCE_FD, events, false); }