ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libev/ev.c
Revision: 1.11
Committed: Wed Oct 31 07:40:49 2007 UTC (16 years, 6 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.10: +2 -1 lines
Log Message:
sugar

File Contents

# User Rev Content
1 root 1.1 #include <math.h>
2     #include <stdlib.h>
3 root 1.7 #include <unistd.h>
4     #include <fcntl.h>
5     #include <signal.h>
6 root 1.1
7     #include <stdio.h>
8    
9 root 1.4 #include <assert.h>
10 root 1.1 #include <errno.h>
11     #include <sys/time.h>
12     #include <time.h>
13    
14 root 1.10 #ifndef HAVE_MONOTONIC
15     # ifdef CLOCK_MONOTONIC
16     # define HAVE_MONOTONIC 1
17     # endif
18 root 1.1 #endif
19    
20 root 1.10 #ifndef HAVE_SELECT
21     # define HAVE_SELECT 1
22     #endif
23    
24     #ifndef HAVE_EPOLL
25     # define HAVE_EPOLL 0
26     #endif
27    
28     #ifndef HAVE_REALTIME
29     # define HAVE_REALTIME 1 /* posix requirement, but might be slower */
30     #endif
31 root 1.1
32 root 1.4 #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
33 root 1.1 #define MAX_BLOCKTIME 60.
34    
35     #include "ev.h"
36    
37     struct ev_watcher {
38     EV_WATCHER (ev_watcher);
39     };
40    
41     struct ev_watcher_list {
42     EV_WATCHER_LIST (ev_watcher_list);
43     };
44    
45 root 1.10 typedef struct ev_watcher *W;
46     typedef struct ev_watcher_list *WL;
47    
48 root 1.4 static ev_tstamp now, diff; /* monotonic clock */
49 root 1.1 ev_tstamp ev_now;
50     int ev_method;
51    
52     static int have_monotonic; /* runtime */
53    
54     static ev_tstamp method_fudge; /* stupid epoll-returns-early bug */
55 root 1.5 static void (*method_modify)(int fd, int oev, int nev);
56 root 1.1 static void (*method_poll)(ev_tstamp timeout);
57    
58 root 1.8 /*****************************************************************************/
59    
60 root 1.1 ev_tstamp
61     ev_time (void)
62     {
63     #if HAVE_REALTIME
64     struct timespec ts;
65     clock_gettime (CLOCK_REALTIME, &ts);
66     return ts.tv_sec + ts.tv_nsec * 1e-9;
67     #else
68     struct timeval tv;
69     gettimeofday (&tv, 0);
70     return tv.tv_sec + tv.tv_usec * 1e-6;
71     #endif
72     }
73    
74     static ev_tstamp
75     get_clock (void)
76     {
77     #if HAVE_MONOTONIC
78     if (have_monotonic)
79     {
80     struct timespec ts;
81     clock_gettime (CLOCK_MONOTONIC, &ts);
82     return ts.tv_sec + ts.tv_nsec * 1e-9;
83     }
84     #endif
85    
86     return ev_time ();
87     }
88    
89     #define array_needsize(base,cur,cnt,init) \
90     if ((cnt) > cur) \
91     { \
92 root 1.2 int newcnt = cur ? cur << 1 : 16; \
93 root 1.1 fprintf (stderr, "resize(" # base ") from %d to %d\n", cur, newcnt);\
94     base = realloc (base, sizeof (*base) * (newcnt)); \
95     init (base + cur, newcnt - cur); \
96     cur = newcnt; \
97     }
98    
99 root 1.8 /*****************************************************************************/
100    
101 root 1.1 typedef struct
102     {
103     struct ev_io *head;
104     unsigned char wev, rev; /* want, received event set */
105     } ANFD;
106    
107     static ANFD *anfds;
108     static int anfdmax;
109    
110     static int *fdchanges;
111     static int fdchangemax, fdchangecnt;
112    
113     static void
114     anfds_init (ANFD *base, int count)
115     {
116     while (count--)
117     {
118     base->head = 0;
119     base->wev = base->rev = EV_NONE;
120     ++base;
121     }
122     }
123    
124     typedef struct
125     {
126 root 1.10 W w;
127 root 1.1 int events;
128     } ANPENDING;
129    
130     static ANPENDING *pendings;
131     static int pendingmax, pendingcnt;
132    
133     static void
134 root 1.10 event (W w, int events)
135 root 1.1 {
136     w->pending = ++pendingcnt;
137     array_needsize (pendings, pendingmax, pendingcnt, );
138     pendings [pendingcnt - 1].w = w;
139     pendings [pendingcnt - 1].events = events;
140     }
141    
142     static void
143     fd_event (int fd, int events)
144     {
145     ANFD *anfd = anfds + fd;
146     struct ev_io *w;
147    
148     for (w = anfd->head; w; w = w->next)
149     {
150     int ev = w->events & events;
151    
152     if (ev)
153 root 1.10 event ((W)w, ev);
154 root 1.1 }
155     }
156    
157 root 1.9 static void
158 root 1.10 queue_events (W *events, int eventcnt, int type)
159 root 1.9 {
160     int i;
161    
162     for (i = 0; i < eventcnt; ++i)
163     event (events [i], type);
164     }
165    
166 root 1.8 /*****************************************************************************/
167    
168 root 1.4 static struct ev_timer **atimers;
169     static int atimermax, atimercnt;
170    
171     static struct ev_timer **rtimers;
172     static int rtimermax, rtimercnt;
173 root 1.1
174     static void
175 root 1.4 upheap (struct ev_timer **timers, int k)
176 root 1.1 {
177     struct ev_timer *w = timers [k];
178    
179     while (k && timers [k >> 1]->at > w->at)
180     {
181     timers [k] = timers [k >> 1];
182     timers [k]->active = k + 1;
183     k >>= 1;
184     }
185    
186     timers [k] = w;
187     timers [k]->active = k + 1;
188    
189     }
190    
191     static void
192 root 1.4 downheap (struct ev_timer **timers, int N, int k)
193 root 1.1 {
194     struct ev_timer *w = timers [k];
195    
196 root 1.4 while (k < (N >> 1))
197 root 1.1 {
198     int j = k << 1;
199    
200 root 1.4 if (j + 1 < N && timers [j]->at > timers [j + 1]->at)
201 root 1.1 ++j;
202    
203     if (w->at <= timers [j]->at)
204     break;
205    
206     timers [k] = timers [j];
207 root 1.2 timers [k]->active = k + 1;
208 root 1.1 k = j;
209     }
210    
211     timers [k] = w;
212     timers [k]->active = k + 1;
213     }
214    
215 root 1.8 /*****************************************************************************/
216    
217 root 1.7 typedef struct
218     {
219     struct ev_signal *head;
220     sig_atomic_t gotsig;
221     } ANSIG;
222    
223     static ANSIG *signals;
224 root 1.4 static int signalmax;
225 root 1.1
226 root 1.7 static int sigpipe [2];
227     static sig_atomic_t gotsig;
228     static struct ev_io sigev;
229    
230 root 1.1 static void
231 root 1.7 signals_init (ANSIG *base, int count)
232 root 1.1 {
233     while (count--)
234 root 1.7 {
235     base->head = 0;
236     base->gotsig = 0;
237     ++base;
238     }
239     }
240    
241     static void
242     sighandler (int signum)
243     {
244     signals [signum - 1].gotsig = 1;
245    
246     if (!gotsig)
247     {
248     gotsig = 1;
249     write (sigpipe [1], &gotsig, 1);
250     }
251     }
252    
253     static void
254     sigcb (struct ev_io *iow, int revents)
255     {
256     struct ev_signal *w;
257     int sig;
258    
259     gotsig = 0;
260     read (sigpipe [0], &revents, 1);
261    
262     for (sig = signalmax; sig--; )
263     if (signals [sig].gotsig)
264     {
265     signals [sig].gotsig = 0;
266    
267     for (w = signals [sig].head; w; w = w->next)
268 root 1.10 event ((W)w, EV_SIGNAL);
269 root 1.7 }
270     }
271    
272     static void
273     siginit (void)
274     {
275     fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC);
276     fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC);
277    
278     /* rather than sort out wether we really need nb, set it */
279     fcntl (sigpipe [0], F_SETFL, O_NONBLOCK);
280     fcntl (sigpipe [1], F_SETFL, O_NONBLOCK);
281    
282     evio_set (&sigev, sigpipe [0], EV_READ);
283     evio_start (&sigev);
284 root 1.1 }
285    
286 root 1.8 /*****************************************************************************/
287    
288 root 1.9 static struct ev_idle **idles;
289     static int idlemax, idlecnt;
290    
291     static struct ev_check **checks;
292     static int checkmax, checkcnt;
293    
294     /*****************************************************************************/
295    
296 root 1.1 #if HAVE_EPOLL
297     # include "ev_epoll.c"
298     #endif
299     #if HAVE_SELECT
300     # include "ev_select.c"
301     #endif
302    
303     int ev_init (int flags)
304     {
305     #if HAVE_MONOTONIC
306     {
307     struct timespec ts;
308     if (!clock_gettime (CLOCK_MONOTONIC, &ts))
309     have_monotonic = 1;
310     }
311     #endif
312    
313     ev_now = ev_time ();
314 root 1.4 now = get_clock ();
315     diff = ev_now - now;
316 root 1.1
317 root 1.7 if (pipe (sigpipe))
318     return 0;
319    
320     ev_method = EVMETHOD_NONE;
321 root 1.1 #if HAVE_EPOLL
322 root 1.7 if (ev_method == EVMETHOD_NONE) epoll_init (flags);
323 root 1.1 #endif
324     #if HAVE_SELECT
325 root 1.7 if (ev_method == EVMETHOD_NONE) select_init (flags);
326 root 1.1 #endif
327    
328 root 1.7 if (ev_method)
329     {
330     evw_init (&sigev, sigcb, 0);
331     siginit ();
332     }
333    
334 root 1.1 return ev_method;
335     }
336    
337 root 1.8 /*****************************************************************************/
338    
339 root 1.1 void ev_prefork (void)
340     {
341 root 1.11 /* nop */
342 root 1.1 }
343    
344     void ev_postfork_parent (void)
345     {
346 root 1.11 /* nop */
347 root 1.1 }
348    
349     void ev_postfork_child (void)
350     {
351     #if HAVE_EPOLL
352 root 1.5 if (ev_method == EVMETHOD_EPOLL)
353     epoll_postfork_child ();
354 root 1.1 #endif
355 root 1.7
356     evio_stop (&sigev);
357     close (sigpipe [0]);
358     close (sigpipe [1]);
359     pipe (sigpipe);
360     siginit ();
361 root 1.1 }
362    
363 root 1.8 /*****************************************************************************/
364    
365 root 1.1 static void
366 root 1.5 fd_reify (void)
367     {
368     int i;
369    
370     for (i = 0; i < fdchangecnt; ++i)
371     {
372     int fd = fdchanges [i];
373     ANFD *anfd = anfds + fd;
374     struct ev_io *w;
375    
376     int wev = 0;
377    
378     for (w = anfd->head; w; w = w->next)
379     wev |= w->events;
380    
381     if (anfd->wev != wev)
382     {
383     method_modify (fd, anfd->wev, wev);
384     anfd->wev = wev;
385     }
386     }
387    
388     fdchangecnt = 0;
389     }
390    
391     static void
392 root 1.1 call_pending ()
393     {
394     int i;
395    
396     for (i = 0; i < pendingcnt; ++i)
397     {
398     ANPENDING *p = pendings + i;
399    
400     if (p->w)
401     {
402     p->w->pending = 0;
403     p->w->cb (p->w, p->events);
404     }
405     }
406    
407     pendingcnt = 0;
408     }
409    
410     static void
411 root 1.4 timers_reify (struct ev_timer **timers, int timercnt, ev_tstamp now)
412 root 1.1 {
413 root 1.4 while (timercnt && timers [0]->at <= now)
414 root 1.1 {
415     struct ev_timer *w = timers [0];
416    
417 root 1.4 /* first reschedule or stop timer */
418 root 1.1 if (w->repeat)
419     {
420     if (w->is_abs)
421 root 1.4 w->at += floor ((now - w->at) / w->repeat + 1.) * w->repeat;
422 root 1.1 else
423 root 1.4 w->at = now + w->repeat;
424    
425     assert (w->at > now);
426 root 1.1
427 root 1.4 downheap (timers, timercnt, 0);
428 root 1.1 }
429     else
430 root 1.4 {
431     evtimer_stop (w); /* nonrepeating: stop timer */
432     --timercnt; /* maybe pass by reference instead? */
433     }
434 root 1.1
435 root 1.10 event ((W)w, EV_TIMEOUT);
436 root 1.1 }
437     }
438    
439 root 1.4 static void
440     time_update ()
441     {
442     int i;
443     ev_now = ev_time ();
444    
445     if (have_monotonic)
446     {
447     ev_tstamp odiff = diff;
448    
449     /* detecting time jumps is much more difficult */
450     for (i = 2; --i; ) /* loop a few times, before making important decisions */
451     {
452     now = get_clock ();
453     diff = ev_now - now;
454    
455     if (fabs (odiff - diff) < MIN_TIMEJUMP)
456     return; /* all is well */
457    
458     ev_now = ev_time ();
459     }
460    
461     /* time jump detected, reschedule atimers */
462     for (i = 0; i < atimercnt; ++i)
463     {
464     struct ev_timer *w = atimers [i];
465     w->at += ceil ((ev_now - w->at) / w->repeat + 1.) * w->repeat;
466     }
467     }
468     else
469     {
470     if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP)
471     /* time jump detected, adjust rtimers */
472     for (i = 0; i < rtimercnt; ++i)
473     rtimers [i]->at += ev_now - now;
474    
475     now = ev_now;
476     }
477     }
478    
479 root 1.1 int ev_loop_done;
480    
481 root 1.4 void ev_loop (int flags)
482 root 1.1 {
483     double block;
484     ev_loop_done = flags & EVLOOP_ONESHOT;
485    
486 root 1.9 if (checkcnt)
487     {
488 root 1.10 queue_events ((W *)checks, checkcnt, EV_CHECK);
489 root 1.9 call_pending ();
490     }
491    
492 root 1.1 do
493     {
494     /* update fd-related kernel structures */
495 root 1.5 fd_reify ();
496 root 1.1
497     /* calculate blocking time */
498 root 1.9 if (flags & EVLOOP_NONBLOCK || idlecnt)
499 root 1.1 block = 0.;
500     else
501     {
502 root 1.4 block = MAX_BLOCKTIME;
503    
504     if (rtimercnt)
505     {
506     ev_tstamp to = rtimers [0]->at - get_clock () + method_fudge;
507     if (block > to) block = to;
508     }
509    
510     if (atimercnt)
511     {
512     ev_tstamp to = atimers [0]->at - ev_time () + method_fudge;
513     if (block > to) block = to;
514     }
515    
516 root 1.1 if (block < 0.) block = 0.;
517     }
518    
519     method_poll (block);
520    
521 root 1.4 /* update ev_now, do magic */
522     time_update ();
523    
524 root 1.9 /* queue pending timers and reschedule them */
525 root 1.4 /* absolute timers first */
526     timers_reify (atimers, atimercnt, ev_now);
527     /* relative timers second */
528     timers_reify (rtimers, rtimercnt, now);
529 root 1.1
530 root 1.9 /* queue idle watchers unless io or timers are pending */
531     if (!pendingcnt)
532 root 1.10 queue_events ((W *)idles, idlecnt, EV_IDLE);
533 root 1.9
534     /* queue check and possibly idle watchers */
535 root 1.10 queue_events ((W *)checks, checkcnt, EV_CHECK);
536 root 1.9
537 root 1.1 call_pending ();
538     }
539     while (!ev_loop_done);
540     }
541    
542 root 1.8 /*****************************************************************************/
543    
544 root 1.1 static void
545 root 1.10 wlist_add (WL *head, WL elem)
546 root 1.1 {
547     elem->next = *head;
548     *head = elem;
549     }
550    
551     static void
552 root 1.10 wlist_del (WL *head, WL elem)
553 root 1.1 {
554     while (*head)
555     {
556     if (*head == elem)
557     {
558     *head = elem->next;
559     return;
560     }
561    
562     head = &(*head)->next;
563     }
564     }
565    
566     static void
567 root 1.10 ev_start (W w, int active)
568 root 1.1 {
569     w->pending = 0;
570     w->active = active;
571     }
572    
573     static void
574 root 1.10 ev_stop (W w)
575 root 1.1 {
576     if (w->pending)
577     pendings [w->pending - 1].w = 0;
578    
579     w->active = 0;
580     }
581    
582 root 1.8 /*****************************************************************************/
583    
584 root 1.1 void
585     evio_start (struct ev_io *w)
586     {
587     if (ev_is_active (w))
588     return;
589    
590     int fd = w->fd;
591    
592 root 1.10 ev_start ((W)w, 1);
593 root 1.1 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
594 root 1.10 wlist_add ((WL *)&anfds[fd].head, (WL)w);
595 root 1.1
596     ++fdchangecnt;
597     array_needsize (fdchanges, fdchangemax, fdchangecnt, );
598     fdchanges [fdchangecnt - 1] = fd;
599     }
600    
601     void
602     evio_stop (struct ev_io *w)
603     {
604     if (!ev_is_active (w))
605     return;
606    
607 root 1.10 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
608     ev_stop ((W)w);
609 root 1.1
610     ++fdchangecnt;
611     array_needsize (fdchanges, fdchangemax, fdchangecnt, );
612     fdchanges [fdchangecnt - 1] = w->fd;
613     }
614    
615     void
616     evtimer_start (struct ev_timer *w)
617     {
618     if (ev_is_active (w))
619     return;
620    
621     if (w->is_abs)
622     {
623 root 1.2 /* this formula differs from the one in timer_reify becuse we do not round up */
624 root 1.1 if (w->repeat)
625     w->at += ceil ((ev_now - w->at) / w->repeat) * w->repeat;
626 root 1.4
627 root 1.10 ev_start ((W)w, ++atimercnt);
628 root 1.4 array_needsize (atimers, atimermax, atimercnt, );
629     atimers [atimercnt - 1] = w;
630     upheap (atimers, atimercnt - 1);
631 root 1.1 }
632     else
633 root 1.4 {
634     w->at += now;
635    
636 root 1.10 ev_start ((W)w, ++rtimercnt);
637 root 1.4 array_needsize (rtimers, rtimermax, rtimercnt, );
638     rtimers [rtimercnt - 1] = w;
639     upheap (rtimers, rtimercnt - 1);
640     }
641 root 1.1
642     }
643    
644     void
645     evtimer_stop (struct ev_timer *w)
646     {
647     if (!ev_is_active (w))
648     return;
649    
650 root 1.4 if (w->is_abs)
651 root 1.2 {
652 root 1.4 if (w->active < atimercnt--)
653     {
654     atimers [w->active - 1] = atimers [atimercnt];
655     downheap (atimers, atimercnt, w->active - 1);
656     }
657     }
658     else
659     {
660     if (w->active < rtimercnt--)
661     {
662     rtimers [w->active - 1] = rtimers [rtimercnt];
663     downheap (rtimers, rtimercnt, w->active - 1);
664     }
665 root 1.2 }
666    
667 root 1.10 ev_stop ((W)w);
668 root 1.1 }
669    
670     void
671     evsignal_start (struct ev_signal *w)
672     {
673     if (ev_is_active (w))
674     return;
675    
676 root 1.10 ev_start ((W)w, 1);
677 root 1.1 array_needsize (signals, signalmax, w->signum, signals_init);
678 root 1.10 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
679 root 1.7
680     if (!w->next)
681     {
682     struct sigaction sa;
683     sa.sa_handler = sighandler;
684     sigfillset (&sa.sa_mask);
685     sa.sa_flags = 0;
686     sigaction (w->signum, &sa, 0);
687     }
688 root 1.1 }
689    
690     void
691     evsignal_stop (struct ev_signal *w)
692     {
693     if (!ev_is_active (w))
694     return;
695    
696 root 1.10 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
697     ev_stop ((W)w);
698 root 1.7
699     if (!signals [w->signum - 1].head)
700     signal (w->signum, SIG_DFL);
701 root 1.1 }
702    
703 root 1.9 void evidle_start (struct ev_idle *w)
704     {
705     if (ev_is_active (w))
706     return;
707    
708 root 1.10 ev_start ((W)w, ++idlecnt);
709 root 1.9 array_needsize (idles, idlemax, idlecnt, );
710     idles [idlecnt - 1] = w;
711     }
712    
713     void evidle_stop (struct ev_idle *w)
714     {
715     idles [w->active - 1] = idles [--idlecnt];
716 root 1.10 ev_stop ((W)w);
717 root 1.9 }
718    
719     void evcheck_start (struct ev_check *w)
720     {
721     if (ev_is_active (w))
722     return;
723    
724 root 1.10 ev_start ((W)w, ++checkcnt);
725 root 1.9 array_needsize (checks, checkmax, checkcnt, );
726     checks [checkcnt - 1] = w;
727     }
728    
729     void evcheck_stop (struct ev_check *w)
730     {
731     checks [w->active - 1] = checks [--checkcnt];
732 root 1.10 ev_stop ((W)w);
733 root 1.9 }
734    
735 root 1.1 /*****************************************************************************/
736 root 1.10
737     #if 0
738 root 1.1
739     static void
740     sin_cb (struct ev_io *w, int revents)
741     {
742     fprintf (stderr, "sin %d, revents %d\n", w->fd, revents);
743     }
744    
745     static void
746     ocb (struct ev_timer *w, int revents)
747     {
748 root 1.4 //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data);
749     evtimer_stop (w);
750     evtimer_start (w);
751 root 1.1 }
752    
753 root 1.7 static void
754     scb (struct ev_signal *w, int revents)
755     {
756     fprintf (stderr, "signal %x,%d\n", revents, w->signum);
757     }
758    
759 root 1.9 static void
760     gcb (struct ev_signal *w, int revents)
761     {
762     fprintf (stderr, "generic %x\n", revents);
763     }
764    
765 root 1.1 int main (void)
766     {
767     struct ev_io sin;
768    
769     ev_init (0);
770    
771     evw_init (&sin, sin_cb, 55);
772     evio_set (&sin, 0, EV_READ);
773     evio_start (&sin);
774    
775 root 1.4 struct ev_timer t[10000];
776 root 1.2
777 root 1.9 #if 0
778 root 1.2 int i;
779 root 1.4 for (i = 0; i < 10000; ++i)
780 root 1.2 {
781     struct ev_timer *w = t + i;
782     evw_init (w, ocb, i);
783 root 1.4 evtimer_set_abs (w, drand48 (), 0.99775533);
784 root 1.2 evtimer_start (w);
785     if (drand48 () < 0.5)
786     evtimer_stop (w);
787     }
788 root 1.4 #endif
789    
790     struct ev_timer t1;
791     evw_init (&t1, ocb, 0);
792     evtimer_set_abs (&t1, 5, 10);
793     evtimer_start (&t1);
794 root 1.1
795 root 1.7 struct ev_signal sig;
796     evw_init (&sig, scb, 65535);
797     evsignal_set (&sig, SIGQUIT);
798     evsignal_start (&sig);
799    
800 root 1.9 struct ev_check cw;
801     evw_init (&cw, gcb, 0);
802     evcheck_start (&cw);
803    
804     struct ev_idle iw;
805     evw_init (&iw, gcb, 0);
806     evidle_start (&iw);
807    
808 root 1.1 ev_loop (0);
809    
810     return 0;
811     }
812    
813     #endif
814    
815    
816    
817