1 | #include <math.h> |
1 | #include <math.h> |
2 | #include <stdlib.h> |
2 | #include <stdlib.h> |
3 | #include <unistd.h> |
3 | #include <unistd.h> |
4 | #include <fcntl.h> |
4 | #include <fcntl.h> |
5 | #include <signal.h> |
5 | #include <signal.h> |
|
|
6 | #include <stddef.h> |
6 | |
7 | |
7 | #include <stdio.h> |
8 | #include <stdio.h> |
8 | |
9 | |
9 | #include <assert.h> |
10 | #include <assert.h> |
10 | #include <errno.h> |
11 | #include <errno.h> |
… | |
… | |
33 | |
34 | |
34 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
35 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
35 | #define MAX_BLOCKTIME 60. |
36 | #define MAX_BLOCKTIME 60. |
36 | |
37 | |
37 | #include "ev.h" |
38 | #include "ev.h" |
38 | |
|
|
39 | struct ev_watcher { |
|
|
40 | EV_WATCHER (ev_watcher); |
|
|
41 | }; |
|
|
42 | |
|
|
43 | struct ev_watcher_list { |
|
|
44 | EV_WATCHER_LIST (ev_watcher_list); |
|
|
45 | }; |
|
|
46 | |
|
|
47 | struct ev_watcher_time { |
|
|
48 | EV_WATCHER_TIME (ev_watcher_time); |
|
|
49 | }; |
|
|
50 | |
39 | |
51 | typedef struct ev_watcher *W; |
40 | typedef struct ev_watcher *W; |
52 | typedef struct ev_watcher_list *WL; |
41 | typedef struct ev_watcher_list *WL; |
53 | typedef struct ev_watcher_time *WT; |
42 | typedef struct ev_watcher_time *WT; |
54 | |
43 | |
… | |
… | |
95 | |
84 | |
96 | #define array_needsize(base,cur,cnt,init) \ |
85 | #define array_needsize(base,cur,cnt,init) \ |
97 | if ((cnt) > cur) \ |
86 | if ((cnt) > cur) \ |
98 | { \ |
87 | { \ |
99 | int newcnt = cur ? cur << 1 : 16; \ |
88 | int newcnt = cur ? cur << 1 : 16; \ |
100 | fprintf (stderr, "resize(" # base ") from %d to %d\n", cur, newcnt);\ |
|
|
101 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
89 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
102 | init (base + cur, newcnt - cur); \ |
90 | init (base + cur, newcnt - cur); \ |
103 | cur = newcnt; \ |
91 | cur = newcnt; \ |
104 | } |
92 | } |
105 | |
93 | |
… | |
… | |
138 | static int pendingmax, pendingcnt; |
126 | static int pendingmax, pendingcnt; |
139 | |
127 | |
140 | static void |
128 | static void |
141 | event (W w, int events) |
129 | event (W w, int events) |
142 | { |
130 | { |
|
|
131 | if (w->active) |
|
|
132 | { |
143 | w->pending = ++pendingcnt; |
133 | w->pending = ++pendingcnt; |
144 | array_needsize (pendings, pendingmax, pendingcnt, ); |
134 | array_needsize (pendings, pendingmax, pendingcnt, ); |
145 | pendings [pendingcnt - 1].w = w; |
135 | pendings [pendingcnt - 1].w = w; |
146 | pendings [pendingcnt - 1].events = events; |
136 | pendings [pendingcnt - 1].events = events; |
|
|
137 | } |
147 | } |
138 | } |
148 | |
139 | |
149 | static void |
140 | static void |
150 | fd_event (int fd, int events) |
141 | fd_event (int fd, int events) |
151 | { |
142 | { |
… | |
… | |
419 | { |
410 | { |
420 | while (timercnt && timers [0]->at <= now) |
411 | while (timercnt && timers [0]->at <= now) |
421 | { |
412 | { |
422 | struct ev_timer *w = timers [0]; |
413 | struct ev_timer *w = timers [0]; |
423 | |
414 | |
|
|
415 | event ((W)w, EV_TIMEOUT); |
|
|
416 | |
424 | /* first reschedule or stop timer */ |
417 | /* first reschedule or stop timer */ |
425 | if (w->repeat) |
418 | if (w->repeat) |
426 | { |
419 | { |
427 | w->at = now + w->repeat; |
420 | w->at = now + w->repeat; |
428 | assert (("timer timeout in the past, negative repeat?", w->at > now)); |
421 | assert (("timer timeout in the past, negative repeat?", w->at > now)); |
429 | downheap ((WT *)timers, timercnt, 0); |
422 | downheap ((WT *)timers, timercnt, 0); |
430 | } |
423 | } |
431 | else |
424 | else |
432 | evtimer_stop (w); /* nonrepeating: stop timer */ |
425 | evtimer_stop (w); /* nonrepeating: stop timer */ |
433 | |
|
|
434 | event ((W)w, EV_TIMEOUT); |
|
|
435 | } |
426 | } |
436 | } |
427 | } |
437 | |
428 | |
438 | static void |
429 | static void |
439 | periodics_reify () |
430 | periodics_reify () |
… | |
… | |
455 | event ((W)w, EV_TIMEOUT); |
446 | event ((W)w, EV_TIMEOUT); |
456 | } |
447 | } |
457 | } |
448 | } |
458 | |
449 | |
459 | static void |
450 | static void |
460 | time_jump (ev_tstamp diff) |
451 | periodics_reschedule (ev_tstamp diff) |
461 | { |
452 | { |
462 | int i; |
453 | int i; |
463 | |
454 | |
464 | /* adjust periodics */ |
455 | /* adjust periodics after time jump */ |
465 | for (i = 0; i < periodiccnt; ++i) |
456 | for (i = 0; i < periodiccnt; ++i) |
466 | { |
457 | { |
467 | struct ev_periodic *w = periodics [i]; |
458 | struct ev_periodic *w = periodics [i]; |
468 | |
459 | |
469 | if (w->interval) |
460 | if (w->interval) |
… | |
… | |
477 | |
468 | |
478 | i = 0; /* restart loop, inefficient, but time jumps should be rare */ |
469 | i = 0; /* restart loop, inefficient, but time jumps should be rare */ |
479 | } |
470 | } |
480 | } |
471 | } |
481 | } |
472 | } |
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 | } |
473 | } |
487 | |
474 | |
488 | static void |
475 | static void |
489 | time_update () |
476 | time_update () |
490 | { |
477 | { |
… | |
… | |
505 | return; /* all is well */ |
492 | return; /* all is well */ |
506 | |
493 | |
507 | ev_now = ev_time (); |
494 | ev_now = ev_time (); |
508 | } |
495 | } |
509 | |
496 | |
510 | time_jump (diff - odiff); |
497 | periodics_reschedule (diff - odiff); |
|
|
498 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
511 | } |
499 | } |
512 | else |
500 | else |
513 | { |
501 | { |
514 | if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) |
502 | if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) |
515 | time_jump (ev_now - now); |
503 | { |
|
|
504 | periodics_reschedule (ev_now - now); |
|
|
505 | |
|
|
506 | /* adjust timers. this is easy, as the offset is the same for all */ |
|
|
507 | for (i = 0; i < timercnt; ++i) |
|
|
508 | timers [i]->at += diff; |
|
|
509 | } |
516 | |
510 | |
517 | now = ev_now; |
511 | now = ev_now; |
518 | } |
512 | } |
519 | } |
513 | } |
520 | |
514 | |
521 | int ev_loop_done; |
515 | int ev_loop_done; |
522 | |
516 | |
523 | void ev_loop (int flags) |
517 | void ev_loop (int flags) |
524 | { |
518 | { |
525 | double block; |
519 | double block; |
526 | ev_loop_done = flags & EVLOOP_ONESHOT; |
520 | ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0; |
527 | |
521 | |
528 | if (checkcnt) |
522 | if (checkcnt) |
529 | { |
523 | { |
530 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
524 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
531 | call_pending (); |
525 | call_pending (); |
… | |
… | |
547 | { |
541 | { |
548 | block = MAX_BLOCKTIME; |
542 | block = MAX_BLOCKTIME; |
549 | |
543 | |
550 | if (timercnt) |
544 | if (timercnt) |
551 | { |
545 | { |
552 | ev_tstamp to = timers [0]->at - get_clock () + method_fudge; |
546 | ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge; |
553 | if (block > to) block = to; |
547 | if (block > to) block = to; |
554 | } |
548 | } |
555 | |
549 | |
556 | if (periodiccnt) |
550 | if (periodiccnt) |
557 | { |
551 | { |
… | |
… | |
579 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
573 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
580 | |
574 | |
581 | call_pending (); |
575 | call_pending (); |
582 | } |
576 | } |
583 | while (!ev_loop_done); |
577 | while (!ev_loop_done); |
|
|
578 | |
|
|
579 | if (ev_loop_done != 2) |
|
|
580 | ev_loop_done = 0; |
584 | } |
581 | } |
585 | |
582 | |
586 | /*****************************************************************************/ |
583 | /*****************************************************************************/ |
587 | |
584 | |
588 | static void |
585 | static void |
… | |
… | |
606 | head = &(*head)->next; |
603 | head = &(*head)->next; |
607 | } |
604 | } |
608 | } |
605 | } |
609 | |
606 | |
610 | static void |
607 | static void |
|
|
608 | ev_clear (W w) |
|
|
609 | { |
|
|
610 | if (w->pending) |
|
|
611 | { |
|
|
612 | pendings [w->pending - 1].w = 0; |
|
|
613 | w->pending = 0; |
|
|
614 | } |
|
|
615 | } |
|
|
616 | |
|
|
617 | static void |
611 | ev_start (W w, int active) |
618 | ev_start (W w, int active) |
612 | { |
619 | { |
613 | w->pending = 0; |
|
|
614 | w->active = active; |
620 | w->active = active; |
615 | } |
621 | } |
616 | |
622 | |
617 | static void |
623 | static void |
618 | ev_stop (W w) |
624 | ev_stop (W w) |
619 | { |
625 | { |
620 | if (w->pending) |
|
|
621 | pendings [w->pending - 1].w = 0; |
|
|
622 | |
|
|
623 | w->active = 0; |
626 | w->active = 0; |
624 | } |
627 | } |
625 | |
628 | |
626 | /*****************************************************************************/ |
629 | /*****************************************************************************/ |
627 | |
630 | |
… | |
… | |
643 | } |
646 | } |
644 | |
647 | |
645 | void |
648 | void |
646 | evio_stop (struct ev_io *w) |
649 | evio_stop (struct ev_io *w) |
647 | { |
650 | { |
|
|
651 | ev_clear ((W)w); |
648 | if (!ev_is_active (w)) |
652 | if (!ev_is_active (w)) |
649 | return; |
653 | return; |
650 | |
654 | |
651 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
655 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
652 | ev_stop ((W)w); |
656 | ev_stop ((W)w); |
… | |
… | |
654 | ++fdchangecnt; |
658 | ++fdchangecnt; |
655 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
659 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
656 | fdchanges [fdchangecnt - 1] = w->fd; |
660 | fdchanges [fdchangecnt - 1] = w->fd; |
657 | } |
661 | } |
658 | |
662 | |
659 | |
|
|
660 | void |
663 | void |
661 | evtimer_start (struct ev_timer *w) |
664 | evtimer_start (struct ev_timer *w) |
662 | { |
665 | { |
663 | if (ev_is_active (w)) |
666 | if (ev_is_active (w)) |
664 | return; |
667 | return; |
665 | |
668 | |
666 | w->at += now; |
669 | w->at += now; |
|
|
670 | |
|
|
671 | assert (("timer repeat value less than zero not allowed", w->repeat >= 0.)); |
667 | |
672 | |
668 | ev_start ((W)w, ++timercnt); |
673 | ev_start ((W)w, ++timercnt); |
669 | array_needsize (timers, timermax, timercnt, ); |
674 | array_needsize (timers, timermax, timercnt, ); |
670 | timers [timercnt - 1] = w; |
675 | timers [timercnt - 1] = w; |
671 | upheap ((WT *)timers, timercnt - 1); |
676 | upheap ((WT *)timers, timercnt - 1); |
672 | } |
677 | } |
673 | |
678 | |
674 | void |
679 | void |
675 | evtimer_stop (struct ev_timer *w) |
680 | evtimer_stop (struct ev_timer *w) |
676 | { |
681 | { |
|
|
682 | ev_clear ((W)w); |
677 | if (!ev_is_active (w)) |
683 | if (!ev_is_active (w)) |
678 | return; |
684 | return; |
679 | |
685 | |
680 | if (w->active < timercnt--) |
686 | if (w->active < timercnt--) |
681 | { |
687 | { |
682 | timers [w->active - 1] = timers [timercnt]; |
688 | timers [w->active - 1] = timers [timercnt]; |
683 | downheap ((WT *)timers, timercnt, w->active - 1); |
689 | downheap ((WT *)timers, timercnt, w->active - 1); |
684 | } |
690 | } |
685 | |
691 | |
|
|
692 | w->at = w->repeat; |
|
|
693 | |
686 | ev_stop ((W)w); |
694 | ev_stop ((W)w); |
|
|
695 | } |
|
|
696 | |
|
|
697 | void |
|
|
698 | evtimer_again (struct ev_timer *w) |
|
|
699 | { |
|
|
700 | if (ev_is_active (w)) |
|
|
701 | { |
|
|
702 | if (w->repeat) |
|
|
703 | { |
|
|
704 | w->at = now + w->repeat; |
|
|
705 | downheap ((WT *)timers, timercnt, w->active - 1); |
|
|
706 | } |
|
|
707 | else |
|
|
708 | evtimer_stop (w); |
|
|
709 | } |
|
|
710 | else if (w->repeat) |
|
|
711 | evtimer_start (w); |
687 | } |
712 | } |
688 | |
713 | |
689 | void |
714 | void |
690 | evperiodic_start (struct ev_periodic *w) |
715 | evperiodic_start (struct ev_periodic *w) |
691 | { |
716 | { |
692 | if (ev_is_active (w)) |
717 | if (ev_is_active (w)) |
693 | return; |
718 | return; |
|
|
719 | |
|
|
720 | assert (("periodic interval value less than zero not allowed", w->interval >= 0.)); |
694 | |
721 | |
695 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
722 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
696 | if (w->interval) |
723 | if (w->interval) |
697 | w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; |
724 | w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; |
698 | |
725 | |
… | |
… | |
703 | } |
730 | } |
704 | |
731 | |
705 | void |
732 | void |
706 | evperiodic_stop (struct ev_periodic *w) |
733 | evperiodic_stop (struct ev_periodic *w) |
707 | { |
734 | { |
|
|
735 | ev_clear ((W)w); |
708 | if (!ev_is_active (w)) |
736 | if (!ev_is_active (w)) |
709 | return; |
737 | return; |
710 | |
738 | |
711 | if (w->active < periodiccnt--) |
739 | if (w->active < periodiccnt--) |
712 | { |
740 | { |
… | |
… | |
738 | } |
766 | } |
739 | |
767 | |
740 | void |
768 | void |
741 | evsignal_stop (struct ev_signal *w) |
769 | evsignal_stop (struct ev_signal *w) |
742 | { |
770 | { |
|
|
771 | ev_clear ((W)w); |
743 | if (!ev_is_active (w)) |
772 | if (!ev_is_active (w)) |
744 | return; |
773 | return; |
745 | |
774 | |
746 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
775 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
747 | ev_stop ((W)w); |
776 | ev_stop ((W)w); |
… | |
… | |
760 | idles [idlecnt - 1] = w; |
789 | idles [idlecnt - 1] = w; |
761 | } |
790 | } |
762 | |
791 | |
763 | void evidle_stop (struct ev_idle *w) |
792 | void evidle_stop (struct ev_idle *w) |
764 | { |
793 | { |
|
|
794 | ev_clear ((W)w); |
|
|
795 | if (ev_is_active (w)) |
|
|
796 | return; |
|
|
797 | |
765 | idles [w->active - 1] = idles [--idlecnt]; |
798 | idles [w->active - 1] = idles [--idlecnt]; |
766 | ev_stop ((W)w); |
799 | ev_stop ((W)w); |
767 | } |
800 | } |
768 | |
801 | |
769 | void evcheck_start (struct ev_check *w) |
802 | void evcheck_start (struct ev_check *w) |
… | |
… | |
776 | checks [checkcnt - 1] = w; |
809 | checks [checkcnt - 1] = w; |
777 | } |
810 | } |
778 | |
811 | |
779 | void evcheck_stop (struct ev_check *w) |
812 | void evcheck_stop (struct ev_check *w) |
780 | { |
813 | { |
|
|
814 | ev_clear ((W)w); |
|
|
815 | if (ev_is_active (w)) |
|
|
816 | return; |
|
|
817 | |
781 | checks [w->active - 1] = checks [--checkcnt]; |
818 | checks [w->active - 1] = checks [--checkcnt]; |
782 | ev_stop ((W)w); |
819 | ev_stop ((W)w); |
783 | } |
820 | } |
784 | |
821 | |
785 | /*****************************************************************************/ |
822 | /*****************************************************************************/ |
786 | |
823 | |
|
|
824 | struct ev_once |
|
|
825 | { |
|
|
826 | struct ev_io io; |
|
|
827 | struct ev_timer to; |
|
|
828 | void (*cb)(int revents, void *arg); |
|
|
829 | void *arg; |
|
|
830 | }; |
|
|
831 | |
|
|
832 | static void |
|
|
833 | once_cb (struct ev_once *once, int revents) |
|
|
834 | { |
|
|
835 | void (*cb)(int revents, void *arg) = once->cb; |
|
|
836 | void *arg = once->arg; |
|
|
837 | |
|
|
838 | evio_stop (&once->io); |
|
|
839 | evtimer_stop (&once->to); |
|
|
840 | free (once); |
|
|
841 | |
|
|
842 | cb (revents, arg); |
|
|
843 | } |
|
|
844 | |
|
|
845 | static void |
|
|
846 | once_cb_io (struct ev_io *w, int revents) |
|
|
847 | { |
|
|
848 | once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents); |
|
|
849 | } |
|
|
850 | |
|
|
851 | static void |
|
|
852 | once_cb_to (struct ev_timer *w, int revents) |
|
|
853 | { |
|
|
854 | once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents); |
|
|
855 | } |
|
|
856 | |
|
|
857 | void |
|
|
858 | ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
|
|
859 | { |
|
|
860 | struct ev_once *once = malloc (sizeof (struct ev_once)); |
|
|
861 | |
|
|
862 | if (!once) |
|
|
863 | cb (EV_ERROR, arg); |
|
|
864 | else |
|
|
865 | { |
|
|
866 | once->cb = cb; |
|
|
867 | once->arg = arg; |
|
|
868 | |
|
|
869 | evw_init (&once->io, once_cb_io); |
|
|
870 | |
|
|
871 | if (fd >= 0) |
|
|
872 | { |
|
|
873 | evio_set (&once->io, fd, events); |
|
|
874 | evio_start (&once->io); |
|
|
875 | } |
|
|
876 | |
|
|
877 | evw_init (&once->to, once_cb_to); |
|
|
878 | |
|
|
879 | if (timeout >= 0.) |
|
|
880 | { |
|
|
881 | evtimer_set (&once->to, timeout, 0.); |
|
|
882 | evtimer_start (&once->to); |
|
|
883 | } |
|
|
884 | } |
|
|
885 | } |
|
|
886 | |
|
|
887 | /*****************************************************************************/ |
|
|
888 | |
787 | #if 1 |
889 | #if 0 |
788 | |
890 | |
789 | struct ev_io wio; |
891 | struct ev_io wio; |
790 | |
892 | |
791 | static void |
893 | static void |
792 | sin_cb (struct ev_io *w, int revents) |
894 | sin_cb (struct ev_io *w, int revents) |