… | |
… | |
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_nextsize(n) (((n) << 1) | 4 & ~3) |
|
|
119 | #define array_prevsize(n) (((n) >> 1) | 4 & ~3) |
114 | |
120 | |
115 | #define array_needsize(base,cur,cnt,init) \ |
121 | #define array_needsize(base,cur,cnt,init) \ |
116 | if ((cnt) > cur) \ |
122 | if ((cnt) > cur) \ |
117 | { \ |
123 | { \ |
118 | int newcnt = cur; \ |
124 | int newcnt = cur; \ |
119 | do \ |
125 | do \ |
120 | { \ |
126 | { \ |
121 | newcnt = (newcnt << 1) | 4 & ~3; \ |
127 | newcnt = array_nextsize (newcnt); \ |
122 | } \ |
128 | } \ |
123 | while ((cnt) > newcnt); \ |
129 | while ((cnt) > newcnt); \ |
124 | \ |
130 | \ |
125 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
131 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
126 | init (base + cur, newcnt - cur); \ |
132 | init (base + cur, newcnt - cur); \ |
… | |
… | |
130 | /*****************************************************************************/ |
136 | /*****************************************************************************/ |
131 | |
137 | |
132 | typedef struct |
138 | typedef struct |
133 | { |
139 | { |
134 | struct ev_io *head; |
140 | struct ev_io *head; |
135 | unsigned char wev, rev; /* want, received event set */ |
141 | int events; |
136 | } ANFD; |
142 | } ANFD; |
137 | |
143 | |
138 | static ANFD *anfds; |
144 | static ANFD *anfds; |
139 | static int anfdmax; |
145 | static int anfdmax; |
140 | |
146 | |
141 | static int *fdchanges; |
|
|
142 | static int fdchangemax, fdchangecnt; |
|
|
143 | |
|
|
144 | static void |
147 | static void |
145 | anfds_init (ANFD *base, int count) |
148 | anfds_init (ANFD *base, int count) |
146 | { |
149 | { |
147 | while (count--) |
150 | while (count--) |
148 | { |
151 | { |
149 | base->head = 0; |
152 | base->head = 0; |
150 | base->wev = base->rev = EV_NONE; |
153 | base->events = EV_NONE; |
151 | ++base; |
154 | ++base; |
152 | } |
155 | } |
153 | } |
156 | } |
154 | |
157 | |
155 | typedef struct |
158 | typedef struct |
… | |
… | |
172 | pendings [pendingcnt - 1].events = events; |
175 | pendings [pendingcnt - 1].events = events; |
173 | } |
176 | } |
174 | } |
177 | } |
175 | |
178 | |
176 | static void |
179 | static void |
|
|
180 | queue_events (W *events, int eventcnt, int type) |
|
|
181 | { |
|
|
182 | int i; |
|
|
183 | |
|
|
184 | for (i = 0; i < eventcnt; ++i) |
|
|
185 | event (events [i], type); |
|
|
186 | } |
|
|
187 | |
|
|
188 | static void |
177 | fd_event (int fd, int events) |
189 | fd_event (int fd, int events) |
178 | { |
190 | { |
179 | ANFD *anfd = anfds + fd; |
191 | ANFD *anfd = anfds + fd; |
180 | struct ev_io *w; |
192 | struct ev_io *w; |
181 | |
193 | |
… | |
… | |
186 | if (ev) |
198 | if (ev) |
187 | event ((W)w, ev); |
199 | event ((W)w, ev); |
188 | } |
200 | } |
189 | } |
201 | } |
190 | |
202 | |
|
|
203 | /*****************************************************************************/ |
|
|
204 | |
|
|
205 | static int *fdchanges; |
|
|
206 | static int fdchangemax, fdchangecnt; |
|
|
207 | |
191 | static void |
208 | static void |
192 | queue_events (W *events, int eventcnt, int type) |
209 | fd_reify (void) |
193 | { |
210 | { |
194 | int i; |
211 | int i; |
195 | |
212 | |
196 | for (i = 0; i < eventcnt; ++i) |
213 | for (i = 0; i < fdchangecnt; ++i) |
197 | event (events [i], type); |
214 | { |
|
|
215 | int fd = fdchanges [i]; |
|
|
216 | ANFD *anfd = anfds + fd; |
|
|
217 | struct ev_io *w; |
|
|
218 | |
|
|
219 | int events = 0; |
|
|
220 | |
|
|
221 | for (w = anfd->head; w; w = w->next) |
|
|
222 | events |= w->events; |
|
|
223 | |
|
|
224 | anfd->events &= ~EV_REIFY; |
|
|
225 | |
|
|
226 | if (anfd->events != events) |
|
|
227 | { |
|
|
228 | method_modify (fd, anfd->events, events); |
|
|
229 | anfd->events = events; |
|
|
230 | } |
|
|
231 | } |
|
|
232 | |
|
|
233 | fdchangecnt = 0; |
|
|
234 | } |
|
|
235 | |
|
|
236 | static void |
|
|
237 | fd_change (int fd) |
|
|
238 | { |
|
|
239 | if (anfds [fd].events & EV_REIFY || fdchangecnt < 0) |
|
|
240 | return; |
|
|
241 | |
|
|
242 | anfds [fd].events |= EV_REIFY; |
|
|
243 | |
|
|
244 | ++fdchangecnt; |
|
|
245 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
|
|
246 | fdchanges [fdchangecnt - 1] = fd; |
198 | } |
247 | } |
199 | |
248 | |
200 | /* called on EBADF to verify fds */ |
249 | /* called on EBADF to verify fds */ |
201 | static void |
250 | static void |
202 | fd_recheck (void) |
251 | fd_recheck (void) |
203 | { |
252 | { |
204 | int fd; |
253 | int fd; |
205 | |
254 | |
206 | for (fd = 0; fd < anfdmax; ++fd) |
255 | for (fd = 0; fd < anfdmax; ++fd) |
207 | if (anfds [fd].wev) |
256 | if (anfds [fd].events) |
208 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
257 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
209 | while (anfds [fd].head) |
258 | while (anfds [fd].head) |
210 | { |
259 | { |
211 | event ((W)anfds [fd].head, EV_ERROR); |
260 | event ((W)anfds [fd].head, EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT); |
212 | evio_stop (anfds [fd].head); |
261 | ev_io_stop (anfds [fd].head); |
213 | } |
262 | } |
214 | } |
263 | } |
215 | |
264 | |
216 | /*****************************************************************************/ |
265 | /*****************************************************************************/ |
217 | |
266 | |
… | |
… | |
327 | |
376 | |
328 | /* rather than sort out wether we really need nb, set it */ |
377 | /* rather than sort out wether we really need nb, set it */ |
329 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
378 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
330 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
379 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
331 | |
380 | |
332 | evio_set (&sigev, sigpipe [0], EV_READ); |
381 | ev_io_set (&sigev, sigpipe [0], EV_READ); |
333 | evio_start (&sigev); |
382 | ev_io_start (&sigev); |
334 | } |
383 | } |
335 | |
384 | |
336 | /*****************************************************************************/ |
385 | /*****************************************************************************/ |
337 | |
386 | |
338 | static struct ev_idle **idles; |
387 | static struct ev_idle **idles; |
… | |
… | |
368 | } |
417 | } |
369 | } |
418 | } |
370 | |
419 | |
371 | /*****************************************************************************/ |
420 | /*****************************************************************************/ |
372 | |
421 | |
373 | #if HAVE_EPOLL |
422 | #if EV_USE_EPOLL |
374 | # include "ev_epoll.c" |
423 | # include "ev_epoll.c" |
375 | #endif |
424 | #endif |
376 | #if HAVE_SELECT |
425 | #if EV_USE_SELECT |
377 | # include "ev_select.c" |
426 | # include "ev_select.c" |
378 | #endif |
427 | #endif |
379 | |
428 | |
380 | int |
429 | int |
381 | ev_version_major (void) |
430 | ev_version_major (void) |
… | |
… | |
391 | |
440 | |
392 | int ev_init (int flags) |
441 | int ev_init (int flags) |
393 | { |
442 | { |
394 | if (!ev_method) |
443 | if (!ev_method) |
395 | { |
444 | { |
396 | #if HAVE_MONOTONIC |
445 | #if EV_USE_MONOTONIC |
397 | { |
446 | { |
398 | struct timespec ts; |
447 | struct timespec ts; |
399 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
448 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
400 | have_monotonic = 1; |
449 | have_monotonic = 1; |
401 | } |
450 | } |
… | |
… | |
407 | |
456 | |
408 | if (pipe (sigpipe)) |
457 | if (pipe (sigpipe)) |
409 | return 0; |
458 | return 0; |
410 | |
459 | |
411 | ev_method = EVMETHOD_NONE; |
460 | ev_method = EVMETHOD_NONE; |
412 | #if HAVE_EPOLL |
461 | #if EV_USE_EPOLL |
413 | if (ev_method == EVMETHOD_NONE) epoll_init (flags); |
462 | if (ev_method == EVMETHOD_NONE) epoll_init (flags); |
414 | #endif |
463 | #endif |
415 | #if HAVE_SELECT |
464 | #if EV_USE_SELECT |
416 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
465 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
417 | #endif |
466 | #endif |
418 | |
467 | |
419 | if (ev_method) |
468 | if (ev_method) |
420 | { |
469 | { |
421 | evw_init (&sigev, sigcb); |
470 | ev_watcher_init (&sigev, sigcb); |
422 | siginit (); |
471 | siginit (); |
423 | |
472 | |
424 | evsignal_init (&childev, childcb, SIGCHLD); |
473 | ev_signal_init (&childev, childcb, SIGCHLD); |
425 | evsignal_start (&childev); |
474 | ev_signal_start (&childev); |
426 | } |
475 | } |
427 | } |
476 | } |
428 | |
477 | |
429 | return ev_method; |
478 | return ev_method; |
430 | } |
479 | } |
… | |
… | |
444 | } |
493 | } |
445 | |
494 | |
446 | void |
495 | void |
447 | ev_postfork_child (void) |
496 | ev_postfork_child (void) |
448 | { |
497 | { |
449 | #if HAVE_EPOLL |
498 | #if EV_USE_EPOLL |
450 | if (ev_method == EVMETHOD_EPOLL) |
499 | if (ev_method == EVMETHOD_EPOLL) |
451 | epoll_postfork_child (); |
500 | epoll_postfork_child (); |
452 | #endif |
501 | #endif |
453 | |
502 | |
454 | evio_stop (&sigev); |
503 | ev_io_stop (&sigev); |
455 | close (sigpipe [0]); |
504 | close (sigpipe [0]); |
456 | close (sigpipe [1]); |
505 | close (sigpipe [1]); |
457 | pipe (sigpipe); |
506 | pipe (sigpipe); |
458 | siginit (); |
507 | siginit (); |
459 | } |
508 | } |
460 | |
509 | |
461 | /*****************************************************************************/ |
510 | /*****************************************************************************/ |
462 | |
511 | |
463 | static void |
512 | static void |
464 | fd_reify (void) |
|
|
465 | { |
|
|
466 | int i; |
|
|
467 | |
|
|
468 | for (i = 0; i < fdchangecnt; ++i) |
|
|
469 | { |
|
|
470 | int fd = fdchanges [i]; |
|
|
471 | ANFD *anfd = anfds + fd; |
|
|
472 | struct ev_io *w; |
|
|
473 | |
|
|
474 | int wev = 0; |
|
|
475 | |
|
|
476 | for (w = anfd->head; w; w = w->next) |
|
|
477 | wev |= w->events; |
|
|
478 | |
|
|
479 | if (anfd->wev != wev) |
|
|
480 | { |
|
|
481 | method_modify (fd, anfd->wev, wev); |
|
|
482 | anfd->wev = wev; |
|
|
483 | } |
|
|
484 | } |
|
|
485 | |
|
|
486 | fdchangecnt = 0; |
|
|
487 | } |
|
|
488 | |
|
|
489 | static void |
|
|
490 | call_pending (void) |
513 | call_pending (void) |
491 | { |
514 | { |
492 | while (pendingcnt) |
515 | while (pendingcnt) |
493 | { |
516 | { |
494 | ANPENDING *p = pendings + --pendingcnt; |
517 | ANPENDING *p = pendings + --pendingcnt; |
… | |
… | |
516 | w->at = now + w->repeat; |
539 | w->at = now + w->repeat; |
517 | assert (("timer timeout in the past, negative repeat?", w->at > now)); |
540 | assert (("timer timeout in the past, negative repeat?", w->at > now)); |
518 | downheap ((WT *)timers, timercnt, 0); |
541 | downheap ((WT *)timers, timercnt, 0); |
519 | } |
542 | } |
520 | else |
543 | else |
521 | evtimer_stop (w); /* nonrepeating: stop timer */ |
544 | ev_timer_stop (w); /* nonrepeating: stop timer */ |
522 | } |
545 | } |
523 | } |
546 | } |
524 | |
547 | |
525 | static void |
548 | static void |
526 | periodics_reify (void) |
549 | periodics_reify (void) |
… | |
… | |
535 | w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; |
558 | w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; |
536 | assert (("periodic timeout in the past, negative interval?", w->at > ev_now)); |
559 | assert (("periodic timeout in the past, negative interval?", w->at > ev_now)); |
537 | downheap ((WT *)periodics, periodiccnt, 0); |
560 | downheap ((WT *)periodics, periodiccnt, 0); |
538 | } |
561 | } |
539 | else |
562 | else |
540 | evperiodic_stop (w); /* nonrepeating: stop timer */ |
563 | ev_periodic_stop (w); /* nonrepeating: stop timer */ |
541 | |
564 | |
542 | event ((W)w, EV_TIMEOUT); |
565 | event ((W)w, EV_TIMEOUT); |
543 | } |
566 | } |
544 | } |
567 | } |
545 | |
568 | |
… | |
… | |
557 | { |
580 | { |
558 | ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval; |
581 | ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval; |
559 | |
582 | |
560 | if (fabs (diff) >= 1e-4) |
583 | if (fabs (diff) >= 1e-4) |
561 | { |
584 | { |
562 | evperiodic_stop (w); |
585 | ev_periodic_stop (w); |
563 | evperiodic_start (w); |
586 | ev_periodic_start (w); |
564 | |
587 | |
565 | i = 0; /* restart loop, inefficient, but time jumps should be rare */ |
588 | i = 0; /* restart loop, inefficient, but time jumps should be rare */ |
566 | } |
589 | } |
567 | } |
590 | } |
568 | } |
591 | } |
… | |
… | |
611 | int ev_loop_done; |
634 | int ev_loop_done; |
612 | |
635 | |
613 | void ev_loop (int flags) |
636 | void ev_loop (int flags) |
614 | { |
637 | { |
615 | double block; |
638 | double block; |
616 | ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0; |
639 | ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; |
617 | |
640 | |
618 | do |
641 | do |
619 | { |
642 | { |
620 | /* queue check watchers (and execute them) */ |
643 | /* queue check watchers (and execute them) */ |
621 | if (preparecnt) |
644 | if (preparecnt) |
… | |
… | |
726 | } |
749 | } |
727 | |
750 | |
728 | /*****************************************************************************/ |
751 | /*****************************************************************************/ |
729 | |
752 | |
730 | void |
753 | void |
731 | evio_start (struct ev_io *w) |
754 | ev_io_start (struct ev_io *w) |
732 | { |
755 | { |
733 | if (ev_is_active (w)) |
756 | if (ev_is_active (w)) |
734 | return; |
757 | return; |
735 | |
758 | |
736 | int fd = w->fd; |
759 | int fd = w->fd; |
737 | |
760 | |
738 | ev_start ((W)w, 1); |
761 | ev_start ((W)w, 1); |
739 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
762 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
740 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
763 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
741 | |
764 | |
742 | ++fdchangecnt; |
765 | fd_change (fd); |
743 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
|
|
744 | fdchanges [fdchangecnt - 1] = fd; |
|
|
745 | } |
766 | } |
746 | |
767 | |
747 | void |
768 | void |
748 | evio_stop (struct ev_io *w) |
769 | ev_io_stop (struct ev_io *w) |
749 | { |
770 | { |
750 | ev_clear ((W)w); |
771 | ev_clear ((W)w); |
751 | if (!ev_is_active (w)) |
772 | if (!ev_is_active (w)) |
752 | return; |
773 | return; |
753 | |
774 | |
754 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
775 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
755 | ev_stop ((W)w); |
776 | ev_stop ((W)w); |
756 | |
777 | |
757 | ++fdchangecnt; |
778 | fd_change (w->fd); |
758 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
|
|
759 | fdchanges [fdchangecnt - 1] = w->fd; |
|
|
760 | } |
779 | } |
761 | |
780 | |
762 | void |
781 | void |
763 | evtimer_start (struct ev_timer *w) |
782 | ev_timer_start (struct ev_timer *w) |
764 | { |
783 | { |
765 | if (ev_is_active (w)) |
784 | if (ev_is_active (w)) |
766 | return; |
785 | return; |
767 | |
786 | |
768 | w->at += now; |
787 | w->at += now; |
… | |
… | |
774 | timers [timercnt - 1] = w; |
793 | timers [timercnt - 1] = w; |
775 | upheap ((WT *)timers, timercnt - 1); |
794 | upheap ((WT *)timers, timercnt - 1); |
776 | } |
795 | } |
777 | |
796 | |
778 | void |
797 | void |
779 | evtimer_stop (struct ev_timer *w) |
798 | ev_timer_stop (struct ev_timer *w) |
780 | { |
799 | { |
781 | ev_clear ((W)w); |
800 | ev_clear ((W)w); |
782 | if (!ev_is_active (w)) |
801 | if (!ev_is_active (w)) |
783 | return; |
802 | return; |
784 | |
803 | |
… | |
… | |
792 | |
811 | |
793 | ev_stop ((W)w); |
812 | ev_stop ((W)w); |
794 | } |
813 | } |
795 | |
814 | |
796 | void |
815 | void |
797 | evtimer_again (struct ev_timer *w) |
816 | ev_timer_again (struct ev_timer *w) |
798 | { |
817 | { |
799 | if (ev_is_active (w)) |
818 | if (ev_is_active (w)) |
800 | { |
819 | { |
801 | if (w->repeat) |
820 | if (w->repeat) |
802 | { |
821 | { |
803 | w->at = now + w->repeat; |
822 | w->at = now + w->repeat; |
804 | downheap ((WT *)timers, timercnt, w->active - 1); |
823 | downheap ((WT *)timers, timercnt, w->active - 1); |
805 | } |
824 | } |
806 | else |
825 | else |
807 | evtimer_stop (w); |
826 | ev_timer_stop (w); |
808 | } |
827 | } |
809 | else if (w->repeat) |
828 | else if (w->repeat) |
810 | evtimer_start (w); |
829 | ev_timer_start (w); |
811 | } |
830 | } |
812 | |
831 | |
813 | void |
832 | void |
814 | evperiodic_start (struct ev_periodic *w) |
833 | ev_periodic_start (struct ev_periodic *w) |
815 | { |
834 | { |
816 | if (ev_is_active (w)) |
835 | if (ev_is_active (w)) |
817 | return; |
836 | return; |
818 | |
837 | |
819 | assert (("periodic interval value less than zero not allowed", w->interval >= 0.)); |
838 | assert (("periodic interval value less than zero not allowed", w->interval >= 0.)); |
… | |
… | |
827 | periodics [periodiccnt - 1] = w; |
846 | periodics [periodiccnt - 1] = w; |
828 | upheap ((WT *)periodics, periodiccnt - 1); |
847 | upheap ((WT *)periodics, periodiccnt - 1); |
829 | } |
848 | } |
830 | |
849 | |
831 | void |
850 | void |
832 | evperiodic_stop (struct ev_periodic *w) |
851 | ev_periodic_stop (struct ev_periodic *w) |
833 | { |
852 | { |
834 | ev_clear ((W)w); |
853 | ev_clear ((W)w); |
835 | if (!ev_is_active (w)) |
854 | if (!ev_is_active (w)) |
836 | return; |
855 | return; |
837 | |
856 | |
… | |
… | |
843 | |
862 | |
844 | ev_stop ((W)w); |
863 | ev_stop ((W)w); |
845 | } |
864 | } |
846 | |
865 | |
847 | void |
866 | void |
848 | evsignal_start (struct ev_signal *w) |
867 | ev_signal_start (struct ev_signal *w) |
849 | { |
868 | { |
850 | if (ev_is_active (w)) |
869 | if (ev_is_active (w)) |
851 | return; |
870 | return; |
852 | |
871 | |
853 | ev_start ((W)w, 1); |
872 | ev_start ((W)w, 1); |
… | |
… | |
863 | sigaction (w->signum, &sa, 0); |
882 | sigaction (w->signum, &sa, 0); |
864 | } |
883 | } |
865 | } |
884 | } |
866 | |
885 | |
867 | void |
886 | void |
868 | evsignal_stop (struct ev_signal *w) |
887 | ev_signal_stop (struct ev_signal *w) |
869 | { |
888 | { |
870 | ev_clear ((W)w); |
889 | ev_clear ((W)w); |
871 | if (!ev_is_active (w)) |
890 | if (!ev_is_active (w)) |
872 | return; |
891 | return; |
873 | |
892 | |
… | |
… | |
876 | |
895 | |
877 | if (!signals [w->signum - 1].head) |
896 | if (!signals [w->signum - 1].head) |
878 | signal (w->signum, SIG_DFL); |
897 | signal (w->signum, SIG_DFL); |
879 | } |
898 | } |
880 | |
899 | |
|
|
900 | void |
881 | void evidle_start (struct ev_idle *w) |
901 | ev_idle_start (struct ev_idle *w) |
882 | { |
902 | { |
883 | if (ev_is_active (w)) |
903 | if (ev_is_active (w)) |
884 | return; |
904 | return; |
885 | |
905 | |
886 | ev_start ((W)w, ++idlecnt); |
906 | ev_start ((W)w, ++idlecnt); |
887 | array_needsize (idles, idlemax, idlecnt, ); |
907 | array_needsize (idles, idlemax, idlecnt, ); |
888 | idles [idlecnt - 1] = w; |
908 | idles [idlecnt - 1] = w; |
889 | } |
909 | } |
890 | |
910 | |
|
|
911 | void |
891 | void evidle_stop (struct ev_idle *w) |
912 | ev_idle_stop (struct ev_idle *w) |
892 | { |
913 | { |
893 | ev_clear ((W)w); |
914 | ev_clear ((W)w); |
894 | if (ev_is_active (w)) |
915 | if (ev_is_active (w)) |
895 | return; |
916 | return; |
896 | |
917 | |
897 | idles [w->active - 1] = idles [--idlecnt]; |
918 | idles [w->active - 1] = idles [--idlecnt]; |
898 | ev_stop ((W)w); |
919 | ev_stop ((W)w); |
899 | } |
920 | } |
900 | |
921 | |
|
|
922 | void |
901 | void evprepare_start (struct ev_prepare *w) |
923 | ev_prepare_start (struct ev_prepare *w) |
902 | { |
924 | { |
903 | if (ev_is_active (w)) |
925 | if (ev_is_active (w)) |
904 | return; |
926 | return; |
905 | |
927 | |
906 | ev_start ((W)w, ++preparecnt); |
928 | ev_start ((W)w, ++preparecnt); |
907 | array_needsize (prepares, preparemax, preparecnt, ); |
929 | array_needsize (prepares, preparemax, preparecnt, ); |
908 | prepares [preparecnt - 1] = w; |
930 | prepares [preparecnt - 1] = w; |
909 | } |
931 | } |
910 | |
932 | |
|
|
933 | void |
911 | void evprepare_stop (struct ev_prepare *w) |
934 | ev_prepare_stop (struct ev_prepare *w) |
912 | { |
935 | { |
913 | ev_clear ((W)w); |
936 | ev_clear ((W)w); |
914 | if (ev_is_active (w)) |
937 | if (ev_is_active (w)) |
915 | return; |
938 | return; |
916 | |
939 | |
917 | prepares [w->active - 1] = prepares [--preparecnt]; |
940 | prepares [w->active - 1] = prepares [--preparecnt]; |
918 | ev_stop ((W)w); |
941 | ev_stop ((W)w); |
919 | } |
942 | } |
920 | |
943 | |
|
|
944 | void |
921 | void evcheck_start (struct ev_check *w) |
945 | ev_check_start (struct ev_check *w) |
922 | { |
946 | { |
923 | if (ev_is_active (w)) |
947 | if (ev_is_active (w)) |
924 | return; |
948 | return; |
925 | |
949 | |
926 | ev_start ((W)w, ++checkcnt); |
950 | ev_start ((W)w, ++checkcnt); |
927 | array_needsize (checks, checkmax, checkcnt, ); |
951 | array_needsize (checks, checkmax, checkcnt, ); |
928 | checks [checkcnt - 1] = w; |
952 | checks [checkcnt - 1] = w; |
929 | } |
953 | } |
930 | |
954 | |
|
|
955 | void |
931 | void evcheck_stop (struct ev_check *w) |
956 | ev_check_stop (struct ev_check *w) |
932 | { |
957 | { |
933 | ev_clear ((W)w); |
958 | ev_clear ((W)w); |
934 | if (ev_is_active (w)) |
959 | if (ev_is_active (w)) |
935 | return; |
960 | return; |
936 | |
961 | |
937 | checks [w->active - 1] = checks [--checkcnt]; |
962 | checks [w->active - 1] = checks [--checkcnt]; |
938 | ev_stop ((W)w); |
963 | ev_stop ((W)w); |
939 | } |
964 | } |
940 | |
965 | |
|
|
966 | void |
941 | void evchild_start (struct ev_child *w) |
967 | ev_child_start (struct ev_child *w) |
942 | { |
968 | { |
943 | if (ev_is_active (w)) |
969 | if (ev_is_active (w)) |
944 | return; |
970 | return; |
945 | |
971 | |
946 | ev_start ((W)w, 1); |
972 | ev_start ((W)w, 1); |
947 | wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
973 | wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
948 | } |
974 | } |
949 | |
975 | |
|
|
976 | void |
950 | void evchild_stop (struct ev_child *w) |
977 | ev_child_stop (struct ev_child *w) |
951 | { |
978 | { |
952 | ev_clear ((W)w); |
979 | ev_clear ((W)w); |
953 | if (ev_is_active (w)) |
980 | if (ev_is_active (w)) |
954 | return; |
981 | return; |
955 | |
982 | |
… | |
… | |
971 | once_cb (struct ev_once *once, int revents) |
998 | once_cb (struct ev_once *once, int revents) |
972 | { |
999 | { |
973 | void (*cb)(int revents, void *arg) = once->cb; |
1000 | void (*cb)(int revents, void *arg) = once->cb; |
974 | void *arg = once->arg; |
1001 | void *arg = once->arg; |
975 | |
1002 | |
976 | evio_stop (&once->io); |
1003 | ev_io_stop (&once->io); |
977 | evtimer_stop (&once->to); |
1004 | ev_timer_stop (&once->to); |
978 | free (once); |
1005 | free (once); |
979 | |
1006 | |
980 | cb (revents, arg); |
1007 | cb (revents, arg); |
981 | } |
1008 | } |
982 | |
1009 | |
… | |
… | |
996 | ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1023 | ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
997 | { |
1024 | { |
998 | struct ev_once *once = malloc (sizeof (struct ev_once)); |
1025 | struct ev_once *once = malloc (sizeof (struct ev_once)); |
999 | |
1026 | |
1000 | if (!once) |
1027 | if (!once) |
1001 | cb (EV_ERROR, arg); |
1028 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
1002 | else |
1029 | else |
1003 | { |
1030 | { |
1004 | once->cb = cb; |
1031 | once->cb = cb; |
1005 | once->arg = arg; |
1032 | once->arg = arg; |
1006 | |
1033 | |
1007 | evw_init (&once->io, once_cb_io); |
1034 | ev_watcher_init (&once->io, once_cb_io); |
1008 | |
|
|
1009 | if (fd >= 0) |
1035 | if (fd >= 0) |
1010 | { |
1036 | { |
1011 | evio_set (&once->io, fd, events); |
1037 | ev_io_set (&once->io, fd, events); |
1012 | evio_start (&once->io); |
1038 | ev_io_start (&once->io); |
1013 | } |
1039 | } |
1014 | |
1040 | |
1015 | evw_init (&once->to, once_cb_to); |
1041 | ev_watcher_init (&once->to, once_cb_to); |
1016 | |
|
|
1017 | if (timeout >= 0.) |
1042 | if (timeout >= 0.) |
1018 | { |
1043 | { |
1019 | evtimer_set (&once->to, timeout, 0.); |
1044 | ev_timer_set (&once->to, timeout, 0.); |
1020 | evtimer_start (&once->to); |
1045 | ev_timer_start (&once->to); |
1021 | } |
1046 | } |
1022 | } |
1047 | } |
1023 | } |
1048 | } |
1024 | |
1049 | |
1025 | /*****************************************************************************/ |
1050 | /*****************************************************************************/ |
… | |
… | |
1036 | |
1061 | |
1037 | static void |
1062 | static void |
1038 | ocb (struct ev_timer *w, int revents) |
1063 | ocb (struct ev_timer *w, int revents) |
1039 | { |
1064 | { |
1040 | //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data); |
1065 | //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data); |
1041 | evtimer_stop (w); |
1066 | ev_timer_stop (w); |
1042 | evtimer_start (w); |
1067 | ev_timer_start (w); |
1043 | } |
1068 | } |
1044 | |
1069 | |
1045 | static void |
1070 | static void |
1046 | scb (struct ev_signal *w, int revents) |
1071 | scb (struct ev_signal *w, int revents) |
1047 | { |
1072 | { |
1048 | fprintf (stderr, "signal %x,%d\n", revents, w->signum); |
1073 | fprintf (stderr, "signal %x,%d\n", revents, w->signum); |
1049 | evio_stop (&wio); |
1074 | ev_io_stop (&wio); |
1050 | evio_start (&wio); |
1075 | ev_io_start (&wio); |
1051 | } |
1076 | } |
1052 | |
1077 | |
1053 | static void |
1078 | static void |
1054 | gcb (struct ev_signal *w, int revents) |
1079 | gcb (struct ev_signal *w, int revents) |
1055 | { |
1080 | { |
… | |
… | |
1059 | |
1084 | |
1060 | int main (void) |
1085 | int main (void) |
1061 | { |
1086 | { |
1062 | ev_init (0); |
1087 | ev_init (0); |
1063 | |
1088 | |
1064 | evio_init (&wio, sin_cb, 0, EV_READ); |
1089 | ev_io_init (&wio, sin_cb, 0, EV_READ); |
1065 | evio_start (&wio); |
1090 | ev_io_start (&wio); |
1066 | |
1091 | |
1067 | struct ev_timer t[10000]; |
1092 | struct ev_timer t[10000]; |
1068 | |
1093 | |
1069 | #if 0 |
1094 | #if 0 |
1070 | int i; |
1095 | int i; |
1071 | for (i = 0; i < 10000; ++i) |
1096 | for (i = 0; i < 10000; ++i) |
1072 | { |
1097 | { |
1073 | struct ev_timer *w = t + i; |
1098 | struct ev_timer *w = t + i; |
1074 | evw_init (w, ocb, i); |
1099 | ev_watcher_init (w, ocb, i); |
1075 | evtimer_init_abs (w, ocb, drand48 (), 0.99775533); |
1100 | ev_timer_init_abs (w, ocb, drand48 (), 0.99775533); |
1076 | evtimer_start (w); |
1101 | ev_timer_start (w); |
1077 | if (drand48 () < 0.5) |
1102 | if (drand48 () < 0.5) |
1078 | evtimer_stop (w); |
1103 | ev_timer_stop (w); |
1079 | } |
1104 | } |
1080 | #endif |
1105 | #endif |
1081 | |
1106 | |
1082 | struct ev_timer t1; |
1107 | struct ev_timer t1; |
1083 | evtimer_init (&t1, ocb, 5, 10); |
1108 | ev_timer_init (&t1, ocb, 5, 10); |
1084 | evtimer_start (&t1); |
1109 | ev_timer_start (&t1); |
1085 | |
1110 | |
1086 | struct ev_signal sig; |
1111 | struct ev_signal sig; |
1087 | evsignal_init (&sig, scb, SIGQUIT); |
1112 | ev_signal_init (&sig, scb, SIGQUIT); |
1088 | evsignal_start (&sig); |
1113 | ev_signal_start (&sig); |
1089 | |
1114 | |
1090 | struct ev_check cw; |
1115 | struct ev_check cw; |
1091 | evcheck_init (&cw, gcb); |
1116 | ev_check_init (&cw, gcb); |
1092 | evcheck_start (&cw); |
1117 | ev_check_start (&cw); |
1093 | |
1118 | |
1094 | struct ev_idle iw; |
1119 | struct ev_idle iw; |
1095 | evidle_init (&iw, gcb); |
1120 | ev_idle_init (&iw, gcb); |
1096 | evidle_start (&iw); |
1121 | ev_idle_start (&iw); |
1097 | |
1122 | |
1098 | ev_loop (0); |
1123 | ev_loop (0); |
1099 | |
1124 | |
1100 | return 0; |
1125 | return 0; |
1101 | } |
1126 | } |