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 fprintf (stderr, "0 %f, %d c%d\n", w->at, w->active, timercnt);//D
262 /* first reschedule timer */
265 fprintf (stderr, "a %f now %f repeat %f, %f\n", w->at, ev_now, w->repeat, w->repeat *1e30);//D
267 w->at += ceil ((ev_now - w->at) / w->repeat + 1.) * w->repeat;
269 w->at = ev_now + w->repeat;
271 fprintf (stderr, "b %f\n", w->at);//D
277 fprintf (stderr, "c %f, %d c%d\n", w->at, w->active, timercnt);//D
278 evtimer_stop (w); /* nonrepeating: stop timer */
281 event ((struct ev_watcher *)w, EV_TIMEOUT);
287 int ev_loop (int flags)
290 ev_loop_done = flags & EVLOOP_ONESHOT;
294 /* update fd-related kernel structures */
295 method_reify (); fdchangecnt = 0;
297 /* calculate blocking time */
300 if (flags & EVLOOP_NONBLOCK)
303 block = MAX_BLOCKTIME;
306 block = timers [0]->at - ev_now + method_fudge;
307 if (block < 0.) block = 0.;
308 else if (block > MAX_BLOCKTIME) block = MAX_BLOCKTIME;
311 fprintf (stderr, "block %f\n", block);//D
314 /* put pending timers into pendign queue and reschedule them */
320 while (!ev_loop_done);
324 wlist_add (struct ev_watcher_list **head, struct ev_watcher_list *elem)
331 wlist_del (struct ev_watcher_list **head, struct ev_watcher_list *elem)
341 head = &(*head)->next;
346 ev_start (struct ev_watcher *w, int active)
353 ev_stop (struct ev_watcher *w)
356 pendings [w->pending - 1].w = 0;
363 evio_start (struct ev_io *w)
365 if (ev_is_active (w))
370 ev_start ((struct ev_watcher *)w, 1);
371 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
372 wlist_add ((struct ev_watcher_list **)&anfds[fd].head, (struct ev_watcher_list *)w);
375 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
376 fdchanges [fdchangecnt - 1] = fd;
380 evio_stop (struct ev_io *w)
382 if (!ev_is_active (w))
385 wlist_del ((struct ev_watcher_list **)&anfds[w->fd].head, (struct ev_watcher_list *)w);
386 ev_stop ((struct ev_watcher *)w);
389 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
390 fdchanges [fdchangecnt - 1] = w->fd;
394 evtimer_start (struct ev_timer *w)
396 if (ev_is_active (w))
399 fprintf (stderr, "t1 %f a %d\n", w->at, w->is_abs);//D
402 /* this formula differs from the one in timer_reify becuse we do not round up */
404 w->at += ceil ((ev_now - w->at) / w->repeat) * w->repeat;
408 fprintf (stderr, "t2 %f a %d\n", w->at, w->is_abs);//D
410 ev_start ((struct ev_watcher *)w, ++timercnt);
411 array_needsize (timers, timermax, timercnt, );
412 timers [timercnt - 1] = w;
413 upheap (timercnt - 1);
417 evtimer_stop (struct ev_timer *w)
419 fprintf (stderr, "-topping %d, %d\n", w->active, timercnt);//D
420 if (!ev_is_active (w))
423 fprintf (stderr, "stopping %d, %d\n", w->active, timercnt);//D
424 if (w->active < timercnt)
426 timers [w->active - 1] = timers [--timercnt];
427 downheap (w->active - 1);
430 ev_stop ((struct ev_watcher *)w);
434 evsignal_start (struct ev_signal *w)
436 if (ev_is_active (w))
439 ev_start ((struct ev_watcher *)w, 1);
440 array_needsize (signals, signalmax, w->signum, signals_init);
441 wlist_add ((struct ev_watcher_list **)&signals [w->signum - 1], (struct ev_watcher_list *)w);
445 evsignal_stop (struct ev_signal *w)
447 if (!ev_is_active (w))
450 wlist_del ((struct ev_watcher_list **)&signals [w->signum - 1], (struct ev_watcher_list *)w);
451 ev_stop ((struct ev_watcher *)w);
454 /*****************************************************************************/
458 sin_cb (struct ev_io *w, int revents)
460 fprintf (stderr, "sin %d, revents %d\n", w->fd, revents);
464 ocb (struct ev_timer *w, int revents)
466 fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data);
475 evw_init (&sin, sin_cb, 55);
476 evio_set (&sin, 0, EV_READ);
479 struct ev_timer t[1000];
482 for (i = 0; i < 1000; ++i)
484 struct ev_timer *w = t + i;
485 evw_init (w, ocb, i);
486 evtimer_set_rel (w, drand48 (), 0);
488 if (drand48 () < 0.5)