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