… | |
… | |
9 | #include <assert.h> |
9 | #include <assert.h> |
10 | #include <errno.h> |
10 | #include <errno.h> |
11 | #include <sys/time.h> |
11 | #include <sys/time.h> |
12 | #include <time.h> |
12 | #include <time.h> |
13 | |
13 | |
|
|
14 | #define HAVE_EPOLL 1 |
|
|
15 | |
14 | #ifndef HAVE_MONOTONIC |
16 | #ifndef HAVE_MONOTONIC |
15 | # ifdef CLOCK_MONOTONIC |
17 | # ifdef CLOCK_MONOTONIC |
16 | # define HAVE_MONOTONIC 1 |
18 | # define HAVE_MONOTONIC 1 |
17 | # endif |
19 | # endif |
18 | #endif |
20 | #endif |
… | |
… | |
40 | |
42 | |
41 | struct ev_watcher_list { |
43 | struct ev_watcher_list { |
42 | EV_WATCHER_LIST (ev_watcher_list); |
44 | EV_WATCHER_LIST (ev_watcher_list); |
43 | }; |
45 | }; |
44 | |
46 | |
|
|
47 | struct ev_watcher_time { |
|
|
48 | EV_WATCHER_TIME (ev_watcher_time); |
|
|
49 | }; |
|
|
50 | |
45 | typedef struct ev_watcher *W; |
51 | typedef struct ev_watcher *W; |
46 | typedef struct ev_watcher_list *WL; |
52 | typedef struct ev_watcher_list *WL; |
|
|
53 | typedef struct ev_watcher_time *WT; |
47 | |
54 | |
48 | static ev_tstamp now, diff; /* monotonic clock */ |
55 | static ev_tstamp now, diff; /* monotonic clock */ |
49 | ev_tstamp ev_now; |
56 | ev_tstamp ev_now; |
50 | int ev_method; |
57 | int ev_method; |
51 | |
58 | |
… | |
… | |
163 | event (events [i], type); |
170 | event (events [i], type); |
164 | } |
171 | } |
165 | |
172 | |
166 | /*****************************************************************************/ |
173 | /*****************************************************************************/ |
167 | |
174 | |
168 | static struct ev_timer **atimers; |
175 | static struct ev_timer **timers; |
169 | static int atimermax, atimercnt; |
176 | static int timermax, timercnt; |
170 | |
177 | |
171 | static struct ev_timer **rtimers; |
178 | static struct ev_periodic **periodics; |
172 | static int rtimermax, rtimercnt; |
179 | static int periodicmax, periodiccnt; |
173 | |
180 | |
174 | static void |
181 | static void |
175 | upheap (struct ev_timer **timers, int k) |
182 | upheap (WT *timers, int k) |
176 | { |
183 | { |
177 | struct ev_timer *w = timers [k]; |
184 | WT w = timers [k]; |
178 | |
185 | |
179 | while (k && timers [k >> 1]->at > w->at) |
186 | while (k && timers [k >> 1]->at > w->at) |
180 | { |
187 | { |
181 | timers [k] = timers [k >> 1]; |
188 | timers [k] = timers [k >> 1]; |
182 | timers [k]->active = k + 1; |
189 | timers [k]->active = k + 1; |
… | |
… | |
187 | timers [k]->active = k + 1; |
194 | timers [k]->active = k + 1; |
188 | |
195 | |
189 | } |
196 | } |
190 | |
197 | |
191 | static void |
198 | static void |
192 | downheap (struct ev_timer **timers, int N, int k) |
199 | downheap (WT *timers, int N, int k) |
193 | { |
200 | { |
194 | struct ev_timer *w = timers [k]; |
201 | WT w = timers [k]; |
195 | |
202 | |
196 | while (k < (N >> 1)) |
203 | while (k < (N >> 1)) |
197 | { |
204 | { |
198 | int j = k << 1; |
205 | int j = k << 1; |
199 | |
206 | |
… | |
… | |
325 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
332 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
326 | #endif |
333 | #endif |
327 | |
334 | |
328 | if (ev_method) |
335 | if (ev_method) |
329 | { |
336 | { |
330 | evw_init (&sigev, sigcb, 0); |
337 | evw_init (&sigev, sigcb); |
331 | siginit (); |
338 | siginit (); |
332 | } |
339 | } |
333 | |
340 | |
334 | return ev_method; |
341 | return ev_method; |
335 | } |
342 | } |
… | |
… | |
406 | |
413 | |
407 | pendingcnt = 0; |
414 | pendingcnt = 0; |
408 | } |
415 | } |
409 | |
416 | |
410 | static void |
417 | static void |
411 | timers_reify (struct ev_timer **timers, int timercnt, ev_tstamp now) |
418 | timers_reify () |
412 | { |
419 | { |
413 | while (timercnt && timers [0]->at <= now) |
420 | while (timercnt && timers [0]->at <= now) |
414 | { |
421 | { |
415 | struct ev_timer *w = timers [0]; |
422 | struct ev_timer *w = timers [0]; |
416 | |
423 | |
417 | /* first reschedule or stop timer */ |
424 | /* first reschedule or stop timer */ |
418 | if (w->repeat) |
425 | if (w->repeat) |
419 | { |
426 | { |
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; |
427 | w->at = now + w->repeat; |
424 | |
428 | assert (("timer timeout in the past, negative repeat?", w->at > now)); |
425 | assert (w->at > now); |
|
|
426 | |
|
|
427 | downheap (timers, timercnt, 0); |
429 | downheap ((WT *)timers, timercnt, 0); |
428 | } |
430 | } |
429 | else |
431 | else |
|
|
432 | evtimer_stop (w); /* nonrepeating: stop timer */ |
|
|
433 | |
|
|
434 | event ((W)w, EV_TIMEOUT); |
|
|
435 | } |
|
|
436 | } |
|
|
437 | |
|
|
438 | static void |
|
|
439 | periodics_reify () |
|
|
440 | { |
|
|
441 | while (periodiccnt && periodics [0]->at <= ev_now) |
|
|
442 | { |
|
|
443 | struct ev_periodic *w = periodics [0]; |
|
|
444 | |
|
|
445 | /* first reschedule or stop timer */ |
|
|
446 | if (w->interval) |
430 | { |
447 | { |
431 | evtimer_stop (w); /* nonrepeating: stop timer */ |
448 | w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; |
432 | --timercnt; /* maybe pass by reference instead? */ |
449 | assert (("periodic timeout in the past, negative interval?", w->at > ev_now)); |
|
|
450 | downheap ((WT *)periodics, periodiccnt, 0); |
433 | } |
451 | } |
|
|
452 | else |
|
|
453 | evperiodic_stop (w); /* nonrepeating: stop timer */ |
434 | |
454 | |
435 | event ((W)w, EV_TIMEOUT); |
455 | event ((W)w, EV_TIMEOUT); |
436 | } |
456 | } |
437 | } |
457 | } |
438 | |
458 | |
439 | static void |
459 | static void |
|
|
460 | time_jump (ev_tstamp diff) |
|
|
461 | { |
|
|
462 | int i; |
|
|
463 | |
|
|
464 | /* adjust periodics */ |
|
|
465 | for (i = 0; i < periodiccnt; ++i) |
|
|
466 | { |
|
|
467 | struct ev_periodic *w = periodics [i]; |
|
|
468 | |
|
|
469 | if (w->interval) |
|
|
470 | { |
|
|
471 | ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval; |
|
|
472 | |
|
|
473 | if (fabs (diff) >= 1e-4) |
|
|
474 | { |
|
|
475 | evperiodic_stop (w); |
|
|
476 | evperiodic_start (w); |
|
|
477 | |
|
|
478 | i = 0; /* restart loop, inefficient, but time jumps should be rare */ |
|
|
479 | } |
|
|
480 | } |
|
|
481 | } |
|
|
482 | |
|
|
483 | /* adjust timers. this is easy, as the offset is the same for all */ |
|
|
484 | for (i = 0; i < timercnt; ++i) |
|
|
485 | timers [i]->at += diff; |
|
|
486 | } |
|
|
487 | |
|
|
488 | static void |
440 | time_update () |
489 | time_update () |
441 | { |
490 | { |
442 | int i; |
491 | int i; |
|
|
492 | |
443 | ev_now = ev_time (); |
493 | ev_now = ev_time (); |
444 | |
494 | |
445 | if (have_monotonic) |
495 | if (have_monotonic) |
446 | { |
496 | { |
447 | ev_tstamp odiff = diff; |
497 | ev_tstamp odiff = diff; |
448 | |
498 | |
449 | /* detecting time jumps is much more difficult */ |
|
|
450 | for (i = 2; --i; ) /* loop a few times, before making important decisions */ |
499 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
451 | { |
500 | { |
452 | now = get_clock (); |
501 | now = get_clock (); |
453 | diff = ev_now - now; |
502 | diff = ev_now - now; |
454 | |
503 | |
455 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
504 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
456 | return; /* all is well */ |
505 | return; /* all is well */ |
457 | |
506 | |
458 | ev_now = ev_time (); |
507 | ev_now = ev_time (); |
459 | } |
508 | } |
460 | |
509 | |
461 | /* time jump detected, reschedule atimers */ |
510 | time_jump (diff - odiff); |
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 | } |
511 | } |
468 | else |
512 | else |
469 | { |
513 | { |
470 | if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) |
514 | if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) |
471 | /* time jump detected, adjust rtimers */ |
515 | time_jump (ev_now - now); |
472 | for (i = 0; i < rtimercnt; ++i) |
|
|
473 | rtimers [i]->at += ev_now - now; |
|
|
474 | |
516 | |
475 | now = ev_now; |
517 | now = ev_now; |
476 | } |
518 | } |
477 | } |
519 | } |
478 | |
520 | |
… | |
… | |
493 | { |
535 | { |
494 | /* update fd-related kernel structures */ |
536 | /* update fd-related kernel structures */ |
495 | fd_reify (); |
537 | fd_reify (); |
496 | |
538 | |
497 | /* calculate blocking time */ |
539 | /* calculate blocking time */ |
|
|
540 | |
|
|
541 | /* we only need this for !monotonic clock, but as we always have timers, we just calculate it every time */ |
|
|
542 | ev_now = ev_time (); |
|
|
543 | |
498 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
544 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
499 | block = 0.; |
545 | block = 0.; |
500 | else |
546 | else |
501 | { |
547 | { |
502 | block = MAX_BLOCKTIME; |
548 | block = MAX_BLOCKTIME; |
503 | |
549 | |
504 | if (rtimercnt) |
550 | if (timercnt) |
505 | { |
551 | { |
506 | ev_tstamp to = rtimers [0]->at - get_clock () + method_fudge; |
552 | ev_tstamp to = timers [0]->at - get_clock () + method_fudge; |
507 | if (block > to) block = to; |
553 | if (block > to) block = to; |
508 | } |
554 | } |
509 | |
555 | |
510 | if (atimercnt) |
556 | if (periodiccnt) |
511 | { |
557 | { |
512 | ev_tstamp to = atimers [0]->at - ev_time () + method_fudge; |
558 | ev_tstamp to = periodics [0]->at - ev_now + method_fudge; |
513 | if (block > to) block = to; |
559 | if (block > to) block = to; |
514 | } |
560 | } |
515 | |
561 | |
516 | if (block < 0.) block = 0.; |
562 | if (block < 0.) block = 0.; |
517 | } |
563 | } |
… | |
… | |
520 | |
566 | |
521 | /* update ev_now, do magic */ |
567 | /* update ev_now, do magic */ |
522 | time_update (); |
568 | time_update (); |
523 | |
569 | |
524 | /* queue pending timers and reschedule them */ |
570 | /* queue pending timers and reschedule them */ |
525 | /* absolute timers first */ |
571 | periodics_reify (); /* absolute timers first */ |
526 | timers_reify (atimers, atimercnt, ev_now); |
|
|
527 | /* relative timers second */ |
572 | timers_reify (); /* relative timers second */ |
528 | timers_reify (rtimers, rtimercnt, now); |
|
|
529 | |
573 | |
530 | /* queue idle watchers unless io or timers are pending */ |
574 | /* queue idle watchers unless io or timers are pending */ |
531 | if (!pendingcnt) |
575 | if (!pendingcnt) |
532 | queue_events ((W *)idles, idlecnt, EV_IDLE); |
576 | queue_events ((W *)idles, idlecnt, EV_IDLE); |
533 | |
577 | |
… | |
… | |
610 | ++fdchangecnt; |
654 | ++fdchangecnt; |
611 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
655 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
612 | fdchanges [fdchangecnt - 1] = w->fd; |
656 | fdchanges [fdchangecnt - 1] = w->fd; |
613 | } |
657 | } |
614 | |
658 | |
|
|
659 | |
615 | void |
660 | void |
616 | evtimer_start (struct ev_timer *w) |
661 | evtimer_start (struct ev_timer *w) |
617 | { |
662 | { |
618 | if (ev_is_active (w)) |
663 | if (ev_is_active (w)) |
619 | return; |
664 | return; |
620 | |
665 | |
621 | if (w->is_abs) |
666 | w->at += now; |
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 | |
667 | |
627 | ev_start ((W)w, ++atimercnt); |
668 | ev_start ((W)w, ++timercnt); |
628 | array_needsize (atimers, atimermax, atimercnt, ); |
669 | array_needsize (timers, timermax, timercnt, ); |
629 | atimers [atimercnt - 1] = w; |
670 | timers [timercnt - 1] = w; |
630 | upheap (atimers, atimercnt - 1); |
671 | upheap ((WT *)timers, timercnt - 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 | } |
672 | } |
643 | |
673 | |
644 | void |
674 | void |
645 | evtimer_stop (struct ev_timer *w) |
675 | evtimer_stop (struct ev_timer *w) |
646 | { |
676 | { |
647 | if (!ev_is_active (w)) |
677 | if (!ev_is_active (w)) |
648 | return; |
678 | return; |
649 | |
679 | |
650 | if (w->is_abs) |
|
|
651 | { |
|
|
652 | if (w->active < atimercnt--) |
680 | if (w->active < timercnt--) |
653 | { |
681 | { |
654 | atimers [w->active - 1] = atimers [atimercnt]; |
682 | timers [w->active - 1] = timers [timercnt]; |
655 | downheap (atimers, atimercnt, w->active - 1); |
683 | downheap ((WT *)timers, timercnt, w->active - 1); |
656 | } |
|
|
657 | } |
|
|
658 | else |
|
|
659 | { |
684 | } |
|
|
685 | |
|
|
686 | ev_stop ((W)w); |
|
|
687 | } |
|
|
688 | |
|
|
689 | void |
|
|
690 | evperiodic_start (struct ev_periodic *w) |
|
|
691 | { |
|
|
692 | if (ev_is_active (w)) |
|
|
693 | return; |
|
|
694 | |
|
|
695 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
|
|
696 | if (w->interval) |
|
|
697 | w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; |
|
|
698 | |
|
|
699 | ev_start ((W)w, ++periodiccnt); |
|
|
700 | array_needsize (periodics, periodicmax, periodiccnt, ); |
|
|
701 | periodics [periodiccnt - 1] = w; |
|
|
702 | upheap ((WT *)periodics, periodiccnt - 1); |
|
|
703 | } |
|
|
704 | |
|
|
705 | void |
|
|
706 | evperiodic_stop (struct ev_periodic *w) |
|
|
707 | { |
|
|
708 | if (!ev_is_active (w)) |
|
|
709 | return; |
|
|
710 | |
660 | if (w->active < rtimercnt--) |
711 | if (w->active < periodiccnt--) |
661 | { |
712 | { |
662 | rtimers [w->active - 1] = rtimers [rtimercnt]; |
713 | periodics [w->active - 1] = periodics [periodiccnt]; |
663 | downheap (rtimers, rtimercnt, w->active - 1); |
714 | downheap ((WT *)periodics, periodiccnt, w->active - 1); |
664 | } |
|
|
665 | } |
715 | } |
666 | |
716 | |
667 | ev_stop ((W)w); |
717 | ev_stop ((W)w); |
668 | } |
718 | } |
669 | |
719 | |
… | |
… | |
732 | ev_stop ((W)w); |
782 | ev_stop ((W)w); |
733 | } |
783 | } |
734 | |
784 | |
735 | /*****************************************************************************/ |
785 | /*****************************************************************************/ |
736 | |
786 | |
737 | #if 0 |
787 | #if 1 |
|
|
788 | |
|
|
789 | struct ev_io wio; |
738 | |
790 | |
739 | static void |
791 | static void |
740 | sin_cb (struct ev_io *w, int revents) |
792 | sin_cb (struct ev_io *w, int revents) |
741 | { |
793 | { |
742 | fprintf (stderr, "sin %d, revents %d\n", w->fd, revents); |
794 | fprintf (stderr, "sin %d, revents %d\n", w->fd, revents); |
… | |
… | |
752 | |
804 | |
753 | static void |
805 | static void |
754 | scb (struct ev_signal *w, int revents) |
806 | scb (struct ev_signal *w, int revents) |
755 | { |
807 | { |
756 | fprintf (stderr, "signal %x,%d\n", revents, w->signum); |
808 | fprintf (stderr, "signal %x,%d\n", revents, w->signum); |
|
|
809 | evio_stop (&wio); |
|
|
810 | evio_start (&wio); |
757 | } |
811 | } |
758 | |
812 | |
759 | static void |
813 | static void |
760 | gcb (struct ev_signal *w, int revents) |
814 | gcb (struct ev_signal *w, int revents) |
761 | { |
815 | { |
762 | fprintf (stderr, "generic %x\n", revents); |
816 | fprintf (stderr, "generic %x\n", revents); |
|
|
817 | |
763 | } |
818 | } |
764 | |
819 | |
765 | int main (void) |
820 | int main (void) |
766 | { |
821 | { |
767 | struct ev_io sin; |
|
|
768 | |
|
|
769 | ev_init (0); |
822 | ev_init (0); |
770 | |
823 | |
771 | evw_init (&sin, sin_cb, 55); |
|
|
772 | evio_set (&sin, 0, EV_READ); |
824 | evio_init (&wio, sin_cb, 0, EV_READ); |
773 | evio_start (&sin); |
825 | evio_start (&wio); |
774 | |
826 | |
775 | struct ev_timer t[10000]; |
827 | struct ev_timer t[10000]; |
776 | |
828 | |
777 | #if 0 |
829 | #if 0 |
778 | int i; |
830 | int i; |
779 | for (i = 0; i < 10000; ++i) |
831 | for (i = 0; i < 10000; ++i) |
780 | { |
832 | { |
781 | struct ev_timer *w = t + i; |
833 | struct ev_timer *w = t + i; |
782 | evw_init (w, ocb, i); |
834 | evw_init (w, ocb, i); |
783 | evtimer_set_abs (w, drand48 (), 0.99775533); |
835 | evtimer_init_abs (w, ocb, drand48 (), 0.99775533); |
784 | evtimer_start (w); |
836 | evtimer_start (w); |
785 | if (drand48 () < 0.5) |
837 | if (drand48 () < 0.5) |
786 | evtimer_stop (w); |
838 | evtimer_stop (w); |
787 | } |
839 | } |
788 | #endif |
840 | #endif |
789 | |
841 | |
790 | struct ev_timer t1; |
842 | struct ev_timer t1; |
791 | evw_init (&t1, ocb, 0); |
843 | evtimer_init (&t1, ocb, 5, 10); |
792 | evtimer_set_abs (&t1, 5, 10); |
|
|
793 | evtimer_start (&t1); |
844 | evtimer_start (&t1); |
794 | |
845 | |
795 | struct ev_signal sig; |
846 | struct ev_signal sig; |
796 | evw_init (&sig, scb, 65535); |
|
|
797 | evsignal_set (&sig, SIGQUIT); |
847 | evsignal_init (&sig, scb, SIGQUIT); |
798 | evsignal_start (&sig); |
848 | evsignal_start (&sig); |
799 | |
849 | |
800 | struct ev_check cw; |
850 | struct ev_check cw; |
801 | evw_init (&cw, gcb, 0); |
851 | evcheck_init (&cw, gcb); |
802 | evcheck_start (&cw); |
852 | evcheck_start (&cw); |
803 | |
853 | |
804 | struct ev_idle iw; |
854 | struct ev_idle iw; |
805 | evw_init (&iw, gcb, 0); |
855 | evidle_init (&iw, gcb); |
806 | evidle_start (&iw); |
856 | evidle_start (&iw); |
807 | |
857 | |
808 | ev_loop (0); |
858 | ev_loop (0); |
809 | |
859 | |
810 | return 0; |
860 | return 0; |