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