14 #ifdef CLOCK_MONOTONIC
15 # define HAVE_MONOTONIC 1
18 #define HAVE_REALTIME 1
22 #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
23 #define MAX_BLOCKTIME 60.
28 EV_WATCHER (ev_watcher);
31 struct ev_watcher_list {
32 EV_WATCHER_LIST (ev_watcher_list);
35 static ev_tstamp now, diff; /* monotonic clock */
39 static int have_monotonic; /* runtime */
41 static ev_tstamp method_fudge; /* stupid epoll-returns-early bug */
42 static void (*method_modify)(int fd, int oev, int nev);
43 static void (*method_poll)(ev_tstamp timeout);
45 /*****************************************************************************/
52 clock_gettime (CLOCK_REALTIME, &ts);
53 return ts.tv_sec + ts.tv_nsec * 1e-9;
56 gettimeofday (&tv, 0);
57 return tv.tv_sec + tv.tv_usec * 1e-6;
68 clock_gettime (CLOCK_MONOTONIC, &ts);
69 return ts.tv_sec + ts.tv_nsec * 1e-9;
76 #define array_needsize(base,cur,cnt,init) \
79 int newcnt = cur ? cur << 1 : 16; \
80 fprintf (stderr, "resize(" # base ") from %d to %d\n", cur, newcnt);\
81 base = realloc (base, sizeof (*base) * (newcnt)); \
82 init (base + cur, newcnt - cur); \
86 /*****************************************************************************/
91 unsigned char wev, rev; /* want, received event set */
97 static int *fdchanges;
98 static int fdchangemax, fdchangecnt;
101 anfds_init (ANFD *base, int count)
106 base->wev = base->rev = EV_NONE;
113 struct ev_watcher *w;
117 static ANPENDING *pendings;
118 static int pendingmax, pendingcnt;
121 event (struct ev_watcher *w, int events)
123 w->pending = ++pendingcnt;
124 array_needsize (pendings, pendingmax, pendingcnt, );
125 pendings [pendingcnt - 1].w = w;
126 pendings [pendingcnt - 1].events = events;
130 fd_event (int fd, int events)
132 ANFD *anfd = anfds + fd;
135 for (w = anfd->head; w; w = w->next)
137 int ev = w->events & events;
140 event ((struct ev_watcher *)w, ev);
145 queue_events (struct ev_watcher **events, int eventcnt, int type)
149 for (i = 0; i < eventcnt; ++i)
150 event (events [i], type);
153 /*****************************************************************************/
155 static struct ev_timer **atimers;
156 static int atimermax, atimercnt;
158 static struct ev_timer **rtimers;
159 static int rtimermax, rtimercnt;
162 upheap (struct ev_timer **timers, int k)
164 struct ev_timer *w = timers [k];
166 while (k && timers [k >> 1]->at > w->at)
168 timers [k] = timers [k >> 1];
169 timers [k]->active = k + 1;
174 timers [k]->active = k + 1;
179 downheap (struct ev_timer **timers, int N, int k)
181 struct ev_timer *w = timers [k];
187 if (j + 1 < N && timers [j]->at > timers [j + 1]->at)
190 if (w->at <= timers [j]->at)
193 timers [k] = timers [j];
194 timers [k]->active = k + 1;
199 timers [k]->active = k + 1;
202 /*****************************************************************************/
206 struct ev_signal *head;
210 static ANSIG *signals;
211 static int signalmax;
213 static int sigpipe [2];
214 static sig_atomic_t gotsig;
215 static struct ev_io sigev;
218 signals_init (ANSIG *base, int count)
229 sighandler (int signum)
231 signals [signum - 1].gotsig = 1;
236 write (sigpipe [1], &gotsig, 1);
241 sigcb (struct ev_io *iow, int revents)
247 read (sigpipe [0], &revents, 1);
249 for (sig = signalmax; sig--; )
250 if (signals [sig].gotsig)
252 signals [sig].gotsig = 0;
254 for (w = signals [sig].head; w; w = w->next)
255 event ((struct ev_watcher *)w, EV_SIGNAL);
262 fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC);
263 fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC);
265 /* rather than sort out wether we really need nb, set it */
266 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK);
267 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK);
269 evio_set (&sigev, sigpipe [0], EV_READ);
273 /*****************************************************************************/
275 static struct ev_idle **idles;
276 static int idlemax, idlecnt;
278 static struct ev_check **checks;
279 static int checkmax, checkcnt;
281 /*****************************************************************************/
284 # include "ev_epoll.c"
287 # include "ev_select.c"
290 int ev_init (int flags)
295 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
307 ev_method = EVMETHOD_NONE;
309 if (ev_method == EVMETHOD_NONE) epoll_init (flags);
312 if (ev_method == EVMETHOD_NONE) select_init (flags);
317 evw_init (&sigev, sigcb, 0);
324 /*****************************************************************************/
326 void ev_prefork (void)
330 void ev_postfork_parent (void)
334 void ev_postfork_child (void)
337 if (ev_method == EVMETHOD_EPOLL)
338 epoll_postfork_child ();
348 /*****************************************************************************/
355 for (i = 0; i < fdchangecnt; ++i)
357 int fd = fdchanges [i];
358 ANFD *anfd = anfds + fd;
363 for (w = anfd->head; w; w = w->next)
366 if (anfd->wev != wev)
368 method_modify (fd, anfd->wev, wev);
381 for (i = 0; i < pendingcnt; ++i)
383 ANPENDING *p = pendings + i;
388 p->w->cb (p->w, p->events);
396 timers_reify (struct ev_timer **timers, int timercnt, ev_tstamp now)
398 while (timercnt && timers [0]->at <= now)
400 struct ev_timer *w = timers [0];
402 /* first reschedule or stop timer */
406 w->at += floor ((now - w->at) / w->repeat + 1.) * w->repeat;
408 w->at = now + w->repeat;
410 assert (w->at > now);
412 downheap (timers, timercnt, 0);
416 evtimer_stop (w); /* nonrepeating: stop timer */
417 --timercnt; /* maybe pass by reference instead? */
420 event ((struct ev_watcher *)w, EV_TIMEOUT);
432 ev_tstamp odiff = diff;
434 /* detecting time jumps is much more difficult */
435 for (i = 2; --i; ) /* loop a few times, before making important decisions */
440 if (fabs (odiff - diff) < MIN_TIMEJUMP)
441 return; /* all is well */
446 /* time jump detected, reschedule atimers */
447 for (i = 0; i < atimercnt; ++i)
449 struct ev_timer *w = atimers [i];
450 w->at += ceil ((ev_now - w->at) / w->repeat + 1.) * w->repeat;
455 if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP)
456 /* time jump detected, adjust rtimers */
457 for (i = 0; i < rtimercnt; ++i)
458 rtimers [i]->at += ev_now - now;
466 void ev_loop (int flags)
469 ev_loop_done = flags & EVLOOP_ONESHOT;
473 queue_events (checks, checkcnt, EV_CHECK);
479 /* update fd-related kernel structures */
482 /* calculate blocking time */
483 if (flags & EVLOOP_NONBLOCK || idlecnt)
487 block = MAX_BLOCKTIME;
491 ev_tstamp to = rtimers [0]->at - get_clock () + method_fudge;
492 if (block > to) block = to;
497 ev_tstamp to = atimers [0]->at - ev_time () + method_fudge;
498 if (block > to) block = to;
501 if (block < 0.) block = 0.;
506 /* update ev_now, do magic */
509 /* queue pending timers and reschedule them */
510 /* absolute timers first */
511 timers_reify (atimers, atimercnt, ev_now);
512 /* relative timers second */
513 timers_reify (rtimers, rtimercnt, now);
515 /* queue idle watchers unless io or timers are pending */
517 queue_events (idles, idlecnt, EV_IDLE);
519 /* queue check and possibly idle watchers */
520 queue_events (checks, checkcnt, EV_CHECK);
524 while (!ev_loop_done);
527 /*****************************************************************************/
530 wlist_add (struct ev_watcher_list **head, struct ev_watcher_list *elem)
537 wlist_del (struct ev_watcher_list **head, struct ev_watcher_list *elem)
547 head = &(*head)->next;
552 ev_start (struct ev_watcher *w, int active)
559 ev_stop (struct ev_watcher *w)
562 pendings [w->pending - 1].w = 0;
568 /*****************************************************************************/
571 evio_start (struct ev_io *w)
573 if (ev_is_active (w))
578 ev_start ((struct ev_watcher *)w, 1);
579 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
580 wlist_add ((struct ev_watcher_list **)&anfds[fd].head, (struct ev_watcher_list *)w);
583 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
584 fdchanges [fdchangecnt - 1] = fd;
588 evio_stop (struct ev_io *w)
590 if (!ev_is_active (w))
593 wlist_del ((struct ev_watcher_list **)&anfds[w->fd].head, (struct ev_watcher_list *)w);
594 ev_stop ((struct ev_watcher *)w);
597 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
598 fdchanges [fdchangecnt - 1] = w->fd;
602 evtimer_start (struct ev_timer *w)
604 if (ev_is_active (w))
609 /* this formula differs from the one in timer_reify becuse we do not round up */
611 w->at += ceil ((ev_now - w->at) / w->repeat) * w->repeat;
613 ev_start ((struct ev_watcher *)w, ++atimercnt);
614 array_needsize (atimers, atimermax, atimercnt, );
615 atimers [atimercnt - 1] = w;
616 upheap (atimers, atimercnt - 1);
622 ev_start ((struct ev_watcher *)w, ++rtimercnt);
623 array_needsize (rtimers, rtimermax, rtimercnt, );
624 rtimers [rtimercnt - 1] = w;
625 upheap (rtimers, rtimercnt - 1);
631 evtimer_stop (struct ev_timer *w)
633 if (!ev_is_active (w))
638 if (w->active < atimercnt--)
640 atimers [w->active - 1] = atimers [atimercnt];
641 downheap (atimers, atimercnt, w->active - 1);
646 if (w->active < rtimercnt--)
648 rtimers [w->active - 1] = rtimers [rtimercnt];
649 downheap (rtimers, rtimercnt, w->active - 1);
653 ev_stop ((struct ev_watcher *)w);
657 evsignal_start (struct ev_signal *w)
659 if (ev_is_active (w))
662 ev_start ((struct ev_watcher *)w, 1);
663 array_needsize (signals, signalmax, w->signum, signals_init);
664 wlist_add ((struct ev_watcher_list **)&signals [w->signum - 1].head, (struct ev_watcher_list *)w);
669 sa.sa_handler = sighandler;
670 sigfillset (&sa.sa_mask);
672 sigaction (w->signum, &sa, 0);
677 evsignal_stop (struct ev_signal *w)
679 if (!ev_is_active (w))
682 wlist_del ((struct ev_watcher_list **)&signals [w->signum - 1].head, (struct ev_watcher_list *)w);
683 ev_stop ((struct ev_watcher *)w);
685 if (!signals [w->signum - 1].head)
686 signal (w->signum, SIG_DFL);
689 void evidle_start (struct ev_idle *w)
691 if (ev_is_active (w))
694 ev_start ((struct ev_watcher *)w, ++idlecnt);
695 array_needsize (idles, idlemax, idlecnt, );
696 idles [idlecnt - 1] = w;
699 void evidle_stop (struct ev_idle *w)
701 idles [w->active - 1] = idles [--idlecnt];
702 ev_stop ((struct ev_watcher *)w);
705 void evcheck_start (struct ev_check *w)
707 if (ev_is_active (w))
710 ev_start ((struct ev_watcher *)w, ++checkcnt);
711 array_needsize (checks, checkmax, checkcnt, );
712 checks [checkcnt - 1] = w;
715 void evcheck_stop (struct ev_check *w)
717 checks [w->active - 1] = checks [--checkcnt];
718 ev_stop ((struct ev_watcher *)w);
721 /*****************************************************************************/
725 sin_cb (struct ev_io *w, int revents)
727 fprintf (stderr, "sin %d, revents %d\n", w->fd, revents);
731 ocb (struct ev_timer *w, int revents)
733 //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data);
739 scb (struct ev_signal *w, int revents)
741 fprintf (stderr, "signal %x,%d\n", revents, w->signum);
745 gcb (struct ev_signal *w, int revents)
747 fprintf (stderr, "generic %x\n", revents);
756 evw_init (&sin, sin_cb, 55);
757 evio_set (&sin, 0, EV_READ);
760 struct ev_timer t[10000];
764 for (i = 0; i < 10000; ++i)
766 struct ev_timer *w = t + i;
767 evw_init (w, ocb, i);
768 evtimer_set_abs (w, drand48 (), 0.99775533);
770 if (drand48 () < 0.5)
776 evw_init (&t1, ocb, 0);
777 evtimer_set_abs (&t1, 5, 10);
780 struct ev_signal sig;
781 evw_init (&sig, scb, 65535);
782 evsignal_set (&sig, SIGQUIT);
783 evsignal_start (&sig);
786 evw_init (&cw, gcb, 0);
790 evw_init (&iw, gcb, 0);