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> |
… | |
… | |
83 | |
84 | |
84 | #define array_needsize(base,cur,cnt,init) \ |
85 | #define array_needsize(base,cur,cnt,init) \ |
85 | if ((cnt) > cur) \ |
86 | if ((cnt) > cur) \ |
86 | { \ |
87 | { \ |
87 | int newcnt = cur ? cur << 1 : 16; \ |
88 | int newcnt = cur ? cur << 1 : 16; \ |
88 | fprintf (stderr, "resize(" # base ") from %d to %d\n", cur, newcnt);\ |
|
|
89 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
89 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
90 | init (base + cur, newcnt - cur); \ |
90 | init (base + cur, newcnt - cur); \ |
91 | cur = newcnt; \ |
91 | cur = newcnt; \ |
92 | } |
92 | } |
93 | |
93 | |
… | |
… | |
126 | static int pendingmax, pendingcnt; |
126 | static int pendingmax, pendingcnt; |
127 | |
127 | |
128 | static void |
128 | static void |
129 | event (W w, int events) |
129 | event (W w, int events) |
130 | { |
130 | { |
|
|
131 | if (w->active) |
|
|
132 | { |
131 | w->pending = ++pendingcnt; |
133 | w->pending = ++pendingcnt; |
132 | array_needsize (pendings, pendingmax, pendingcnt, ); |
134 | array_needsize (pendings, pendingmax, pendingcnt, ); |
133 | pendings [pendingcnt - 1].w = w; |
135 | pendings [pendingcnt - 1].w = w; |
134 | pendings [pendingcnt - 1].events = events; |
136 | pendings [pendingcnt - 1].events = events; |
|
|
137 | } |
135 | } |
138 | } |
136 | |
139 | |
137 | static void |
140 | static void |
138 | fd_event (int fd, int events) |
141 | fd_event (int fd, int events) |
139 | { |
142 | { |
… | |
… | |
407 | { |
410 | { |
408 | while (timercnt && timers [0]->at <= now) |
411 | while (timercnt && timers [0]->at <= now) |
409 | { |
412 | { |
410 | struct ev_timer *w = timers [0]; |
413 | struct ev_timer *w = timers [0]; |
411 | |
414 | |
|
|
415 | event ((W)w, EV_TIMEOUT); |
|
|
416 | |
412 | /* first reschedule or stop timer */ |
417 | /* first reschedule or stop timer */ |
413 | if (w->repeat) |
418 | if (w->repeat) |
414 | { |
419 | { |
415 | w->at = now + w->repeat; |
420 | w->at = now + w->repeat; |
416 | assert (("timer timeout in the past, negative repeat?", w->at > now)); |
421 | assert (("timer timeout in the past, negative repeat?", w->at > now)); |
417 | downheap ((WT *)timers, timercnt, 0); |
422 | downheap ((WT *)timers, timercnt, 0); |
418 | } |
423 | } |
419 | else |
424 | else |
420 | evtimer_stop (w); /* nonrepeating: stop timer */ |
425 | evtimer_stop (w); /* nonrepeating: stop timer */ |
421 | |
|
|
422 | event ((W)w, EV_TIMEOUT); |
|
|
423 | } |
426 | } |
424 | } |
427 | } |
425 | |
428 | |
426 | static void |
429 | static void |
427 | periodics_reify () |
430 | periodics_reify () |
… | |
… | |
600 | head = &(*head)->next; |
603 | head = &(*head)->next; |
601 | } |
604 | } |
602 | } |
605 | } |
603 | |
606 | |
604 | 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 |
605 | ev_start (W w, int active) |
618 | ev_start (W w, int active) |
606 | { |
619 | { |
607 | w->pending = 0; |
|
|
608 | w->active = active; |
620 | w->active = active; |
609 | } |
621 | } |
610 | |
622 | |
611 | static void |
623 | static void |
612 | ev_stop (W w) |
624 | ev_stop (W w) |
613 | { |
625 | { |
614 | if (w->pending) |
|
|
615 | pendings [w->pending - 1].w = 0; |
|
|
616 | |
|
|
617 | w->active = 0; |
626 | w->active = 0; |
618 | } |
627 | } |
619 | |
628 | |
620 | /*****************************************************************************/ |
629 | /*****************************************************************************/ |
621 | |
630 | |
… | |
… | |
637 | } |
646 | } |
638 | |
647 | |
639 | void |
648 | void |
640 | evio_stop (struct ev_io *w) |
649 | evio_stop (struct ev_io *w) |
641 | { |
650 | { |
|
|
651 | ev_clear ((W)w); |
642 | if (!ev_is_active (w)) |
652 | if (!ev_is_active (w)) |
643 | return; |
653 | return; |
644 | |
654 | |
645 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
655 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
646 | ev_stop ((W)w); |
656 | ev_stop ((W)w); |
647 | |
657 | |
648 | ++fdchangecnt; |
658 | ++fdchangecnt; |
649 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
659 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
650 | fdchanges [fdchangecnt - 1] = w->fd; |
660 | fdchanges [fdchangecnt - 1] = w->fd; |
651 | } |
661 | } |
652 | |
|
|
653 | |
662 | |
654 | void |
663 | void |
655 | evtimer_start (struct ev_timer *w) |
664 | evtimer_start (struct ev_timer *w) |
656 | { |
665 | { |
657 | if (ev_is_active (w)) |
666 | if (ev_is_active (w)) |
… | |
… | |
668 | } |
677 | } |
669 | |
678 | |
670 | void |
679 | void |
671 | evtimer_stop (struct ev_timer *w) |
680 | evtimer_stop (struct ev_timer *w) |
672 | { |
681 | { |
|
|
682 | ev_clear ((W)w); |
673 | if (!ev_is_active (w)) |
683 | if (!ev_is_active (w)) |
674 | return; |
684 | return; |
675 | |
685 | |
676 | if (w->active < timercnt--) |
686 | if (w->active < timercnt--) |
677 | { |
687 | { |
… | |
… | |
720 | } |
730 | } |
721 | |
731 | |
722 | void |
732 | void |
723 | evperiodic_stop (struct ev_periodic *w) |
733 | evperiodic_stop (struct ev_periodic *w) |
724 | { |
734 | { |
|
|
735 | ev_clear ((W)w); |
725 | if (!ev_is_active (w)) |
736 | if (!ev_is_active (w)) |
726 | return; |
737 | return; |
727 | |
738 | |
728 | if (w->active < periodiccnt--) |
739 | if (w->active < periodiccnt--) |
729 | { |
740 | { |
… | |
… | |
755 | } |
766 | } |
756 | |
767 | |
757 | void |
768 | void |
758 | evsignal_stop (struct ev_signal *w) |
769 | evsignal_stop (struct ev_signal *w) |
759 | { |
770 | { |
|
|
771 | ev_clear ((W)w); |
760 | if (!ev_is_active (w)) |
772 | if (!ev_is_active (w)) |
761 | return; |
773 | return; |
762 | |
774 | |
763 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
775 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
764 | ev_stop ((W)w); |
776 | ev_stop ((W)w); |
… | |
… | |
777 | idles [idlecnt - 1] = w; |
789 | idles [idlecnt - 1] = w; |
778 | } |
790 | } |
779 | |
791 | |
780 | void evidle_stop (struct ev_idle *w) |
792 | void evidle_stop (struct ev_idle *w) |
781 | { |
793 | { |
|
|
794 | ev_clear ((W)w); |
|
|
795 | if (ev_is_active (w)) |
|
|
796 | return; |
|
|
797 | |
782 | idles [w->active - 1] = idles [--idlecnt]; |
798 | idles [w->active - 1] = idles [--idlecnt]; |
783 | ev_stop ((W)w); |
799 | ev_stop ((W)w); |
784 | } |
800 | } |
785 | |
801 | |
786 | void evcheck_start (struct ev_check *w) |
802 | void evcheck_start (struct ev_check *w) |
… | |
… | |
793 | checks [checkcnt - 1] = w; |
809 | checks [checkcnt - 1] = w; |
794 | } |
810 | } |
795 | |
811 | |
796 | void evcheck_stop (struct ev_check *w) |
812 | void evcheck_stop (struct ev_check *w) |
797 | { |
813 | { |
|
|
814 | ev_clear ((W)w); |
|
|
815 | if (ev_is_active (w)) |
|
|
816 | return; |
|
|
817 | |
798 | checks [w->active - 1] = checks [--checkcnt]; |
818 | checks [w->active - 1] = checks [--checkcnt]; |
799 | ev_stop ((W)w); |
819 | ev_stop ((W)w); |
|
|
820 | } |
|
|
821 | |
|
|
822 | /*****************************************************************************/ |
|
|
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 | } |
800 | } |
885 | } |
801 | |
886 | |
802 | /*****************************************************************************/ |
887 | /*****************************************************************************/ |
803 | |
888 | |
804 | #if 0 |
889 | #if 0 |