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