… | |
… | |
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
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. |
27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | */ |
28 | */ |
|
|
29 | #if EV_USE_CONFIG_H |
|
|
30 | # include "config.h" |
|
|
31 | #endif |
29 | |
32 | |
30 | #include <math.h> |
33 | #include <math.h> |
31 | #include <stdlib.h> |
34 | #include <stdlib.h> |
32 | #include <unistd.h> |
35 | #include <unistd.h> |
33 | #include <fcntl.h> |
36 | #include <fcntl.h> |
… | |
… | |
41 | #include <sys/types.h> |
44 | #include <sys/types.h> |
42 | #include <sys/wait.h> |
45 | #include <sys/wait.h> |
43 | #include <sys/time.h> |
46 | #include <sys/time.h> |
44 | #include <time.h> |
47 | #include <time.h> |
45 | |
48 | |
46 | #ifndef HAVE_MONOTONIC |
49 | #ifndef EV_USE_MONOTONIC |
47 | # ifdef CLOCK_MONOTONIC |
50 | # ifdef CLOCK_MONOTONIC |
48 | # define HAVE_MONOTONIC 1 |
51 | # define EV_USE_MONOTONIC 1 |
49 | # endif |
52 | # endif |
50 | #endif |
53 | #endif |
51 | |
54 | |
52 | #ifndef HAVE_SELECT |
55 | #ifndef EV_USE_SELECT |
53 | # define HAVE_SELECT 1 |
56 | # define EV_USE_SELECT 1 |
54 | #endif |
57 | #endif |
55 | |
58 | |
56 | #ifndef HAVE_EPOLL |
59 | #ifndef EV_USE_EPOLL |
57 | # define HAVE_EPOLL 0 |
60 | # define EV_USE_EPOLL 0 |
58 | #endif |
61 | #endif |
59 | |
62 | |
60 | #ifndef HAVE_REALTIME |
63 | #ifndef EV_USE_REALTIME |
61 | # define HAVE_REALTIME 1 /* posix requirement, but might be slower */ |
64 | # define EV_USE_REALTIME 1 /* posix requirement, but might be slower */ |
62 | #endif |
65 | #endif |
63 | |
66 | |
64 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
67 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
65 | #define MAX_BLOCKTIME 60. |
68 | #define MAX_BLOCKTIME 59.731 |
66 | #define PID_HASHSIZE 16 /* size of pid hahs table, must be power of two */ |
69 | #define PID_HASHSIZE 16 /* size of pid hahs table, must be power of two */ |
67 | |
70 | |
68 | #include "ev.h" |
71 | #include "ev.h" |
69 | |
72 | |
70 | typedef struct ev_watcher *W; |
73 | typedef struct ev_watcher *W; |
… | |
… | |
84 | /*****************************************************************************/ |
87 | /*****************************************************************************/ |
85 | |
88 | |
86 | ev_tstamp |
89 | ev_tstamp |
87 | ev_time (void) |
90 | ev_time (void) |
88 | { |
91 | { |
89 | #if HAVE_REALTIME |
92 | #if EV_USE_REALTIME |
90 | struct timespec ts; |
93 | struct timespec ts; |
91 | clock_gettime (CLOCK_REALTIME, &ts); |
94 | clock_gettime (CLOCK_REALTIME, &ts); |
92 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
95 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
93 | #else |
96 | #else |
94 | struct timeval tv; |
97 | struct timeval tv; |
… | |
… | |
98 | } |
101 | } |
99 | |
102 | |
100 | static ev_tstamp |
103 | static ev_tstamp |
101 | get_clock (void) |
104 | get_clock (void) |
102 | { |
105 | { |
103 | #if HAVE_MONOTONIC |
106 | #if EV_USE_MONOTONIC |
104 | if (have_monotonic) |
107 | if (have_monotonic) |
105 | { |
108 | { |
106 | struct timespec ts; |
109 | struct timespec ts; |
107 | clock_gettime (CLOCK_MONOTONIC, &ts); |
110 | clock_gettime (CLOCK_MONOTONIC, &ts); |
108 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
111 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
109 | } |
112 | } |
110 | #endif |
113 | #endif |
111 | |
114 | |
112 | return ev_time (); |
115 | return ev_time (); |
113 | } |
116 | } |
|
|
117 | |
|
|
118 | #define array_roundsize(base,n) ((n) | 4 & ~3) |
114 | |
119 | |
115 | #define array_needsize(base,cur,cnt,init) \ |
120 | #define array_needsize(base,cur,cnt,init) \ |
116 | if ((cnt) > cur) \ |
121 | if ((cnt) > cur) \ |
117 | { \ |
122 | { \ |
118 | int newcnt = cur; \ |
123 | int newcnt = cur; \ |
119 | do \ |
124 | do \ |
120 | { \ |
125 | { \ |
121 | newcnt = (newcnt << 1) | 4 & ~3; \ |
126 | newcnt = array_roundsize (base, newcnt << 1); \ |
122 | } \ |
127 | } \ |
123 | while ((cnt) > newcnt); \ |
128 | while ((cnt) > newcnt); \ |
124 | \ |
129 | \ |
125 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
130 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
126 | init (base + cur, newcnt - cur); \ |
131 | init (base + cur, newcnt - cur); \ |
… | |
… | |
159 | static int pendingmax, pendingcnt; |
164 | static int pendingmax, pendingcnt; |
160 | |
165 | |
161 | static void |
166 | static void |
162 | event (W w, int events) |
167 | event (W w, int events) |
163 | { |
168 | { |
164 | if (w->active) |
|
|
165 | { |
|
|
166 | w->pending = ++pendingcnt; |
169 | w->pending = ++pendingcnt; |
167 | array_needsize (pendings, pendingmax, pendingcnt, ); |
170 | array_needsize (pendings, pendingmax, pendingcnt, ); |
168 | pendings [pendingcnt - 1].w = w; |
171 | pendings [pendingcnt - 1].w = w; |
169 | pendings [pendingcnt - 1].events = events; |
172 | pendings [pendingcnt - 1].events = events; |
170 | } |
|
|
171 | } |
173 | } |
172 | |
174 | |
173 | static void |
175 | static void |
174 | queue_events (W *events, int eventcnt, int type) |
176 | queue_events (W *events, int eventcnt, int type) |
175 | { |
177 | { |
… | |
… | |
228 | } |
230 | } |
229 | |
231 | |
230 | static void |
232 | static void |
231 | fd_change (int fd) |
233 | fd_change (int fd) |
232 | { |
234 | { |
233 | if (anfds [fd].events & EV_REIFY) |
235 | if (anfds [fd].events & EV_REIFY || fdchangecnt < 0) |
234 | return; |
236 | return; |
235 | |
237 | |
236 | anfds [fd].events |= EV_REIFY; |
238 | anfds [fd].events |= EV_REIFY; |
237 | |
239 | |
238 | ++fdchangecnt; |
240 | ++fdchangecnt; |
… | |
… | |
249 | for (fd = 0; fd < anfdmax; ++fd) |
251 | for (fd = 0; fd < anfdmax; ++fd) |
250 | if (anfds [fd].events) |
252 | if (anfds [fd].events) |
251 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
253 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
252 | while (anfds [fd].head) |
254 | while (anfds [fd].head) |
253 | { |
255 | { |
254 | event ((W)anfds [fd].head, EV_ERROR); |
256 | event ((W)anfds [fd].head, EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT); |
255 | evio_stop (anfds [fd].head); |
257 | ev_io_stop (anfds [fd].head); |
256 | } |
258 | } |
257 | } |
259 | } |
258 | |
260 | |
259 | /*****************************************************************************/ |
261 | /*****************************************************************************/ |
260 | |
262 | |
… | |
… | |
370 | |
372 | |
371 | /* rather than sort out wether we really need nb, set it */ |
373 | /* rather than sort out wether we really need nb, set it */ |
372 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
374 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
373 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
375 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
374 | |
376 | |
375 | evio_set (&sigev, sigpipe [0], EV_READ); |
377 | ev_io_set (&sigev, sigpipe [0], EV_READ); |
376 | evio_start (&sigev); |
378 | ev_io_start (&sigev); |
377 | } |
379 | } |
378 | |
380 | |
379 | /*****************************************************************************/ |
381 | /*****************************************************************************/ |
380 | |
382 | |
381 | static struct ev_idle **idles; |
383 | static struct ev_idle **idles; |
… | |
… | |
411 | } |
413 | } |
412 | } |
414 | } |
413 | |
415 | |
414 | /*****************************************************************************/ |
416 | /*****************************************************************************/ |
415 | |
417 | |
416 | #if HAVE_EPOLL |
418 | #if EV_USE_EPOLL |
417 | # include "ev_epoll.c" |
419 | # include "ev_epoll.c" |
418 | #endif |
420 | #endif |
419 | #if HAVE_SELECT |
421 | #if EV_USE_SELECT |
420 | # include "ev_select.c" |
422 | # include "ev_select.c" |
421 | #endif |
423 | #endif |
422 | |
424 | |
423 | int |
425 | int |
424 | ev_version_major (void) |
426 | ev_version_major (void) |
… | |
… | |
434 | |
436 | |
435 | int ev_init (int flags) |
437 | int ev_init (int flags) |
436 | { |
438 | { |
437 | if (!ev_method) |
439 | if (!ev_method) |
438 | { |
440 | { |
439 | #if HAVE_MONOTONIC |
441 | #if EV_USE_MONOTONIC |
440 | { |
442 | { |
441 | struct timespec ts; |
443 | struct timespec ts; |
442 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
444 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
443 | have_monotonic = 1; |
445 | have_monotonic = 1; |
444 | } |
446 | } |
… | |
… | |
450 | |
452 | |
451 | if (pipe (sigpipe)) |
453 | if (pipe (sigpipe)) |
452 | return 0; |
454 | return 0; |
453 | |
455 | |
454 | ev_method = EVMETHOD_NONE; |
456 | ev_method = EVMETHOD_NONE; |
455 | #if HAVE_EPOLL |
457 | #if EV_USE_EPOLL |
456 | if (ev_method == EVMETHOD_NONE) epoll_init (flags); |
458 | if (ev_method == EVMETHOD_NONE) epoll_init (flags); |
457 | #endif |
459 | #endif |
458 | #if HAVE_SELECT |
460 | #if EV_USE_SELECT |
459 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
461 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
460 | #endif |
462 | #endif |
461 | |
463 | |
462 | if (ev_method) |
464 | if (ev_method) |
463 | { |
465 | { |
464 | evw_init (&sigev, sigcb); |
466 | ev_watcher_init (&sigev, sigcb); |
465 | siginit (); |
467 | siginit (); |
466 | |
468 | |
467 | evsignal_init (&childev, childcb, SIGCHLD); |
469 | ev_signal_init (&childev, childcb, SIGCHLD); |
468 | evsignal_start (&childev); |
470 | ev_signal_start (&childev); |
469 | } |
471 | } |
470 | } |
472 | } |
471 | |
473 | |
472 | return ev_method; |
474 | return ev_method; |
473 | } |
475 | } |
… | |
… | |
487 | } |
489 | } |
488 | |
490 | |
489 | void |
491 | void |
490 | ev_postfork_child (void) |
492 | ev_postfork_child (void) |
491 | { |
493 | { |
492 | #if HAVE_EPOLL |
494 | #if EV_USE_EPOLL |
493 | if (ev_method == EVMETHOD_EPOLL) |
495 | if (ev_method == EVMETHOD_EPOLL) |
494 | epoll_postfork_child (); |
496 | epoll_postfork_child (); |
495 | #endif |
497 | #endif |
496 | |
498 | |
497 | evio_stop (&sigev); |
499 | ev_io_stop (&sigev); |
498 | close (sigpipe [0]); |
500 | close (sigpipe [0]); |
499 | close (sigpipe [1]); |
501 | close (sigpipe [1]); |
500 | pipe (sigpipe); |
502 | pipe (sigpipe); |
501 | siginit (); |
503 | siginit (); |
502 | } |
504 | } |
… | |
… | |
523 | { |
525 | { |
524 | while (timercnt && timers [0]->at <= now) |
526 | while (timercnt && timers [0]->at <= now) |
525 | { |
527 | { |
526 | struct ev_timer *w = timers [0]; |
528 | struct ev_timer *w = timers [0]; |
527 | |
529 | |
528 | event ((W)w, EV_TIMEOUT); |
|
|
529 | |
|
|
530 | /* first reschedule or stop timer */ |
530 | /* first reschedule or stop timer */ |
531 | if (w->repeat) |
531 | if (w->repeat) |
532 | { |
532 | { |
533 | w->at = now + w->repeat; |
533 | w->at = now + w->repeat; |
534 | assert (("timer timeout in the past, negative repeat?", w->at > now)); |
534 | assert (("timer timeout in the past, negative repeat?", w->at > now)); |
535 | downheap ((WT *)timers, timercnt, 0); |
535 | downheap ((WT *)timers, timercnt, 0); |
536 | } |
536 | } |
537 | else |
537 | else |
538 | evtimer_stop (w); /* nonrepeating: stop timer */ |
538 | ev_timer_stop (w); /* nonrepeating: stop timer */ |
|
|
539 | |
|
|
540 | event ((W)w, EV_TIMEOUT); |
539 | } |
541 | } |
540 | } |
542 | } |
541 | |
543 | |
542 | static void |
544 | static void |
543 | periodics_reify (void) |
545 | periodics_reify (void) |
… | |
… | |
552 | w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; |
554 | w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; |
553 | assert (("periodic timeout in the past, negative interval?", w->at > ev_now)); |
555 | assert (("periodic timeout in the past, negative interval?", w->at > ev_now)); |
554 | downheap ((WT *)periodics, periodiccnt, 0); |
556 | downheap ((WT *)periodics, periodiccnt, 0); |
555 | } |
557 | } |
556 | else |
558 | else |
557 | evperiodic_stop (w); /* nonrepeating: stop timer */ |
559 | ev_periodic_stop (w); /* nonrepeating: stop timer */ |
558 | |
560 | |
559 | event ((W)w, EV_TIMEOUT); |
561 | event ((W)w, EV_TIMEOUT); |
560 | } |
562 | } |
561 | } |
563 | } |
562 | |
564 | |
… | |
… | |
574 | { |
576 | { |
575 | ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval; |
577 | ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval; |
576 | |
578 | |
577 | if (fabs (diff) >= 1e-4) |
579 | if (fabs (diff) >= 1e-4) |
578 | { |
580 | { |
579 | evperiodic_stop (w); |
581 | ev_periodic_stop (w); |
580 | evperiodic_start (w); |
582 | ev_periodic_start (w); |
581 | |
583 | |
582 | i = 0; /* restart loop, inefficient, but time jumps should be rare */ |
584 | i = 0; /* restart loop, inefficient, but time jumps should be rare */ |
583 | } |
585 | } |
584 | } |
586 | } |
585 | } |
587 | } |
… | |
… | |
743 | } |
745 | } |
744 | |
746 | |
745 | /*****************************************************************************/ |
747 | /*****************************************************************************/ |
746 | |
748 | |
747 | void |
749 | void |
748 | evio_start (struct ev_io *w) |
750 | ev_io_start (struct ev_io *w) |
749 | { |
751 | { |
750 | if (ev_is_active (w)) |
752 | if (ev_is_active (w)) |
751 | return; |
753 | return; |
752 | |
754 | |
753 | int fd = w->fd; |
755 | int fd = w->fd; |
… | |
… | |
758 | |
760 | |
759 | fd_change (fd); |
761 | fd_change (fd); |
760 | } |
762 | } |
761 | |
763 | |
762 | void |
764 | void |
763 | evio_stop (struct ev_io *w) |
765 | ev_io_stop (struct ev_io *w) |
764 | { |
766 | { |
765 | ev_clear ((W)w); |
767 | ev_clear ((W)w); |
766 | if (!ev_is_active (w)) |
768 | if (!ev_is_active (w)) |
767 | return; |
769 | return; |
768 | |
770 | |
… | |
… | |
771 | |
773 | |
772 | fd_change (w->fd); |
774 | fd_change (w->fd); |
773 | } |
775 | } |
774 | |
776 | |
775 | void |
777 | void |
776 | evtimer_start (struct ev_timer *w) |
778 | ev_timer_start (struct ev_timer *w) |
777 | { |
779 | { |
778 | if (ev_is_active (w)) |
780 | if (ev_is_active (w)) |
779 | return; |
781 | return; |
780 | |
782 | |
781 | w->at += now; |
783 | w->at += now; |
… | |
… | |
787 | timers [timercnt - 1] = w; |
789 | timers [timercnt - 1] = w; |
788 | upheap ((WT *)timers, timercnt - 1); |
790 | upheap ((WT *)timers, timercnt - 1); |
789 | } |
791 | } |
790 | |
792 | |
791 | void |
793 | void |
792 | evtimer_stop (struct ev_timer *w) |
794 | ev_timer_stop (struct ev_timer *w) |
793 | { |
795 | { |
794 | ev_clear ((W)w); |
796 | ev_clear ((W)w); |
795 | if (!ev_is_active (w)) |
797 | if (!ev_is_active (w)) |
796 | return; |
798 | return; |
797 | |
799 | |
… | |
… | |
805 | |
807 | |
806 | ev_stop ((W)w); |
808 | ev_stop ((W)w); |
807 | } |
809 | } |
808 | |
810 | |
809 | void |
811 | void |
810 | evtimer_again (struct ev_timer *w) |
812 | ev_timer_again (struct ev_timer *w) |
811 | { |
813 | { |
812 | if (ev_is_active (w)) |
814 | if (ev_is_active (w)) |
813 | { |
815 | { |
814 | if (w->repeat) |
816 | if (w->repeat) |
815 | { |
817 | { |
816 | w->at = now + w->repeat; |
818 | w->at = now + w->repeat; |
817 | downheap ((WT *)timers, timercnt, w->active - 1); |
819 | downheap ((WT *)timers, timercnt, w->active - 1); |
818 | } |
820 | } |
819 | else |
821 | else |
820 | evtimer_stop (w); |
822 | ev_timer_stop (w); |
821 | } |
823 | } |
822 | else if (w->repeat) |
824 | else if (w->repeat) |
823 | evtimer_start (w); |
825 | ev_timer_start (w); |
824 | } |
826 | } |
825 | |
827 | |
826 | void |
828 | void |
827 | evperiodic_start (struct ev_periodic *w) |
829 | ev_periodic_start (struct ev_periodic *w) |
828 | { |
830 | { |
829 | if (ev_is_active (w)) |
831 | if (ev_is_active (w)) |
830 | return; |
832 | return; |
831 | |
833 | |
832 | assert (("periodic interval value less than zero not allowed", w->interval >= 0.)); |
834 | assert (("periodic interval value less than zero not allowed", w->interval >= 0.)); |
… | |
… | |
840 | periodics [periodiccnt - 1] = w; |
842 | periodics [periodiccnt - 1] = w; |
841 | upheap ((WT *)periodics, periodiccnt - 1); |
843 | upheap ((WT *)periodics, periodiccnt - 1); |
842 | } |
844 | } |
843 | |
845 | |
844 | void |
846 | void |
845 | evperiodic_stop (struct ev_periodic *w) |
847 | ev_periodic_stop (struct ev_periodic *w) |
846 | { |
848 | { |
847 | ev_clear ((W)w); |
849 | ev_clear ((W)w); |
848 | if (!ev_is_active (w)) |
850 | if (!ev_is_active (w)) |
849 | return; |
851 | return; |
850 | |
852 | |
… | |
… | |
856 | |
858 | |
857 | ev_stop ((W)w); |
859 | ev_stop ((W)w); |
858 | } |
860 | } |
859 | |
861 | |
860 | void |
862 | void |
861 | evsignal_start (struct ev_signal *w) |
863 | ev_signal_start (struct ev_signal *w) |
862 | { |
864 | { |
863 | if (ev_is_active (w)) |
865 | if (ev_is_active (w)) |
864 | return; |
866 | return; |
865 | |
867 | |
866 | ev_start ((W)w, 1); |
868 | ev_start ((W)w, 1); |
… | |
… | |
876 | sigaction (w->signum, &sa, 0); |
878 | sigaction (w->signum, &sa, 0); |
877 | } |
879 | } |
878 | } |
880 | } |
879 | |
881 | |
880 | void |
882 | void |
881 | evsignal_stop (struct ev_signal *w) |
883 | ev_signal_stop (struct ev_signal *w) |
882 | { |
884 | { |
883 | ev_clear ((W)w); |
885 | ev_clear ((W)w); |
884 | if (!ev_is_active (w)) |
886 | if (!ev_is_active (w)) |
885 | return; |
887 | return; |
886 | |
888 | |
… | |
… | |
889 | |
891 | |
890 | if (!signals [w->signum - 1].head) |
892 | if (!signals [w->signum - 1].head) |
891 | signal (w->signum, SIG_DFL); |
893 | signal (w->signum, SIG_DFL); |
892 | } |
894 | } |
893 | |
895 | |
|
|
896 | void |
894 | void evidle_start (struct ev_idle *w) |
897 | ev_idle_start (struct ev_idle *w) |
895 | { |
898 | { |
896 | if (ev_is_active (w)) |
899 | if (ev_is_active (w)) |
897 | return; |
900 | return; |
898 | |
901 | |
899 | ev_start ((W)w, ++idlecnt); |
902 | ev_start ((W)w, ++idlecnt); |
900 | array_needsize (idles, idlemax, idlecnt, ); |
903 | array_needsize (idles, idlemax, idlecnt, ); |
901 | idles [idlecnt - 1] = w; |
904 | idles [idlecnt - 1] = w; |
902 | } |
905 | } |
903 | |
906 | |
|
|
907 | void |
904 | void evidle_stop (struct ev_idle *w) |
908 | ev_idle_stop (struct ev_idle *w) |
905 | { |
909 | { |
906 | ev_clear ((W)w); |
910 | ev_clear ((W)w); |
907 | if (ev_is_active (w)) |
911 | if (ev_is_active (w)) |
908 | return; |
912 | return; |
909 | |
913 | |
910 | idles [w->active - 1] = idles [--idlecnt]; |
914 | idles [w->active - 1] = idles [--idlecnt]; |
911 | ev_stop ((W)w); |
915 | ev_stop ((W)w); |
912 | } |
916 | } |
913 | |
917 | |
|
|
918 | void |
914 | void evprepare_start (struct ev_prepare *w) |
919 | ev_prepare_start (struct ev_prepare *w) |
915 | { |
920 | { |
916 | if (ev_is_active (w)) |
921 | if (ev_is_active (w)) |
917 | return; |
922 | return; |
918 | |
923 | |
919 | ev_start ((W)w, ++preparecnt); |
924 | ev_start ((W)w, ++preparecnt); |
920 | array_needsize (prepares, preparemax, preparecnt, ); |
925 | array_needsize (prepares, preparemax, preparecnt, ); |
921 | prepares [preparecnt - 1] = w; |
926 | prepares [preparecnt - 1] = w; |
922 | } |
927 | } |
923 | |
928 | |
|
|
929 | void |
924 | void evprepare_stop (struct ev_prepare *w) |
930 | ev_prepare_stop (struct ev_prepare *w) |
925 | { |
931 | { |
926 | ev_clear ((W)w); |
932 | ev_clear ((W)w); |
927 | if (ev_is_active (w)) |
933 | if (ev_is_active (w)) |
928 | return; |
934 | return; |
929 | |
935 | |
930 | prepares [w->active - 1] = prepares [--preparecnt]; |
936 | prepares [w->active - 1] = prepares [--preparecnt]; |
931 | ev_stop ((W)w); |
937 | ev_stop ((W)w); |
932 | } |
938 | } |
933 | |
939 | |
|
|
940 | void |
934 | void evcheck_start (struct ev_check *w) |
941 | ev_check_start (struct ev_check *w) |
935 | { |
942 | { |
936 | if (ev_is_active (w)) |
943 | if (ev_is_active (w)) |
937 | return; |
944 | return; |
938 | |
945 | |
939 | ev_start ((W)w, ++checkcnt); |
946 | ev_start ((W)w, ++checkcnt); |
940 | array_needsize (checks, checkmax, checkcnt, ); |
947 | array_needsize (checks, checkmax, checkcnt, ); |
941 | checks [checkcnt - 1] = w; |
948 | checks [checkcnt - 1] = w; |
942 | } |
949 | } |
943 | |
950 | |
|
|
951 | void |
944 | void evcheck_stop (struct ev_check *w) |
952 | ev_check_stop (struct ev_check *w) |
945 | { |
953 | { |
946 | ev_clear ((W)w); |
954 | ev_clear ((W)w); |
947 | if (ev_is_active (w)) |
955 | if (ev_is_active (w)) |
948 | return; |
956 | return; |
949 | |
957 | |
950 | checks [w->active - 1] = checks [--checkcnt]; |
958 | checks [w->active - 1] = checks [--checkcnt]; |
951 | ev_stop ((W)w); |
959 | ev_stop ((W)w); |
952 | } |
960 | } |
953 | |
961 | |
|
|
962 | void |
954 | void evchild_start (struct ev_child *w) |
963 | ev_child_start (struct ev_child *w) |
955 | { |
964 | { |
956 | if (ev_is_active (w)) |
965 | if (ev_is_active (w)) |
957 | return; |
966 | return; |
958 | |
967 | |
959 | ev_start ((W)w, 1); |
968 | ev_start ((W)w, 1); |
960 | wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
969 | wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
961 | } |
970 | } |
962 | |
971 | |
|
|
972 | void |
963 | void evchild_stop (struct ev_child *w) |
973 | ev_child_stop (struct ev_child *w) |
964 | { |
974 | { |
965 | ev_clear ((W)w); |
975 | ev_clear ((W)w); |
966 | if (ev_is_active (w)) |
976 | if (ev_is_active (w)) |
967 | return; |
977 | return; |
968 | |
978 | |
… | |
… | |
984 | once_cb (struct ev_once *once, int revents) |
994 | once_cb (struct ev_once *once, int revents) |
985 | { |
995 | { |
986 | void (*cb)(int revents, void *arg) = once->cb; |
996 | void (*cb)(int revents, void *arg) = once->cb; |
987 | void *arg = once->arg; |
997 | void *arg = once->arg; |
988 | |
998 | |
989 | evio_stop (&once->io); |
999 | ev_io_stop (&once->io); |
990 | evtimer_stop (&once->to); |
1000 | ev_timer_stop (&once->to); |
991 | free (once); |
1001 | free (once); |
992 | |
1002 | |
993 | cb (revents, arg); |
1003 | cb (revents, arg); |
994 | } |
1004 | } |
995 | |
1005 | |
… | |
… | |
1009 | ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1019 | ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1010 | { |
1020 | { |
1011 | struct ev_once *once = malloc (sizeof (struct ev_once)); |
1021 | struct ev_once *once = malloc (sizeof (struct ev_once)); |
1012 | |
1022 | |
1013 | if (!once) |
1023 | if (!once) |
1014 | cb (EV_ERROR, arg); |
1024 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
1015 | else |
1025 | else |
1016 | { |
1026 | { |
1017 | once->cb = cb; |
1027 | once->cb = cb; |
1018 | once->arg = arg; |
1028 | once->arg = arg; |
1019 | |
1029 | |
1020 | evw_init (&once->io, once_cb_io); |
1030 | ev_watcher_init (&once->io, once_cb_io); |
1021 | |
|
|
1022 | if (fd >= 0) |
1031 | if (fd >= 0) |
1023 | { |
1032 | { |
1024 | evio_set (&once->io, fd, events); |
1033 | ev_io_set (&once->io, fd, events); |
1025 | evio_start (&once->io); |
1034 | ev_io_start (&once->io); |
1026 | } |
1035 | } |
1027 | |
1036 | |
1028 | evw_init (&once->to, once_cb_to); |
1037 | ev_watcher_init (&once->to, once_cb_to); |
1029 | |
|
|
1030 | if (timeout >= 0.) |
1038 | if (timeout >= 0.) |
1031 | { |
1039 | { |
1032 | evtimer_set (&once->to, timeout, 0.); |
1040 | ev_timer_set (&once->to, timeout, 0.); |
1033 | evtimer_start (&once->to); |
1041 | ev_timer_start (&once->to); |
1034 | } |
1042 | } |
1035 | } |
1043 | } |
1036 | } |
1044 | } |
1037 | |
1045 | |
1038 | /*****************************************************************************/ |
1046 | /*****************************************************************************/ |
… | |
… | |
1049 | |
1057 | |
1050 | static void |
1058 | static void |
1051 | ocb (struct ev_timer *w, int revents) |
1059 | ocb (struct ev_timer *w, int revents) |
1052 | { |
1060 | { |
1053 | //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data); |
1061 | //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data); |
1054 | evtimer_stop (w); |
1062 | ev_timer_stop (w); |
1055 | evtimer_start (w); |
1063 | ev_timer_start (w); |
1056 | } |
1064 | } |
1057 | |
1065 | |
1058 | static void |
1066 | static void |
1059 | scb (struct ev_signal *w, int revents) |
1067 | scb (struct ev_signal *w, int revents) |
1060 | { |
1068 | { |
1061 | fprintf (stderr, "signal %x,%d\n", revents, w->signum); |
1069 | fprintf (stderr, "signal %x,%d\n", revents, w->signum); |
1062 | evio_stop (&wio); |
1070 | ev_io_stop (&wio); |
1063 | evio_start (&wio); |
1071 | ev_io_start (&wio); |
1064 | } |
1072 | } |
1065 | |
1073 | |
1066 | static void |
1074 | static void |
1067 | gcb (struct ev_signal *w, int revents) |
1075 | gcb (struct ev_signal *w, int revents) |
1068 | { |
1076 | { |
… | |
… | |
1072 | |
1080 | |
1073 | int main (void) |
1081 | int main (void) |
1074 | { |
1082 | { |
1075 | ev_init (0); |
1083 | ev_init (0); |
1076 | |
1084 | |
1077 | evio_init (&wio, sin_cb, 0, EV_READ); |
1085 | ev_io_init (&wio, sin_cb, 0, EV_READ); |
1078 | evio_start (&wio); |
1086 | ev_io_start (&wio); |
1079 | |
1087 | |
1080 | struct ev_timer t[10000]; |
1088 | struct ev_timer t[10000]; |
1081 | |
1089 | |
1082 | #if 0 |
1090 | #if 0 |
1083 | int i; |
1091 | int i; |
1084 | for (i = 0; i < 10000; ++i) |
1092 | for (i = 0; i < 10000; ++i) |
1085 | { |
1093 | { |
1086 | struct ev_timer *w = t + i; |
1094 | struct ev_timer *w = t + i; |
1087 | evw_init (w, ocb, i); |
1095 | ev_watcher_init (w, ocb, i); |
1088 | evtimer_init_abs (w, ocb, drand48 (), 0.99775533); |
1096 | ev_timer_init_abs (w, ocb, drand48 (), 0.99775533); |
1089 | evtimer_start (w); |
1097 | ev_timer_start (w); |
1090 | if (drand48 () < 0.5) |
1098 | if (drand48 () < 0.5) |
1091 | evtimer_stop (w); |
1099 | ev_timer_stop (w); |
1092 | } |
1100 | } |
1093 | #endif |
1101 | #endif |
1094 | |
1102 | |
1095 | struct ev_timer t1; |
1103 | struct ev_timer t1; |
1096 | evtimer_init (&t1, ocb, 5, 10); |
1104 | ev_timer_init (&t1, ocb, 5, 10); |
1097 | evtimer_start (&t1); |
1105 | ev_timer_start (&t1); |
1098 | |
1106 | |
1099 | struct ev_signal sig; |
1107 | struct ev_signal sig; |
1100 | evsignal_init (&sig, scb, SIGQUIT); |
1108 | ev_signal_init (&sig, scb, SIGQUIT); |
1101 | evsignal_start (&sig); |
1109 | ev_signal_start (&sig); |
1102 | |
1110 | |
1103 | struct ev_check cw; |
1111 | struct ev_check cw; |
1104 | evcheck_init (&cw, gcb); |
1112 | ev_check_init (&cw, gcb); |
1105 | evcheck_start (&cw); |
1113 | ev_check_start (&cw); |
1106 | |
1114 | |
1107 | struct ev_idle iw; |
1115 | struct ev_idle iw; |
1108 | evidle_init (&iw, gcb); |
1116 | ev_idle_init (&iw, gcb); |
1109 | evidle_start (&iw); |
1117 | ev_idle_start (&iw); |
1110 | |
1118 | |
1111 | ev_loop (0); |
1119 | ev_loop (0); |
1112 | |
1120 | |
1113 | return 0; |
1121 | return 0; |
1114 | } |
1122 | } |