… | |
… | |
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); \ |
… | |
… | |
130 | /*****************************************************************************/ |
139 | /*****************************************************************************/ |
131 | |
140 | |
132 | typedef struct |
141 | typedef struct |
133 | { |
142 | { |
134 | struct ev_io *head; |
143 | struct ev_io *head; |
135 | int events; |
144 | unsigned char events; |
|
|
145 | unsigned char reify; |
136 | } ANFD; |
146 | } ANFD; |
137 | |
147 | |
138 | static ANFD *anfds; |
148 | static ANFD *anfds; |
139 | static int anfdmax; |
149 | static int anfdmax; |
140 | |
150 | |
… | |
… | |
143 | { |
153 | { |
144 | while (count--) |
154 | while (count--) |
145 | { |
155 | { |
146 | base->head = 0; |
156 | base->head = 0; |
147 | base->events = EV_NONE; |
157 | base->events = EV_NONE; |
|
|
158 | base->reify = 0; |
|
|
159 | |
148 | ++base; |
160 | ++base; |
149 | } |
161 | } |
150 | } |
162 | } |
151 | |
163 | |
152 | typedef struct |
164 | typedef struct |
… | |
… | |
159 | static int pendingmax, pendingcnt; |
171 | static int pendingmax, pendingcnt; |
160 | |
172 | |
161 | static void |
173 | static void |
162 | event (W w, int events) |
174 | event (W w, int events) |
163 | { |
175 | { |
164 | if (w->active) |
176 | if (w->pending) |
|
|
177 | { |
|
|
178 | pendings [w->pending - 1].events |= events; |
|
|
179 | return; |
165 | { |
180 | } |
|
|
181 | |
166 | w->pending = ++pendingcnt; |
182 | w->pending = ++pendingcnt; |
167 | array_needsize (pendings, pendingmax, pendingcnt, ); |
183 | array_needsize (pendings, pendingmax, pendingcnt, ); |
168 | pendings [pendingcnt - 1].w = w; |
184 | pendings [pendingcnt - 1].w = w; |
169 | pendings [pendingcnt - 1].events = events; |
185 | pendings [pendingcnt - 1].events = events; |
170 | } |
|
|
171 | } |
186 | } |
172 | |
187 | |
173 | static void |
188 | static void |
174 | queue_events (W *events, int eventcnt, int type) |
189 | queue_events (W *events, int eventcnt, int type) |
175 | { |
190 | { |
… | |
… | |
213 | int events = 0; |
228 | int events = 0; |
214 | |
229 | |
215 | for (w = anfd->head; w; w = w->next) |
230 | for (w = anfd->head; w; w = w->next) |
216 | events |= w->events; |
231 | events |= w->events; |
217 | |
232 | |
218 | anfd->events &= ~EV_REIFY; |
233 | anfd->reify = 0; |
219 | |
234 | |
220 | if (anfd->events != events) |
235 | if (anfd->events != events) |
221 | { |
236 | { |
222 | method_modify (fd, anfd->events, events); |
237 | method_modify (fd, anfd->events, events); |
223 | anfd->events = events; |
238 | anfd->events = events; |
… | |
… | |
228 | } |
243 | } |
229 | |
244 | |
230 | static void |
245 | static void |
231 | fd_change (int fd) |
246 | fd_change (int fd) |
232 | { |
247 | { |
233 | if (anfds [fd].events & EV_REIFY) |
248 | if (anfds [fd].reify || fdchangecnt < 0) |
234 | return; |
249 | return; |
235 | |
250 | |
236 | anfds [fd].events |= EV_REIFY; |
251 | anfds [fd].reify = 1; |
237 | |
252 | |
238 | ++fdchangecnt; |
253 | ++fdchangecnt; |
239 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
254 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
240 | fdchanges [fdchangecnt - 1] = fd; |
255 | fdchanges [fdchangecnt - 1] = fd; |
241 | } |
256 | } |
… | |
… | |
249 | for (fd = 0; fd < anfdmax; ++fd) |
264 | for (fd = 0; fd < anfdmax; ++fd) |
250 | if (anfds [fd].events) |
265 | if (anfds [fd].events) |
251 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
266 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
252 | while (anfds [fd].head) |
267 | while (anfds [fd].head) |
253 | { |
268 | { |
254 | event ((W)anfds [fd].head, EV_ERROR); |
|
|
255 | evio_stop (anfds [fd].head); |
269 | ev_io_stop (anfds [fd].head); |
|
|
270 | event ((W)anfds [fd].head, EV_ERROR | EV_READ | EV_WRITE); |
256 | } |
271 | } |
257 | } |
272 | } |
258 | |
273 | |
259 | /*****************************************************************************/ |
274 | /*****************************************************************************/ |
260 | |
275 | |
… | |
… | |
325 | { |
340 | { |
326 | while (count--) |
341 | while (count--) |
327 | { |
342 | { |
328 | base->head = 0; |
343 | base->head = 0; |
329 | base->gotsig = 0; |
344 | base->gotsig = 0; |
|
|
345 | |
330 | ++base; |
346 | ++base; |
331 | } |
347 | } |
332 | } |
348 | } |
333 | |
349 | |
334 | static void |
350 | static void |
… | |
… | |
370 | |
386 | |
371 | /* rather than sort out wether we really need nb, set it */ |
387 | /* rather than sort out wether we really need nb, set it */ |
372 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
388 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
373 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
389 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
374 | |
390 | |
375 | evio_set (&sigev, sigpipe [0], EV_READ); |
391 | ev_io_set (&sigev, sigpipe [0], EV_READ); |
376 | evio_start (&sigev); |
392 | ev_io_start (&sigev); |
377 | } |
393 | } |
378 | |
394 | |
379 | /*****************************************************************************/ |
395 | /*****************************************************************************/ |
380 | |
396 | |
381 | static struct ev_idle **idles; |
397 | static struct ev_idle **idles; |
… | |
… | |
411 | } |
427 | } |
412 | } |
428 | } |
413 | |
429 | |
414 | /*****************************************************************************/ |
430 | /*****************************************************************************/ |
415 | |
431 | |
416 | #if HAVE_EPOLL |
432 | #if EV_USE_EPOLL |
417 | # include "ev_epoll.c" |
433 | # include "ev_epoll.c" |
418 | #endif |
434 | #endif |
419 | #if HAVE_SELECT |
435 | #if EV_USE_SELECT |
420 | # include "ev_select.c" |
436 | # include "ev_select.c" |
421 | #endif |
437 | #endif |
422 | |
438 | |
423 | int |
439 | int |
424 | ev_version_major (void) |
440 | ev_version_major (void) |
… | |
… | |
434 | |
450 | |
435 | int ev_init (int flags) |
451 | int ev_init (int flags) |
436 | { |
452 | { |
437 | if (!ev_method) |
453 | if (!ev_method) |
438 | { |
454 | { |
439 | #if HAVE_MONOTONIC |
455 | #if EV_USE_MONOTONIC |
440 | { |
456 | { |
441 | struct timespec ts; |
457 | struct timespec ts; |
442 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
458 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
443 | have_monotonic = 1; |
459 | have_monotonic = 1; |
444 | } |
460 | } |
… | |
… | |
450 | |
466 | |
451 | if (pipe (sigpipe)) |
467 | if (pipe (sigpipe)) |
452 | return 0; |
468 | return 0; |
453 | |
469 | |
454 | ev_method = EVMETHOD_NONE; |
470 | ev_method = EVMETHOD_NONE; |
455 | #if HAVE_EPOLL |
471 | #if EV_USE_EPOLL |
456 | if (ev_method == EVMETHOD_NONE) epoll_init (flags); |
472 | if (ev_method == EVMETHOD_NONE) epoll_init (flags); |
457 | #endif |
473 | #endif |
458 | #if HAVE_SELECT |
474 | #if EV_USE_SELECT |
459 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
475 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
460 | #endif |
476 | #endif |
461 | |
477 | |
462 | if (ev_method) |
478 | if (ev_method) |
463 | { |
479 | { |
464 | evw_init (&sigev, sigcb); |
480 | ev_watcher_init (&sigev, sigcb); |
465 | siginit (); |
481 | siginit (); |
466 | |
482 | |
467 | evsignal_init (&childev, childcb, SIGCHLD); |
483 | ev_signal_init (&childev, childcb, SIGCHLD); |
468 | evsignal_start (&childev); |
484 | ev_signal_start (&childev); |
469 | } |
485 | } |
470 | } |
486 | } |
471 | |
487 | |
472 | return ev_method; |
488 | return ev_method; |
473 | } |
489 | } |
… | |
… | |
487 | } |
503 | } |
488 | |
504 | |
489 | void |
505 | void |
490 | ev_postfork_child (void) |
506 | ev_postfork_child (void) |
491 | { |
507 | { |
492 | #if HAVE_EPOLL |
508 | #if EV_USE_EPOLL |
493 | if (ev_method == EVMETHOD_EPOLL) |
509 | if (ev_method == EVMETHOD_EPOLL) |
494 | epoll_postfork_child (); |
510 | epoll_postfork_child (); |
495 | #endif |
511 | #endif |
496 | |
512 | |
497 | evio_stop (&sigev); |
513 | ev_io_stop (&sigev); |
498 | close (sigpipe [0]); |
514 | close (sigpipe [0]); |
499 | close (sigpipe [1]); |
515 | close (sigpipe [1]); |
500 | pipe (sigpipe); |
516 | pipe (sigpipe); |
501 | siginit (); |
517 | siginit (); |
502 | } |
518 | } |
… | |
… | |
523 | { |
539 | { |
524 | while (timercnt && timers [0]->at <= now) |
540 | while (timercnt && timers [0]->at <= now) |
525 | { |
541 | { |
526 | struct ev_timer *w = timers [0]; |
542 | struct ev_timer *w = timers [0]; |
527 | |
543 | |
528 | event ((W)w, EV_TIMEOUT); |
|
|
529 | |
|
|
530 | /* first reschedule or stop timer */ |
544 | /* first reschedule or stop timer */ |
531 | if (w->repeat) |
545 | if (w->repeat) |
532 | { |
546 | { |
|
|
547 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
533 | w->at = now + w->repeat; |
548 | w->at = now + w->repeat; |
534 | assert (("timer timeout in the past, negative repeat?", w->at > now)); |
|
|
535 | downheap ((WT *)timers, timercnt, 0); |
549 | downheap ((WT *)timers, timercnt, 0); |
536 | } |
550 | } |
537 | else |
551 | else |
538 | evtimer_stop (w); /* nonrepeating: stop timer */ |
552 | ev_timer_stop (w); /* nonrepeating: stop timer */ |
|
|
553 | |
|
|
554 | event ((W)w, EV_TIMEOUT); |
539 | } |
555 | } |
540 | } |
556 | } |
541 | |
557 | |
542 | static void |
558 | static void |
543 | periodics_reify (void) |
559 | periodics_reify (void) |
… | |
… | |
548 | |
564 | |
549 | /* first reschedule or stop timer */ |
565 | /* first reschedule or stop timer */ |
550 | if (w->interval) |
566 | if (w->interval) |
551 | { |
567 | { |
552 | w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; |
568 | 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)); |
569 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > ev_now)); |
554 | downheap ((WT *)periodics, periodiccnt, 0); |
570 | downheap ((WT *)periodics, periodiccnt, 0); |
555 | } |
571 | } |
556 | else |
572 | else |
557 | evperiodic_stop (w); /* nonrepeating: stop timer */ |
573 | ev_periodic_stop (w); /* nonrepeating: stop timer */ |
558 | |
574 | |
559 | event ((W)w, EV_TIMEOUT); |
575 | event ((W)w, EV_PERIODIC); |
560 | } |
576 | } |
561 | } |
577 | } |
562 | |
578 | |
563 | static void |
579 | static void |
564 | periodics_reschedule (ev_tstamp diff) |
580 | periodics_reschedule (ev_tstamp diff) |
… | |
… | |
574 | { |
590 | { |
575 | ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval; |
591 | ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval; |
576 | |
592 | |
577 | if (fabs (diff) >= 1e-4) |
593 | if (fabs (diff) >= 1e-4) |
578 | { |
594 | { |
579 | evperiodic_stop (w); |
595 | ev_periodic_stop (w); |
580 | evperiodic_start (w); |
596 | ev_periodic_start (w); |
581 | |
597 | |
582 | i = 0; /* restart loop, inefficient, but time jumps should be rare */ |
598 | i = 0; /* restart loop, inefficient, but time jumps should be rare */ |
583 | } |
599 | } |
584 | } |
600 | } |
585 | } |
601 | } |
… | |
… | |
719 | head = &(*head)->next; |
735 | head = &(*head)->next; |
720 | } |
736 | } |
721 | } |
737 | } |
722 | |
738 | |
723 | static void |
739 | static void |
724 | ev_clear (W w) |
740 | ev_clear_pending (W w) |
725 | { |
741 | { |
726 | if (w->pending) |
742 | if (w->pending) |
727 | { |
743 | { |
728 | pendings [w->pending - 1].w = 0; |
744 | pendings [w->pending - 1].w = 0; |
729 | w->pending = 0; |
745 | w->pending = 0; |
… | |
… | |
743 | } |
759 | } |
744 | |
760 | |
745 | /*****************************************************************************/ |
761 | /*****************************************************************************/ |
746 | |
762 | |
747 | void |
763 | void |
748 | evio_start (struct ev_io *w) |
764 | ev_io_start (struct ev_io *w) |
749 | { |
765 | { |
750 | if (ev_is_active (w)) |
766 | if (ev_is_active (w)) |
751 | return; |
767 | return; |
752 | |
768 | |
753 | int fd = w->fd; |
769 | int fd = w->fd; |
|
|
770 | |
|
|
771 | assert (("ev_io_start called with negative fd", fd >= 0)); |
754 | |
772 | |
755 | ev_start ((W)w, 1); |
773 | ev_start ((W)w, 1); |
756 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
774 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
757 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
775 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
758 | |
776 | |
759 | fd_change (fd); |
777 | fd_change (fd); |
760 | } |
778 | } |
761 | |
779 | |
762 | void |
780 | void |
763 | evio_stop (struct ev_io *w) |
781 | ev_io_stop (struct ev_io *w) |
764 | { |
782 | { |
765 | ev_clear ((W)w); |
783 | ev_clear_pending ((W)w); |
766 | if (!ev_is_active (w)) |
784 | if (!ev_is_active (w)) |
767 | return; |
785 | return; |
768 | |
786 | |
769 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
787 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
770 | ev_stop ((W)w); |
788 | ev_stop ((W)w); |
771 | |
789 | |
772 | fd_change (w->fd); |
790 | fd_change (w->fd); |
773 | } |
791 | } |
774 | |
792 | |
775 | void |
793 | void |
776 | evtimer_start (struct ev_timer *w) |
794 | ev_timer_start (struct ev_timer *w) |
777 | { |
795 | { |
778 | if (ev_is_active (w)) |
796 | if (ev_is_active (w)) |
779 | return; |
797 | return; |
780 | |
798 | |
781 | w->at += now; |
799 | w->at += now; |
782 | |
800 | |
783 | assert (("timer repeat value less than zero not allowed", w->repeat >= 0.)); |
801 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
784 | |
802 | |
785 | ev_start ((W)w, ++timercnt); |
803 | ev_start ((W)w, ++timercnt); |
786 | array_needsize (timers, timermax, timercnt, ); |
804 | array_needsize (timers, timermax, timercnt, ); |
787 | timers [timercnt - 1] = w; |
805 | timers [timercnt - 1] = w; |
788 | upheap ((WT *)timers, timercnt - 1); |
806 | upheap ((WT *)timers, timercnt - 1); |
789 | } |
807 | } |
790 | |
808 | |
791 | void |
809 | void |
792 | evtimer_stop (struct ev_timer *w) |
810 | ev_timer_stop (struct ev_timer *w) |
793 | { |
811 | { |
794 | ev_clear ((W)w); |
812 | ev_clear_pending ((W)w); |
795 | if (!ev_is_active (w)) |
813 | if (!ev_is_active (w)) |
796 | return; |
814 | return; |
797 | |
815 | |
798 | if (w->active < timercnt--) |
816 | if (w->active < timercnt--) |
799 | { |
817 | { |
… | |
… | |
805 | |
823 | |
806 | ev_stop ((W)w); |
824 | ev_stop ((W)w); |
807 | } |
825 | } |
808 | |
826 | |
809 | void |
827 | void |
810 | evtimer_again (struct ev_timer *w) |
828 | ev_timer_again (struct ev_timer *w) |
811 | { |
829 | { |
812 | if (ev_is_active (w)) |
830 | if (ev_is_active (w)) |
813 | { |
831 | { |
814 | if (w->repeat) |
832 | if (w->repeat) |
815 | { |
833 | { |
816 | w->at = now + w->repeat; |
834 | w->at = now + w->repeat; |
817 | downheap ((WT *)timers, timercnt, w->active - 1); |
835 | downheap ((WT *)timers, timercnt, w->active - 1); |
818 | } |
836 | } |
819 | else |
837 | else |
820 | evtimer_stop (w); |
838 | ev_timer_stop (w); |
821 | } |
839 | } |
822 | else if (w->repeat) |
840 | else if (w->repeat) |
823 | evtimer_start (w); |
841 | ev_timer_start (w); |
824 | } |
842 | } |
825 | |
843 | |
826 | void |
844 | void |
827 | evperiodic_start (struct ev_periodic *w) |
845 | ev_periodic_start (struct ev_periodic *w) |
828 | { |
846 | { |
829 | if (ev_is_active (w)) |
847 | if (ev_is_active (w)) |
830 | return; |
848 | return; |
831 | |
849 | |
832 | assert (("periodic interval value less than zero not allowed", w->interval >= 0.)); |
850 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
833 | |
851 | |
834 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
852 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
835 | if (w->interval) |
853 | if (w->interval) |
836 | w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; |
854 | w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; |
837 | |
855 | |
… | |
… | |
840 | periodics [periodiccnt - 1] = w; |
858 | periodics [periodiccnt - 1] = w; |
841 | upheap ((WT *)periodics, periodiccnt - 1); |
859 | upheap ((WT *)periodics, periodiccnt - 1); |
842 | } |
860 | } |
843 | |
861 | |
844 | void |
862 | void |
845 | evperiodic_stop (struct ev_periodic *w) |
863 | ev_periodic_stop (struct ev_periodic *w) |
846 | { |
864 | { |
847 | ev_clear ((W)w); |
865 | ev_clear_pending ((W)w); |
848 | if (!ev_is_active (w)) |
866 | if (!ev_is_active (w)) |
849 | return; |
867 | return; |
850 | |
868 | |
851 | if (w->active < periodiccnt--) |
869 | if (w->active < periodiccnt--) |
852 | { |
870 | { |
… | |
… | |
856 | |
874 | |
857 | ev_stop ((W)w); |
875 | ev_stop ((W)w); |
858 | } |
876 | } |
859 | |
877 | |
860 | void |
878 | void |
861 | evsignal_start (struct ev_signal *w) |
879 | ev_signal_start (struct ev_signal *w) |
862 | { |
880 | { |
863 | if (ev_is_active (w)) |
881 | if (ev_is_active (w)) |
864 | return; |
882 | return; |
|
|
883 | |
|
|
884 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
865 | |
885 | |
866 | ev_start ((W)w, 1); |
886 | ev_start ((W)w, 1); |
867 | array_needsize (signals, signalmax, w->signum, signals_init); |
887 | array_needsize (signals, signalmax, w->signum, signals_init); |
868 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
888 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
869 | |
889 | |
… | |
… | |
876 | sigaction (w->signum, &sa, 0); |
896 | sigaction (w->signum, &sa, 0); |
877 | } |
897 | } |
878 | } |
898 | } |
879 | |
899 | |
880 | void |
900 | void |
881 | evsignal_stop (struct ev_signal *w) |
901 | ev_signal_stop (struct ev_signal *w) |
882 | { |
902 | { |
883 | ev_clear ((W)w); |
903 | ev_clear_pending ((W)w); |
884 | if (!ev_is_active (w)) |
904 | if (!ev_is_active (w)) |
885 | return; |
905 | return; |
886 | |
906 | |
887 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
907 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
888 | ev_stop ((W)w); |
908 | ev_stop ((W)w); |
889 | |
909 | |
890 | if (!signals [w->signum - 1].head) |
910 | if (!signals [w->signum - 1].head) |
891 | signal (w->signum, SIG_DFL); |
911 | signal (w->signum, SIG_DFL); |
892 | } |
912 | } |
893 | |
913 | |
|
|
914 | void |
894 | void evidle_start (struct ev_idle *w) |
915 | ev_idle_start (struct ev_idle *w) |
895 | { |
916 | { |
896 | if (ev_is_active (w)) |
917 | if (ev_is_active (w)) |
897 | return; |
918 | return; |
898 | |
919 | |
899 | ev_start ((W)w, ++idlecnt); |
920 | ev_start ((W)w, ++idlecnt); |
900 | array_needsize (idles, idlemax, idlecnt, ); |
921 | array_needsize (idles, idlemax, idlecnt, ); |
901 | idles [idlecnt - 1] = w; |
922 | idles [idlecnt - 1] = w; |
902 | } |
923 | } |
903 | |
924 | |
|
|
925 | void |
904 | void evidle_stop (struct ev_idle *w) |
926 | ev_idle_stop (struct ev_idle *w) |
905 | { |
927 | { |
906 | ev_clear ((W)w); |
928 | ev_clear_pending ((W)w); |
907 | if (ev_is_active (w)) |
929 | if (ev_is_active (w)) |
908 | return; |
930 | return; |
909 | |
931 | |
910 | idles [w->active - 1] = idles [--idlecnt]; |
932 | idles [w->active - 1] = idles [--idlecnt]; |
911 | ev_stop ((W)w); |
933 | ev_stop ((W)w); |
912 | } |
934 | } |
913 | |
935 | |
|
|
936 | void |
914 | void evprepare_start (struct ev_prepare *w) |
937 | ev_prepare_start (struct ev_prepare *w) |
915 | { |
938 | { |
916 | if (ev_is_active (w)) |
939 | if (ev_is_active (w)) |
917 | return; |
940 | return; |
918 | |
941 | |
919 | ev_start ((W)w, ++preparecnt); |
942 | ev_start ((W)w, ++preparecnt); |
920 | array_needsize (prepares, preparemax, preparecnt, ); |
943 | array_needsize (prepares, preparemax, preparecnt, ); |
921 | prepares [preparecnt - 1] = w; |
944 | prepares [preparecnt - 1] = w; |
922 | } |
945 | } |
923 | |
946 | |
|
|
947 | void |
924 | void evprepare_stop (struct ev_prepare *w) |
948 | ev_prepare_stop (struct ev_prepare *w) |
925 | { |
949 | { |
926 | ev_clear ((W)w); |
950 | ev_clear_pending ((W)w); |
927 | if (ev_is_active (w)) |
951 | if (ev_is_active (w)) |
928 | return; |
952 | return; |
929 | |
953 | |
930 | prepares [w->active - 1] = prepares [--preparecnt]; |
954 | prepares [w->active - 1] = prepares [--preparecnt]; |
931 | ev_stop ((W)w); |
955 | ev_stop ((W)w); |
932 | } |
956 | } |
933 | |
957 | |
|
|
958 | void |
934 | void evcheck_start (struct ev_check *w) |
959 | ev_check_start (struct ev_check *w) |
935 | { |
960 | { |
936 | if (ev_is_active (w)) |
961 | if (ev_is_active (w)) |
937 | return; |
962 | return; |
938 | |
963 | |
939 | ev_start ((W)w, ++checkcnt); |
964 | ev_start ((W)w, ++checkcnt); |
940 | array_needsize (checks, checkmax, checkcnt, ); |
965 | array_needsize (checks, checkmax, checkcnt, ); |
941 | checks [checkcnt - 1] = w; |
966 | checks [checkcnt - 1] = w; |
942 | } |
967 | } |
943 | |
968 | |
|
|
969 | void |
944 | void evcheck_stop (struct ev_check *w) |
970 | ev_check_stop (struct ev_check *w) |
945 | { |
971 | { |
946 | ev_clear ((W)w); |
972 | ev_clear_pending ((W)w); |
947 | if (ev_is_active (w)) |
973 | if (ev_is_active (w)) |
948 | return; |
974 | return; |
949 | |
975 | |
950 | checks [w->active - 1] = checks [--checkcnt]; |
976 | checks [w->active - 1] = checks [--checkcnt]; |
951 | ev_stop ((W)w); |
977 | ev_stop ((W)w); |
952 | } |
978 | } |
953 | |
979 | |
|
|
980 | void |
954 | void evchild_start (struct ev_child *w) |
981 | ev_child_start (struct ev_child *w) |
955 | { |
982 | { |
956 | if (ev_is_active (w)) |
983 | if (ev_is_active (w)) |
957 | return; |
984 | return; |
958 | |
985 | |
959 | ev_start ((W)w, 1); |
986 | ev_start ((W)w, 1); |
960 | wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
987 | wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
961 | } |
988 | } |
962 | |
989 | |
|
|
990 | void |
963 | void evchild_stop (struct ev_child *w) |
991 | ev_child_stop (struct ev_child *w) |
964 | { |
992 | { |
965 | ev_clear ((W)w); |
993 | ev_clear_pending ((W)w); |
966 | if (ev_is_active (w)) |
994 | if (ev_is_active (w)) |
967 | return; |
995 | return; |
968 | |
996 | |
969 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
997 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
970 | ev_stop ((W)w); |
998 | ev_stop ((W)w); |
… | |
… | |
984 | once_cb (struct ev_once *once, int revents) |
1012 | once_cb (struct ev_once *once, int revents) |
985 | { |
1013 | { |
986 | void (*cb)(int revents, void *arg) = once->cb; |
1014 | void (*cb)(int revents, void *arg) = once->cb; |
987 | void *arg = once->arg; |
1015 | void *arg = once->arg; |
988 | |
1016 | |
989 | evio_stop (&once->io); |
1017 | ev_io_stop (&once->io); |
990 | evtimer_stop (&once->to); |
1018 | ev_timer_stop (&once->to); |
991 | free (once); |
1019 | free (once); |
992 | |
1020 | |
993 | cb (revents, arg); |
1021 | cb (revents, arg); |
994 | } |
1022 | } |
995 | |
1023 | |
… | |
… | |
1009 | ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1037 | ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1010 | { |
1038 | { |
1011 | struct ev_once *once = malloc (sizeof (struct ev_once)); |
1039 | struct ev_once *once = malloc (sizeof (struct ev_once)); |
1012 | |
1040 | |
1013 | if (!once) |
1041 | if (!once) |
1014 | cb (EV_ERROR, arg); |
1042 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
1015 | else |
1043 | else |
1016 | { |
1044 | { |
1017 | once->cb = cb; |
1045 | once->cb = cb; |
1018 | once->arg = arg; |
1046 | once->arg = arg; |
1019 | |
1047 | |
1020 | evw_init (&once->io, once_cb_io); |
1048 | ev_watcher_init (&once->io, once_cb_io); |
1021 | |
|
|
1022 | if (fd >= 0) |
1049 | if (fd >= 0) |
1023 | { |
1050 | { |
1024 | evio_set (&once->io, fd, events); |
1051 | ev_io_set (&once->io, fd, events); |
1025 | evio_start (&once->io); |
1052 | ev_io_start (&once->io); |
1026 | } |
1053 | } |
1027 | |
1054 | |
1028 | evw_init (&once->to, once_cb_to); |
1055 | ev_watcher_init (&once->to, once_cb_to); |
1029 | |
|
|
1030 | if (timeout >= 0.) |
1056 | if (timeout >= 0.) |
1031 | { |
1057 | { |
1032 | evtimer_set (&once->to, timeout, 0.); |
1058 | ev_timer_set (&once->to, timeout, 0.); |
1033 | evtimer_start (&once->to); |
1059 | ev_timer_start (&once->to); |
1034 | } |
1060 | } |
1035 | } |
1061 | } |
1036 | } |
1062 | } |
1037 | |
1063 | |
1038 | /*****************************************************************************/ |
1064 | /*****************************************************************************/ |
… | |
… | |
1049 | |
1075 | |
1050 | static void |
1076 | static void |
1051 | ocb (struct ev_timer *w, int revents) |
1077 | ocb (struct ev_timer *w, int revents) |
1052 | { |
1078 | { |
1053 | //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data); |
1079 | //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); |
1080 | ev_timer_stop (w); |
1055 | evtimer_start (w); |
1081 | ev_timer_start (w); |
1056 | } |
1082 | } |
1057 | |
1083 | |
1058 | static void |
1084 | static void |
1059 | scb (struct ev_signal *w, int revents) |
1085 | scb (struct ev_signal *w, int revents) |
1060 | { |
1086 | { |
1061 | fprintf (stderr, "signal %x,%d\n", revents, w->signum); |
1087 | fprintf (stderr, "signal %x,%d\n", revents, w->signum); |
1062 | evio_stop (&wio); |
1088 | ev_io_stop (&wio); |
1063 | evio_start (&wio); |
1089 | ev_io_start (&wio); |
1064 | } |
1090 | } |
1065 | |
1091 | |
1066 | static void |
1092 | static void |
1067 | gcb (struct ev_signal *w, int revents) |
1093 | gcb (struct ev_signal *w, int revents) |
1068 | { |
1094 | { |
… | |
… | |
1072 | |
1098 | |
1073 | int main (void) |
1099 | int main (void) |
1074 | { |
1100 | { |
1075 | ev_init (0); |
1101 | ev_init (0); |
1076 | |
1102 | |
1077 | evio_init (&wio, sin_cb, 0, EV_READ); |
1103 | ev_io_init (&wio, sin_cb, 0, EV_READ); |
1078 | evio_start (&wio); |
1104 | ev_io_start (&wio); |
1079 | |
1105 | |
1080 | struct ev_timer t[10000]; |
1106 | struct ev_timer t[10000]; |
1081 | |
1107 | |
1082 | #if 0 |
1108 | #if 0 |
1083 | int i; |
1109 | int i; |
1084 | for (i = 0; i < 10000; ++i) |
1110 | for (i = 0; i < 10000; ++i) |
1085 | { |
1111 | { |
1086 | struct ev_timer *w = t + i; |
1112 | struct ev_timer *w = t + i; |
1087 | evw_init (w, ocb, i); |
1113 | ev_watcher_init (w, ocb, i); |
1088 | evtimer_init_abs (w, ocb, drand48 (), 0.99775533); |
1114 | ev_timer_init_abs (w, ocb, drand48 (), 0.99775533); |
1089 | evtimer_start (w); |
1115 | ev_timer_start (w); |
1090 | if (drand48 () < 0.5) |
1116 | if (drand48 () < 0.5) |
1091 | evtimer_stop (w); |
1117 | ev_timer_stop (w); |
1092 | } |
1118 | } |
1093 | #endif |
1119 | #endif |
1094 | |
1120 | |
1095 | struct ev_timer t1; |
1121 | struct ev_timer t1; |
1096 | evtimer_init (&t1, ocb, 5, 10); |
1122 | ev_timer_init (&t1, ocb, 5, 10); |
1097 | evtimer_start (&t1); |
1123 | ev_timer_start (&t1); |
1098 | |
1124 | |
1099 | struct ev_signal sig; |
1125 | struct ev_signal sig; |
1100 | evsignal_init (&sig, scb, SIGQUIT); |
1126 | ev_signal_init (&sig, scb, SIGQUIT); |
1101 | evsignal_start (&sig); |
1127 | ev_signal_start (&sig); |
1102 | |
1128 | |
1103 | struct ev_check cw; |
1129 | struct ev_check cw; |
1104 | evcheck_init (&cw, gcb); |
1130 | ev_check_init (&cw, gcb); |
1105 | evcheck_start (&cw); |
1131 | ev_check_start (&cw); |
1106 | |
1132 | |
1107 | struct ev_idle iw; |
1133 | struct ev_idle iw; |
1108 | evidle_init (&iw, gcb); |
1134 | ev_idle_init (&iw, gcb); |
1109 | evidle_start (&iw); |
1135 | ev_idle_start (&iw); |
1110 | |
1136 | |
1111 | ev_loop (0); |
1137 | ev_loop (0); |
1112 | |
1138 | |
1113 | return 0; |
1139 | return 0; |
1114 | } |
1140 | } |