2 0a53bd62 2023-08-16 op * This is free and unencumbered software released into the public domain.
4 0a53bd62 2023-08-16 op * Anyone is free to copy, modify, publish, use, compile, sell, or
5 0a53bd62 2023-08-16 op * distribute this software, either in source code form or as a compiled
6 0a53bd62 2023-08-16 op * binary, for any purpose, commercial or non-commercial, and by any
9 0a53bd62 2023-08-16 op * In jurisdictions that recognize copyright laws, the author or authors
10 0a53bd62 2023-08-16 op * of this software dedicate any and all copyright interest in the
11 0a53bd62 2023-08-16 op * software to the public domain. We make this dedication for the benefit
12 0a53bd62 2023-08-16 op * of the public at large and to the detriment of our heirs and
13 0a53bd62 2023-08-16 op * successors. We intend this dedication to be an overt act of
14 0a53bd62 2023-08-16 op * relinquishment in perpetuity of all present and future rights to this
15 0a53bd62 2023-08-16 op * software under copyright law.
17 0a53bd62 2023-08-16 op * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 0a53bd62 2023-08-16 op * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 0a53bd62 2023-08-16 op * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 0a53bd62 2023-08-16 op * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 0a53bd62 2023-08-16 op * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 0a53bd62 2023-08-16 op * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 0a53bd62 2023-08-16 op * OTHER DEALINGS IN THE SOFTWARE.
26 0a53bd62 2023-08-16 op #include "config.h"
28 6be81433 2024-04-14 op #include <sys/time.h>
30 0a53bd62 2023-08-16 op #include <errno.h>
31 0a53bd62 2023-08-16 op #include <fcntl.h>
32 0a53bd62 2023-08-16 op #include <limits.h>
33 0a53bd62 2023-08-16 op #include <poll.h>
34 0a53bd62 2023-08-16 op #include <signal.h>
35 0a53bd62 2023-08-16 op #include <stdlib.h>
36 6be81433 2024-04-14 op #include <string.h>
37 27bf07a4 2023-08-16 op #include <time.h>
38 0a53bd62 2023-08-16 op #include <unistd.h>
40 0a53bd62 2023-08-16 op #include "ev.h"
43 0a53bd62 2023-08-16 op void (*cb)(int, int, void *);
47 6be81433 2024-04-14 op struct evtimer {
48 6be81433 2024-04-14 op unsigned int id;
49 6be81433 2024-04-14 op struct timeval tv;
50 6be81433 2024-04-14 op struct evcb cb;
53 0a53bd62 2023-08-16 op struct evbase {
56 0a53bd62 2023-08-16 op struct pollfd *pfds;
57 0a53bd62 2023-08-16 op size_t pfdlen;
59 0a53bd62 2023-08-16 op struct evcb *cbs;
62 0a53bd62 2023-08-16 op int sigpipe[2];
63 0a53bd62 2023-08-16 op struct evcb sigcb;
65 6be81433 2024-04-14 op unsigned int tid;
68 6be81433 2024-04-14 op * Binary heap of timers. At runtime, new timers are added in
69 6be81433 2024-04-14 op * the ``reserve'', a space after the caninocal end of the
70 6be81433 2024-04-14 op * array, and at the end of every tick they're added to the
73 6be81433 2024-04-14 op struct evtimer *timers;
74 6be81433 2024-04-14 op size_t ntimers;
75 6be81433 2024-04-14 op size_t reserve_from;
76 6be81433 2024-04-14 op size_t reserve_till;
77 6be81433 2024-04-14 op size_t timerscap;
80 0a53bd62 2023-08-16 op static struct evbase *base;
81 0a53bd62 2023-08-16 op static int ev_stop;
84 0a53bd62 2023-08-16 op ev_resize(size_t len)
89 0a53bd62 2023-08-16 op t = recallocarray(base->pfds, base->pfdlen, len, sizeof(*base->pfds));
90 0a53bd62 2023-08-16 op if (t == NULL)
92 0a53bd62 2023-08-16 op base->pfds = t;
93 0a53bd62 2023-08-16 op base->pfdlen = len;
95 0a53bd62 2023-08-16 op for (i = base->len; i < len; ++i)
96 0a53bd62 2023-08-16 op base->pfds[i].fd = -1;
98 0a53bd62 2023-08-16 op t = recallocarray(base->cbs, base->cblen, len, sizeof(*base->cbs));
99 0a53bd62 2023-08-16 op if (t == NULL)
101 0a53bd62 2023-08-16 op base->cbs = t;
102 0a53bd62 2023-08-16 op base->cblen = len;
104 0a53bd62 2023-08-16 op base->len = len;
109 0a53bd62 2023-08-16 op ev_init(void)
111 0a53bd62 2023-08-16 op if (base != NULL) {
112 0a53bd62 2023-08-16 op errno = EINVAL;
116 0a53bd62 2023-08-16 op if ((base = calloc(1, sizeof(*base))) == NULL)
119 0a53bd62 2023-08-16 op base->sigpipe[0] = -1;
120 0a53bd62 2023-08-16 op base->sigpipe[1] = -1;
122 0a53bd62 2023-08-16 op if (ev_resize(16) == -1) {
123 0a53bd62 2023-08-16 op free(base->pfds);
124 0a53bd62 2023-08-16 op free(base->cbs);
133 6be81433 2024-04-14 op static inline int
134 6be81433 2024-04-14 op ev2poll(int ev)
138 6be81433 2024-04-14 op if (ev & EV_READ)
139 6be81433 2024-04-14 op ret |= POLLIN;
140 6be81433 2024-04-14 op if (ev & EV_WRITE)
141 6be81433 2024-04-14 op ret |= POLLOUT;
143 6be81433 2024-04-14 op return (ret);
147 0a53bd62 2023-08-16 op ev_add(int fd, int ev, void (*cb)(int, int, void *), void *udata)
149 6be81433 2024-04-14 op if (fd < 0) {
150 6be81433 2024-04-14 op errno = EBADF;
154 6be81433 2024-04-14 op if ((size_t)fd >= base->len) {
155 0a53bd62 2023-08-16 op if (ev_resize(fd + 1) == -1)
159 0a53bd62 2023-08-16 op base->pfds[fd].fd = fd;
160 6be81433 2024-04-14 op base->pfds[fd].events = ev2poll(ev);
161 6be81433 2024-04-14 op base->pfds[fd].revents = 0;
163 0a53bd62 2023-08-16 op base->cbs[fd].cb = cb;
164 0a53bd62 2023-08-16 op base->cbs[fd].udata = udata;
170 0a53bd62 2023-08-16 op ev_sigcatch(int signo)
172 0a53bd62 2023-08-16 op unsigned char s;
178 0a53bd62 2023-08-16 op * We should be able to write up to PIPE_BUF bytes without
182 0a53bd62 2023-08-16 op (void) write(base->sigpipe[1], &s, sizeof(s));
188 0a53bd62 2023-08-16 op ev_sigdispatch(int fd, int ev, void *data)
190 0a53bd62 2023-08-16 op unsigned char signo;
192 0a53bd62 2023-08-16 op if (read(fd, &signo, sizeof(signo)) != sizeof(signo))
195 6be81433 2024-04-14 op base->sigcb.cb(signo, EV_SIGNAL, base->sigcb.udata);
199 0a53bd62 2023-08-16 op ev_signal(int sig, void (*cb)(int, int, void *), void *udata)
203 0a53bd62 2023-08-16 op if (base->sigpipe[0] == -1) {
204 dc3e25b1 2023-10-07 op /* pipe2(2) is not available everywhere... sigh */
205 dc3e25b1 2023-10-07 op if (pipe(base->sigpipe) == -1)
208 dc3e25b1 2023-10-07 op if ((flags = fcntl(base->sigpipe[1], F_GETFL)) == -1 ||
209 dc3e25b1 2023-10-07 op fcntl(base->sigpipe[1], F_SETFL, flags | O_NONBLOCK) == -1)
212 6be81433 2024-04-14 op if (ev_add(base->sigpipe[0], EV_READ, ev_sigdispatch, NULL)
217 0a53bd62 2023-08-16 op base->sigcb.cb = cb;
218 0a53bd62 2023-08-16 op base->sigcb.udata = udata;
220 0a53bd62 2023-08-16 op signal(sig, ev_sigcatch);
225 0a53bd62 2023-08-16 op ev_timer(const struct timeval *tv, void (*cb)(int, int, void*), void *udata)
227 6be81433 2024-04-14 op struct evtimer *evt;
229 6be81433 2024-04-14 op size_t newcap;
230 6be81433 2024-04-14 op unsigned int nextid;
232 6be81433 2024-04-14 op if (tv == NULL) {
233 6be81433 2024-04-14 op errno = EINVAL;
237 6be81433 2024-04-14 op if (base->reserve_till == base->timerscap) {
238 6be81433 2024-04-14 op newcap = base->timerscap + 8;
239 6be81433 2024-04-14 op t = recallocarray(base->timers, base->timerscap, newcap,
240 6be81433 2024-04-14 op sizeof(*base->timers));
241 6be81433 2024-04-14 op if (t == NULL)
243 6be81433 2024-04-14 op base->timers = t;
244 6be81433 2024-04-14 op base->timerscap = newcap;
247 6be81433 2024-04-14 op if ((nextid = ++base->tid) == 0)
248 6be81433 2024-04-14 op nextid = ++base->tid;
250 6be81433 2024-04-14 op evt = &base->timers[base->reserve_till];
251 6be81433 2024-04-14 op evt->id = nextid;
252 6be81433 2024-04-14 op memcpy(&evt->tv, tv, sizeof(*tv));
253 6be81433 2024-04-14 op evt->cb.cb = cb;
254 6be81433 2024-04-14 op evt->cb.udata = udata;
256 6be81433 2024-04-14 op base->reserve_till++;
257 6be81433 2024-04-14 op return (nextid);
261 6be81433 2024-04-14 op find_timer(unsigned int id, size_t *pos)
268 6be81433 2024-04-14 op for (i = 0; i < base->ntimers; ++i) {
269 6be81433 2024-04-14 op if (base->timers[i].id == id) {
275 6be81433 2024-04-14 op for (i = base->reserve_from; i < base->reserve_till; ++i) {
276 6be81433 2024-04-14 op if (base->timers[i].id == id) {
286 6be81433 2024-04-14 op ev_timer_pending(unsigned int id)
290 6be81433 2024-04-14 op return (find_timer(id, &i));
294 6be81433 2024-04-14 op bubbledown(size_t i)
296 6be81433 2024-04-14 op struct evtimer tmp;
297 6be81433 2024-04-14 op size_t l, r, s;
300 6be81433 2024-04-14 op l = 2 * i + 1;
301 6be81433 2024-04-14 op r = 2 * i + 2;
303 6be81433 2024-04-14 op /* base case: there are no children */
304 6be81433 2024-04-14 op if (l >= base->ntimers)
307 6be81433 2024-04-14 op /* find the smaller child */
309 6be81433 2024-04-14 op if (r >= base->ntimers ||
310 6be81433 2024-04-14 op timercmp(&base->timers[l].tv, &base->timers[r].tv, <))
313 6be81433 2024-04-14 op /* other base case: it's at the right place */
314 6be81433 2024-04-14 op if (timercmp(&base->timers[i].tv, &base->timers[s].tv, <))
318 6be81433 2024-04-14 op memcpy(&tmp, &base->timers[s], sizeof(tmp));
319 6be81433 2024-04-14 op memcpy(&base->timers[s], &base->timers[i], sizeof(tmp));
320 6be81433 2024-04-14 op memcpy(&base->timers[i], &tmp, sizeof(tmp));
326 6be81433 2024-04-14 op static inline void
327 6be81433 2024-04-14 op cancel_timer(size_t i)
329 6be81433 2024-04-14 op base->ntimers--;
330 6be81433 2024-04-14 op if (i != base->ntimers) {
331 6be81433 2024-04-14 op memcpy(&base->timers[i], &base->timers[base->ntimers],
332 6be81433 2024-04-14 op sizeof(*base->timers));
333 6be81433 2024-04-14 op bubbledown(i);
338 6be81433 2024-04-14 op ev_timer_cancel(unsigned int id)
342 6be81433 2024-04-14 op if (!find_timer(id, &i))
345 6be81433 2024-04-14 op if (i < base->ntimers) {
346 6be81433 2024-04-14 op cancel_timer(i);
350 6be81433 2024-04-14 op base->reserve_till--;
351 6be81433 2024-04-14 op if (i != base->reserve_till)
352 6be81433 2024-04-14 op memcpy(&base->timers[i], &base->timers[base->reserve_till],
353 6be81433 2024-04-14 op sizeof(*base->timers));
358 0a53bd62 2023-08-16 op ev_del(int fd)
360 6be81433 2024-04-14 op if (fd < 0) {
361 6be81433 2024-04-14 op errno = EBADF;
365 6be81433 2024-04-14 op if ((size_t)fd >= base->len) {
366 0a53bd62 2023-08-16 op errno = ERANGE;
370 0a53bd62 2023-08-16 op base->pfds[fd].fd = -1;
371 0a53bd62 2023-08-16 op base->pfds[fd].events = 0;
373 0a53bd62 2023-08-16 op base->cbs[fd].cb = NULL;
374 0a53bd62 2023-08-16 op base->cbs[fd].udata = NULL;
380 6be81433 2024-04-14 op timerheapify(void)
382 6be81433 2024-04-14 op size_t i, reserve, gap;
384 6be81433 2024-04-14 op reserve = base->reserve_till - base->reserve_from;
385 6be81433 2024-04-14 op if (reserve == 0)
388 6be81433 2024-04-14 op gap = base->reserve_from - base->ntimers;
389 6be81433 2024-04-14 op if (gap != 0) {
390 6be81433 2024-04-14 op memmove(&base->timers[base->ntimers],
391 6be81433 2024-04-14 op &base->timers[base->reserve_from],
392 6be81433 2024-04-14 op reserve * sizeof(*base->timers));
393 6be81433 2024-04-14 op base->reserve_from -= gap;
394 6be81433 2024-04-14 op base->reserve_till -= gap;
397 6be81433 2024-04-14 op base->ntimers = base->reserve_till;
399 6be81433 2024-04-14 op if (base->ntimers < 2)
402 6be81433 2024-04-14 op i = base->ntimers / 2 - 1;
404 6be81433 2024-04-14 op bubbledown(i);
411 6be81433 2024-04-14 op static inline int
412 6be81433 2024-04-14 op poll2ev(int ev)
416 6be81433 2024-04-14 op if (ev & (POLLIN|POLLHUP))
417 6be81433 2024-04-14 op r |= EV_READ;
418 6be81433 2024-04-14 op if (ev & (POLLOUT|POLLWRNORM|POLLWRBAND))
419 6be81433 2024-04-14 op r |= EV_WRITE;
425 6be81433 2024-04-14 op ev_step(void)
427 0a53bd62 2023-08-16 op struct timespec elapsed, beg, end;
428 6be81433 2024-04-14 op struct timeval tv, sub, *min;
429 6be81433 2024-04-14 op struct evcb cb;
433 6be81433 2024-04-14 op timerheapify();
434 6be81433 2024-04-14 op base->reserve_from = base->ntimers;
435 6be81433 2024-04-14 op base->reserve_till = base->ntimers;
439 6be81433 2024-04-14 op if (base->ntimers) {
440 6be81433 2024-04-14 op min = &base->timers[0].tv;
441 6be81433 2024-04-14 op msec = min->tv_sec * 1000 + (min->tv_usec + 999) / 1000;
444 6be81433 2024-04-14 op clock_gettime(CLOCK_MONOTONIC, &beg);
445 6be81433 2024-04-14 op if ((n = poll(base->pfds, base->len, msec)) == -1) {
446 6be81433 2024-04-14 op if (errno != EINTR)
450 6be81433 2024-04-14 op if (n == 0 && min)
451 6be81433 2024-04-14 op memcpy(&tv, min, sizeof(tv));
453 6be81433 2024-04-14 op clock_gettime(CLOCK_MONOTONIC, &end);
454 0a53bd62 2023-08-16 op timespecsub(&end, &beg, &elapsed);
455 6be81433 2024-04-14 op TIMESPEC_TO_TIMEVAL(&tv, &elapsed);
458 6be81433 2024-04-14 op for (i = 0; i < base->ntimers && !ev_stop; /* nop */) {
459 6be81433 2024-04-14 op timersub(&base->timers[i].tv, &tv, &sub);
460 6be81433 2024-04-14 op if (sub.tv_sec <= 0) {
462 6be81433 2024-04-14 op * delete the timer before calling its
463 6be81433 2024-04-14 op * callback; protects from timer that
464 6be81433 2024-04-14 op * attempt to delete themselves.
466 6be81433 2024-04-14 op memcpy(&cb, &base->timers[i].cb, sizeof(cb));
467 6be81433 2024-04-14 op cancel_timer(i);
468 6be81433 2024-04-14 op cb.cb(-1, EV_TIMEOUT, cb.udata);
472 6be81433 2024-04-14 op memcpy(&base->timers[i].tv, &sub, sizeof(sub));
476 6be81433 2024-04-14 op for (i = 0; i < base->len && n > 0 && !ev_stop; ++i) {
477 6be81433 2024-04-14 op if (base->pfds[i].fd == -1)
479 6be81433 2024-04-14 op if (base->pfds[i].revents & (POLLIN|POLLOUT|POLLHUP)) {
481 6be81433 2024-04-14 op base->cbs[i].cb(base->pfds[i].fd,
482 6be81433 2024-04-14 op poll2ev(base->pfds[i].revents),
483 6be81433 2024-04-14 op base->cbs[i].udata);
491 6be81433 2024-04-14 op ev_loop(void)
493 6be81433 2024-04-14 op while (!ev_stop) {
494 6be81433 2024-04-14 op if (ev_step() == -1)
502 0a53bd62 2023-08-16 op ev_break(void)