… | |
… | |
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 | ev_io_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 | |
… | |
… | |
308 | /*****************************************************************************/ |
323 | /*****************************************************************************/ |
309 | |
324 | |
310 | typedef struct |
325 | typedef struct |
311 | { |
326 | { |
312 | struct ev_signal *head; |
327 | struct ev_signal *head; |
313 | sig_atomic_t gotsig; |
328 | sig_atomic_t volatile gotsig; |
314 | } ANSIG; |
329 | } ANSIG; |
315 | |
330 | |
316 | static ANSIG *signals; |
331 | static ANSIG *signals; |
317 | static int signalmax; |
332 | static int signalmax; |
318 | |
333 | |
319 | static int sigpipe [2]; |
334 | static int sigpipe [2]; |
320 | static sig_atomic_t gotsig; |
335 | static sig_atomic_t volatile gotsig; |
321 | static struct ev_io sigev; |
336 | static struct ev_io sigev; |
322 | |
337 | |
323 | static void |
338 | static void |
324 | signals_init (ANSIG *base, int count) |
339 | signals_init (ANSIG *base, int count) |
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 |
… | |
… | |
337 | signals [signum - 1].gotsig = 1; |
353 | signals [signum - 1].gotsig = 1; |
338 | |
354 | |
339 | if (!gotsig) |
355 | if (!gotsig) |
340 | { |
356 | { |
341 | gotsig = 1; |
357 | gotsig = 1; |
342 | write (sigpipe [1], &gotsig, 1); |
358 | write (sigpipe [1], &signum, 1); |
343 | } |
359 | } |
344 | } |
360 | } |
345 | |
361 | |
346 | static void |
362 | static void |
347 | sigcb (struct ev_io *iow, int revents) |
363 | sigcb (struct ev_io *iow, int revents) |
348 | { |
364 | { |
349 | struct ev_signal *w; |
365 | struct ev_signal *w; |
350 | int sig; |
366 | int sig; |
351 | |
367 | |
|
|
368 | read (sigpipe [0], &revents, 1); |
352 | gotsig = 0; |
369 | gotsig = 0; |
353 | read (sigpipe [0], &revents, 1); |
|
|
354 | |
370 | |
355 | for (sig = signalmax; sig--; ) |
371 | for (sig = signalmax; sig--; ) |
356 | if (signals [sig].gotsig) |
372 | if (signals [sig].gotsig) |
357 | { |
373 | { |
358 | signals [sig].gotsig = 0; |
374 | signals [sig].gotsig = 0; |
… | |
… | |
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 | { |
… | |
… | |
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 | ev_io_stop (&sigev); |
513 | ev_io_stop (&sigev); |
… | |
… | |
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 | ev_timer_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 | ev_periodic_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) |
… | |
… | |
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; |
… | |
… | |
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 | |
… | |
… | |
760 | } |
778 | } |
761 | |
779 | |
762 | void |
780 | void |
763 | ev_io_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); |
… | |
… | |
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 | ev_timer_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 | { |
… | |
… | |
827 | ev_periodic_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 | |
… | |
… | |
842 | } |
860 | } |
843 | |
861 | |
844 | void |
862 | void |
845 | ev_periodic_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 | { |
… | |
… | |
860 | void |
878 | void |
861 | ev_signal_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 | |
… | |
… | |
878 | } |
898 | } |
879 | |
899 | |
880 | void |
900 | void |
881 | ev_signal_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); |
… | |
… | |
903 | } |
923 | } |
904 | |
924 | |
905 | void |
925 | void |
906 | ev_idle_stop (struct ev_idle *w) |
926 | ev_idle_stop (struct ev_idle *w) |
907 | { |
927 | { |
908 | ev_clear ((W)w); |
928 | ev_clear_pending ((W)w); |
909 | if (ev_is_active (w)) |
929 | if (ev_is_active (w)) |
910 | return; |
930 | return; |
911 | |
931 | |
912 | idles [w->active - 1] = idles [--idlecnt]; |
932 | idles [w->active - 1] = idles [--idlecnt]; |
913 | ev_stop ((W)w); |
933 | ev_stop ((W)w); |
… | |
… | |
925 | } |
945 | } |
926 | |
946 | |
927 | void |
947 | void |
928 | ev_prepare_stop (struct ev_prepare *w) |
948 | ev_prepare_stop (struct ev_prepare *w) |
929 | { |
949 | { |
930 | ev_clear ((W)w); |
950 | ev_clear_pending ((W)w); |
931 | if (ev_is_active (w)) |
951 | if (ev_is_active (w)) |
932 | return; |
952 | return; |
933 | |
953 | |
934 | prepares [w->active - 1] = prepares [--preparecnt]; |
954 | prepares [w->active - 1] = prepares [--preparecnt]; |
935 | ev_stop ((W)w); |
955 | ev_stop ((W)w); |
… | |
… | |
947 | } |
967 | } |
948 | |
968 | |
949 | void |
969 | void |
950 | ev_check_stop (struct ev_check *w) |
970 | ev_check_stop (struct ev_check *w) |
951 | { |
971 | { |
952 | ev_clear ((W)w); |
972 | ev_clear_pending ((W)w); |
953 | if (ev_is_active (w)) |
973 | if (ev_is_active (w)) |
954 | return; |
974 | return; |
955 | |
975 | |
956 | checks [w->active - 1] = checks [--checkcnt]; |
976 | checks [w->active - 1] = checks [--checkcnt]; |
957 | ev_stop ((W)w); |
977 | ev_stop ((W)w); |
… | |
… | |
968 | } |
988 | } |
969 | |
989 | |
970 | void |
990 | void |
971 | ev_child_stop (struct ev_child *w) |
991 | ev_child_stop (struct ev_child *w) |
972 | { |
992 | { |
973 | ev_clear ((W)w); |
993 | ev_clear_pending ((W)w); |
974 | if (ev_is_active (w)) |
994 | if (ev_is_active (w)) |
975 | return; |
995 | return; |
976 | |
996 | |
977 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
997 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
978 | ev_stop ((W)w); |
998 | ev_stop ((W)w); |
… | |
… | |
1017 | 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) |
1018 | { |
1038 | { |
1019 | struct ev_once *once = malloc (sizeof (struct ev_once)); |
1039 | struct ev_once *once = malloc (sizeof (struct ev_once)); |
1020 | |
1040 | |
1021 | if (!once) |
1041 | if (!once) |
1022 | cb (EV_ERROR, arg); |
1042 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
1023 | else |
1043 | else |
1024 | { |
1044 | { |
1025 | once->cb = cb; |
1045 | once->cb = cb; |
1026 | once->arg = arg; |
1046 | once->arg = arg; |
1027 | |
1047 | |
1028 | ev_watcher_init (&once->io, once_cb_io); |
1048 | ev_watcher_init (&once->io, once_cb_io); |
1029 | |
|
|
1030 | if (fd >= 0) |
1049 | if (fd >= 0) |
1031 | { |
1050 | { |
1032 | ev_io_set (&once->io, fd, events); |
1051 | ev_io_set (&once->io, fd, events); |
1033 | ev_io_start (&once->io); |
1052 | ev_io_start (&once->io); |
1034 | } |
1053 | } |
1035 | |
1054 | |
1036 | ev_watcher_init (&once->to, once_cb_to); |
1055 | ev_watcher_init (&once->to, once_cb_to); |
1037 | |
|
|
1038 | if (timeout >= 0.) |
1056 | if (timeout >= 0.) |
1039 | { |
1057 | { |
1040 | ev_timer_set (&once->to, timeout, 0.); |
1058 | ev_timer_set (&once->to, timeout, 0.); |
1041 | ev_timer_start (&once->to); |
1059 | ev_timer_start (&once->to); |
1042 | } |
1060 | } |