2 * This is free and unencumbered software released into the public domain.
4 * Anyone is free to copy, modify, publish, use, compile, sell, or
5 * distribute this software, either in source code form or as a compiled
6 * binary, for any purpose, commercial or non-commercial, and by any
9 * In jurisdictions that recognize copyright laws, the author or authors
10 * of this software dedicate any and all copyright interest in the
11 * software to the public domain. We make this dedication for the benefit
12 * of the public at large and to the detriment of our heirs and
13 * successors. We intend this dedication to be an overt act of
14 * relinquishment in perpetuity of all present and future rights to this
15 * software under copyright law.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
43 void (*cb)(int, int, void *);
68 * Binary heap of timers. At runtime, new timers are added in
69 * the ``reserve'', a space after the caninocal end of the
70 * array, and at the end of every tick they're added to the
73 struct evtimer *timers;
80 static struct evbase *base;
89 t = recallocarray(base->pfds, base->pfdlen, len, sizeof(*base->pfds));
95 for (i = base->len; i < len; ++i)
96 base->pfds[i].fd = -1;
98 t = recallocarray(base->cbs, base->cblen, len, sizeof(*base->cbs));
116 if ((base = calloc(1, sizeof(*base))) == NULL)
119 base->sigpipe[0] = -1;
120 base->sigpipe[1] = -1;
122 if (ev_resize(16) == -1) {
147 ev_add(int fd, int ev, void (*cb)(int, int, void *), void *udata)
154 if ((size_t)fd >= base->len) {
155 if (ev_resize(fd + 1) == -1)
159 base->pfds[fd].fd = fd;
160 base->pfds[fd].events = ev2poll(ev);
161 base->pfds[fd].revents = 0;
163 base->cbs[fd].cb = cb;
164 base->cbs[fd].udata = udata;
170 ev_sigcatch(int signo)
178 * We should be able to write up to PIPE_BUF bytes without
182 (void) write(base->sigpipe[1], &s, sizeof(s));
188 ev_sigdispatch(int fd, int ev, void *data)
192 if (read(fd, &signo, sizeof(signo)) != sizeof(signo))
195 base->sigcb.cb(signo, EV_SIGNAL, base->sigcb.udata);
199 ev_signal(int sig, void (*cb)(int, int, void *), void *udata)
203 if (base->sigpipe[0] == -1) {
204 /* pipe2(2) is not available everywhere... sigh */
205 if (pipe(base->sigpipe) == -1)
208 if ((flags = fcntl(base->sigpipe[1], F_GETFL)) == -1 ||
209 fcntl(base->sigpipe[1], F_SETFL, flags | O_NONBLOCK) == -1)
212 if (ev_add(base->sigpipe[0], EV_READ, ev_sigdispatch, NULL)
218 base->sigcb.udata = udata;
220 signal(sig, ev_sigcatch);
225 ev_timer(const struct timeval *tv, void (*cb)(int, int, void*), void *udata)
237 if (base->reserve_till == base->timerscap) {
238 newcap = base->timerscap + 8;
239 t = recallocarray(base->timers, base->timerscap, newcap,
240 sizeof(*base->timers));
244 base->timerscap = newcap;
247 if ((nextid = ++base->tid) == 0)
248 nextid = ++base->tid;
250 evt = &base->timers[base->reserve_till];
252 memcpy(&evt->tv, tv, sizeof(*tv));
254 evt->cb.udata = udata;
256 base->reserve_till++;
261 find_timer(unsigned int id, size_t *pos)
268 for (i = 0; i < base->ntimers; ++i) {
269 if (base->timers[i].id == id) {
275 for (i = base->reserve_from; i < base->reserve_till; ++i) {
276 if (base->timers[i].id == id) {
286 ev_timer_pending(unsigned int id)
290 return (find_timer(id, &i));
303 /* base case: there are no children */
304 if (l >= base->ntimers)
307 /* find the smaller child */
309 if (r >= base->ntimers ||
310 timercmp(&base->timers[l].tv, &base->timers[r].tv, <))
313 /* other base case: it's at the right place */
314 if (timercmp(&base->timers[i].tv, &base->timers[s].tv, <))
318 memcpy(&tmp, &base->timers[s], sizeof(tmp));
319 memcpy(&base->timers[s], &base->timers[i], sizeof(tmp));
320 memcpy(&base->timers[i], &tmp, sizeof(tmp));
327 cancel_timer(size_t i)
330 if (i != base->ntimers) {
331 memcpy(&base->timers[i], &base->timers[base->ntimers],
332 sizeof(*base->timers));
338 ev_timer_cancel(unsigned int id)
342 if (!find_timer(id, &i))
345 if (i < base->ntimers) {
350 base->reserve_till--;
351 if (i != base->reserve_till)
352 memcpy(&base->timers[i], &base->timers[base->reserve_till],
353 sizeof(*base->timers));
365 if ((size_t)fd >= base->len) {
370 base->pfds[fd].fd = -1;
371 base->pfds[fd].events = 0;
373 base->cbs[fd].cb = NULL;
374 base->cbs[fd].udata = NULL;
382 size_t i, reserve, gap;
384 reserve = base->reserve_till - base->reserve_from;
388 gap = base->reserve_from - base->ntimers;
390 memmove(&base->timers[base->ntimers],
391 &base->timers[base->reserve_from],
392 reserve * sizeof(*base->timers));
393 base->reserve_from -= gap;
394 base->reserve_till -= gap;
397 base->ntimers = base->reserve_till;
399 if (base->ntimers < 2)
402 i = base->ntimers / 2 - 1;
416 if (ev & (POLLIN|POLLHUP))
418 if (ev & (POLLOUT|POLLWRNORM|POLLWRBAND))
427 struct timespec elapsed, beg, end;
428 struct timeval tv, sub, *min;
434 base->reserve_from = base->ntimers;
435 base->reserve_till = base->ntimers;
440 min = &base->timers[0].tv;
441 msec = min->tv_sec * 1000 + (min->tv_usec + 999) / 1000;
444 clock_gettime(CLOCK_MONOTONIC, &beg);
445 if ((n = poll(base->pfds, base->len, msec)) == -1) {
451 memcpy(&tv, min, sizeof(tv));
453 clock_gettime(CLOCK_MONOTONIC, &end);
454 timespecsub(&end, &beg, &elapsed);
455 TIMESPEC_TO_TIMEVAL(&tv, &elapsed);
458 for (i = 0; i < base->ntimers && !ev_stop; /* nop */) {
459 timersub(&base->timers[i].tv, &tv, &sub);
460 if (sub.tv_sec <= 0) {
462 * delete the timer before calling its
463 * callback; protects from timer that
464 * attempt to delete themselves.
466 memcpy(&cb, &base->timers[i].cb, sizeof(cb));
468 cb.cb(-1, EV_TIMEOUT, cb.udata);
472 memcpy(&base->timers[i].tv, &sub, sizeof(sub));
476 for (i = 0; i < base->len && n > 0 && !ev_stop; ++i) {
477 if (base->pfds[i].fd == -1)
479 if (base->pfds[i].revents & (POLLIN|POLLOUT|POLLHUP)) {
481 base->cbs[i].cb(base->pfds[i].fd,
482 poll2ev(base->pfds[i].revents),