… | |
… | |
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 | |
|
|
63 | #ifndef CLOCK_REALTIME |
|
|
64 | # define EV_USE_REALTIME 0 |
|
|
65 | #endif |
60 | #ifndef HAVE_REALTIME |
66 | #ifndef EV_USE_REALTIME |
61 | # define HAVE_REALTIME 1 /* posix requirement, but might be slower */ |
67 | # define EV_USE_REALTIME 1 /* posix requirement, but might be slower */ |
62 | #endif |
68 | #endif |
63 | |
69 | |
64 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
70 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
65 | #define MAX_BLOCKTIME 60. |
71 | #define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detetc time jumps) */ |
66 | #define PID_HASHSIZE 16 /* size of pid hahs table, must be power of two */ |
72 | #define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */ |
|
|
73 | #define CLEANUP_INTERVAL (MAX_BLOCKTIME * 5.) /* how often to try to free memory and re-check fds */ |
67 | |
74 | |
68 | #include "ev.h" |
75 | #include "ev.h" |
69 | |
76 | |
70 | typedef struct ev_watcher *W; |
77 | typedef struct ev_watcher *W; |
71 | typedef struct ev_watcher_list *WL; |
78 | typedef struct ev_watcher_list *WL; |
… | |
… | |
84 | /*****************************************************************************/ |
91 | /*****************************************************************************/ |
85 | |
92 | |
86 | ev_tstamp |
93 | ev_tstamp |
87 | ev_time (void) |
94 | ev_time (void) |
88 | { |
95 | { |
89 | #if HAVE_REALTIME |
96 | #if EV_USE_REALTIME |
90 | struct timespec ts; |
97 | struct timespec ts; |
91 | clock_gettime (CLOCK_REALTIME, &ts); |
98 | clock_gettime (CLOCK_REALTIME, &ts); |
92 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
99 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
93 | #else |
100 | #else |
94 | struct timeval tv; |
101 | struct timeval tv; |
… | |
… | |
98 | } |
105 | } |
99 | |
106 | |
100 | static ev_tstamp |
107 | static ev_tstamp |
101 | get_clock (void) |
108 | get_clock (void) |
102 | { |
109 | { |
103 | #if HAVE_MONOTONIC |
110 | #if EV_USE_MONOTONIC |
104 | if (have_monotonic) |
111 | if (have_monotonic) |
105 | { |
112 | { |
106 | struct timespec ts; |
113 | struct timespec ts; |
107 | clock_gettime (CLOCK_MONOTONIC, &ts); |
114 | clock_gettime (CLOCK_MONOTONIC, &ts); |
108 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
115 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
109 | } |
116 | } |
110 | #endif |
117 | #endif |
111 | |
118 | |
112 | return ev_time (); |
119 | return ev_time (); |
113 | } |
120 | } |
|
|
121 | |
|
|
122 | #define array_roundsize(base,n) ((n) | 4 & ~3) |
114 | |
123 | |
115 | #define array_needsize(base,cur,cnt,init) \ |
124 | #define array_needsize(base,cur,cnt,init) \ |
116 | if ((cnt) > cur) \ |
125 | if ((cnt) > cur) \ |
117 | { \ |
126 | { \ |
118 | int newcnt = cur; \ |
127 | int newcnt = cur; \ |
119 | do \ |
128 | do \ |
120 | { \ |
129 | { \ |
121 | newcnt = (newcnt << 1) | 4 & ~3; \ |
130 | newcnt = array_roundsize (base, newcnt << 1); \ |
122 | } \ |
131 | } \ |
123 | while ((cnt) > newcnt); \ |
132 | while ((cnt) > newcnt); \ |
124 | \ |
133 | \ |
125 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
134 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
126 | init (base + cur, newcnt - cur); \ |
135 | init (base + cur, newcnt - cur); \ |
… | |
… | |
159 | static int pendingmax, pendingcnt; |
168 | static int pendingmax, pendingcnt; |
160 | |
169 | |
161 | static void |
170 | static void |
162 | event (W w, int events) |
171 | event (W w, int events) |
163 | { |
172 | { |
164 | if (w->active) |
173 | if (w->pending) |
|
|
174 | { |
|
|
175 | pendings [w->pending - 1].events |= events; |
|
|
176 | return; |
165 | { |
177 | } |
|
|
178 | |
166 | w->pending = ++pendingcnt; |
179 | w->pending = ++pendingcnt; |
167 | array_needsize (pendings, pendingmax, pendingcnt, ); |
180 | array_needsize (pendings, pendingmax, pendingcnt, ); |
168 | pendings [pendingcnt - 1].w = w; |
181 | pendings [pendingcnt - 1].w = w; |
169 | pendings [pendingcnt - 1].events = events; |
182 | pendings [pendingcnt - 1].events = events; |
170 | } |
|
|
171 | } |
183 | } |
172 | |
184 | |
173 | static void |
185 | static void |
174 | queue_events (W *events, int eventcnt, int type) |
186 | queue_events (W *events, int eventcnt, int type) |
175 | { |
187 | { |
… | |
… | |
228 | } |
240 | } |
229 | |
241 | |
230 | static void |
242 | static void |
231 | fd_change (int fd) |
243 | fd_change (int fd) |
232 | { |
244 | { |
233 | if (anfds [fd].events & EV_REIFY) |
245 | if (anfds [fd].events & EV_REIFY || fdchangecnt < 0) |
234 | return; |
246 | return; |
235 | |
247 | |
236 | anfds [fd].events |= EV_REIFY; |
248 | anfds [fd].events |= EV_REIFY; |
237 | |
249 | |
238 | ++fdchangecnt; |
250 | ++fdchangecnt; |
… | |
… | |
249 | for (fd = 0; fd < anfdmax; ++fd) |
261 | for (fd = 0; fd < anfdmax; ++fd) |
250 | if (anfds [fd].events) |
262 | if (anfds [fd].events) |
251 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
263 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
252 | while (anfds [fd].head) |
264 | while (anfds [fd].head) |
253 | { |
265 | { |
254 | event ((W)anfds [fd].head, EV_ERROR); |
|
|
255 | evio_stop (anfds [fd].head); |
266 | ev_io_stop (anfds [fd].head); |
|
|
267 | event ((W)anfds [fd].head, EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT); |
256 | } |
268 | } |
257 | } |
269 | } |
258 | |
270 | |
259 | /*****************************************************************************/ |
271 | /*****************************************************************************/ |
260 | |
272 | |
… | |
… | |
370 | |
382 | |
371 | /* rather than sort out wether we really need nb, set it */ |
383 | /* rather than sort out wether we really need nb, set it */ |
372 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
384 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
373 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
385 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
374 | |
386 | |
375 | evio_set (&sigev, sigpipe [0], EV_READ); |
387 | ev_io_set (&sigev, sigpipe [0], EV_READ); |
376 | evio_start (&sigev); |
388 | ev_io_start (&sigev); |
377 | } |
389 | } |
378 | |
390 | |
379 | /*****************************************************************************/ |
391 | /*****************************************************************************/ |
380 | |
392 | |
381 | static struct ev_idle **idles; |
393 | static struct ev_idle **idles; |
… | |
… | |
411 | } |
423 | } |
412 | } |
424 | } |
413 | |
425 | |
414 | /*****************************************************************************/ |
426 | /*****************************************************************************/ |
415 | |
427 | |
416 | #if HAVE_EPOLL |
428 | #if EV_USE_EPOLL |
417 | # include "ev_epoll.c" |
429 | # include "ev_epoll.c" |
418 | #endif |
430 | #endif |
419 | #if HAVE_SELECT |
431 | #if EV_USE_SELECT |
420 | # include "ev_select.c" |
432 | # include "ev_select.c" |
421 | #endif |
433 | #endif |
422 | |
434 | |
423 | int |
435 | int |
424 | ev_version_major (void) |
436 | ev_version_major (void) |
… | |
… | |
434 | |
446 | |
435 | int ev_init (int flags) |
447 | int ev_init (int flags) |
436 | { |
448 | { |
437 | if (!ev_method) |
449 | if (!ev_method) |
438 | { |
450 | { |
439 | #if HAVE_MONOTONIC |
451 | #if EV_USE_MONOTONIC |
440 | { |
452 | { |
441 | struct timespec ts; |
453 | struct timespec ts; |
442 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
454 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
443 | have_monotonic = 1; |
455 | have_monotonic = 1; |
444 | } |
456 | } |
… | |
… | |
450 | |
462 | |
451 | if (pipe (sigpipe)) |
463 | if (pipe (sigpipe)) |
452 | return 0; |
464 | return 0; |
453 | |
465 | |
454 | ev_method = EVMETHOD_NONE; |
466 | ev_method = EVMETHOD_NONE; |
455 | #if HAVE_EPOLL |
467 | #if EV_USE_EPOLL |
456 | if (ev_method == EVMETHOD_NONE) epoll_init (flags); |
468 | if (ev_method == EVMETHOD_NONE) epoll_init (flags); |
457 | #endif |
469 | #endif |
458 | #if HAVE_SELECT |
470 | #if EV_USE_SELECT |
459 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
471 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
460 | #endif |
472 | #endif |
461 | |
473 | |
462 | if (ev_method) |
474 | if (ev_method) |
463 | { |
475 | { |
464 | evw_init (&sigev, sigcb); |
476 | ev_watcher_init (&sigev, sigcb); |
465 | siginit (); |
477 | siginit (); |
466 | |
478 | |
467 | evsignal_init (&childev, childcb, SIGCHLD); |
479 | ev_signal_init (&childev, childcb, SIGCHLD); |
468 | evsignal_start (&childev); |
480 | ev_signal_start (&childev); |
469 | } |
481 | } |
470 | } |
482 | } |
471 | |
483 | |
472 | return ev_method; |
484 | return ev_method; |
473 | } |
485 | } |
… | |
… | |
487 | } |
499 | } |
488 | |
500 | |
489 | void |
501 | void |
490 | ev_postfork_child (void) |
502 | ev_postfork_child (void) |
491 | { |
503 | { |
492 | #if HAVE_EPOLL |
504 | #if EV_USE_EPOLL |
493 | if (ev_method == EVMETHOD_EPOLL) |
505 | if (ev_method == EVMETHOD_EPOLL) |
494 | epoll_postfork_child (); |
506 | epoll_postfork_child (); |
495 | #endif |
507 | #endif |
496 | |
508 | |
497 | evio_stop (&sigev); |
509 | ev_io_stop (&sigev); |
498 | close (sigpipe [0]); |
510 | close (sigpipe [0]); |
499 | close (sigpipe [1]); |
511 | close (sigpipe [1]); |
500 | pipe (sigpipe); |
512 | pipe (sigpipe); |
501 | siginit (); |
513 | siginit (); |
502 | } |
514 | } |
… | |
… | |
523 | { |
535 | { |
524 | while (timercnt && timers [0]->at <= now) |
536 | while (timercnt && timers [0]->at <= now) |
525 | { |
537 | { |
526 | struct ev_timer *w = timers [0]; |
538 | struct ev_timer *w = timers [0]; |
527 | |
539 | |
528 | event ((W)w, EV_TIMEOUT); |
|
|
529 | |
|
|
530 | /* first reschedule or stop timer */ |
540 | /* first reschedule or stop timer */ |
531 | if (w->repeat) |
541 | if (w->repeat) |
532 | { |
542 | { |
533 | w->at = now + w->repeat; |
543 | w->at = now + w->repeat; |
534 | assert (("timer timeout in the past, negative repeat?", w->at > now)); |
544 | assert (("timer timeout in the past, negative repeat?", w->at > now)); |
535 | downheap ((WT *)timers, timercnt, 0); |
545 | downheap ((WT *)timers, timercnt, 0); |
536 | } |
546 | } |
537 | else |
547 | else |
538 | evtimer_stop (w); /* nonrepeating: stop timer */ |
548 | ev_timer_stop (w); /* nonrepeating: stop timer */ |
|
|
549 | |
|
|
550 | event ((W)w, EV_TIMEOUT); |
539 | } |
551 | } |
540 | } |
552 | } |
541 | |
553 | |
542 | static void |
554 | static void |
543 | periodics_reify (void) |
555 | periodics_reify (void) |
… | |
… | |
552 | w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; |
564 | 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)); |
565 | assert (("periodic timeout in the past, negative interval?", w->at > ev_now)); |
554 | downheap ((WT *)periodics, periodiccnt, 0); |
566 | downheap ((WT *)periodics, periodiccnt, 0); |
555 | } |
567 | } |
556 | else |
568 | else |
557 | evperiodic_stop (w); /* nonrepeating: stop timer */ |
569 | ev_periodic_stop (w); /* nonrepeating: stop timer */ |
558 | |
570 | |
559 | event ((W)w, EV_TIMEOUT); |
571 | event ((W)w, EV_TIMEOUT); |
560 | } |
572 | } |
561 | } |
573 | } |
562 | |
574 | |
… | |
… | |
574 | { |
586 | { |
575 | ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval; |
587 | ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval; |
576 | |
588 | |
577 | if (fabs (diff) >= 1e-4) |
589 | if (fabs (diff) >= 1e-4) |
578 | { |
590 | { |
579 | evperiodic_stop (w); |
591 | ev_periodic_stop (w); |
580 | evperiodic_start (w); |
592 | ev_periodic_start (w); |
581 | |
593 | |
582 | i = 0; /* restart loop, inefficient, but time jumps should be rare */ |
594 | i = 0; /* restart loop, inefficient, but time jumps should be rare */ |
583 | } |
595 | } |
584 | } |
596 | } |
585 | } |
597 | } |
… | |
… | |
743 | } |
755 | } |
744 | |
756 | |
745 | /*****************************************************************************/ |
757 | /*****************************************************************************/ |
746 | |
758 | |
747 | void |
759 | void |
748 | evio_start (struct ev_io *w) |
760 | ev_io_start (struct ev_io *w) |
749 | { |
761 | { |
750 | if (ev_is_active (w)) |
762 | if (ev_is_active (w)) |
751 | return; |
763 | return; |
752 | |
764 | |
753 | int fd = w->fd; |
765 | int fd = w->fd; |
… | |
… | |
758 | |
770 | |
759 | fd_change (fd); |
771 | fd_change (fd); |
760 | } |
772 | } |
761 | |
773 | |
762 | void |
774 | void |
763 | evio_stop (struct ev_io *w) |
775 | ev_io_stop (struct ev_io *w) |
764 | { |
776 | { |
765 | ev_clear ((W)w); |
777 | ev_clear ((W)w); |
766 | if (!ev_is_active (w)) |
778 | if (!ev_is_active (w)) |
767 | return; |
779 | return; |
768 | |
780 | |
… | |
… | |
771 | |
783 | |
772 | fd_change (w->fd); |
784 | fd_change (w->fd); |
773 | } |
785 | } |
774 | |
786 | |
775 | void |
787 | void |
776 | evtimer_start (struct ev_timer *w) |
788 | ev_timer_start (struct ev_timer *w) |
777 | { |
789 | { |
778 | if (ev_is_active (w)) |
790 | if (ev_is_active (w)) |
779 | return; |
791 | return; |
780 | |
792 | |
781 | w->at += now; |
793 | w->at += now; |
… | |
… | |
787 | timers [timercnt - 1] = w; |
799 | timers [timercnt - 1] = w; |
788 | upheap ((WT *)timers, timercnt - 1); |
800 | upheap ((WT *)timers, timercnt - 1); |
789 | } |
801 | } |
790 | |
802 | |
791 | void |
803 | void |
792 | evtimer_stop (struct ev_timer *w) |
804 | ev_timer_stop (struct ev_timer *w) |
793 | { |
805 | { |
794 | ev_clear ((W)w); |
806 | ev_clear ((W)w); |
795 | if (!ev_is_active (w)) |
807 | if (!ev_is_active (w)) |
796 | return; |
808 | return; |
797 | |
809 | |
… | |
… | |
805 | |
817 | |
806 | ev_stop ((W)w); |
818 | ev_stop ((W)w); |
807 | } |
819 | } |
808 | |
820 | |
809 | void |
821 | void |
810 | evtimer_again (struct ev_timer *w) |
822 | ev_timer_again (struct ev_timer *w) |
811 | { |
823 | { |
812 | if (ev_is_active (w)) |
824 | if (ev_is_active (w)) |
813 | { |
825 | { |
814 | if (w->repeat) |
826 | if (w->repeat) |
815 | { |
827 | { |
816 | w->at = now + w->repeat; |
828 | w->at = now + w->repeat; |
817 | downheap ((WT *)timers, timercnt, w->active - 1); |
829 | downheap ((WT *)timers, timercnt, w->active - 1); |
818 | } |
830 | } |
819 | else |
831 | else |
820 | evtimer_stop (w); |
832 | ev_timer_stop (w); |
821 | } |
833 | } |
822 | else if (w->repeat) |
834 | else if (w->repeat) |
823 | evtimer_start (w); |
835 | ev_timer_start (w); |
824 | } |
836 | } |
825 | |
837 | |
826 | void |
838 | void |
827 | evperiodic_start (struct ev_periodic *w) |
839 | ev_periodic_start (struct ev_periodic *w) |
828 | { |
840 | { |
829 | if (ev_is_active (w)) |
841 | if (ev_is_active (w)) |
830 | return; |
842 | return; |
831 | |
843 | |
832 | assert (("periodic interval value less than zero not allowed", w->interval >= 0.)); |
844 | assert (("periodic interval value less than zero not allowed", w->interval >= 0.)); |
… | |
… | |
840 | periodics [periodiccnt - 1] = w; |
852 | periodics [periodiccnt - 1] = w; |
841 | upheap ((WT *)periodics, periodiccnt - 1); |
853 | upheap ((WT *)periodics, periodiccnt - 1); |
842 | } |
854 | } |
843 | |
855 | |
844 | void |
856 | void |
845 | evperiodic_stop (struct ev_periodic *w) |
857 | ev_periodic_stop (struct ev_periodic *w) |
846 | { |
858 | { |
847 | ev_clear ((W)w); |
859 | ev_clear ((W)w); |
848 | if (!ev_is_active (w)) |
860 | if (!ev_is_active (w)) |
849 | return; |
861 | return; |
850 | |
862 | |
… | |
… | |
856 | |
868 | |
857 | ev_stop ((W)w); |
869 | ev_stop ((W)w); |
858 | } |
870 | } |
859 | |
871 | |
860 | void |
872 | void |
861 | evsignal_start (struct ev_signal *w) |
873 | ev_signal_start (struct ev_signal *w) |
862 | { |
874 | { |
863 | if (ev_is_active (w)) |
875 | if (ev_is_active (w)) |
864 | return; |
876 | return; |
865 | |
877 | |
866 | ev_start ((W)w, 1); |
878 | ev_start ((W)w, 1); |
… | |
… | |
876 | sigaction (w->signum, &sa, 0); |
888 | sigaction (w->signum, &sa, 0); |
877 | } |
889 | } |
878 | } |
890 | } |
879 | |
891 | |
880 | void |
892 | void |
881 | evsignal_stop (struct ev_signal *w) |
893 | ev_signal_stop (struct ev_signal *w) |
882 | { |
894 | { |
883 | ev_clear ((W)w); |
895 | ev_clear ((W)w); |
884 | if (!ev_is_active (w)) |
896 | if (!ev_is_active (w)) |
885 | return; |
897 | return; |
886 | |
898 | |
… | |
… | |
889 | |
901 | |
890 | if (!signals [w->signum - 1].head) |
902 | if (!signals [w->signum - 1].head) |
891 | signal (w->signum, SIG_DFL); |
903 | signal (w->signum, SIG_DFL); |
892 | } |
904 | } |
893 | |
905 | |
|
|
906 | void |
894 | void evidle_start (struct ev_idle *w) |
907 | ev_idle_start (struct ev_idle *w) |
895 | { |
908 | { |
896 | if (ev_is_active (w)) |
909 | if (ev_is_active (w)) |
897 | return; |
910 | return; |
898 | |
911 | |
899 | ev_start ((W)w, ++idlecnt); |
912 | ev_start ((W)w, ++idlecnt); |
900 | array_needsize (idles, idlemax, idlecnt, ); |
913 | array_needsize (idles, idlemax, idlecnt, ); |
901 | idles [idlecnt - 1] = w; |
914 | idles [idlecnt - 1] = w; |
902 | } |
915 | } |
903 | |
916 | |
|
|
917 | void |
904 | void evidle_stop (struct ev_idle *w) |
918 | ev_idle_stop (struct ev_idle *w) |
905 | { |
919 | { |
906 | ev_clear ((W)w); |
920 | ev_clear ((W)w); |
907 | if (ev_is_active (w)) |
921 | if (ev_is_active (w)) |
908 | return; |
922 | return; |
909 | |
923 | |
910 | idles [w->active - 1] = idles [--idlecnt]; |
924 | idles [w->active - 1] = idles [--idlecnt]; |
911 | ev_stop ((W)w); |
925 | ev_stop ((W)w); |
912 | } |
926 | } |
913 | |
927 | |
|
|
928 | void |
914 | void evprepare_start (struct ev_prepare *w) |
929 | ev_prepare_start (struct ev_prepare *w) |
915 | { |
930 | { |
916 | if (ev_is_active (w)) |
931 | if (ev_is_active (w)) |
917 | return; |
932 | return; |
918 | |
933 | |
919 | ev_start ((W)w, ++preparecnt); |
934 | ev_start ((W)w, ++preparecnt); |
920 | array_needsize (prepares, preparemax, preparecnt, ); |
935 | array_needsize (prepares, preparemax, preparecnt, ); |
921 | prepares [preparecnt - 1] = w; |
936 | prepares [preparecnt - 1] = w; |
922 | } |
937 | } |
923 | |
938 | |
|
|
939 | void |
924 | void evprepare_stop (struct ev_prepare *w) |
940 | ev_prepare_stop (struct ev_prepare *w) |
925 | { |
941 | { |
926 | ev_clear ((W)w); |
942 | ev_clear ((W)w); |
927 | if (ev_is_active (w)) |
943 | if (ev_is_active (w)) |
928 | return; |
944 | return; |
929 | |
945 | |
930 | prepares [w->active - 1] = prepares [--preparecnt]; |
946 | prepares [w->active - 1] = prepares [--preparecnt]; |
931 | ev_stop ((W)w); |
947 | ev_stop ((W)w); |
932 | } |
948 | } |
933 | |
949 | |
|
|
950 | void |
934 | void evcheck_start (struct ev_check *w) |
951 | ev_check_start (struct ev_check *w) |
935 | { |
952 | { |
936 | if (ev_is_active (w)) |
953 | if (ev_is_active (w)) |
937 | return; |
954 | return; |
938 | |
955 | |
939 | ev_start ((W)w, ++checkcnt); |
956 | ev_start ((W)w, ++checkcnt); |
940 | array_needsize (checks, checkmax, checkcnt, ); |
957 | array_needsize (checks, checkmax, checkcnt, ); |
941 | checks [checkcnt - 1] = w; |
958 | checks [checkcnt - 1] = w; |
942 | } |
959 | } |
943 | |
960 | |
|
|
961 | void |
944 | void evcheck_stop (struct ev_check *w) |
962 | ev_check_stop (struct ev_check *w) |
945 | { |
963 | { |
946 | ev_clear ((W)w); |
964 | ev_clear ((W)w); |
947 | if (ev_is_active (w)) |
965 | if (ev_is_active (w)) |
948 | return; |
966 | return; |
949 | |
967 | |
950 | checks [w->active - 1] = checks [--checkcnt]; |
968 | checks [w->active - 1] = checks [--checkcnt]; |
951 | ev_stop ((W)w); |
969 | ev_stop ((W)w); |
952 | } |
970 | } |
953 | |
971 | |
|
|
972 | void |
954 | void evchild_start (struct ev_child *w) |
973 | ev_child_start (struct ev_child *w) |
955 | { |
974 | { |
956 | if (ev_is_active (w)) |
975 | if (ev_is_active (w)) |
957 | return; |
976 | return; |
958 | |
977 | |
959 | ev_start ((W)w, 1); |
978 | ev_start ((W)w, 1); |
960 | wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
979 | wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
961 | } |
980 | } |
962 | |
981 | |
|
|
982 | void |
963 | void evchild_stop (struct ev_child *w) |
983 | ev_child_stop (struct ev_child *w) |
964 | { |
984 | { |
965 | ev_clear ((W)w); |
985 | ev_clear ((W)w); |
966 | if (ev_is_active (w)) |
986 | if (ev_is_active (w)) |
967 | return; |
987 | return; |
968 | |
988 | |
… | |
… | |
984 | once_cb (struct ev_once *once, int revents) |
1004 | once_cb (struct ev_once *once, int revents) |
985 | { |
1005 | { |
986 | void (*cb)(int revents, void *arg) = once->cb; |
1006 | void (*cb)(int revents, void *arg) = once->cb; |
987 | void *arg = once->arg; |
1007 | void *arg = once->arg; |
988 | |
1008 | |
989 | evio_stop (&once->io); |
1009 | ev_io_stop (&once->io); |
990 | evtimer_stop (&once->to); |
1010 | ev_timer_stop (&once->to); |
991 | free (once); |
1011 | free (once); |
992 | |
1012 | |
993 | cb (revents, arg); |
1013 | cb (revents, arg); |
994 | } |
1014 | } |
995 | |
1015 | |
… | |
… | |
1009 | ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1029 | ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1010 | { |
1030 | { |
1011 | struct ev_once *once = malloc (sizeof (struct ev_once)); |
1031 | struct ev_once *once = malloc (sizeof (struct ev_once)); |
1012 | |
1032 | |
1013 | if (!once) |
1033 | if (!once) |
1014 | cb (EV_ERROR, arg); |
1034 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
1015 | else |
1035 | else |
1016 | { |
1036 | { |
1017 | once->cb = cb; |
1037 | once->cb = cb; |
1018 | once->arg = arg; |
1038 | once->arg = arg; |
1019 | |
1039 | |
1020 | evw_init (&once->io, once_cb_io); |
1040 | ev_watcher_init (&once->io, once_cb_io); |
1021 | |
|
|
1022 | if (fd >= 0) |
1041 | if (fd >= 0) |
1023 | { |
1042 | { |
1024 | evio_set (&once->io, fd, events); |
1043 | ev_io_set (&once->io, fd, events); |
1025 | evio_start (&once->io); |
1044 | ev_io_start (&once->io); |
1026 | } |
1045 | } |
1027 | |
1046 | |
1028 | evw_init (&once->to, once_cb_to); |
1047 | ev_watcher_init (&once->to, once_cb_to); |
1029 | |
|
|
1030 | if (timeout >= 0.) |
1048 | if (timeout >= 0.) |
1031 | { |
1049 | { |
1032 | evtimer_set (&once->to, timeout, 0.); |
1050 | ev_timer_set (&once->to, timeout, 0.); |
1033 | evtimer_start (&once->to); |
1051 | ev_timer_start (&once->to); |
1034 | } |
1052 | } |
1035 | } |
1053 | } |
1036 | } |
1054 | } |
1037 | |
1055 | |
1038 | /*****************************************************************************/ |
1056 | /*****************************************************************************/ |
… | |
… | |
1049 | |
1067 | |
1050 | static void |
1068 | static void |
1051 | ocb (struct ev_timer *w, int revents) |
1069 | ocb (struct ev_timer *w, int revents) |
1052 | { |
1070 | { |
1053 | //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data); |
1071 | //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); |
1072 | ev_timer_stop (w); |
1055 | evtimer_start (w); |
1073 | ev_timer_start (w); |
1056 | } |
1074 | } |
1057 | |
1075 | |
1058 | static void |
1076 | static void |
1059 | scb (struct ev_signal *w, int revents) |
1077 | scb (struct ev_signal *w, int revents) |
1060 | { |
1078 | { |
1061 | fprintf (stderr, "signal %x,%d\n", revents, w->signum); |
1079 | fprintf (stderr, "signal %x,%d\n", revents, w->signum); |
1062 | evio_stop (&wio); |
1080 | ev_io_stop (&wio); |
1063 | evio_start (&wio); |
1081 | ev_io_start (&wio); |
1064 | } |
1082 | } |
1065 | |
1083 | |
1066 | static void |
1084 | static void |
1067 | gcb (struct ev_signal *w, int revents) |
1085 | gcb (struct ev_signal *w, int revents) |
1068 | { |
1086 | { |
… | |
… | |
1072 | |
1090 | |
1073 | int main (void) |
1091 | int main (void) |
1074 | { |
1092 | { |
1075 | ev_init (0); |
1093 | ev_init (0); |
1076 | |
1094 | |
1077 | evio_init (&wio, sin_cb, 0, EV_READ); |
1095 | ev_io_init (&wio, sin_cb, 0, EV_READ); |
1078 | evio_start (&wio); |
1096 | ev_io_start (&wio); |
1079 | |
1097 | |
1080 | struct ev_timer t[10000]; |
1098 | struct ev_timer t[10000]; |
1081 | |
1099 | |
1082 | #if 0 |
1100 | #if 0 |
1083 | int i; |
1101 | int i; |
1084 | for (i = 0; i < 10000; ++i) |
1102 | for (i = 0; i < 10000; ++i) |
1085 | { |
1103 | { |
1086 | struct ev_timer *w = t + i; |
1104 | struct ev_timer *w = t + i; |
1087 | evw_init (w, ocb, i); |
1105 | ev_watcher_init (w, ocb, i); |
1088 | evtimer_init_abs (w, ocb, drand48 (), 0.99775533); |
1106 | ev_timer_init_abs (w, ocb, drand48 (), 0.99775533); |
1089 | evtimer_start (w); |
1107 | ev_timer_start (w); |
1090 | if (drand48 () < 0.5) |
1108 | if (drand48 () < 0.5) |
1091 | evtimer_stop (w); |
1109 | ev_timer_stop (w); |
1092 | } |
1110 | } |
1093 | #endif |
1111 | #endif |
1094 | |
1112 | |
1095 | struct ev_timer t1; |
1113 | struct ev_timer t1; |
1096 | evtimer_init (&t1, ocb, 5, 10); |
1114 | ev_timer_init (&t1, ocb, 5, 10); |
1097 | evtimer_start (&t1); |
1115 | ev_timer_start (&t1); |
1098 | |
1116 | |
1099 | struct ev_signal sig; |
1117 | struct ev_signal sig; |
1100 | evsignal_init (&sig, scb, SIGQUIT); |
1118 | ev_signal_init (&sig, scb, SIGQUIT); |
1101 | evsignal_start (&sig); |
1119 | ev_signal_start (&sig); |
1102 | |
1120 | |
1103 | struct ev_check cw; |
1121 | struct ev_check cw; |
1104 | evcheck_init (&cw, gcb); |
1122 | ev_check_init (&cw, gcb); |
1105 | evcheck_start (&cw); |
1123 | ev_check_start (&cw); |
1106 | |
1124 | |
1107 | struct ev_idle iw; |
1125 | struct ev_idle iw; |
1108 | evidle_init (&iw, gcb); |
1126 | ev_idle_init (&iw, gcb); |
1109 | evidle_start (&iw); |
1127 | ev_idle_start (&iw); |
1110 | |
1128 | |
1111 | ev_loop (0); |
1129 | ev_loop (0); |
1112 | |
1130 | |
1113 | return 0; |
1131 | return 0; |
1114 | } |
1132 | } |