|
|
1 | /* |
|
|
2 | * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de> |
|
|
3 | * All rights reserved. |
|
|
4 | * |
|
|
5 | * Redistribution and use in source and binary forms, with or without |
|
|
6 | * modification, are permitted provided that the following conditions are |
|
|
7 | * met: |
|
|
8 | * |
|
|
9 | * * Redistributions of source code must retain the above copyright |
|
|
10 | * notice, this list of conditions and the following disclaimer. |
|
|
11 | * |
|
|
12 | * * Redistributions in binary form must reproduce the above |
|
|
13 | * copyright notice, this list of conditions and the following |
|
|
14 | * disclaimer in the documentation and/or other materials provided |
|
|
15 | * with the distribution. |
|
|
16 | * |
|
|
17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
|
18 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
|
19 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
|
20 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
|
21 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
|
22 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
|
23 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
|
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
|
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
|
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
|
27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
|
28 | */ |
|
|
29 | |
1 | #include <math.h> |
30 | #include <math.h> |
2 | #include <stdlib.h> |
31 | #include <stdlib.h> |
3 | #include <unistd.h> |
32 | #include <unistd.h> |
4 | #include <fcntl.h> |
33 | #include <fcntl.h> |
5 | #include <signal.h> |
34 | #include <signal.h> |
|
|
35 | #include <stddef.h> |
6 | |
36 | |
7 | #include <stdio.h> |
37 | #include <stdio.h> |
8 | |
38 | |
9 | #include <assert.h> |
39 | #include <assert.h> |
10 | #include <errno.h> |
40 | #include <errno.h> |
11 | #include <sys/time.h> |
41 | #include <sys/time.h> |
12 | #include <time.h> |
42 | #include <time.h> |
13 | |
|
|
14 | #define HAVE_EPOLL 1 |
|
|
15 | |
43 | |
16 | #ifndef HAVE_MONOTONIC |
44 | #ifndef HAVE_MONOTONIC |
17 | # ifdef CLOCK_MONOTONIC |
45 | # ifdef CLOCK_MONOTONIC |
18 | # define HAVE_MONOTONIC 1 |
46 | # define HAVE_MONOTONIC 1 |
19 | # endif |
47 | # endif |
… | |
… | |
83 | |
111 | |
84 | #define array_needsize(base,cur,cnt,init) \ |
112 | #define array_needsize(base,cur,cnt,init) \ |
85 | if ((cnt) > cur) \ |
113 | if ((cnt) > cur) \ |
86 | { \ |
114 | { \ |
87 | int newcnt = cur ? cur << 1 : 16; \ |
115 | 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)); \ |
116 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
90 | init (base + cur, newcnt - cur); \ |
117 | init (base + cur, newcnt - cur); \ |
91 | cur = newcnt; \ |
118 | cur = newcnt; \ |
92 | } |
119 | } |
93 | |
120 | |
… | |
… | |
126 | static int pendingmax, pendingcnt; |
153 | static int pendingmax, pendingcnt; |
127 | |
154 | |
128 | static void |
155 | static void |
129 | event (W w, int events) |
156 | event (W w, int events) |
130 | { |
157 | { |
|
|
158 | if (w->active) |
|
|
159 | { |
131 | w->pending = ++pendingcnt; |
160 | w->pending = ++pendingcnt; |
132 | array_needsize (pendings, pendingmax, pendingcnt, ); |
161 | array_needsize (pendings, pendingmax, pendingcnt, ); |
133 | pendings [pendingcnt - 1].w = w; |
162 | pendings [pendingcnt - 1].w = w; |
134 | pendings [pendingcnt - 1].events = events; |
163 | pendings [pendingcnt - 1].events = events; |
|
|
164 | } |
135 | } |
165 | } |
136 | |
166 | |
137 | static void |
167 | static void |
138 | fd_event (int fd, int events) |
168 | fd_event (int fd, int events) |
139 | { |
169 | { |
… | |
… | |
154 | { |
184 | { |
155 | int i; |
185 | int i; |
156 | |
186 | |
157 | for (i = 0; i < eventcnt; ++i) |
187 | for (i = 0; i < eventcnt; ++i) |
158 | event (events [i], type); |
188 | event (events [i], type); |
|
|
189 | } |
|
|
190 | |
|
|
191 | /* called on EBADF to verify fds */ |
|
|
192 | static void |
|
|
193 | fd_recheck () |
|
|
194 | { |
|
|
195 | int fd; |
|
|
196 | |
|
|
197 | for (fd = 0; fd < anfdmax; ++fd) |
|
|
198 | if (anfds [fd].wev) |
|
|
199 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
|
|
200 | while (anfds [fd].head) |
|
|
201 | evio_stop (anfds [fd].head); |
159 | } |
202 | } |
160 | |
203 | |
161 | /*****************************************************************************/ |
204 | /*****************************************************************************/ |
162 | |
205 | |
163 | static struct ev_timer **timers; |
206 | static struct ev_timer **timers; |
… | |
… | |
384 | } |
427 | } |
385 | |
428 | |
386 | static void |
429 | static void |
387 | call_pending () |
430 | call_pending () |
388 | { |
431 | { |
389 | int i; |
432 | while (pendingcnt) |
390 | |
|
|
391 | for (i = 0; i < pendingcnt; ++i) |
|
|
392 | { |
433 | { |
393 | ANPENDING *p = pendings + i; |
434 | ANPENDING *p = pendings + --pendingcnt; |
394 | |
435 | |
395 | if (p->w) |
436 | if (p->w) |
396 | { |
437 | { |
397 | p->w->pending = 0; |
438 | p->w->pending = 0; |
398 | p->w->cb (p->w, p->events); |
439 | p->w->cb (p->w, p->events); |
399 | } |
440 | } |
400 | } |
441 | } |
401 | |
|
|
402 | pendingcnt = 0; |
|
|
403 | } |
442 | } |
404 | |
443 | |
405 | static void |
444 | static void |
406 | timers_reify () |
445 | timers_reify () |
407 | { |
446 | { |
408 | while (timercnt && timers [0]->at <= now) |
447 | while (timercnt && timers [0]->at <= now) |
409 | { |
448 | { |
410 | struct ev_timer *w = timers [0]; |
449 | struct ev_timer *w = timers [0]; |
|
|
450 | |
|
|
451 | event ((W)w, EV_TIMEOUT); |
411 | |
452 | |
412 | /* first reschedule or stop timer */ |
453 | /* first reschedule or stop timer */ |
413 | if (w->repeat) |
454 | if (w->repeat) |
414 | { |
455 | { |
415 | w->at = now + w->repeat; |
456 | w->at = now + w->repeat; |
416 | assert (("timer timeout in the past, negative repeat?", w->at > now)); |
457 | assert (("timer timeout in the past, negative repeat?", w->at > now)); |
417 | downheap ((WT *)timers, timercnt, 0); |
458 | downheap ((WT *)timers, timercnt, 0); |
418 | } |
459 | } |
419 | else |
460 | else |
420 | evtimer_stop (w); /* nonrepeating: stop timer */ |
461 | evtimer_stop (w); /* nonrepeating: stop timer */ |
421 | |
|
|
422 | event ((W)w, EV_TIMEOUT); |
|
|
423 | } |
462 | } |
424 | } |
463 | } |
425 | |
464 | |
426 | static void |
465 | static void |
427 | periodics_reify () |
466 | periodics_reify () |
… | |
… | |
538 | { |
577 | { |
539 | block = MAX_BLOCKTIME; |
578 | block = MAX_BLOCKTIME; |
540 | |
579 | |
541 | if (timercnt) |
580 | if (timercnt) |
542 | { |
581 | { |
543 | ev_tstamp to = timers [0]->at - get_clock () + method_fudge; |
582 | ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge; |
544 | if (block > to) block = to; |
583 | if (block > to) block = to; |
545 | } |
584 | } |
546 | |
585 | |
547 | if (periodiccnt) |
586 | if (periodiccnt) |
548 | { |
587 | { |
… | |
… | |
600 | head = &(*head)->next; |
639 | head = &(*head)->next; |
601 | } |
640 | } |
602 | } |
641 | } |
603 | |
642 | |
604 | static void |
643 | static void |
|
|
644 | ev_clear (W w) |
|
|
645 | { |
|
|
646 | if (w->pending) |
|
|
647 | { |
|
|
648 | pendings [w->pending - 1].w = 0; |
|
|
649 | w->pending = 0; |
|
|
650 | } |
|
|
651 | } |
|
|
652 | |
|
|
653 | static void |
605 | ev_start (W w, int active) |
654 | ev_start (W w, int active) |
606 | { |
655 | { |
607 | w->pending = 0; |
|
|
608 | w->active = active; |
656 | w->active = active; |
609 | } |
657 | } |
610 | |
658 | |
611 | static void |
659 | static void |
612 | ev_stop (W w) |
660 | ev_stop (W w) |
613 | { |
661 | { |
614 | if (w->pending) |
|
|
615 | pendings [w->pending - 1].w = 0; |
|
|
616 | |
|
|
617 | w->active = 0; |
662 | w->active = 0; |
618 | } |
663 | } |
619 | |
664 | |
620 | /*****************************************************************************/ |
665 | /*****************************************************************************/ |
621 | |
666 | |
… | |
… | |
637 | } |
682 | } |
638 | |
683 | |
639 | void |
684 | void |
640 | evio_stop (struct ev_io *w) |
685 | evio_stop (struct ev_io *w) |
641 | { |
686 | { |
|
|
687 | ev_clear ((W)w); |
642 | if (!ev_is_active (w)) |
688 | if (!ev_is_active (w)) |
643 | return; |
689 | return; |
644 | |
690 | |
645 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
691 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
646 | ev_stop ((W)w); |
692 | ev_stop ((W)w); |
647 | |
693 | |
648 | ++fdchangecnt; |
694 | ++fdchangecnt; |
649 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
695 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
650 | fdchanges [fdchangecnt - 1] = w->fd; |
696 | fdchanges [fdchangecnt - 1] = w->fd; |
651 | } |
697 | } |
652 | |
|
|
653 | |
698 | |
654 | void |
699 | void |
655 | evtimer_start (struct ev_timer *w) |
700 | evtimer_start (struct ev_timer *w) |
656 | { |
701 | { |
657 | if (ev_is_active (w)) |
702 | if (ev_is_active (w)) |
… | |
… | |
668 | } |
713 | } |
669 | |
714 | |
670 | void |
715 | void |
671 | evtimer_stop (struct ev_timer *w) |
716 | evtimer_stop (struct ev_timer *w) |
672 | { |
717 | { |
|
|
718 | ev_clear ((W)w); |
673 | if (!ev_is_active (w)) |
719 | if (!ev_is_active (w)) |
674 | return; |
720 | return; |
675 | |
721 | |
676 | if (w->active < timercnt--) |
722 | if (w->active < timercnt--) |
677 | { |
723 | { |
678 | timers [w->active - 1] = timers [timercnt]; |
724 | timers [w->active - 1] = timers [timercnt]; |
679 | downheap ((WT *)timers, timercnt, w->active - 1); |
725 | downheap ((WT *)timers, timercnt, w->active - 1); |
680 | } |
726 | } |
681 | |
727 | |
|
|
728 | w->at = w->repeat; |
|
|
729 | |
682 | ev_stop ((W)w); |
730 | ev_stop ((W)w); |
|
|
731 | } |
|
|
732 | |
|
|
733 | void |
|
|
734 | evtimer_again (struct ev_timer *w) |
|
|
735 | { |
|
|
736 | if (ev_is_active (w)) |
|
|
737 | { |
|
|
738 | if (w->repeat) |
|
|
739 | { |
|
|
740 | w->at = now + w->repeat; |
|
|
741 | downheap ((WT *)timers, timercnt, w->active - 1); |
|
|
742 | } |
|
|
743 | else |
|
|
744 | evtimer_stop (w); |
|
|
745 | } |
|
|
746 | else if (w->repeat) |
|
|
747 | evtimer_start (w); |
683 | } |
748 | } |
684 | |
749 | |
685 | void |
750 | void |
686 | evperiodic_start (struct ev_periodic *w) |
751 | evperiodic_start (struct ev_periodic *w) |
687 | { |
752 | { |
… | |
… | |
701 | } |
766 | } |
702 | |
767 | |
703 | void |
768 | void |
704 | evperiodic_stop (struct ev_periodic *w) |
769 | evperiodic_stop (struct ev_periodic *w) |
705 | { |
770 | { |
|
|
771 | ev_clear ((W)w); |
706 | if (!ev_is_active (w)) |
772 | if (!ev_is_active (w)) |
707 | return; |
773 | return; |
708 | |
774 | |
709 | if (w->active < periodiccnt--) |
775 | if (w->active < periodiccnt--) |
710 | { |
776 | { |
… | |
… | |
736 | } |
802 | } |
737 | |
803 | |
738 | void |
804 | void |
739 | evsignal_stop (struct ev_signal *w) |
805 | evsignal_stop (struct ev_signal *w) |
740 | { |
806 | { |
|
|
807 | ev_clear ((W)w); |
741 | if (!ev_is_active (w)) |
808 | if (!ev_is_active (w)) |
742 | return; |
809 | return; |
743 | |
810 | |
744 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
811 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
745 | ev_stop ((W)w); |
812 | ev_stop ((W)w); |
… | |
… | |
758 | idles [idlecnt - 1] = w; |
825 | idles [idlecnt - 1] = w; |
759 | } |
826 | } |
760 | |
827 | |
761 | void evidle_stop (struct ev_idle *w) |
828 | void evidle_stop (struct ev_idle *w) |
762 | { |
829 | { |
|
|
830 | ev_clear ((W)w); |
|
|
831 | if (ev_is_active (w)) |
|
|
832 | return; |
|
|
833 | |
763 | idles [w->active - 1] = idles [--idlecnt]; |
834 | idles [w->active - 1] = idles [--idlecnt]; |
764 | ev_stop ((W)w); |
835 | ev_stop ((W)w); |
765 | } |
836 | } |
766 | |
837 | |
767 | void evcheck_start (struct ev_check *w) |
838 | void evcheck_start (struct ev_check *w) |
… | |
… | |
774 | checks [checkcnt - 1] = w; |
845 | checks [checkcnt - 1] = w; |
775 | } |
846 | } |
776 | |
847 | |
777 | void evcheck_stop (struct ev_check *w) |
848 | void evcheck_stop (struct ev_check *w) |
778 | { |
849 | { |
|
|
850 | ev_clear ((W)w); |
|
|
851 | if (ev_is_active (w)) |
|
|
852 | return; |
|
|
853 | |
779 | checks [w->active - 1] = checks [--checkcnt]; |
854 | checks [w->active - 1] = checks [--checkcnt]; |
780 | ev_stop ((W)w); |
855 | ev_stop ((W)w); |
|
|
856 | } |
|
|
857 | |
|
|
858 | /*****************************************************************************/ |
|
|
859 | |
|
|
860 | struct ev_once |
|
|
861 | { |
|
|
862 | struct ev_io io; |
|
|
863 | struct ev_timer to; |
|
|
864 | void (*cb)(int revents, void *arg); |
|
|
865 | void *arg; |
|
|
866 | }; |
|
|
867 | |
|
|
868 | static void |
|
|
869 | once_cb (struct ev_once *once, int revents) |
|
|
870 | { |
|
|
871 | void (*cb)(int revents, void *arg) = once->cb; |
|
|
872 | void *arg = once->arg; |
|
|
873 | |
|
|
874 | evio_stop (&once->io); |
|
|
875 | evtimer_stop (&once->to); |
|
|
876 | free (once); |
|
|
877 | |
|
|
878 | cb (revents, arg); |
|
|
879 | } |
|
|
880 | |
|
|
881 | static void |
|
|
882 | once_cb_io (struct ev_io *w, int revents) |
|
|
883 | { |
|
|
884 | once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents); |
|
|
885 | } |
|
|
886 | |
|
|
887 | static void |
|
|
888 | once_cb_to (struct ev_timer *w, int revents) |
|
|
889 | { |
|
|
890 | once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents); |
|
|
891 | } |
|
|
892 | |
|
|
893 | void |
|
|
894 | ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
|
|
895 | { |
|
|
896 | struct ev_once *once = malloc (sizeof (struct ev_once)); |
|
|
897 | |
|
|
898 | if (!once) |
|
|
899 | cb (EV_ERROR, arg); |
|
|
900 | else |
|
|
901 | { |
|
|
902 | once->cb = cb; |
|
|
903 | once->arg = arg; |
|
|
904 | |
|
|
905 | evw_init (&once->io, once_cb_io); |
|
|
906 | |
|
|
907 | if (fd >= 0) |
|
|
908 | { |
|
|
909 | evio_set (&once->io, fd, events); |
|
|
910 | evio_start (&once->io); |
|
|
911 | } |
|
|
912 | |
|
|
913 | evw_init (&once->to, once_cb_to); |
|
|
914 | |
|
|
915 | if (timeout >= 0.) |
|
|
916 | { |
|
|
917 | evtimer_set (&once->to, timeout, 0.); |
|
|
918 | evtimer_start (&once->to); |
|
|
919 | } |
|
|
920 | } |
781 | } |
921 | } |
782 | |
922 | |
783 | /*****************************************************************************/ |
923 | /*****************************************************************************/ |
784 | |
924 | |
785 | #if 0 |
925 | #if 0 |