16 #ifndef HAVE_MONOTONIC
17 # ifdef CLOCK_MONOTONIC
18 # define HAVE_MONOTONIC 1
23 # define HAVE_SELECT 1
31 # define HAVE_REALTIME 1 /* posix requirement, but might be slower */
34 #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
35 #define MAX_BLOCKTIME 60.
40 EV_WATCHER (ev_watcher);
43 struct ev_watcher_list {
44 EV_WATCHER_LIST (ev_watcher_list);
47 struct ev_watcher_time {
48 EV_WATCHER_TIME (ev_watcher_time);
51 typedef struct ev_watcher *W;
52 typedef struct ev_watcher_list *WL;
53 typedef struct ev_watcher_time *WT;
55 static ev_tstamp now, diff; /* monotonic clock */
59 static int have_monotonic; /* runtime */
61 static ev_tstamp method_fudge; /* stupid epoll-returns-early bug */
62 static void (*method_modify)(int fd, int oev, int nev);
63 static void (*method_poll)(ev_tstamp timeout);
65 /*****************************************************************************/
72 clock_gettime (CLOCK_REALTIME, &ts);
73 return ts.tv_sec + ts.tv_nsec * 1e-9;
76 gettimeofday (&tv, 0);
77 return tv.tv_sec + tv.tv_usec * 1e-6;
88 clock_gettime (CLOCK_MONOTONIC, &ts);
89 return ts.tv_sec + ts.tv_nsec * 1e-9;
96 #define array_needsize(base,cur,cnt,init) \
99 int newcnt = cur ? cur << 1 : 16; \
100 fprintf (stderr, "resize(" # base ") from %d to %d\n", cur, newcnt);\
101 base = realloc (base, sizeof (*base) * (newcnt)); \
102 init (base + cur, newcnt - cur); \
106 /*****************************************************************************/
111 unsigned char wev, rev; /* want, received event set */
117 static int *fdchanges;
118 static int fdchangemax, fdchangecnt;
121 anfds_init (ANFD *base, int count)
126 base->wev = base->rev = EV_NONE;
137 static ANPENDING *pendings;
138 static int pendingmax, pendingcnt;
141 event (W w, int events)
143 w->pending = ++pendingcnt;
144 array_needsize (pendings, pendingmax, pendingcnt, );
145 pendings [pendingcnt - 1].w = w;
146 pendings [pendingcnt - 1].events = events;
150 fd_event (int fd, int events)
152 ANFD *anfd = anfds + fd;
155 for (w = anfd->head; w; w = w->next)
157 int ev = w->events & events;
165 queue_events (W *events, int eventcnt, int type)
169 for (i = 0; i < eventcnt; ++i)
170 event (events [i], type);
173 /*****************************************************************************/
175 static struct ev_timer **timers;
176 static int timermax, timercnt;
178 static struct ev_periodic **periodics;
179 static int periodicmax, periodiccnt;
182 upheap (WT *timers, int k)
186 while (k && timers [k >> 1]->at > w->at)
188 timers [k] = timers [k >> 1];
189 timers [k]->active = k + 1;
194 timers [k]->active = k + 1;
199 downheap (WT *timers, int N, int k)
207 if (j + 1 < N && timers [j]->at > timers [j + 1]->at)
210 if (w->at <= timers [j]->at)
213 timers [k] = timers [j];
214 timers [k]->active = k + 1;
219 timers [k]->active = k + 1;
222 /*****************************************************************************/
226 struct ev_signal *head;
230 static ANSIG *signals;
231 static int signalmax;
233 static int sigpipe [2];
234 static sig_atomic_t gotsig;
235 static struct ev_io sigev;
238 signals_init (ANSIG *base, int count)
249 sighandler (int signum)
251 signals [signum - 1].gotsig = 1;
256 write (sigpipe [1], &gotsig, 1);
261 sigcb (struct ev_io *iow, int revents)
267 read (sigpipe [0], &revents, 1);
269 for (sig = signalmax; sig--; )
270 if (signals [sig].gotsig)
272 signals [sig].gotsig = 0;
274 for (w = signals [sig].head; w; w = w->next)
275 event ((W)w, EV_SIGNAL);
282 fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC);
283 fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC);
285 /* rather than sort out wether we really need nb, set it */
286 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK);
287 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK);
289 evio_set (&sigev, sigpipe [0], EV_READ);
293 /*****************************************************************************/
295 static struct ev_idle **idles;
296 static int idlemax, idlecnt;
298 static struct ev_check **checks;
299 static int checkmax, checkcnt;
301 /*****************************************************************************/
304 # include "ev_epoll.c"
307 # include "ev_select.c"
310 int ev_init (int flags)
315 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
327 ev_method = EVMETHOD_NONE;
329 if (ev_method == EVMETHOD_NONE) epoll_init (flags);
332 if (ev_method == EVMETHOD_NONE) select_init (flags);
337 evw_init (&sigev, sigcb);
344 /*****************************************************************************/
346 void ev_prefork (void)
351 void ev_postfork_parent (void)
356 void ev_postfork_child (void)
359 if (ev_method == EVMETHOD_EPOLL)
360 epoll_postfork_child ();
370 /*****************************************************************************/
377 for (i = 0; i < fdchangecnt; ++i)
379 int fd = fdchanges [i];
380 ANFD *anfd = anfds + fd;
385 for (w = anfd->head; w; w = w->next)
388 if (anfd->wev != wev)
390 method_modify (fd, anfd->wev, wev);
403 for (i = 0; i < pendingcnt; ++i)
405 ANPENDING *p = pendings + i;
410 p->w->cb (p->w, p->events);
420 while (timercnt && timers [0]->at <= now)
422 struct ev_timer *w = timers [0];
424 /* first reschedule or stop timer */
427 w->at = now + w->repeat;
428 assert (("timer timeout in the past, negative repeat?", w->at > now));
429 downheap ((WT *)timers, timercnt, 0);
432 evtimer_stop (w); /* nonrepeating: stop timer */
434 event ((W)w, EV_TIMEOUT);
441 while (periodiccnt && periodics [0]->at <= ev_now)
443 struct ev_periodic *w = periodics [0];
445 /* first reschedule or stop timer */
448 w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval;
449 assert (("periodic timeout in the past, negative interval?", w->at > ev_now));
450 downheap ((WT *)periodics, periodiccnt, 0);
453 evperiodic_stop (w); /* nonrepeating: stop timer */
455 event ((W)w, EV_TIMEOUT);
460 time_jump (ev_tstamp diff)
464 /* adjust periodics */
465 for (i = 0; i < periodiccnt; ++i)
467 struct ev_periodic *w = periodics [i];
471 ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval;
473 if (fabs (diff) >= 1e-4)
476 evperiodic_start (w);
478 i = 0; /* restart loop, inefficient, but time jumps should be rare */
483 /* adjust timers. this is easy, as the offset is the same for all */
484 for (i = 0; i < timercnt; ++i)
485 timers [i]->at += diff;
497 ev_tstamp odiff = diff;
499 for (i = 4; --i; ) /* loop a few times, before making important decisions */
504 if (fabs (odiff - diff) < MIN_TIMEJUMP)
505 return; /* all is well */
510 time_jump (diff - odiff);
514 if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP)
515 time_jump (ev_now - now);
523 void ev_loop (int flags)
526 ev_loop_done = flags & EVLOOP_ONESHOT;
530 queue_events ((W *)checks, checkcnt, EV_CHECK);
536 /* update fd-related kernel structures */
539 /* calculate blocking time */
541 /* we only need this for !monotonic clock, but as we always have timers, we just calculate it every time */
544 if (flags & EVLOOP_NONBLOCK || idlecnt)
548 block = MAX_BLOCKTIME;
552 ev_tstamp to = timers [0]->at - get_clock () + method_fudge;
553 if (block > to) block = to;
558 ev_tstamp to = periodics [0]->at - ev_now + method_fudge;
559 if (block > to) block = to;
562 if (block < 0.) block = 0.;
567 /* update ev_now, do magic */
570 /* queue pending timers and reschedule them */
571 periodics_reify (); /* absolute timers first */
572 timers_reify (); /* relative timers second */
574 /* queue idle watchers unless io or timers are pending */
576 queue_events ((W *)idles, idlecnt, EV_IDLE);
578 /* queue check and possibly idle watchers */
579 queue_events ((W *)checks, checkcnt, EV_CHECK);
583 while (!ev_loop_done);
586 /*****************************************************************************/
589 wlist_add (WL *head, WL elem)
596 wlist_del (WL *head, WL elem)
606 head = &(*head)->next;
611 ev_start (W w, int active)
621 pendings [w->pending - 1].w = 0;
626 /*****************************************************************************/
629 evio_start (struct ev_io *w)
631 if (ev_is_active (w))
637 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
638 wlist_add ((WL *)&anfds[fd].head, (WL)w);
641 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
642 fdchanges [fdchangecnt - 1] = fd;
646 evio_stop (struct ev_io *w)
648 if (!ev_is_active (w))
651 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
655 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
656 fdchanges [fdchangecnt - 1] = w->fd;
661 evtimer_start (struct ev_timer *w)
663 if (ev_is_active (w))
668 ev_start ((W)w, ++timercnt);
669 array_needsize (timers, timermax, timercnt, );
670 timers [timercnt - 1] = w;
671 upheap ((WT *)timers, timercnt - 1);
675 evtimer_stop (struct ev_timer *w)
677 if (!ev_is_active (w))
680 if (w->active < timercnt--)
682 timers [w->active - 1] = timers [timercnt];
683 downheap ((WT *)timers, timercnt, w->active - 1);
690 evperiodic_start (struct ev_periodic *w)
692 if (ev_is_active (w))
695 /* this formula differs from the one in periodic_reify because we do not always round up */
697 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval;
699 ev_start ((W)w, ++periodiccnt);
700 array_needsize (periodics, periodicmax, periodiccnt, );
701 periodics [periodiccnt - 1] = w;
702 upheap ((WT *)periodics, periodiccnt - 1);
706 evperiodic_stop (struct ev_periodic *w)
708 if (!ev_is_active (w))
711 if (w->active < periodiccnt--)
713 periodics [w->active - 1] = periodics [periodiccnt];
714 downheap ((WT *)periodics, periodiccnt, w->active - 1);
721 evsignal_start (struct ev_signal *w)
723 if (ev_is_active (w))
727 array_needsize (signals, signalmax, w->signum, signals_init);
728 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
733 sa.sa_handler = sighandler;
734 sigfillset (&sa.sa_mask);
736 sigaction (w->signum, &sa, 0);
741 evsignal_stop (struct ev_signal *w)
743 if (!ev_is_active (w))
746 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
749 if (!signals [w->signum - 1].head)
750 signal (w->signum, SIG_DFL);
753 void evidle_start (struct ev_idle *w)
755 if (ev_is_active (w))
758 ev_start ((W)w, ++idlecnt);
759 array_needsize (idles, idlemax, idlecnt, );
760 idles [idlecnt - 1] = w;
763 void evidle_stop (struct ev_idle *w)
765 idles [w->active - 1] = idles [--idlecnt];
769 void evcheck_start (struct ev_check *w)
771 if (ev_is_active (w))
774 ev_start ((W)w, ++checkcnt);
775 array_needsize (checks, checkmax, checkcnt, );
776 checks [checkcnt - 1] = w;
779 void evcheck_stop (struct ev_check *w)
781 checks [w->active - 1] = checks [--checkcnt];
785 /*****************************************************************************/
792 sin_cb (struct ev_io *w, int revents)
794 fprintf (stderr, "sin %d, revents %d\n", w->fd, revents);
798 ocb (struct ev_timer *w, int revents)
800 //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data);
806 scb (struct ev_signal *w, int revents)
808 fprintf (stderr, "signal %x,%d\n", revents, w->signum);
814 gcb (struct ev_signal *w, int revents)
816 fprintf (stderr, "generic %x\n", revents);
824 evio_init (&wio, sin_cb, 0, EV_READ);
827 struct ev_timer t[10000];
831 for (i = 0; i < 10000; ++i)
833 struct ev_timer *w = t + i;
834 evw_init (w, ocb, i);
835 evtimer_init_abs (w, ocb, drand48 (), 0.99775533);
837 if (drand48 () < 0.5)
843 evtimer_init (&t1, ocb, 5, 10);
846 struct ev_signal sig;
847 evsignal_init (&sig, scb, SIGQUIT);
848 evsignal_start (&sig);
851 evcheck_init (&cw, gcb);
855 evidle_init (&iw, gcb);