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

# Content
1 #include <math.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <fcntl.h>
5 #include <signal.h>
6
7 #include <stdio.h>
8
9 #include <assert.h>
10 #include <errno.h>
11 #include <sys/time.h>
12 #include <time.h>
13
14 #ifndef HAVE_MONOTONIC
15 # ifdef CLOCK_MONOTONIC
16 # define HAVE_MONOTONIC 1
17 # endif
18 #endif
19
20 #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
32 #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
33 #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 typedef struct ev_watcher *W;
46 typedef struct ev_watcher_list *WL;
47
48 static ev_tstamp now, diff; /* monotonic clock */
49 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 static void (*method_modify)(int fd, int oev, int nev);
56 static void (*method_poll)(ev_tstamp timeout);
57
58 /*****************************************************************************/
59
60 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 int newcnt = cur ? cur << 1 : 16; \
93 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 /*****************************************************************************/
100
101 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 W w;
127 int events;
128 } ANPENDING;
129
130 static ANPENDING *pendings;
131 static int pendingmax, pendingcnt;
132
133 static void
134 event (W w, int events)
135 {
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 event ((W)w, ev);
154 }
155 }
156
157 static void
158 queue_events (W *events, int eventcnt, int type)
159 {
160 int i;
161
162 for (i = 0; i < eventcnt; ++i)
163 event (events [i], type);
164 }
165
166 /*****************************************************************************/
167
168 static struct ev_timer **atimers;
169 static int atimermax, atimercnt;
170
171 static struct ev_timer **rtimers;
172 static int rtimermax, rtimercnt;
173
174 static void
175 upheap (struct ev_timer **timers, int k)
176 {
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 downheap (struct ev_timer **timers, int N, int k)
193 {
194 struct ev_timer *w = timers [k];
195
196 while (k < (N >> 1))
197 {
198 int j = k << 1;
199
200 if (j + 1 < N && timers [j]->at > timers [j + 1]->at)
201 ++j;
202
203 if (w->at <= timers [j]->at)
204 break;
205
206 timers [k] = timers [j];
207 timers [k]->active = k + 1;
208 k = j;
209 }
210
211 timers [k] = w;
212 timers [k]->active = k + 1;
213 }
214
215 /*****************************************************************************/
216
217 typedef struct
218 {
219 struct ev_signal *head;
220 sig_atomic_t gotsig;
221 } ANSIG;
222
223 static ANSIG *signals;
224 static int signalmax;
225
226 static int sigpipe [2];
227 static sig_atomic_t gotsig;
228 static struct ev_io sigev;
229
230 static void
231 signals_init (ANSIG *base, int count)
232 {
233 while (count--)
234 {
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 event ((W)w, EV_SIGNAL);
269 }
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 }
285
286 /*****************************************************************************/
287
288 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 #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 now = get_clock ();
315 diff = ev_now - now;
316
317 if (pipe (sigpipe))
318 return 0;
319
320 ev_method = EVMETHOD_NONE;
321 #if HAVE_EPOLL
322 if (ev_method == EVMETHOD_NONE) epoll_init (flags);
323 #endif
324 #if HAVE_SELECT
325 if (ev_method == EVMETHOD_NONE) select_init (flags);
326 #endif
327
328 if (ev_method)
329 {
330 evw_init (&sigev, sigcb, 0);
331 siginit ();
332 }
333
334 return ev_method;
335 }
336
337 /*****************************************************************************/
338
339 void ev_prefork (void)
340 {
341 /* nop */
342 }
343
344 void ev_postfork_parent (void)
345 {
346 /* nop */
347 }
348
349 void ev_postfork_child (void)
350 {
351 #if HAVE_EPOLL
352 if (ev_method == EVMETHOD_EPOLL)
353 epoll_postfork_child ();
354 #endif
355
356 evio_stop (&sigev);
357 close (sigpipe [0]);
358 close (sigpipe [1]);
359 pipe (sigpipe);
360 siginit ();
361 }
362
363 /*****************************************************************************/
364
365 static void
366 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 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 timers_reify (struct ev_timer **timers, int timercnt, ev_tstamp now)
412 {
413 while (timercnt && timers [0]->at <= now)
414 {
415 struct ev_timer *w = timers [0];
416
417 /* first reschedule or stop timer */
418 if (w->repeat)
419 {
420 if (w->is_abs)
421 w->at += floor ((now - w->at) / w->repeat + 1.) * w->repeat;
422 else
423 w->at = now + w->repeat;
424
425 assert (w->at > now);
426
427 downheap (timers, timercnt, 0);
428 }
429 else
430 {
431 evtimer_stop (w); /* nonrepeating: stop timer */
432 --timercnt; /* maybe pass by reference instead? */
433 }
434
435 event ((W)w, EV_TIMEOUT);
436 }
437 }
438
439 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 int ev_loop_done;
480
481 void ev_loop (int flags)
482 {
483 double block;
484 ev_loop_done = flags & EVLOOP_ONESHOT;
485
486 if (checkcnt)
487 {
488 queue_events ((W *)checks, checkcnt, EV_CHECK);
489 call_pending ();
490 }
491
492 do
493 {
494 /* update fd-related kernel structures */
495 fd_reify ();
496
497 /* calculate blocking time */
498 if (flags & EVLOOP_NONBLOCK || idlecnt)
499 block = 0.;
500 else
501 {
502 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 if (block < 0.) block = 0.;
517 }
518
519 method_poll (block);
520
521 /* update ev_now, do magic */
522 time_update ();
523
524 /* queue pending timers and reschedule them */
525 /* absolute timers first */
526 timers_reify (atimers, atimercnt, ev_now);
527 /* relative timers second */
528 timers_reify (rtimers, rtimercnt, now);
529
530 /* queue idle watchers unless io or timers are pending */
531 if (!pendingcnt)
532 queue_events ((W *)idles, idlecnt, EV_IDLE);
533
534 /* queue check and possibly idle watchers */
535 queue_events ((W *)checks, checkcnt, EV_CHECK);
536
537 call_pending ();
538 }
539 while (!ev_loop_done);
540 }
541
542 /*****************************************************************************/
543
544 static void
545 wlist_add (WL *head, WL elem)
546 {
547 elem->next = *head;
548 *head = elem;
549 }
550
551 static void
552 wlist_del (WL *head, WL elem)
553 {
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 ev_start (W w, int active)
568 {
569 w->pending = 0;
570 w->active = active;
571 }
572
573 static void
574 ev_stop (W w)
575 {
576 if (w->pending)
577 pendings [w->pending - 1].w = 0;
578
579 w->active = 0;
580 }
581
582 /*****************************************************************************/
583
584 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 ev_start ((W)w, 1);
593 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
594 wlist_add ((WL *)&anfds[fd].head, (WL)w);
595
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 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
608 ev_stop ((W)w);
609
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 /* this formula differs from the one in timer_reify becuse we do not round up */
624 if (w->repeat)
625 w->at += ceil ((ev_now - w->at) / w->repeat) * w->repeat;
626
627 ev_start ((W)w, ++atimercnt);
628 array_needsize (atimers, atimermax, atimercnt, );
629 atimers [atimercnt - 1] = w;
630 upheap (atimers, atimercnt - 1);
631 }
632 else
633 {
634 w->at += now;
635
636 ev_start ((W)w, ++rtimercnt);
637 array_needsize (rtimers, rtimermax, rtimercnt, );
638 rtimers [rtimercnt - 1] = w;
639 upheap (rtimers, rtimercnt - 1);
640 }
641
642 }
643
644 void
645 evtimer_stop (struct ev_timer *w)
646 {
647 if (!ev_is_active (w))
648 return;
649
650 if (w->is_abs)
651 {
652 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 }
666
667 ev_stop ((W)w);
668 }
669
670 void
671 evsignal_start (struct ev_signal *w)
672 {
673 if (ev_is_active (w))
674 return;
675
676 ev_start ((W)w, 1);
677 array_needsize (signals, signalmax, w->signum, signals_init);
678 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
679
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 }
689
690 void
691 evsignal_stop (struct ev_signal *w)
692 {
693 if (!ev_is_active (w))
694 return;
695
696 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
697 ev_stop ((W)w);
698
699 if (!signals [w->signum - 1].head)
700 signal (w->signum, SIG_DFL);
701 }
702
703 void evidle_start (struct ev_idle *w)
704 {
705 if (ev_is_active (w))
706 return;
707
708 ev_start ((W)w, ++idlecnt);
709 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 ev_stop ((W)w);
717 }
718
719 void evcheck_start (struct ev_check *w)
720 {
721 if (ev_is_active (w))
722 return;
723
724 ev_start ((W)w, ++checkcnt);
725 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 ev_stop ((W)w);
733 }
734
735 /*****************************************************************************/
736
737 #if 0
738
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 //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 }
752
753 static void
754 scb (struct ev_signal *w, int revents)
755 {
756 fprintf (stderr, "signal %x,%d\n", revents, w->signum);
757 }
758
759 static void
760 gcb (struct ev_signal *w, int revents)
761 {
762 fprintf (stderr, "generic %x\n", revents);
763 }
764
765 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 struct ev_timer t[10000];
776
777 #if 0
778 int i;
779 for (i = 0; i < 10000; ++i)
780 {
781 struct ev_timer *w = t + i;
782 evw_init (w, ocb, i);
783 evtimer_set_abs (w, drand48 (), 0.99775533);
784 evtimer_start (w);
785 if (drand48 () < 0.5)
786 evtimer_stop (w);
787 }
788 #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
795 struct ev_signal sig;
796 evw_init (&sig, scb, 65535);
797 evsignal_set (&sig, SIGQUIT);
798 evsignal_start (&sig);
799
800 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 ev_loop (0);
809
810 return 0;
811 }
812
813 #endif
814
815
816
817