10 #ifdef CLOCK_MONOTONIC
11 # define HAVE_MONOTONIC 1
15 #define HAVE_REALTIME 1
18 #define MAX_BLOCKTIME 60.
23 EV_WATCHER (ev_watcher);
26 struct ev_watcher_list {
27 EV_WATCHER_LIST (ev_watcher_list);
33 static int have_monotonic; /* runtime */
35 static ev_tstamp method_fudge; /* stupid epoll-returns-early bug */
36 static void (*method_reify)(void);
37 static void (*method_poll)(ev_tstamp timeout);
44 clock_gettime (CLOCK_REALTIME, &ts);
45 return ts.tv_sec + ts.tv_nsec * 1e-9;
48 gettimeofday (&tv, 0);
49 return tv.tv_sec + tv.tv_usec * 1e-6;
60 clock_gettime (CLOCK_MONOTONIC, &ts);
61 return ts.tv_sec + ts.tv_nsec * 1e-9;
68 #define array_needsize(base,cur,cnt,init) \
71 int newcnt = cur ? cur << 1 : 16; \
72 fprintf (stderr, "resize(" # base ") from %d to %d\n", cur, newcnt);\
73 base = realloc (base, sizeof (*base) * (newcnt)); \
74 init (base + cur, newcnt - cur); \
81 unsigned char wev, rev; /* want, received event set */
87 static int *fdchanges;
88 static int fdchangemax, fdchangecnt;
91 anfds_init (ANFD *base, int count)
96 base->wev = base->rev = EV_NONE;
103 struct ev_watcher *w;
107 static ANPENDING *pendings;
108 static int pendingmax, pendingcnt;
111 event (struct ev_watcher *w, int events)
113 w->pending = ++pendingcnt;
114 array_needsize (pendings, pendingmax, pendingcnt, );
115 pendings [pendingcnt - 1].w = w;
116 pendings [pendingcnt - 1].events = events;
120 fd_event (int fd, int events)
122 ANFD *anfd = anfds + fd;
125 for (w = anfd->head; w; w = w->next)
127 int ev = w->events & events;
130 event ((struct ev_watcher *)w, ev);
134 static struct ev_timer **timers;
135 static int timermax, timercnt;
140 struct ev_timer *w = timers [k];
142 while (k && timers [k >> 1]->at > w->at)
144 timers [k] = timers [k >> 1];
145 timers [k]->active = k + 1;
150 timers [k]->active = k + 1;
157 struct ev_timer *w = timers [k];
159 while (k < (timercnt >> 1))
163 if (j + 1 < timercnt && timers [j]->at > timers [j + 1]->at)
166 if (w->at <= timers [j]->at)
169 timers [k] = timers [j];
170 timers [k]->active = k + 1;
175 timers [k]->active = k + 1;
178 static struct ev_signal **signals;
179 static int signalmax, signalcnt;
182 signals_init (struct ev_signal **base, int count)
189 # include "ev_epoll.c"
192 # include "ev_select.c"
195 int ev_init (int flags)
200 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
208 if (epoll_init (flags))
212 if (select_init (flags))
216 ev_method = EVMETHOD_NONE;
220 void ev_prefork (void)
224 void ev_postfork_parent (void)
228 void ev_postfork_child (void)
231 epoll_postfork_child ();
240 for (i = 0; i < pendingcnt; ++i)
242 ANPENDING *p = pendings + i;
247 p->w->cb (p->w, p->events);
257 while (timercnt && timers [0]->at <= ev_now)
259 struct ev_timer *w = timers [0];
261 /* first reschedule timer */
265 w->at += ceil ((ev_now - w->at) / w->repeat + 1.) * w->repeat;
267 w->at = ev_now + w->repeat;
272 evtimer_stop (w); /* nonrepeating: stop timer */
274 event ((struct ev_watcher *)w, EV_TIMEOUT);
280 int ev_loop (int flags)
283 ev_loop_done = flags & EVLOOP_ONESHOT;
287 /* update fd-related kernel structures */
288 method_reify (); fdchangecnt = 0;
290 /* calculate blocking time */
293 if (flags & EVLOOP_NONBLOCK)
296 block = MAX_BLOCKTIME;
299 block = timers [0]->at - ev_now + method_fudge;
300 if (block < 0.) block = 0.;
301 else if (block > MAX_BLOCKTIME) block = MAX_BLOCKTIME;
306 /* put pending timers into pendign queue and reschedule them */
312 while (!ev_loop_done);
316 wlist_add (struct ev_watcher_list **head, struct ev_watcher_list *elem)
323 wlist_del (struct ev_watcher_list **head, struct ev_watcher_list *elem)
333 head = &(*head)->next;
338 ev_start (struct ev_watcher *w, int active)
345 ev_stop (struct ev_watcher *w)
348 pendings [w->pending - 1].w = 0;
355 evio_start (struct ev_io *w)
357 if (ev_is_active (w))
362 ev_start ((struct ev_watcher *)w, 1);
363 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
364 wlist_add ((struct ev_watcher_list **)&anfds[fd].head, (struct ev_watcher_list *)w);
367 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
368 fdchanges [fdchangecnt - 1] = fd;
372 evio_stop (struct ev_io *w)
374 if (!ev_is_active (w))
377 wlist_del ((struct ev_watcher_list **)&anfds[w->fd].head, (struct ev_watcher_list *)w);
378 ev_stop ((struct ev_watcher *)w);
381 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
382 fdchanges [fdchangecnt - 1] = w->fd;
386 evtimer_start (struct ev_timer *w)
388 if (ev_is_active (w))
393 /* this formula differs from the one in timer_reify becuse we do not round up */
395 w->at += ceil ((ev_now - w->at) / w->repeat) * w->repeat;
400 ev_start ((struct ev_watcher *)w, ++timercnt);
401 array_needsize (timers, timermax, timercnt, );
402 timers [timercnt - 1] = w;
403 upheap (timercnt - 1);
407 evtimer_stop (struct ev_timer *w)
409 if (!ev_is_active (w))
412 if (w->active < timercnt--)
414 timers [w->active - 1] = timers [timercnt];
415 downheap (w->active - 1);
418 ev_stop ((struct ev_watcher *)w);
422 evsignal_start (struct ev_signal *w)
424 if (ev_is_active (w))
427 ev_start ((struct ev_watcher *)w, 1);
428 array_needsize (signals, signalmax, w->signum, signals_init);
429 wlist_add ((struct ev_watcher_list **)&signals [w->signum - 1], (struct ev_watcher_list *)w);
433 evsignal_stop (struct ev_signal *w)
435 if (!ev_is_active (w))
438 wlist_del ((struct ev_watcher_list **)&signals [w->signum - 1], (struct ev_watcher_list *)w);
439 ev_stop ((struct ev_watcher *)w);
442 /*****************************************************************************/
446 sin_cb (struct ev_io *w, int revents)
448 fprintf (stderr, "sin %d, revents %d\n", w->fd, revents);
452 ocb (struct ev_timer *w, int revents)
454 fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data);
463 evw_init (&sin, sin_cb, 55);
464 evio_set (&sin, 0, EV_READ);
467 struct ev_timer t[1000];
470 for (i = 0; i < 1000; ++i)
472 struct ev_timer *w = t + i;
473 evw_init (w, ocb, i);
474 evtimer_set_rel (w, drand48 (), 0);
476 if (drand48 () < 0.5)