11 #ifdef CLOCK_MONOTONIC
12 # define HAVE_MONOTONIC 1
16 #define HAVE_REALTIME 1
19 #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
20 #define MAX_BLOCKTIME 60.
25 EV_WATCHER (ev_watcher);
28 struct ev_watcher_list {
29 EV_WATCHER_LIST (ev_watcher_list);
32 static ev_tstamp now, diff; /* monotonic clock */
36 static int have_monotonic; /* runtime */
38 static ev_tstamp method_fudge; /* stupid epoll-returns-early bug */
39 static void (*method_modify)(int fd, int oev, int nev);
40 static void (*method_poll)(ev_tstamp timeout);
47 clock_gettime (CLOCK_REALTIME, &ts);
48 return ts.tv_sec + ts.tv_nsec * 1e-9;
51 gettimeofday (&tv, 0);
52 return tv.tv_sec + tv.tv_usec * 1e-6;
63 clock_gettime (CLOCK_MONOTONIC, &ts);
64 return ts.tv_sec + ts.tv_nsec * 1e-9;
71 #define array_needsize(base,cur,cnt,init) \
74 int newcnt = cur ? cur << 1 : 16; \
75 fprintf (stderr, "resize(" # base ") from %d to %d\n", cur, newcnt);\
76 base = realloc (base, sizeof (*base) * (newcnt)); \
77 init (base + cur, newcnt - cur); \
84 unsigned char wev, rev; /* want, received event set */
90 static int *fdchanges;
91 static int fdchangemax, fdchangecnt;
94 anfds_init (ANFD *base, int count)
99 base->wev = base->rev = EV_NONE;
106 struct ev_watcher *w;
110 static ANPENDING *pendings;
111 static int pendingmax, pendingcnt;
114 event (struct ev_watcher *w, int events)
116 w->pending = ++pendingcnt;
117 array_needsize (pendings, pendingmax, pendingcnt, );
118 pendings [pendingcnt - 1].w = w;
119 pendings [pendingcnt - 1].events = events;
123 fd_event (int fd, int events)
125 ANFD *anfd = anfds + fd;
128 for (w = anfd->head; w; w = w->next)
130 int ev = w->events & events;
133 event ((struct ev_watcher *)w, ev);
137 static struct ev_timer **atimers;
138 static int atimermax, atimercnt;
140 static struct ev_timer **rtimers;
141 static int rtimermax, rtimercnt;
144 upheap (struct ev_timer **timers, int k)
146 struct ev_timer *w = timers [k];
148 while (k && timers [k >> 1]->at > w->at)
150 timers [k] = timers [k >> 1];
151 timers [k]->active = k + 1;
156 timers [k]->active = k + 1;
161 downheap (struct ev_timer **timers, int N, int k)
163 struct ev_timer *w = timers [k];
169 if (j + 1 < N && timers [j]->at > timers [j + 1]->at)
172 if (w->at <= timers [j]->at)
175 timers [k] = timers [j];
176 timers [k]->active = k + 1;
181 timers [k]->active = k + 1;
184 static struct ev_signal **signals;
185 static int signalmax;
188 signals_init (struct ev_signal **base, int count)
195 # include "ev_epoll.c"
198 # include "ev_select.c"
201 int ev_init (int flags)
206 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
216 if (epoll_init (flags))
220 if (select_init (flags))
224 ev_method = EVMETHOD_NONE;
228 void ev_prefork (void)
232 void ev_postfork_parent (void)
236 void ev_postfork_child (void)
239 if (ev_method == EVMETHOD_EPOLL)
240 epoll_postfork_child ();
249 for (i = 0; i < fdchangecnt; ++i)
251 int fd = fdchanges [i];
252 ANFD *anfd = anfds + fd;
257 for (w = anfd->head; w; w = w->next)
260 if (anfd->wev != wev)
262 method_modify (fd, anfd->wev, wev);
275 for (i = 0; i < pendingcnt; ++i)
277 ANPENDING *p = pendings + i;
282 p->w->cb (p->w, p->events);
290 timers_reify (struct ev_timer **timers, int timercnt, ev_tstamp now)
292 while (timercnt && timers [0]->at <= now)
294 struct ev_timer *w = timers [0];
296 /* first reschedule or stop timer */
300 w->at += floor ((now - w->at) / w->repeat + 1.) * w->repeat;
302 w->at = now + w->repeat;
304 assert (w->at > now);
306 downheap (timers, timercnt, 0);
310 evtimer_stop (w); /* nonrepeating: stop timer */
311 --timercnt; /* maybe pass by reference instead? */
314 event ((struct ev_watcher *)w, EV_TIMEOUT);
326 ev_tstamp odiff = diff;
328 /* detecting time jumps is much more difficult */
329 for (i = 2; --i; ) /* loop a few times, before making important decisions */
334 if (fabs (odiff - diff) < MIN_TIMEJUMP)
335 return; /* all is well */
340 /* time jump detected, reschedule atimers */
341 for (i = 0; i < atimercnt; ++i)
343 struct ev_timer *w = atimers [i];
344 w->at += ceil ((ev_now - w->at) / w->repeat + 1.) * w->repeat;
349 if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP)
350 /* time jump detected, adjust rtimers */
351 for (i = 0; i < rtimercnt; ++i)
352 rtimers [i]->at += ev_now - now;
360 void ev_loop (int flags)
363 ev_loop_done = flags & EVLOOP_ONESHOT;
367 /* update fd-related kernel structures */
370 /* calculate blocking time */
371 if (flags & EVLOOP_NONBLOCK)
375 block = MAX_BLOCKTIME;
379 ev_tstamp to = rtimers [0]->at - get_clock () + method_fudge;
380 if (block > to) block = to;
385 ev_tstamp to = atimers [0]->at - ev_time () + method_fudge;
386 if (block > to) block = to;
389 if (block < 0.) block = 0.;
394 /* update ev_now, do magic */
397 /* put pending timers into pendign queue and reschedule them */
398 /* absolute timers first */
399 timers_reify (atimers, atimercnt, ev_now);
400 /* relative timers second */
401 timers_reify (rtimers, rtimercnt, now);
405 while (!ev_loop_done);
409 wlist_add (struct ev_watcher_list **head, struct ev_watcher_list *elem)
416 wlist_del (struct ev_watcher_list **head, struct ev_watcher_list *elem)
426 head = &(*head)->next;
431 ev_start (struct ev_watcher *w, int active)
438 ev_stop (struct ev_watcher *w)
441 pendings [w->pending - 1].w = 0;
448 evio_start (struct ev_io *w)
450 if (ev_is_active (w))
455 ev_start ((struct ev_watcher *)w, 1);
456 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
457 wlist_add ((struct ev_watcher_list **)&anfds[fd].head, (struct ev_watcher_list *)w);
460 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
461 fdchanges [fdchangecnt - 1] = fd;
465 evio_stop (struct ev_io *w)
467 if (!ev_is_active (w))
470 wlist_del ((struct ev_watcher_list **)&anfds[w->fd].head, (struct ev_watcher_list *)w);
471 ev_stop ((struct ev_watcher *)w);
474 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
475 fdchanges [fdchangecnt - 1] = w->fd;
479 evtimer_start (struct ev_timer *w)
481 if (ev_is_active (w))
486 /* this formula differs from the one in timer_reify becuse we do not round up */
488 w->at += ceil ((ev_now - w->at) / w->repeat) * w->repeat;
490 ev_start ((struct ev_watcher *)w, ++atimercnt);
491 array_needsize (atimers, atimermax, atimercnt, );
492 atimers [atimercnt - 1] = w;
493 upheap (atimers, atimercnt - 1);
499 ev_start ((struct ev_watcher *)w, ++rtimercnt);
500 array_needsize (rtimers, rtimermax, rtimercnt, );
501 rtimers [rtimercnt - 1] = w;
502 upheap (rtimers, rtimercnt - 1);
508 evtimer_stop (struct ev_timer *w)
510 if (!ev_is_active (w))
515 if (w->active < atimercnt--)
517 atimers [w->active - 1] = atimers [atimercnt];
518 downheap (atimers, atimercnt, w->active - 1);
523 if (w->active < rtimercnt--)
525 rtimers [w->active - 1] = rtimers [rtimercnt];
526 downheap (rtimers, rtimercnt, w->active - 1);
530 ev_stop ((struct ev_watcher *)w);
534 evsignal_start (struct ev_signal *w)
536 if (ev_is_active (w))
539 ev_start ((struct ev_watcher *)w, 1);
540 array_needsize (signals, signalmax, w->signum, signals_init);
541 wlist_add ((struct ev_watcher_list **)&signals [w->signum - 1], (struct ev_watcher_list *)w);
545 evsignal_stop (struct ev_signal *w)
547 if (!ev_is_active (w))
550 wlist_del ((struct ev_watcher_list **)&signals [w->signum - 1], (struct ev_watcher_list *)w);
551 ev_stop ((struct ev_watcher *)w);
554 /*****************************************************************************/
558 sin_cb (struct ev_io *w, int revents)
560 fprintf (stderr, "sin %d, revents %d\n", w->fd, revents);
564 ocb (struct ev_timer *w, int revents)
566 //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data);
577 evw_init (&sin, sin_cb, 55);
578 evio_set (&sin, 0, EV_READ);
581 struct ev_timer t[10000];
585 for (i = 0; i < 10000; ++i)
587 struct ev_timer *w = t + i;
588 evw_init (w, ocb, i);
589 evtimer_set_abs (w, drand48 (), 0.99775533);
591 if (drand48 () < 0.5)
597 evw_init (&t1, ocb, 0);
598 evtimer_set_abs (&t1, 5, 10);