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.
39 typedef struct ev_watcher *W;
40 typedef struct ev_watcher_list *WL;
41 typedef struct ev_watcher_time *WT;
43 static ev_tstamp now, diff; /* monotonic clock */
47 static int have_monotonic; /* runtime */
49 static ev_tstamp method_fudge; /* stupid epoll-returns-early bug */
50 static void (*method_modify)(int fd, int oev, int nev);
51 static void (*method_poll)(ev_tstamp timeout);
53 /*****************************************************************************/
60 clock_gettime (CLOCK_REALTIME, &ts);
61 return ts.tv_sec + ts.tv_nsec * 1e-9;
64 gettimeofday (&tv, 0);
65 return tv.tv_sec + tv.tv_usec * 1e-6;
76 clock_gettime (CLOCK_MONOTONIC, &ts);
77 return ts.tv_sec + ts.tv_nsec * 1e-9;
84 #define array_needsize(base,cur,cnt,init) \
87 int newcnt = cur ? cur << 1 : 16; \
88 fprintf (stderr, "resize(" # base ") from %d to %d\n", cur, newcnt);\
89 base = realloc (base, sizeof (*base) * (newcnt)); \
90 init (base + cur, newcnt - cur); \
94 /*****************************************************************************/
99 unsigned char wev, rev; /* want, received event set */
105 static int *fdchanges;
106 static int fdchangemax, fdchangecnt;
109 anfds_init (ANFD *base, int count)
114 base->wev = base->rev = EV_NONE;
125 static ANPENDING *pendings;
126 static int pendingmax, pendingcnt;
129 event (W w, int events)
131 w->pending = ++pendingcnt;
132 array_needsize (pendings, pendingmax, pendingcnt, );
133 pendings [pendingcnt - 1].w = w;
134 pendings [pendingcnt - 1].events = events;
138 fd_event (int fd, int events)
140 ANFD *anfd = anfds + fd;
143 for (w = anfd->head; w; w = w->next)
145 int ev = w->events & events;
153 queue_events (W *events, int eventcnt, int type)
157 for (i = 0; i < eventcnt; ++i)
158 event (events [i], type);
161 /*****************************************************************************/
163 static struct ev_timer **timers;
164 static int timermax, timercnt;
166 static struct ev_periodic **periodics;
167 static int periodicmax, periodiccnt;
170 upheap (WT *timers, int k)
174 while (k && timers [k >> 1]->at > w->at)
176 timers [k] = timers [k >> 1];
177 timers [k]->active = k + 1;
182 timers [k]->active = k + 1;
187 downheap (WT *timers, int N, int k)
195 if (j + 1 < N && timers [j]->at > timers [j + 1]->at)
198 if (w->at <= timers [j]->at)
201 timers [k] = timers [j];
202 timers [k]->active = k + 1;
207 timers [k]->active = k + 1;
210 /*****************************************************************************/
214 struct ev_signal *head;
218 static ANSIG *signals;
219 static int signalmax;
221 static int sigpipe [2];
222 static sig_atomic_t gotsig;
223 static struct ev_io sigev;
226 signals_init (ANSIG *base, int count)
237 sighandler (int signum)
239 signals [signum - 1].gotsig = 1;
244 write (sigpipe [1], &gotsig, 1);
249 sigcb (struct ev_io *iow, int revents)
255 read (sigpipe [0], &revents, 1);
257 for (sig = signalmax; sig--; )
258 if (signals [sig].gotsig)
260 signals [sig].gotsig = 0;
262 for (w = signals [sig].head; w; w = w->next)
263 event ((W)w, EV_SIGNAL);
270 fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC);
271 fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC);
273 /* rather than sort out wether we really need nb, set it */
274 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK);
275 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK);
277 evio_set (&sigev, sigpipe [0], EV_READ);
281 /*****************************************************************************/
283 static struct ev_idle **idles;
284 static int idlemax, idlecnt;
286 static struct ev_check **checks;
287 static int checkmax, checkcnt;
289 /*****************************************************************************/
292 # include "ev_epoll.c"
295 # include "ev_select.c"
298 int ev_init (int flags)
303 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
315 ev_method = EVMETHOD_NONE;
317 if (ev_method == EVMETHOD_NONE) epoll_init (flags);
320 if (ev_method == EVMETHOD_NONE) select_init (flags);
325 evw_init (&sigev, sigcb);
332 /*****************************************************************************/
334 void ev_prefork (void)
339 void ev_postfork_parent (void)
344 void ev_postfork_child (void)
347 if (ev_method == EVMETHOD_EPOLL)
348 epoll_postfork_child ();
358 /*****************************************************************************/
365 for (i = 0; i < fdchangecnt; ++i)
367 int fd = fdchanges [i];
368 ANFD *anfd = anfds + fd;
373 for (w = anfd->head; w; w = w->next)
376 if (anfd->wev != wev)
378 method_modify (fd, anfd->wev, wev);
391 for (i = 0; i < pendingcnt; ++i)
393 ANPENDING *p = pendings + i;
398 p->w->cb (p->w, p->events);
408 while (timercnt && timers [0]->at <= now)
410 struct ev_timer *w = timers [0];
412 /* first reschedule or stop timer */
415 w->at = now + w->repeat;
416 assert (("timer timeout in the past, negative repeat?", w->at > now));
417 downheap ((WT *)timers, timercnt, 0);
420 evtimer_stop (w); /* nonrepeating: stop timer */
422 event ((W)w, EV_TIMEOUT);
429 while (periodiccnt && periodics [0]->at <= ev_now)
431 struct ev_periodic *w = periodics [0];
433 /* first reschedule or stop timer */
436 w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval;
437 assert (("periodic timeout in the past, negative interval?", w->at > ev_now));
438 downheap ((WT *)periodics, periodiccnt, 0);
441 evperiodic_stop (w); /* nonrepeating: stop timer */
443 event ((W)w, EV_TIMEOUT);
448 periodics_reschedule (ev_tstamp diff)
452 /* adjust periodics after time jump */
453 for (i = 0; i < periodiccnt; ++i)
455 struct ev_periodic *w = periodics [i];
459 ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval;
461 if (fabs (diff) >= 1e-4)
464 evperiodic_start (w);
466 i = 0; /* restart loop, inefficient, but time jumps should be rare */
481 ev_tstamp odiff = diff;
483 for (i = 4; --i; ) /* loop a few times, before making important decisions */
488 if (fabs (odiff - diff) < MIN_TIMEJUMP)
489 return; /* all is well */
494 periodics_reschedule (diff - odiff);
495 /* no timer adjustment, as the monotonic clock doesn't jump */
499 if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP)
501 periodics_reschedule (ev_now - now);
503 /* adjust timers. this is easy, as the offset is the same for all */
504 for (i = 0; i < timercnt; ++i)
505 timers [i]->at += diff;
514 void ev_loop (int flags)
517 ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0;
521 queue_events ((W *)checks, checkcnt, EV_CHECK);
527 /* update fd-related kernel structures */
530 /* calculate blocking time */
532 /* we only need this for !monotonic clock, but as we always have timers, we just calculate it every time */
535 if (flags & EVLOOP_NONBLOCK || idlecnt)
539 block = MAX_BLOCKTIME;
543 ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge;
544 if (block > to) block = to;
549 ev_tstamp to = periodics [0]->at - ev_now + method_fudge;
550 if (block > to) block = to;
553 if (block < 0.) block = 0.;
558 /* update ev_now, do magic */
561 /* queue pending timers and reschedule them */
562 periodics_reify (); /* absolute timers first */
563 timers_reify (); /* relative timers second */
565 /* queue idle watchers unless io or timers are pending */
567 queue_events ((W *)idles, idlecnt, EV_IDLE);
569 /* queue check and possibly idle watchers */
570 queue_events ((W *)checks, checkcnt, EV_CHECK);
574 while (!ev_loop_done);
576 if (ev_loop_done != 2)
580 /*****************************************************************************/
583 wlist_add (WL *head, WL elem)
590 wlist_del (WL *head, WL elem)
600 head = &(*head)->next;
605 ev_start (W w, int active)
615 pendings [w->pending - 1].w = 0;
620 /*****************************************************************************/
623 evio_start (struct ev_io *w)
625 if (ev_is_active (w))
631 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
632 wlist_add ((WL *)&anfds[fd].head, (WL)w);
635 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
636 fdchanges [fdchangecnt - 1] = fd;
640 evio_stop (struct ev_io *w)
642 if (!ev_is_active (w))
645 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
649 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
650 fdchanges [fdchangecnt - 1] = w->fd;
655 evtimer_start (struct ev_timer *w)
657 if (ev_is_active (w))
662 assert (("timer repeat value less than zero not allowed", w->repeat >= 0.));
664 ev_start ((W)w, ++timercnt);
665 array_needsize (timers, timermax, timercnt, );
666 timers [timercnt - 1] = w;
667 upheap ((WT *)timers, timercnt - 1);
671 evtimer_stop (struct ev_timer *w)
673 if (!ev_is_active (w))
676 if (w->active < timercnt--)
678 timers [w->active - 1] = timers [timercnt];
679 downheap ((WT *)timers, timercnt, w->active - 1);
688 evtimer_again (struct ev_timer *w)
690 if (ev_is_active (w))
694 w->at = now + w->repeat;
695 downheap ((WT *)timers, timercnt, w->active - 1);
705 evperiodic_start (struct ev_periodic *w)
707 if (ev_is_active (w))
710 assert (("periodic interval value less than zero not allowed", w->interval >= 0.));
712 /* this formula differs from the one in periodic_reify because we do not always round up */
714 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval;
716 ev_start ((W)w, ++periodiccnt);
717 array_needsize (periodics, periodicmax, periodiccnt, );
718 periodics [periodiccnt - 1] = w;
719 upheap ((WT *)periodics, periodiccnt - 1);
723 evperiodic_stop (struct ev_periodic *w)
725 if (!ev_is_active (w))
728 if (w->active < periodiccnt--)
730 periodics [w->active - 1] = periodics [periodiccnt];
731 downheap ((WT *)periodics, periodiccnt, w->active - 1);
738 evsignal_start (struct ev_signal *w)
740 if (ev_is_active (w))
744 array_needsize (signals, signalmax, w->signum, signals_init);
745 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
750 sa.sa_handler = sighandler;
751 sigfillset (&sa.sa_mask);
753 sigaction (w->signum, &sa, 0);
758 evsignal_stop (struct ev_signal *w)
760 if (!ev_is_active (w))
763 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
766 if (!signals [w->signum - 1].head)
767 signal (w->signum, SIG_DFL);
770 void evidle_start (struct ev_idle *w)
772 if (ev_is_active (w))
775 ev_start ((W)w, ++idlecnt);
776 array_needsize (idles, idlemax, idlecnt, );
777 idles [idlecnt - 1] = w;
780 void evidle_stop (struct ev_idle *w)
782 idles [w->active - 1] = idles [--idlecnt];
786 void evcheck_start (struct ev_check *w)
788 if (ev_is_active (w))
791 ev_start ((W)w, ++checkcnt);
792 array_needsize (checks, checkmax, checkcnt, );
793 checks [checkcnt - 1] = w;
796 void evcheck_stop (struct ev_check *w)
798 checks [w->active - 1] = checks [--checkcnt];
802 /*****************************************************************************/
809 sin_cb (struct ev_io *w, int revents)
811 fprintf (stderr, "sin %d, revents %d\n", w->fd, revents);
815 ocb (struct ev_timer *w, int revents)
817 //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data);
823 scb (struct ev_signal *w, int revents)
825 fprintf (stderr, "signal %x,%d\n", revents, w->signum);
831 gcb (struct ev_signal *w, int revents)
833 fprintf (stderr, "generic %x\n", revents);
841 evio_init (&wio, sin_cb, 0, EV_READ);
844 struct ev_timer t[10000];
848 for (i = 0; i < 10000; ++i)
850 struct ev_timer *w = t + i;
851 evw_init (w, ocb, i);
852 evtimer_init_abs (w, ocb, drand48 (), 0.99775533);
854 if (drand48 () < 0.5)
860 evtimer_init (&t1, ocb, 5, 10);
863 struct ev_signal sig;
864 evsignal_init (&sig, scb, SIGQUIT);
865 evsignal_start (&sig);
868 evcheck_init (&cw, gcb);
872 evidle_init (&iw, gcb);