14 #ifndef HAVE_MONOTONIC
15 # ifdef CLOCK_MONOTONIC
16 # define HAVE_MONOTONIC 1
21 # define HAVE_SELECT 1
29 # define HAVE_REALTIME 1 /* posix requirement, but might be slower */
32 #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
33 #define MAX_BLOCKTIME 60.
38 EV_WATCHER (ev_watcher);
41 struct ev_watcher_list {
42 EV_WATCHER_LIST (ev_watcher_list);
45 typedef struct ev_watcher *W;
46 typedef struct ev_watcher_list *WL;
48 static ev_tstamp now, diff; /* monotonic clock */
52 static int have_monotonic; /* runtime */
54 static ev_tstamp method_fudge; /* stupid epoll-returns-early bug */
55 static void (*method_modify)(int fd, int oev, int nev);
56 static void (*method_poll)(ev_tstamp timeout);
58 /*****************************************************************************/
65 clock_gettime (CLOCK_REALTIME, &ts);
66 return ts.tv_sec + ts.tv_nsec * 1e-9;
69 gettimeofday (&tv, 0);
70 return tv.tv_sec + tv.tv_usec * 1e-6;
81 clock_gettime (CLOCK_MONOTONIC, &ts);
82 return ts.tv_sec + ts.tv_nsec * 1e-9;
89 #define array_needsize(base,cur,cnt,init) \
92 int newcnt = cur ? cur << 1 : 16; \
93 fprintf (stderr, "resize(" # base ") from %d to %d\n", cur, newcnt);\
94 base = realloc (base, sizeof (*base) * (newcnt)); \
95 init (base + cur, newcnt - cur); \
99 /*****************************************************************************/
104 unsigned char wev, rev; /* want, received event set */
110 static int *fdchanges;
111 static int fdchangemax, fdchangecnt;
114 anfds_init (ANFD *base, int count)
119 base->wev = base->rev = EV_NONE;
130 static ANPENDING *pendings;
131 static int pendingmax, pendingcnt;
134 event (W w, int events)
136 w->pending = ++pendingcnt;
137 array_needsize (pendings, pendingmax, pendingcnt, );
138 pendings [pendingcnt - 1].w = w;
139 pendings [pendingcnt - 1].events = events;
143 fd_event (int fd, int events)
145 ANFD *anfd = anfds + fd;
148 for (w = anfd->head; w; w = w->next)
150 int ev = w->events & events;
158 queue_events (W *events, int eventcnt, int type)
162 for (i = 0; i < eventcnt; ++i)
163 event (events [i], type);
166 /*****************************************************************************/
168 static struct ev_timer **atimers;
169 static int atimermax, atimercnt;
171 static struct ev_timer **rtimers;
172 static int rtimermax, rtimercnt;
175 upheap (struct ev_timer **timers, int k)
177 struct ev_timer *w = timers [k];
179 while (k && timers [k >> 1]->at > w->at)
181 timers [k] = timers [k >> 1];
182 timers [k]->active = k + 1;
187 timers [k]->active = k + 1;
192 downheap (struct ev_timer **timers, int N, int k)
194 struct ev_timer *w = timers [k];
200 if (j + 1 < N && timers [j]->at > timers [j + 1]->at)
203 if (w->at <= timers [j]->at)
206 timers [k] = timers [j];
207 timers [k]->active = k + 1;
212 timers [k]->active = k + 1;
215 /*****************************************************************************/
219 struct ev_signal *head;
223 static ANSIG *signals;
224 static int signalmax;
226 static int sigpipe [2];
227 static sig_atomic_t gotsig;
228 static struct ev_io sigev;
231 signals_init (ANSIG *base, int count)
242 sighandler (int signum)
244 signals [signum - 1].gotsig = 1;
249 write (sigpipe [1], &gotsig, 1);
254 sigcb (struct ev_io *iow, int revents)
260 read (sigpipe [0], &revents, 1);
262 for (sig = signalmax; sig--; )
263 if (signals [sig].gotsig)
265 signals [sig].gotsig = 0;
267 for (w = signals [sig].head; w; w = w->next)
268 event ((W)w, EV_SIGNAL);
275 fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC);
276 fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC);
278 /* rather than sort out wether we really need nb, set it */
279 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK);
280 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK);
282 evio_set (&sigev, sigpipe [0], EV_READ);
286 /*****************************************************************************/
288 static struct ev_idle **idles;
289 static int idlemax, idlecnt;
291 static struct ev_check **checks;
292 static int checkmax, checkcnt;
294 /*****************************************************************************/
297 # include "ev_epoll.c"
300 # include "ev_select.c"
303 int ev_init (int flags)
308 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
320 ev_method = EVMETHOD_NONE;
322 if (ev_method == EVMETHOD_NONE) epoll_init (flags);
325 if (ev_method == EVMETHOD_NONE) select_init (flags);
330 evw_init (&sigev, sigcb, 0);
337 /*****************************************************************************/
339 void ev_prefork (void)
344 void ev_postfork_parent (void)
349 void ev_postfork_child (void)
352 if (ev_method == EVMETHOD_EPOLL)
353 epoll_postfork_child ();
363 /*****************************************************************************/
370 for (i = 0; i < fdchangecnt; ++i)
372 int fd = fdchanges [i];
373 ANFD *anfd = anfds + fd;
378 for (w = anfd->head; w; w = w->next)
381 if (anfd->wev != wev)
383 method_modify (fd, anfd->wev, wev);
396 for (i = 0; i < pendingcnt; ++i)
398 ANPENDING *p = pendings + i;
403 p->w->cb (p->w, p->events);
411 timers_reify (struct ev_timer **timers, int timercnt, ev_tstamp now)
413 while (timercnt && timers [0]->at <= now)
415 struct ev_timer *w = timers [0];
417 /* first reschedule or stop timer */
421 w->at += floor ((now - w->at) / w->repeat + 1.) * w->repeat;
423 w->at = now + w->repeat;
425 assert (w->at > now);
427 downheap (timers, timercnt, 0);
431 evtimer_stop (w); /* nonrepeating: stop timer */
432 --timercnt; /* maybe pass by reference instead? */
435 event ((W)w, EV_TIMEOUT);
447 ev_tstamp odiff = diff;
449 /* detecting time jumps is much more difficult */
450 for (i = 2; --i; ) /* loop a few times, before making important decisions */
455 if (fabs (odiff - diff) < MIN_TIMEJUMP)
456 return; /* all is well */
461 /* time jump detected, reschedule atimers */
462 for (i = 0; i < atimercnt; ++i)
464 struct ev_timer *w = atimers [i];
465 w->at += ceil ((ev_now - w->at) / w->repeat + 1.) * w->repeat;
470 if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP)
471 /* time jump detected, adjust rtimers */
472 for (i = 0; i < rtimercnt; ++i)
473 rtimers [i]->at += ev_now - now;
481 void ev_loop (int flags)
484 ev_loop_done = flags & EVLOOP_ONESHOT;
488 queue_events ((W *)checks, checkcnt, EV_CHECK);
494 /* update fd-related kernel structures */
497 /* calculate blocking time */
498 if (flags & EVLOOP_NONBLOCK || idlecnt)
502 block = MAX_BLOCKTIME;
506 ev_tstamp to = rtimers [0]->at - get_clock () + method_fudge;
507 if (block > to) block = to;
512 ev_tstamp to = atimers [0]->at - ev_time () + method_fudge;
513 if (block > to) block = to;
516 if (block < 0.) block = 0.;
521 /* update ev_now, do magic */
524 /* queue pending timers and reschedule them */
525 /* absolute timers first */
526 timers_reify (atimers, atimercnt, ev_now);
527 /* relative timers second */
528 timers_reify (rtimers, rtimercnt, now);
530 /* queue idle watchers unless io or timers are pending */
532 queue_events ((W *)idles, idlecnt, EV_IDLE);
534 /* queue check and possibly idle watchers */
535 queue_events ((W *)checks, checkcnt, EV_CHECK);
539 while (!ev_loop_done);
542 /*****************************************************************************/
545 wlist_add (WL *head, WL elem)
552 wlist_del (WL *head, WL elem)
562 head = &(*head)->next;
567 ev_start (W w, int active)
577 pendings [w->pending - 1].w = 0;
582 /*****************************************************************************/
585 evio_start (struct ev_io *w)
587 if (ev_is_active (w))
593 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
594 wlist_add ((WL *)&anfds[fd].head, (WL)w);
597 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
598 fdchanges [fdchangecnt - 1] = fd;
602 evio_stop (struct ev_io *w)
604 if (!ev_is_active (w))
607 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
611 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
612 fdchanges [fdchangecnt - 1] = w->fd;
616 evtimer_start (struct ev_timer *w)
618 if (ev_is_active (w))
623 /* this formula differs from the one in timer_reify becuse we do not round up */
625 w->at += ceil ((ev_now - w->at) / w->repeat) * w->repeat;
627 ev_start ((W)w, ++atimercnt);
628 array_needsize (atimers, atimermax, atimercnt, );
629 atimers [atimercnt - 1] = w;
630 upheap (atimers, atimercnt - 1);
636 ev_start ((W)w, ++rtimercnt);
637 array_needsize (rtimers, rtimermax, rtimercnt, );
638 rtimers [rtimercnt - 1] = w;
639 upheap (rtimers, rtimercnt - 1);
645 evtimer_stop (struct ev_timer *w)
647 if (!ev_is_active (w))
652 if (w->active < atimercnt--)
654 atimers [w->active - 1] = atimers [atimercnt];
655 downheap (atimers, atimercnt, w->active - 1);
660 if (w->active < rtimercnt--)
662 rtimers [w->active - 1] = rtimers [rtimercnt];
663 downheap (rtimers, rtimercnt, w->active - 1);
671 evsignal_start (struct ev_signal *w)
673 if (ev_is_active (w))
677 array_needsize (signals, signalmax, w->signum, signals_init);
678 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
683 sa.sa_handler = sighandler;
684 sigfillset (&sa.sa_mask);
686 sigaction (w->signum, &sa, 0);
691 evsignal_stop (struct ev_signal *w)
693 if (!ev_is_active (w))
696 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
699 if (!signals [w->signum - 1].head)
700 signal (w->signum, SIG_DFL);
703 void evidle_start (struct ev_idle *w)
705 if (ev_is_active (w))
708 ev_start ((W)w, ++idlecnt);
709 array_needsize (idles, idlemax, idlecnt, );
710 idles [idlecnt - 1] = w;
713 void evidle_stop (struct ev_idle *w)
715 idles [w->active - 1] = idles [--idlecnt];
719 void evcheck_start (struct ev_check *w)
721 if (ev_is_active (w))
724 ev_start ((W)w, ++checkcnt);
725 array_needsize (checks, checkmax, checkcnt, );
726 checks [checkcnt - 1] = w;
729 void evcheck_stop (struct ev_check *w)
731 checks [w->active - 1] = checks [--checkcnt];
735 /*****************************************************************************/
740 sin_cb (struct ev_io *w, int revents)
742 fprintf (stderr, "sin %d, revents %d\n", w->fd, revents);
746 ocb (struct ev_timer *w, int revents)
748 //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data);
754 scb (struct ev_signal *w, int revents)
756 fprintf (stderr, "signal %x,%d\n", revents, w->signum);
760 gcb (struct ev_signal *w, int revents)
762 fprintf (stderr, "generic %x\n", revents);
771 evw_init (&sin, sin_cb, 55);
772 evio_set (&sin, 0, EV_READ);
775 struct ev_timer t[10000];
779 for (i = 0; i < 10000; ++i)
781 struct ev_timer *w = t + i;
782 evw_init (w, ocb, i);
783 evtimer_set_abs (w, drand48 (), 0.99775533);
785 if (drand48 () < 0.5)
791 evw_init (&t1, ocb, 0);
792 evtimer_set_abs (&t1, 5, 10);
795 struct ev_signal sig;
796 evw_init (&sig, scb, 65535);
797 evsignal_set (&sig, SIGQUIT);
798 evsignal_start (&sig);
801 evw_init (&cw, gcb, 0);
805 evw_init (&iw, gcb, 0);