1 | /* |
1 | /* |
|
|
2 | * libev event processing core, watcher management |
|
|
3 | * |
2 | * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de> |
4 | * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de> |
3 | * All rights reserved. |
5 | * All rights reserved. |
4 | * |
6 | * |
5 | * Redistribution and use in source and binary forms, with or without |
7 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions are |
8 | * modification, are permitted provided that the following conditions are |
… | |
… | |
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
26 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
28 | * (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. |
29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | */ |
30 | */ |
|
|
31 | #if EV_USE_CONFIG_H |
|
|
32 | # include "config.h" |
|
|
33 | #endif |
29 | |
34 | |
30 | #include <math.h> |
35 | #include <math.h> |
31 | #include <stdlib.h> |
36 | #include <stdlib.h> |
32 | #include <unistd.h> |
37 | #include <unistd.h> |
33 | #include <fcntl.h> |
38 | #include <fcntl.h> |
… | |
… | |
41 | #include <sys/types.h> |
46 | #include <sys/types.h> |
42 | #include <sys/wait.h> |
47 | #include <sys/wait.h> |
43 | #include <sys/time.h> |
48 | #include <sys/time.h> |
44 | #include <time.h> |
49 | #include <time.h> |
45 | |
50 | |
46 | #ifndef HAVE_MONOTONIC |
51 | #ifndef EV_USE_MONOTONIC |
|
|
52 | # define EV_USE_MONOTONIC 1 |
|
|
53 | #endif |
|
|
54 | |
47 | # ifdef CLOCK_MONOTONIC |
55 | #ifndef CLOCK_MONOTONIC |
|
|
56 | # undef EV_USE_MONOTONIC |
48 | # define HAVE_MONOTONIC 1 |
57 | # define EV_USE_MONOTONIC 0 |
49 | # endif |
58 | #endif |
50 | #endif |
|
|
51 | |
59 | |
52 | #ifndef HAVE_SELECT |
60 | #ifndef EV_USE_SELECT |
53 | # define HAVE_SELECT 1 |
61 | # define EV_USE_SELECT 1 |
54 | #endif |
62 | #endif |
55 | |
63 | |
56 | #ifndef HAVE_EPOLL |
64 | #ifndef EV_USE_EPOLL |
57 | # define HAVE_EPOLL 0 |
65 | # define EV_USE_EPOLL 0 |
58 | #endif |
66 | #endif |
59 | |
67 | |
|
|
68 | #ifndef CLOCK_REALTIME |
|
|
69 | # define EV_USE_REALTIME 0 |
|
|
70 | #endif |
60 | #ifndef HAVE_REALTIME |
71 | #ifndef EV_USE_REALTIME |
61 | # define HAVE_REALTIME 1 /* posix requirement, but might be slower */ |
72 | # define EV_USE_REALTIME 1 /* posix requirement, but might be slower */ |
62 | #endif |
73 | #endif |
63 | |
74 | |
64 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
75 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
65 | #define MAX_BLOCKTIME 60. |
76 | #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 */ |
77 | #define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */ |
|
|
78 | #define CLEANUP_INTERVAL (MAX_BLOCKTIME * 5.) /* how often to try to free memory and re-check fds */ |
67 | |
79 | |
68 | #include "ev.h" |
80 | #include "ev.h" |
69 | |
81 | |
70 | typedef struct ev_watcher *W; |
82 | typedef struct ev_watcher *W; |
71 | typedef struct ev_watcher_list *WL; |
83 | typedef struct ev_watcher_list *WL; |
… | |
… | |
84 | /*****************************************************************************/ |
96 | /*****************************************************************************/ |
85 | |
97 | |
86 | ev_tstamp |
98 | ev_tstamp |
87 | ev_time (void) |
99 | ev_time (void) |
88 | { |
100 | { |
89 | #if HAVE_REALTIME |
101 | #if EV_USE_REALTIME |
90 | struct timespec ts; |
102 | struct timespec ts; |
91 | clock_gettime (CLOCK_REALTIME, &ts); |
103 | clock_gettime (CLOCK_REALTIME, &ts); |
92 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
104 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
93 | #else |
105 | #else |
94 | struct timeval tv; |
106 | struct timeval tv; |
… | |
… | |
98 | } |
110 | } |
99 | |
111 | |
100 | static ev_tstamp |
112 | static ev_tstamp |
101 | get_clock (void) |
113 | get_clock (void) |
102 | { |
114 | { |
103 | #if HAVE_MONOTONIC |
115 | #if EV_USE_MONOTONIC |
104 | if (have_monotonic) |
116 | if (have_monotonic) |
105 | { |
117 | { |
106 | struct timespec ts; |
118 | struct timespec ts; |
107 | clock_gettime (CLOCK_MONOTONIC, &ts); |
119 | clock_gettime (CLOCK_MONOTONIC, &ts); |
108 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
120 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
109 | } |
121 | } |
110 | #endif |
122 | #endif |
111 | |
123 | |
112 | return ev_time (); |
124 | return ev_time (); |
113 | } |
125 | } |
|
|
126 | |
|
|
127 | #define array_roundsize(base,n) ((n) | 4 & ~3) |
114 | |
128 | |
115 | #define array_needsize(base,cur,cnt,init) \ |
129 | #define array_needsize(base,cur,cnt,init) \ |
116 | if ((cnt) > cur) \ |
130 | if ((cnt) > cur) \ |
117 | { \ |
131 | { \ |
118 | int newcnt = cur; \ |
132 | int newcnt = cur; \ |
119 | do \ |
133 | do \ |
120 | { \ |
134 | { \ |
121 | newcnt = (newcnt << 1) | 4 & ~3; \ |
135 | newcnt = array_roundsize (base, newcnt << 1); \ |
122 | } \ |
136 | } \ |
123 | while ((cnt) > newcnt); \ |
137 | while ((cnt) > newcnt); \ |
124 | \ |
138 | \ |
125 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
139 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
126 | init (base + cur, newcnt - cur); \ |
140 | init (base + cur, newcnt - cur); \ |
… | |
… | |
130 | /*****************************************************************************/ |
144 | /*****************************************************************************/ |
131 | |
145 | |
132 | typedef struct |
146 | typedef struct |
133 | { |
147 | { |
134 | struct ev_io *head; |
148 | struct ev_io *head; |
135 | int events; |
149 | unsigned char events; |
|
|
150 | unsigned char reify; |
136 | } ANFD; |
151 | } ANFD; |
137 | |
152 | |
138 | static ANFD *anfds; |
153 | static ANFD *anfds; |
139 | static int anfdmax; |
154 | static int anfdmax; |
140 | |
155 | |
… | |
… | |
143 | { |
158 | { |
144 | while (count--) |
159 | while (count--) |
145 | { |
160 | { |
146 | base->head = 0; |
161 | base->head = 0; |
147 | base->events = EV_NONE; |
162 | base->events = EV_NONE; |
|
|
163 | base->reify = 0; |
|
|
164 | |
148 | ++base; |
165 | ++base; |
149 | } |
166 | } |
150 | } |
167 | } |
151 | |
168 | |
152 | typedef struct |
169 | typedef struct |
… | |
… | |
159 | static int pendingmax, pendingcnt; |
176 | static int pendingmax, pendingcnt; |
160 | |
177 | |
161 | static void |
178 | static void |
162 | event (W w, int events) |
179 | event (W w, int events) |
163 | { |
180 | { |
164 | if (w->active) |
181 | if (w->pending) |
|
|
182 | { |
|
|
183 | pendings [w->pending - 1].events |= events; |
|
|
184 | return; |
165 | { |
185 | } |
|
|
186 | |
166 | w->pending = ++pendingcnt; |
187 | w->pending = ++pendingcnt; |
167 | array_needsize (pendings, pendingmax, pendingcnt, ); |
188 | array_needsize (pendings, pendingmax, pendingcnt, ); |
168 | pendings [pendingcnt - 1].w = w; |
189 | pendings [pendingcnt - 1].w = w; |
169 | pendings [pendingcnt - 1].events = events; |
190 | pendings [pendingcnt - 1].events = events; |
170 | } |
|
|
171 | } |
191 | } |
172 | |
192 | |
173 | static void |
193 | static void |
174 | queue_events (W *events, int eventcnt, int type) |
194 | queue_events (W *events, int eventcnt, int type) |
175 | { |
195 | { |
… | |
… | |
213 | int events = 0; |
233 | int events = 0; |
214 | |
234 | |
215 | for (w = anfd->head; w; w = w->next) |
235 | for (w = anfd->head; w; w = w->next) |
216 | events |= w->events; |
236 | events |= w->events; |
217 | |
237 | |
218 | anfd->events &= ~EV_REIFY; |
238 | anfd->reify = 0; |
219 | |
239 | |
220 | if (anfd->events != events) |
240 | if (anfd->events != events) |
221 | { |
241 | { |
222 | method_modify (fd, anfd->events, events); |
242 | method_modify (fd, anfd->events, events); |
223 | anfd->events = events; |
243 | anfd->events = events; |
… | |
… | |
228 | } |
248 | } |
229 | |
249 | |
230 | static void |
250 | static void |
231 | fd_change (int fd) |
251 | fd_change (int fd) |
232 | { |
252 | { |
233 | if (anfds [fd].events & EV_REIFY) |
253 | if (anfds [fd].reify || fdchangecnt < 0) |
234 | return; |
254 | return; |
235 | |
255 | |
236 | anfds [fd].events |= EV_REIFY; |
256 | anfds [fd].reify = 1; |
237 | |
257 | |
238 | ++fdchangecnt; |
258 | ++fdchangecnt; |
239 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
259 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
240 | fdchanges [fdchangecnt - 1] = fd; |
260 | fdchanges [fdchangecnt - 1] = fd; |
241 | } |
261 | } |
… | |
… | |
249 | for (fd = 0; fd < anfdmax; ++fd) |
269 | for (fd = 0; fd < anfdmax; ++fd) |
250 | if (anfds [fd].events) |
270 | if (anfds [fd].events) |
251 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
271 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
252 | while (anfds [fd].head) |
272 | while (anfds [fd].head) |
253 | { |
273 | { |
254 | event ((W)anfds [fd].head, EV_ERROR); |
|
|
255 | ev_io_stop (anfds [fd].head); |
274 | ev_io_stop (anfds [fd].head); |
|
|
275 | event ((W)anfds [fd].head, EV_ERROR | EV_READ | EV_WRITE); |
256 | } |
276 | } |
257 | } |
277 | } |
258 | |
278 | |
259 | /*****************************************************************************/ |
279 | /*****************************************************************************/ |
260 | |
280 | |
… | |
… | |
308 | /*****************************************************************************/ |
328 | /*****************************************************************************/ |
309 | |
329 | |
310 | typedef struct |
330 | typedef struct |
311 | { |
331 | { |
312 | struct ev_signal *head; |
332 | struct ev_signal *head; |
313 | sig_atomic_t gotsig; |
333 | sig_atomic_t volatile gotsig; |
314 | } ANSIG; |
334 | } ANSIG; |
315 | |
335 | |
316 | static ANSIG *signals; |
336 | static ANSIG *signals; |
317 | static int signalmax; |
337 | static int signalmax; |
318 | |
338 | |
319 | static int sigpipe [2]; |
339 | static int sigpipe [2]; |
320 | static sig_atomic_t gotsig; |
340 | static sig_atomic_t volatile gotsig; |
321 | static struct ev_io sigev; |
341 | static struct ev_io sigev; |
322 | |
342 | |
323 | static void |
343 | static void |
324 | signals_init (ANSIG *base, int count) |
344 | signals_init (ANSIG *base, int count) |
325 | { |
345 | { |
326 | while (count--) |
346 | while (count--) |
327 | { |
347 | { |
328 | base->head = 0; |
348 | base->head = 0; |
329 | base->gotsig = 0; |
349 | base->gotsig = 0; |
|
|
350 | |
330 | ++base; |
351 | ++base; |
331 | } |
352 | } |
332 | } |
353 | } |
333 | |
354 | |
334 | static void |
355 | static void |
… | |
… | |
337 | signals [signum - 1].gotsig = 1; |
358 | signals [signum - 1].gotsig = 1; |
338 | |
359 | |
339 | if (!gotsig) |
360 | if (!gotsig) |
340 | { |
361 | { |
341 | gotsig = 1; |
362 | gotsig = 1; |
342 | write (sigpipe [1], &gotsig, 1); |
363 | write (sigpipe [1], &signum, 1); |
343 | } |
364 | } |
344 | } |
365 | } |
345 | |
366 | |
346 | static void |
367 | static void |
347 | sigcb (struct ev_io *iow, int revents) |
368 | sigcb (struct ev_io *iow, int revents) |
348 | { |
369 | { |
349 | struct ev_signal *w; |
370 | struct ev_signal *w; |
350 | int sig; |
371 | int sig; |
351 | |
372 | |
|
|
373 | read (sigpipe [0], &revents, 1); |
352 | gotsig = 0; |
374 | gotsig = 0; |
353 | read (sigpipe [0], &revents, 1); |
|
|
354 | |
375 | |
355 | for (sig = signalmax; sig--; ) |
376 | for (sig = signalmax; sig--; ) |
356 | if (signals [sig].gotsig) |
377 | if (signals [sig].gotsig) |
357 | { |
378 | { |
358 | signals [sig].gotsig = 0; |
379 | signals [sig].gotsig = 0; |
… | |
… | |
411 | } |
432 | } |
412 | } |
433 | } |
413 | |
434 | |
414 | /*****************************************************************************/ |
435 | /*****************************************************************************/ |
415 | |
436 | |
416 | #if HAVE_EPOLL |
437 | #if EV_USE_EPOLL |
417 | # include "ev_epoll.c" |
438 | # include "ev_epoll.c" |
418 | #endif |
439 | #endif |
419 | #if HAVE_SELECT |
440 | #if EV_USE_SELECT |
420 | # include "ev_select.c" |
441 | # include "ev_select.c" |
421 | #endif |
442 | #endif |
422 | |
443 | |
423 | int |
444 | int |
424 | ev_version_major (void) |
445 | ev_version_major (void) |
… | |
… | |
434 | |
455 | |
435 | int ev_init (int flags) |
456 | int ev_init (int flags) |
436 | { |
457 | { |
437 | if (!ev_method) |
458 | if (!ev_method) |
438 | { |
459 | { |
439 | #if HAVE_MONOTONIC |
460 | #if EV_USE_MONOTONIC |
440 | { |
461 | { |
441 | struct timespec ts; |
462 | struct timespec ts; |
442 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
463 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
443 | have_monotonic = 1; |
464 | have_monotonic = 1; |
444 | } |
465 | } |
… | |
… | |
450 | |
471 | |
451 | if (pipe (sigpipe)) |
472 | if (pipe (sigpipe)) |
452 | return 0; |
473 | return 0; |
453 | |
474 | |
454 | ev_method = EVMETHOD_NONE; |
475 | ev_method = EVMETHOD_NONE; |
455 | #if HAVE_EPOLL |
476 | #if EV_USE_EPOLL |
456 | if (ev_method == EVMETHOD_NONE) epoll_init (flags); |
477 | if (ev_method == EVMETHOD_NONE) epoll_init (flags); |
457 | #endif |
478 | #endif |
458 | #if HAVE_SELECT |
479 | #if EV_USE_SELECT |
459 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
480 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
460 | #endif |
481 | #endif |
461 | |
482 | |
462 | if (ev_method) |
483 | if (ev_method) |
463 | { |
484 | { |
… | |
… | |
473 | } |
494 | } |
474 | |
495 | |
475 | /*****************************************************************************/ |
496 | /*****************************************************************************/ |
476 | |
497 | |
477 | void |
498 | void |
478 | ev_prefork (void) |
499 | ev_fork_prepare (void) |
479 | { |
500 | { |
480 | /* nop */ |
501 | /* nop */ |
481 | } |
502 | } |
482 | |
503 | |
483 | void |
504 | void |
484 | ev_postfork_parent (void) |
505 | ev_fork_parent (void) |
485 | { |
506 | { |
486 | /* nop */ |
507 | /* nop */ |
487 | } |
508 | } |
488 | |
509 | |
489 | void |
510 | void |
490 | ev_postfork_child (void) |
511 | ev_fork_child (void) |
491 | { |
512 | { |
492 | #if HAVE_EPOLL |
513 | #if EV_USE_EPOLL |
493 | if (ev_method == EVMETHOD_EPOLL) |
514 | if (ev_method == EVMETHOD_EPOLL) |
494 | epoll_postfork_child (); |
515 | epoll_postfork_child (); |
495 | #endif |
516 | #endif |
496 | |
517 | |
497 | ev_io_stop (&sigev); |
518 | ev_io_stop (&sigev); |
… | |
… | |
523 | { |
544 | { |
524 | while (timercnt && timers [0]->at <= now) |
545 | while (timercnt && timers [0]->at <= now) |
525 | { |
546 | { |
526 | struct ev_timer *w = timers [0]; |
547 | struct ev_timer *w = timers [0]; |
527 | |
548 | |
528 | event ((W)w, EV_TIMEOUT); |
|
|
529 | |
|
|
530 | /* first reschedule or stop timer */ |
549 | /* first reschedule or stop timer */ |
531 | if (w->repeat) |
550 | if (w->repeat) |
532 | { |
551 | { |
|
|
552 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
533 | w->at = now + w->repeat; |
553 | w->at = now + w->repeat; |
534 | assert (("timer timeout in the past, negative repeat?", w->at > now)); |
|
|
535 | downheap ((WT *)timers, timercnt, 0); |
554 | downheap ((WT *)timers, timercnt, 0); |
536 | } |
555 | } |
537 | else |
556 | else |
538 | ev_timer_stop (w); /* nonrepeating: stop timer */ |
557 | ev_timer_stop (w); /* nonrepeating: stop timer */ |
|
|
558 | |
|
|
559 | event ((W)w, EV_TIMEOUT); |
539 | } |
560 | } |
540 | } |
561 | } |
541 | |
562 | |
542 | static void |
563 | static void |
543 | periodics_reify (void) |
564 | periodics_reify (void) |
… | |
… | |
548 | |
569 | |
549 | /* first reschedule or stop timer */ |
570 | /* first reschedule or stop timer */ |
550 | if (w->interval) |
571 | if (w->interval) |
551 | { |
572 | { |
552 | w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; |
573 | 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)); |
574 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > ev_now)); |
554 | downheap ((WT *)periodics, periodiccnt, 0); |
575 | downheap ((WT *)periodics, periodiccnt, 0); |
555 | } |
576 | } |
556 | else |
577 | else |
557 | ev_periodic_stop (w); /* nonrepeating: stop timer */ |
578 | ev_periodic_stop (w); /* nonrepeating: stop timer */ |
558 | |
579 | |
559 | event ((W)w, EV_TIMEOUT); |
580 | event ((W)w, EV_PERIODIC); |
560 | } |
581 | } |
561 | } |
582 | } |
562 | |
583 | |
563 | static void |
584 | static void |
564 | periodics_reschedule (ev_tstamp diff) |
585 | periodics_reschedule (ev_tstamp diff) |
… | |
… | |
719 | head = &(*head)->next; |
740 | head = &(*head)->next; |
720 | } |
741 | } |
721 | } |
742 | } |
722 | |
743 | |
723 | static void |
744 | static void |
724 | ev_clear (W w) |
745 | ev_clear_pending (W w) |
725 | { |
746 | { |
726 | if (w->pending) |
747 | if (w->pending) |
727 | { |
748 | { |
728 | pendings [w->pending - 1].w = 0; |
749 | pendings [w->pending - 1].w = 0; |
729 | w->pending = 0; |
750 | w->pending = 0; |
… | |
… | |
745 | /*****************************************************************************/ |
766 | /*****************************************************************************/ |
746 | |
767 | |
747 | void |
768 | void |
748 | ev_io_start (struct ev_io *w) |
769 | ev_io_start (struct ev_io *w) |
749 | { |
770 | { |
750 | if (ev_is_active (w)) |
|
|
751 | return; |
|
|
752 | |
|
|
753 | int fd = w->fd; |
771 | int fd = w->fd; |
|
|
772 | |
|
|
773 | if (ev_is_active (w)) |
|
|
774 | return; |
|
|
775 | |
|
|
776 | assert (("ev_io_start called with negative fd", fd >= 0)); |
754 | |
777 | |
755 | ev_start ((W)w, 1); |
778 | ev_start ((W)w, 1); |
756 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
779 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
757 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
780 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
758 | |
781 | |
… | |
… | |
760 | } |
783 | } |
761 | |
784 | |
762 | void |
785 | void |
763 | ev_io_stop (struct ev_io *w) |
786 | ev_io_stop (struct ev_io *w) |
764 | { |
787 | { |
765 | ev_clear ((W)w); |
788 | ev_clear_pending ((W)w); |
766 | if (!ev_is_active (w)) |
789 | if (!ev_is_active (w)) |
767 | return; |
790 | return; |
768 | |
791 | |
769 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
792 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
770 | ev_stop ((W)w); |
793 | ev_stop ((W)w); |
… | |
… | |
778 | if (ev_is_active (w)) |
801 | if (ev_is_active (w)) |
779 | return; |
802 | return; |
780 | |
803 | |
781 | w->at += now; |
804 | w->at += now; |
782 | |
805 | |
783 | assert (("timer repeat value less than zero not allowed", w->repeat >= 0.)); |
806 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
784 | |
807 | |
785 | ev_start ((W)w, ++timercnt); |
808 | ev_start ((W)w, ++timercnt); |
786 | array_needsize (timers, timermax, timercnt, ); |
809 | array_needsize (timers, timermax, timercnt, ); |
787 | timers [timercnt - 1] = w; |
810 | timers [timercnt - 1] = w; |
788 | upheap ((WT *)timers, timercnt - 1); |
811 | upheap ((WT *)timers, timercnt - 1); |
789 | } |
812 | } |
790 | |
813 | |
791 | void |
814 | void |
792 | ev_timer_stop (struct ev_timer *w) |
815 | ev_timer_stop (struct ev_timer *w) |
793 | { |
816 | { |
794 | ev_clear ((W)w); |
817 | ev_clear_pending ((W)w); |
795 | if (!ev_is_active (w)) |
818 | if (!ev_is_active (w)) |
796 | return; |
819 | return; |
797 | |
820 | |
798 | if (w->active < timercnt--) |
821 | if (w->active < timercnt--) |
799 | { |
822 | { |
… | |
… | |
827 | ev_periodic_start (struct ev_periodic *w) |
850 | ev_periodic_start (struct ev_periodic *w) |
828 | { |
851 | { |
829 | if (ev_is_active (w)) |
852 | if (ev_is_active (w)) |
830 | return; |
853 | return; |
831 | |
854 | |
832 | assert (("periodic interval value less than zero not allowed", w->interval >= 0.)); |
855 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
833 | |
856 | |
834 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
857 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
835 | if (w->interval) |
858 | if (w->interval) |
836 | w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; |
859 | w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; |
837 | |
860 | |
… | |
… | |
842 | } |
865 | } |
843 | |
866 | |
844 | void |
867 | void |
845 | ev_periodic_stop (struct ev_periodic *w) |
868 | ev_periodic_stop (struct ev_periodic *w) |
846 | { |
869 | { |
847 | ev_clear ((W)w); |
870 | ev_clear_pending ((W)w); |
848 | if (!ev_is_active (w)) |
871 | if (!ev_is_active (w)) |
849 | return; |
872 | return; |
850 | |
873 | |
851 | if (w->active < periodiccnt--) |
874 | if (w->active < periodiccnt--) |
852 | { |
875 | { |
… | |
… | |
860 | void |
883 | void |
861 | ev_signal_start (struct ev_signal *w) |
884 | ev_signal_start (struct ev_signal *w) |
862 | { |
885 | { |
863 | if (ev_is_active (w)) |
886 | if (ev_is_active (w)) |
864 | return; |
887 | return; |
|
|
888 | |
|
|
889 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
865 | |
890 | |
866 | ev_start ((W)w, 1); |
891 | ev_start ((W)w, 1); |
867 | array_needsize (signals, signalmax, w->signum, signals_init); |
892 | array_needsize (signals, signalmax, w->signum, signals_init); |
868 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
893 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
869 | |
894 | |
… | |
… | |
878 | } |
903 | } |
879 | |
904 | |
880 | void |
905 | void |
881 | ev_signal_stop (struct ev_signal *w) |
906 | ev_signal_stop (struct ev_signal *w) |
882 | { |
907 | { |
883 | ev_clear ((W)w); |
908 | ev_clear_pending ((W)w); |
884 | if (!ev_is_active (w)) |
909 | if (!ev_is_active (w)) |
885 | return; |
910 | return; |
886 | |
911 | |
887 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
912 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
888 | ev_stop ((W)w); |
913 | ev_stop ((W)w); |
… | |
… | |
903 | } |
928 | } |
904 | |
929 | |
905 | void |
930 | void |
906 | ev_idle_stop (struct ev_idle *w) |
931 | ev_idle_stop (struct ev_idle *w) |
907 | { |
932 | { |
908 | ev_clear ((W)w); |
933 | ev_clear_pending ((W)w); |
909 | if (ev_is_active (w)) |
934 | if (ev_is_active (w)) |
910 | return; |
935 | return; |
911 | |
936 | |
912 | idles [w->active - 1] = idles [--idlecnt]; |
937 | idles [w->active - 1] = idles [--idlecnt]; |
913 | ev_stop ((W)w); |
938 | ev_stop ((W)w); |
… | |
… | |
925 | } |
950 | } |
926 | |
951 | |
927 | void |
952 | void |
928 | ev_prepare_stop (struct ev_prepare *w) |
953 | ev_prepare_stop (struct ev_prepare *w) |
929 | { |
954 | { |
930 | ev_clear ((W)w); |
955 | ev_clear_pending ((W)w); |
931 | if (ev_is_active (w)) |
956 | if (ev_is_active (w)) |
932 | return; |
957 | return; |
933 | |
958 | |
934 | prepares [w->active - 1] = prepares [--preparecnt]; |
959 | prepares [w->active - 1] = prepares [--preparecnt]; |
935 | ev_stop ((W)w); |
960 | ev_stop ((W)w); |
… | |
… | |
947 | } |
972 | } |
948 | |
973 | |
949 | void |
974 | void |
950 | ev_check_stop (struct ev_check *w) |
975 | ev_check_stop (struct ev_check *w) |
951 | { |
976 | { |
952 | ev_clear ((W)w); |
977 | ev_clear_pending ((W)w); |
953 | if (ev_is_active (w)) |
978 | if (ev_is_active (w)) |
954 | return; |
979 | return; |
955 | |
980 | |
956 | checks [w->active - 1] = checks [--checkcnt]; |
981 | checks [w->active - 1] = checks [--checkcnt]; |
957 | ev_stop ((W)w); |
982 | ev_stop ((W)w); |
… | |
… | |
968 | } |
993 | } |
969 | |
994 | |
970 | void |
995 | void |
971 | ev_child_stop (struct ev_child *w) |
996 | ev_child_stop (struct ev_child *w) |
972 | { |
997 | { |
973 | ev_clear ((W)w); |
998 | ev_clear_pending ((W)w); |
974 | if (ev_is_active (w)) |
999 | if (ev_is_active (w)) |
975 | return; |
1000 | return; |
976 | |
1001 | |
977 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1002 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
978 | ev_stop ((W)w); |
1003 | ev_stop ((W)w); |
… | |
… | |
1017 | ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1042 | ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1018 | { |
1043 | { |
1019 | struct ev_once *once = malloc (sizeof (struct ev_once)); |
1044 | struct ev_once *once = malloc (sizeof (struct ev_once)); |
1020 | |
1045 | |
1021 | if (!once) |
1046 | if (!once) |
1022 | cb (EV_ERROR, arg); |
1047 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
1023 | else |
1048 | else |
1024 | { |
1049 | { |
1025 | once->cb = cb; |
1050 | once->cb = cb; |
1026 | once->arg = arg; |
1051 | once->arg = arg; |
1027 | |
1052 | |
1028 | ev_watcher_init (&once->io, once_cb_io); |
1053 | ev_watcher_init (&once->io, once_cb_io); |
1029 | |
|
|
1030 | if (fd >= 0) |
1054 | if (fd >= 0) |
1031 | { |
1055 | { |
1032 | ev_io_set (&once->io, fd, events); |
1056 | ev_io_set (&once->io, fd, events); |
1033 | ev_io_start (&once->io); |
1057 | ev_io_start (&once->io); |
1034 | } |
1058 | } |
1035 | |
1059 | |
1036 | ev_watcher_init (&once->to, once_cb_to); |
1060 | ev_watcher_init (&once->to, once_cb_to); |
1037 | |
|
|
1038 | if (timeout >= 0.) |
1061 | if (timeout >= 0.) |
1039 | { |
1062 | { |
1040 | ev_timer_set (&once->to, timeout, 0.); |
1063 | ev_timer_set (&once->to, timeout, 0.); |
1041 | ev_timer_start (&once->to); |
1064 | ev_timer_start (&once->to); |
1042 | } |
1065 | } |