… | |
… | |
26 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
26 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
27 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
28 | * (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 |
29 | * 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. |
30 | */ |
30 | */ |
|
|
31 | |
|
|
32 | #ifdef __cplusplus |
|
|
33 | extern "C" { |
|
|
34 | #endif |
|
|
35 | |
31 | #ifndef EV_STANDALONE |
36 | #ifndef EV_STANDALONE |
32 | # include "config.h" |
37 | # include "config.h" |
33 | |
38 | |
34 | # if HAVE_CLOCK_GETTIME |
39 | # if HAVE_CLOCK_GETTIME |
35 | # define EV_USE_MONOTONIC 1 |
40 | # define EV_USE_MONOTONIC 1 |
… | |
… | |
46 | |
51 | |
47 | # if HAVE_EPOLL && HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H |
52 | # if HAVE_EPOLL && HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H |
48 | # define EV_USE_EPOLL 1 |
53 | # define EV_USE_EPOLL 1 |
49 | # endif |
54 | # endif |
50 | |
55 | |
51 | # if HAVE_KQUEUE && HAVE_WORKING_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H |
56 | # if HAVE_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H |
52 | # define EV_USE_KQUEUE 1 |
57 | # define EV_USE_KQUEUE 1 |
53 | # endif |
58 | # endif |
54 | |
59 | |
55 | #endif |
60 | #endif |
56 | |
61 | |
57 | #include <math.h> |
62 | #include <math.h> |
58 | #include <stdlib.h> |
63 | #include <stdlib.h> |
59 | #include <unistd.h> |
|
|
60 | #include <fcntl.h> |
64 | #include <fcntl.h> |
61 | #include <signal.h> |
|
|
62 | #include <stddef.h> |
65 | #include <stddef.h> |
63 | |
66 | |
64 | #include <stdio.h> |
67 | #include <stdio.h> |
65 | |
68 | |
66 | #include <assert.h> |
69 | #include <assert.h> |
67 | #include <errno.h> |
70 | #include <errno.h> |
68 | #include <sys/types.h> |
71 | #include <sys/types.h> |
|
|
72 | #include <time.h> |
|
|
73 | |
|
|
74 | #include <signal.h> |
|
|
75 | |
69 | #ifndef WIN32 |
76 | #ifndef WIN32 |
|
|
77 | # include <unistd.h> |
|
|
78 | # include <sys/time.h> |
70 | # include <sys/wait.h> |
79 | # include <sys/wait.h> |
71 | #endif |
80 | #endif |
72 | #include <sys/time.h> |
|
|
73 | #include <time.h> |
|
|
74 | |
|
|
75 | /**/ |
81 | /**/ |
76 | |
82 | |
77 | #ifndef EV_USE_MONOTONIC |
83 | #ifndef EV_USE_MONOTONIC |
78 | # define EV_USE_MONOTONIC 1 |
84 | # define EV_USE_MONOTONIC 1 |
79 | #endif |
85 | #endif |
… | |
… | |
94 | # define EV_USE_KQUEUE 0 |
100 | # define EV_USE_KQUEUE 0 |
95 | #endif |
101 | #endif |
96 | |
102 | |
97 | #ifndef EV_USE_WIN32 |
103 | #ifndef EV_USE_WIN32 |
98 | # ifdef WIN32 |
104 | # ifdef WIN32 |
|
|
105 | # define EV_USE_WIN32 0 /* it does not exist, use select */ |
|
|
106 | # undef EV_USE_SELECT |
99 | # define EV_USE_WIN32 1 |
107 | # define EV_USE_SELECT 1 |
100 | # else |
108 | # else |
101 | # define EV_USE_WIN32 0 |
109 | # define EV_USE_WIN32 0 |
102 | # endif |
110 | # endif |
103 | #endif |
111 | #endif |
104 | |
112 | |
… | |
… | |
123 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
131 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
124 | #define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detect time jumps) */ |
132 | #define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detect time jumps) */ |
125 | #define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */ |
133 | #define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */ |
126 | /*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */ |
134 | /*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */ |
127 | |
135 | |
|
|
136 | #ifdef EV_H |
|
|
137 | # include EV_H |
|
|
138 | #else |
128 | #include "ev.h" |
139 | # include "ev.h" |
|
|
140 | #endif |
129 | |
141 | |
130 | #if __GNUC__ >= 3 |
142 | #if __GNUC__ >= 3 |
131 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
143 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
132 | # define inline inline |
144 | # define inline inline |
133 | #else |
145 | #else |
… | |
… | |
145 | typedef struct ev_watcher_list *WL; |
157 | typedef struct ev_watcher_list *WL; |
146 | typedef struct ev_watcher_time *WT; |
158 | typedef struct ev_watcher_time *WT; |
147 | |
159 | |
148 | static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
160 | static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
149 | |
161 | |
150 | #if WIN32 |
162 | #include "ev_win32.c" |
151 | /* note: the comment below could not be substantiated, but what would I care */ |
|
|
152 | /* MSDN says this is required to handle SIGFPE */ |
|
|
153 | volatile double SIGFPE_REQ = 0.0f; |
|
|
154 | #endif |
|
|
155 | |
163 | |
156 | /*****************************************************************************/ |
164 | /*****************************************************************************/ |
157 | |
165 | |
158 | static void (*syserr_cb)(const char *msg); |
166 | static void (*syserr_cb)(const char *msg); |
159 | |
167 | |
… | |
… | |
216 | int events; |
224 | int events; |
217 | } ANPENDING; |
225 | } ANPENDING; |
218 | |
226 | |
219 | #if EV_MULTIPLICITY |
227 | #if EV_MULTIPLICITY |
220 | |
228 | |
221 | struct ev_loop |
229 | struct ev_loop |
222 | { |
230 | { |
|
|
231 | ev_tstamp ev_rt_now; |
223 | # define VAR(name,decl) decl; |
232 | #define VAR(name,decl) decl; |
224 | # include "ev_vars.h" |
233 | #include "ev_vars.h" |
225 | }; |
|
|
226 | # undef VAR |
234 | #undef VAR |
|
|
235 | }; |
227 | # include "ev_wrap.h" |
236 | #include "ev_wrap.h" |
|
|
237 | |
|
|
238 | struct ev_loop default_loop_struct; |
|
|
239 | static struct ev_loop *default_loop; |
228 | |
240 | |
229 | #else |
241 | #else |
230 | |
242 | |
|
|
243 | ev_tstamp ev_rt_now; |
231 | # define VAR(name,decl) static decl; |
244 | #define VAR(name,decl) static decl; |
232 | # include "ev_vars.h" |
245 | #include "ev_vars.h" |
233 | # undef VAR |
246 | #undef VAR |
|
|
247 | |
|
|
248 | static int default_loop; |
234 | |
249 | |
235 | #endif |
250 | #endif |
236 | |
251 | |
237 | /*****************************************************************************/ |
252 | /*****************************************************************************/ |
238 | |
253 | |
239 | inline ev_tstamp |
254 | ev_tstamp |
240 | ev_time (void) |
255 | ev_time (void) |
241 | { |
256 | { |
242 | #if EV_USE_REALTIME |
257 | #if EV_USE_REALTIME |
243 | struct timespec ts; |
258 | struct timespec ts; |
244 | clock_gettime (CLOCK_REALTIME, &ts); |
259 | clock_gettime (CLOCK_REALTIME, &ts); |
… | |
… | |
263 | #endif |
278 | #endif |
264 | |
279 | |
265 | return ev_time (); |
280 | return ev_time (); |
266 | } |
281 | } |
267 | |
282 | |
|
|
283 | #if EV_MULTIPLICITY |
268 | ev_tstamp |
284 | ev_tstamp |
269 | ev_now (EV_P) |
285 | ev_now (EV_P) |
270 | { |
286 | { |
271 | return rt_now; |
287 | return ev_rt_now; |
272 | } |
288 | } |
|
|
289 | #endif |
273 | |
290 | |
274 | #define array_roundsize(base,n) ((n) | 4 & ~3) |
291 | #define array_roundsize(type,n) ((n) | 4 & ~3) |
275 | |
292 | |
276 | #define array_needsize(base,cur,cnt,init) \ |
293 | #define array_needsize(type,base,cur,cnt,init) \ |
277 | if (expect_false ((cnt) > cur)) \ |
294 | if (expect_false ((cnt) > cur)) \ |
278 | { \ |
295 | { \ |
279 | int newcnt = cur; \ |
296 | int newcnt = cur; \ |
280 | do \ |
297 | do \ |
281 | { \ |
298 | { \ |
282 | newcnt = array_roundsize (base, newcnt << 1); \ |
299 | newcnt = array_roundsize (type, newcnt << 1); \ |
283 | } \ |
300 | } \ |
284 | while ((cnt) > newcnt); \ |
301 | while ((cnt) > newcnt); \ |
285 | \ |
302 | \ |
286 | base = ev_realloc (base, sizeof (*base) * (newcnt)); \ |
303 | base = (type *)ev_realloc (base, sizeof (type) * (newcnt));\ |
287 | init (base + cur, newcnt - cur); \ |
304 | init (base + cur, newcnt - cur); \ |
288 | cur = newcnt; \ |
305 | cur = newcnt; \ |
289 | } |
306 | } |
290 | |
307 | |
291 | #define array_slim(stem) \ |
308 | #define array_slim(type,stem) \ |
292 | if (stem ## max < array_roundsize (stem ## cnt >> 2)) \ |
309 | if (stem ## max < array_roundsize (stem ## cnt >> 2)) \ |
293 | { \ |
310 | { \ |
294 | stem ## max = array_roundsize (stem ## cnt >> 1); \ |
311 | stem ## max = array_roundsize (stem ## cnt >> 1); \ |
295 | base = ev_realloc (base, sizeof (*base) * (stem ## max)); \ |
312 | base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\ |
296 | fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ |
313 | fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ |
297 | } |
314 | } |
|
|
315 | |
|
|
316 | /* microsoft's pseudo-c is quite far from C as the rest of the world and the standard knows it */ |
|
|
317 | /* bringing us everlasting joy in form of stupid extra macros that are not required in C */ |
|
|
318 | #define array_free_microshit(stem) \ |
|
|
319 | ev_free (stem ## s); stem ## cnt = stem ## max = 0; |
298 | |
320 | |
299 | #define array_free(stem, idx) \ |
321 | #define array_free(stem, idx) \ |
300 | ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; |
322 | ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; |
301 | |
323 | |
302 | /*****************************************************************************/ |
324 | /*****************************************************************************/ |
… | |
… | |
312 | |
334 | |
313 | ++base; |
335 | ++base; |
314 | } |
336 | } |
315 | } |
337 | } |
316 | |
338 | |
317 | static void |
339 | void |
318 | event (EV_P_ W w, int events) |
340 | ev_feed_event (EV_P_ void *w, int revents) |
319 | { |
341 | { |
|
|
342 | W w_ = (W)w; |
|
|
343 | |
320 | if (w->pending) |
344 | if (w_->pending) |
321 | { |
345 | { |
322 | pendings [ABSPRI (w)][w->pending - 1].events |= events; |
346 | pendings [ABSPRI (w_)][w_->pending - 1].events |= revents; |
323 | return; |
347 | return; |
324 | } |
348 | } |
325 | |
349 | |
326 | w->pending = ++pendingcnt [ABSPRI (w)]; |
350 | w_->pending = ++pendingcnt [ABSPRI (w_)]; |
327 | array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], ); |
351 | array_needsize (ANPENDING, pendings [ABSPRI (w_)], pendingmax [ABSPRI (w_)], pendingcnt [ABSPRI (w_)], (void)); |
328 | pendings [ABSPRI (w)][w->pending - 1].w = w; |
352 | pendings [ABSPRI (w_)][w_->pending - 1].w = w_; |
329 | pendings [ABSPRI (w)][w->pending - 1].events = events; |
353 | pendings [ABSPRI (w_)][w_->pending - 1].events = revents; |
330 | } |
354 | } |
331 | |
355 | |
332 | static void |
356 | static void |
333 | queue_events (EV_P_ W *events, int eventcnt, int type) |
357 | queue_events (EV_P_ W *events, int eventcnt, int type) |
334 | { |
358 | { |
335 | int i; |
359 | int i; |
336 | |
360 | |
337 | for (i = 0; i < eventcnt; ++i) |
361 | for (i = 0; i < eventcnt; ++i) |
338 | event (EV_A_ events [i], type); |
362 | ev_feed_event (EV_A_ events [i], type); |
339 | } |
363 | } |
340 | |
364 | |
341 | static void |
365 | inline void |
342 | fd_event (EV_P_ int fd, int events) |
366 | fd_event (EV_P_ int fd, int revents) |
343 | { |
367 | { |
344 | ANFD *anfd = anfds + fd; |
368 | ANFD *anfd = anfds + fd; |
345 | struct ev_io *w; |
369 | struct ev_io *w; |
346 | |
370 | |
347 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
371 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
348 | { |
372 | { |
349 | int ev = w->events & events; |
373 | int ev = w->events & revents; |
350 | |
374 | |
351 | if (ev) |
375 | if (ev) |
352 | event (EV_A_ (W)w, ev); |
376 | ev_feed_event (EV_A_ (W)w, ev); |
353 | } |
377 | } |
|
|
378 | } |
|
|
379 | |
|
|
380 | void |
|
|
381 | ev_feed_fd_event (EV_P_ int fd, int revents) |
|
|
382 | { |
|
|
383 | fd_event (EV_A_ fd, revents); |
354 | } |
384 | } |
355 | |
385 | |
356 | /*****************************************************************************/ |
386 | /*****************************************************************************/ |
357 | |
387 | |
358 | static void |
388 | static void |
… | |
… | |
387 | return; |
417 | return; |
388 | |
418 | |
389 | anfds [fd].reify = 1; |
419 | anfds [fd].reify = 1; |
390 | |
420 | |
391 | ++fdchangecnt; |
421 | ++fdchangecnt; |
392 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
422 | array_needsize (int, fdchanges, fdchangemax, fdchangecnt, (void)); |
393 | fdchanges [fdchangecnt - 1] = fd; |
423 | fdchanges [fdchangecnt - 1] = fd; |
394 | } |
424 | } |
395 | |
425 | |
396 | static void |
426 | static void |
397 | fd_kill (EV_P_ int fd) |
427 | fd_kill (EV_P_ int fd) |
… | |
… | |
399 | struct ev_io *w; |
429 | struct ev_io *w; |
400 | |
430 | |
401 | while ((w = (struct ev_io *)anfds [fd].head)) |
431 | while ((w = (struct ev_io *)anfds [fd].head)) |
402 | { |
432 | { |
403 | ev_io_stop (EV_A_ w); |
433 | ev_io_stop (EV_A_ w); |
404 | event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); |
434 | ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); |
405 | } |
435 | } |
|
|
436 | } |
|
|
437 | |
|
|
438 | static int |
|
|
439 | fd_valid (int fd) |
|
|
440 | { |
|
|
441 | #ifdef WIN32 |
|
|
442 | return !!win32_get_osfhandle (fd); |
|
|
443 | #else |
|
|
444 | return fcntl (fd, F_GETFD) != -1; |
|
|
445 | #endif |
406 | } |
446 | } |
407 | |
447 | |
408 | /* called on EBADF to verify fds */ |
448 | /* called on EBADF to verify fds */ |
409 | static void |
449 | static void |
410 | fd_ebadf (EV_P) |
450 | fd_ebadf (EV_P) |
411 | { |
451 | { |
412 | int fd; |
452 | int fd; |
413 | |
453 | |
414 | for (fd = 0; fd < anfdmax; ++fd) |
454 | for (fd = 0; fd < anfdmax; ++fd) |
415 | if (anfds [fd].events) |
455 | if (anfds [fd].events) |
416 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
456 | if (!fd_valid (fd) == -1 && errno == EBADF) |
417 | fd_kill (EV_A_ fd); |
457 | fd_kill (EV_A_ fd); |
418 | } |
458 | } |
419 | |
459 | |
420 | /* called on ENOMEM in select/poll to kill some fds and retry */ |
460 | /* called on ENOMEM in select/poll to kill some fds and retry */ |
421 | static void |
461 | static void |
… | |
… | |
487 | |
527 | |
488 | heap [k] = w; |
528 | heap [k] = w; |
489 | ((W)heap [k])->active = k + 1; |
529 | ((W)heap [k])->active = k + 1; |
490 | } |
530 | } |
491 | |
531 | |
|
|
532 | inline void |
|
|
533 | adjustheap (WT *heap, int N, int k, ev_tstamp at) |
|
|
534 | { |
|
|
535 | ev_tstamp old_at = heap [k]->at; |
|
|
536 | heap [k]->at = at; |
|
|
537 | |
|
|
538 | if (old_at < at) |
|
|
539 | downheap (heap, N, k); |
|
|
540 | else |
|
|
541 | upheap (heap, k); |
|
|
542 | } |
|
|
543 | |
492 | /*****************************************************************************/ |
544 | /*****************************************************************************/ |
493 | |
545 | |
494 | typedef struct |
546 | typedef struct |
495 | { |
547 | { |
496 | WL head; |
548 | WL head; |
… | |
… | |
527 | |
579 | |
528 | if (!gotsig) |
580 | if (!gotsig) |
529 | { |
581 | { |
530 | int old_errno = errno; |
582 | int old_errno = errno; |
531 | gotsig = 1; |
583 | gotsig = 1; |
|
|
584 | #ifdef WIN32 |
|
|
585 | send (sigpipe [1], &signum, 1, MSG_DONTWAIT); |
|
|
586 | #else |
532 | write (sigpipe [1], &signum, 1); |
587 | write (sigpipe [1], &signum, 1); |
|
|
588 | #endif |
533 | errno = old_errno; |
589 | errno = old_errno; |
534 | } |
590 | } |
535 | } |
591 | } |
536 | |
592 | |
|
|
593 | void |
|
|
594 | ev_feed_signal_event (EV_P_ int signum) |
|
|
595 | { |
|
|
596 | WL w; |
|
|
597 | |
|
|
598 | #if EV_MULTIPLICITY |
|
|
599 | assert (("feeding signal events is only supported in the default loop", loop == default_loop)); |
|
|
600 | #endif |
|
|
601 | |
|
|
602 | --signum; |
|
|
603 | |
|
|
604 | if (signum < 0 || signum >= signalmax) |
|
|
605 | return; |
|
|
606 | |
|
|
607 | signals [signum].gotsig = 0; |
|
|
608 | |
|
|
609 | for (w = signals [signum].head; w; w = w->next) |
|
|
610 | ev_feed_event (EV_A_ (W)w, EV_SIGNAL); |
|
|
611 | } |
|
|
612 | |
537 | static void |
613 | static void |
538 | sigcb (EV_P_ struct ev_io *iow, int revents) |
614 | sigcb (EV_P_ struct ev_io *iow, int revents) |
539 | { |
615 | { |
540 | WL w; |
|
|
541 | int signum; |
616 | int signum; |
542 | |
617 | |
|
|
618 | #ifdef WIN32 |
|
|
619 | recv (sigpipe [0], &revents, 1, MSG_DONTWAIT); |
|
|
620 | #else |
543 | read (sigpipe [0], &revents, 1); |
621 | read (sigpipe [0], &revents, 1); |
|
|
622 | #endif |
544 | gotsig = 0; |
623 | gotsig = 0; |
545 | |
624 | |
546 | for (signum = signalmax; signum--; ) |
625 | for (signum = signalmax; signum--; ) |
547 | if (signals [signum].gotsig) |
626 | if (signals [signum].gotsig) |
548 | { |
627 | ev_feed_signal_event (EV_A_ signum + 1); |
549 | signals [signum].gotsig = 0; |
|
|
550 | |
|
|
551 | for (w = signals [signum].head; w; w = w->next) |
|
|
552 | event (EV_A_ (W)w, EV_SIGNAL); |
|
|
553 | } |
|
|
554 | } |
628 | } |
555 | |
629 | |
556 | static void |
630 | static void |
557 | siginit (EV_P) |
631 | siginit (EV_P) |
558 | { |
632 | { |
… | |
… | |
570 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
644 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
571 | } |
645 | } |
572 | |
646 | |
573 | /*****************************************************************************/ |
647 | /*****************************************************************************/ |
574 | |
648 | |
|
|
649 | static struct ev_child *childs [PID_HASHSIZE]; |
|
|
650 | |
575 | #ifndef WIN32 |
651 | #ifndef WIN32 |
576 | |
652 | |
577 | static struct ev_child *childs [PID_HASHSIZE]; |
|
|
578 | static struct ev_signal childev; |
653 | static struct ev_signal childev; |
579 | |
654 | |
580 | #ifndef WCONTINUED |
655 | #ifndef WCONTINUED |
581 | # define WCONTINUED 0 |
656 | # define WCONTINUED 0 |
582 | #endif |
657 | #endif |
… | |
… | |
590 | if (w->pid == pid || !w->pid) |
665 | if (w->pid == pid || !w->pid) |
591 | { |
666 | { |
592 | ev_priority (w) = ev_priority (sw); /* need to do it *now* */ |
667 | ev_priority (w) = ev_priority (sw); /* need to do it *now* */ |
593 | w->rpid = pid; |
668 | w->rpid = pid; |
594 | w->rstatus = status; |
669 | w->rstatus = status; |
595 | event (EV_A_ (W)w, EV_CHILD); |
670 | ev_feed_event (EV_A_ (W)w, EV_CHILD); |
596 | } |
671 | } |
597 | } |
672 | } |
598 | |
673 | |
599 | static void |
674 | static void |
600 | childcb (EV_P_ struct ev_signal *sw, int revents) |
675 | childcb (EV_P_ struct ev_signal *sw, int revents) |
… | |
… | |
602 | int pid, status; |
677 | int pid, status; |
603 | |
678 | |
604 | if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) |
679 | if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) |
605 | { |
680 | { |
606 | /* make sure we are called again until all childs have been reaped */ |
681 | /* make sure we are called again until all childs have been reaped */ |
607 | event (EV_A_ (W)sw, EV_SIGNAL); |
682 | ev_feed_event (EV_A_ (W)sw, EV_SIGNAL); |
608 | |
683 | |
609 | child_reap (EV_A_ sw, pid, pid, status); |
684 | child_reap (EV_A_ sw, pid, pid, status); |
610 | child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */ |
685 | child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */ |
611 | } |
686 | } |
612 | } |
687 | } |
… | |
… | |
669 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
744 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
670 | have_monotonic = 1; |
745 | have_monotonic = 1; |
671 | } |
746 | } |
672 | #endif |
747 | #endif |
673 | |
748 | |
674 | rt_now = ev_time (); |
749 | ev_rt_now = ev_time (); |
675 | mn_now = get_clock (); |
750 | mn_now = get_clock (); |
676 | now_floor = mn_now; |
751 | now_floor = mn_now; |
677 | rtmn_diff = rt_now - mn_now; |
752 | rtmn_diff = ev_rt_now - mn_now; |
678 | |
753 | |
679 | if (methods == EVMETHOD_AUTO) |
754 | if (methods == EVMETHOD_AUTO) |
680 | if (!enable_secure () && getenv ("LIBEV_METHODS")) |
755 | if (!enable_secure () && getenv ("LIBEV_METHODS")) |
681 | methods = atoi (getenv ("LIBEV_METHODS")); |
756 | methods = atoi (getenv ("LIBEV_METHODS")); |
682 | else |
757 | else |
… | |
… | |
697 | #endif |
772 | #endif |
698 | #if EV_USE_SELECT |
773 | #if EV_USE_SELECT |
699 | if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); |
774 | if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); |
700 | #endif |
775 | #endif |
701 | |
776 | |
702 | ev_watcher_init (&sigev, sigcb); |
777 | ev_init (&sigev, sigcb); |
703 | ev_set_priority (&sigev, EV_MAXPRI); |
778 | ev_set_priority (&sigev, EV_MAXPRI); |
704 | } |
779 | } |
705 | } |
780 | } |
706 | |
781 | |
707 | void |
782 | void |
… | |
… | |
726 | #endif |
801 | #endif |
727 | |
802 | |
728 | for (i = NUMPRI; i--; ) |
803 | for (i = NUMPRI; i--; ) |
729 | array_free (pending, [i]); |
804 | array_free (pending, [i]); |
730 | |
805 | |
|
|
806 | /* have to use the microsoft-never-gets-it-right macro */ |
731 | array_free (fdchange, ); |
807 | array_free_microshit (fdchange); |
732 | array_free (timer, ); |
808 | array_free_microshit (timer); |
733 | array_free (periodic, ); |
809 | array_free_microshit (periodic); |
734 | array_free (idle, ); |
810 | array_free_microshit (idle); |
735 | array_free (prepare, ); |
811 | array_free_microshit (prepare); |
736 | array_free (check, ); |
812 | array_free_microshit (check); |
737 | |
813 | |
738 | method = 0; |
814 | method = 0; |
739 | } |
815 | } |
740 | |
816 | |
741 | static void |
817 | static void |
… | |
… | |
796 | } |
872 | } |
797 | |
873 | |
798 | #endif |
874 | #endif |
799 | |
875 | |
800 | #if EV_MULTIPLICITY |
876 | #if EV_MULTIPLICITY |
801 | struct ev_loop default_loop_struct; |
|
|
802 | static struct ev_loop *default_loop; |
|
|
803 | |
|
|
804 | struct ev_loop * |
877 | struct ev_loop * |
805 | #else |
878 | #else |
806 | static int default_loop; |
|
|
807 | |
|
|
808 | int |
879 | int |
809 | #endif |
880 | #endif |
810 | ev_default_loop (int methods) |
881 | ev_default_loop (int methods) |
811 | { |
882 | { |
812 | if (sigpipe [0] == sigpipe [1]) |
883 | if (sigpipe [0] == sigpipe [1]) |
… | |
… | |
846 | { |
917 | { |
847 | #if EV_MULTIPLICITY |
918 | #if EV_MULTIPLICITY |
848 | struct ev_loop *loop = default_loop; |
919 | struct ev_loop *loop = default_loop; |
849 | #endif |
920 | #endif |
850 | |
921 | |
|
|
922 | #ifndef WIN32 |
851 | ev_ref (EV_A); /* child watcher */ |
923 | ev_ref (EV_A); /* child watcher */ |
852 | ev_signal_stop (EV_A_ &childev); |
924 | ev_signal_stop (EV_A_ &childev); |
|
|
925 | #endif |
853 | |
926 | |
854 | ev_ref (EV_A); /* signal watcher */ |
927 | ev_ref (EV_A); /* signal watcher */ |
855 | ev_io_stop (EV_A_ &sigev); |
928 | ev_io_stop (EV_A_ &sigev); |
856 | |
929 | |
857 | close (sigpipe [0]); sigpipe [0] = 0; |
930 | close (sigpipe [0]); sigpipe [0] = 0; |
… | |
… | |
870 | if (method) |
943 | if (method) |
871 | postfork = 1; |
944 | postfork = 1; |
872 | } |
945 | } |
873 | |
946 | |
874 | /*****************************************************************************/ |
947 | /*****************************************************************************/ |
|
|
948 | |
|
|
949 | static int |
|
|
950 | any_pending (EV_P) |
|
|
951 | { |
|
|
952 | int pri; |
|
|
953 | |
|
|
954 | for (pri = NUMPRI; pri--; ) |
|
|
955 | if (pendingcnt [pri]) |
|
|
956 | return 1; |
|
|
957 | |
|
|
958 | return 0; |
|
|
959 | } |
875 | |
960 | |
876 | static void |
961 | static void |
877 | call_pending (EV_P) |
962 | call_pending (EV_P) |
878 | { |
963 | { |
879 | int pri; |
964 | int pri; |
… | |
… | |
884 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
969 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
885 | |
970 | |
886 | if (p->w) |
971 | if (p->w) |
887 | { |
972 | { |
888 | p->w->pending = 0; |
973 | p->w->pending = 0; |
889 | p->w->cb (EV_A_ p->w, p->events); |
974 | EV_CB_INVOKE (p->w, p->events); |
890 | } |
975 | } |
891 | } |
976 | } |
892 | } |
977 | } |
893 | |
978 | |
894 | static void |
979 | static void |
… | |
… | |
902 | |
987 | |
903 | /* first reschedule or stop timer */ |
988 | /* first reschedule or stop timer */ |
904 | if (w->repeat) |
989 | if (w->repeat) |
905 | { |
990 | { |
906 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
991 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
|
|
992 | |
907 | ((WT)w)->at = mn_now + w->repeat; |
993 | ((WT)w)->at += w->repeat; |
|
|
994 | if (((WT)w)->at < mn_now) |
|
|
995 | ((WT)w)->at = mn_now; |
|
|
996 | |
908 | downheap ((WT *)timers, timercnt, 0); |
997 | downheap ((WT *)timers, timercnt, 0); |
909 | } |
998 | } |
910 | else |
999 | else |
911 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
1000 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
912 | |
1001 | |
913 | event (EV_A_ (W)w, EV_TIMEOUT); |
1002 | ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); |
914 | } |
1003 | } |
915 | } |
1004 | } |
916 | |
1005 | |
917 | static void |
1006 | static void |
918 | periodics_reify (EV_P) |
1007 | periodics_reify (EV_P) |
919 | { |
1008 | { |
920 | while (periodiccnt && ((WT)periodics [0])->at <= rt_now) |
1009 | while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) |
921 | { |
1010 | { |
922 | struct ev_periodic *w = periodics [0]; |
1011 | struct ev_periodic *w = periodics [0]; |
923 | |
1012 | |
924 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
1013 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
925 | |
1014 | |
926 | /* first reschedule or stop timer */ |
1015 | /* first reschedule or stop timer */ |
927 | if (w->interval) |
1016 | if (w->reschedule_cb) |
928 | { |
1017 | { |
|
|
1018 | ev_tstamp at = ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + 0.0001); |
|
|
1019 | |
|
|
1020 | assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now)); |
|
|
1021 | downheap ((WT *)periodics, periodiccnt, 0); |
|
|
1022 | } |
|
|
1023 | else if (w->interval) |
|
|
1024 | { |
929 | ((WT)w)->at += floor ((rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval; |
1025 | ((WT)w)->at += floor ((ev_rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval; |
930 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > rt_now)); |
1026 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > ev_rt_now)); |
931 | downheap ((WT *)periodics, periodiccnt, 0); |
1027 | downheap ((WT *)periodics, periodiccnt, 0); |
932 | } |
1028 | } |
933 | else |
1029 | else |
934 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
1030 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
935 | |
1031 | |
936 | event (EV_A_ (W)w, EV_PERIODIC); |
1032 | ev_feed_event (EV_A_ (W)w, EV_PERIODIC); |
937 | } |
1033 | } |
938 | } |
1034 | } |
939 | |
1035 | |
940 | static void |
1036 | static void |
941 | periodics_reschedule (EV_P) |
1037 | periodics_reschedule (EV_P) |
… | |
… | |
945 | /* adjust periodics after time jump */ |
1041 | /* adjust periodics after time jump */ |
946 | for (i = 0; i < periodiccnt; ++i) |
1042 | for (i = 0; i < periodiccnt; ++i) |
947 | { |
1043 | { |
948 | struct ev_periodic *w = periodics [i]; |
1044 | struct ev_periodic *w = periodics [i]; |
949 | |
1045 | |
|
|
1046 | if (w->reschedule_cb) |
|
|
1047 | ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); |
950 | if (w->interval) |
1048 | else if (w->interval) |
951 | { |
|
|
952 | ev_tstamp diff = ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1049 | ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; |
953 | |
|
|
954 | if (fabs (diff) >= 1e-4) |
|
|
955 | { |
|
|
956 | ev_periodic_stop (EV_A_ w); |
|
|
957 | ev_periodic_start (EV_A_ w); |
|
|
958 | |
|
|
959 | i = 0; /* restart loop, inefficient, but time jumps should be rare */ |
|
|
960 | } |
|
|
961 | } |
|
|
962 | } |
1050 | } |
|
|
1051 | |
|
|
1052 | /* now rebuild the heap */ |
|
|
1053 | for (i = periodiccnt >> 1; i--; ) |
|
|
1054 | downheap ((WT *)periodics, periodiccnt, i); |
963 | } |
1055 | } |
964 | |
1056 | |
965 | inline int |
1057 | inline int |
966 | time_update_monotonic (EV_P) |
1058 | time_update_monotonic (EV_P) |
967 | { |
1059 | { |
968 | mn_now = get_clock (); |
1060 | mn_now = get_clock (); |
969 | |
1061 | |
970 | if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) |
1062 | if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) |
971 | { |
1063 | { |
972 | rt_now = rtmn_diff + mn_now; |
1064 | ev_rt_now = rtmn_diff + mn_now; |
973 | return 0; |
1065 | return 0; |
974 | } |
1066 | } |
975 | else |
1067 | else |
976 | { |
1068 | { |
977 | now_floor = mn_now; |
1069 | now_floor = mn_now; |
978 | rt_now = ev_time (); |
1070 | ev_rt_now = ev_time (); |
979 | return 1; |
1071 | return 1; |
980 | } |
1072 | } |
981 | } |
1073 | } |
982 | |
1074 | |
983 | static void |
1075 | static void |
… | |
… | |
992 | { |
1084 | { |
993 | ev_tstamp odiff = rtmn_diff; |
1085 | ev_tstamp odiff = rtmn_diff; |
994 | |
1086 | |
995 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
1087 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
996 | { |
1088 | { |
997 | rtmn_diff = rt_now - mn_now; |
1089 | rtmn_diff = ev_rt_now - mn_now; |
998 | |
1090 | |
999 | if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) |
1091 | if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) |
1000 | return; /* all is well */ |
1092 | return; /* all is well */ |
1001 | |
1093 | |
1002 | rt_now = ev_time (); |
1094 | ev_rt_now = ev_time (); |
1003 | mn_now = get_clock (); |
1095 | mn_now = get_clock (); |
1004 | now_floor = mn_now; |
1096 | now_floor = mn_now; |
1005 | } |
1097 | } |
1006 | |
1098 | |
1007 | periodics_reschedule (EV_A); |
1099 | periodics_reschedule (EV_A); |
… | |
… | |
1010 | } |
1102 | } |
1011 | } |
1103 | } |
1012 | else |
1104 | else |
1013 | #endif |
1105 | #endif |
1014 | { |
1106 | { |
1015 | rt_now = ev_time (); |
1107 | ev_rt_now = ev_time (); |
1016 | |
1108 | |
1017 | if (expect_false (mn_now > rt_now || mn_now < rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
1109 | if (expect_false (mn_now > ev_rt_now || mn_now < ev_rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
1018 | { |
1110 | { |
1019 | periodics_reschedule (EV_A); |
1111 | periodics_reschedule (EV_A); |
1020 | |
1112 | |
1021 | /* adjust timers. this is easy, as the offset is the same for all */ |
1113 | /* adjust timers. this is easy, as the offset is the same for all */ |
1022 | for (i = 0; i < timercnt; ++i) |
1114 | for (i = 0; i < timercnt; ++i) |
1023 | ((WT)timers [i])->at += rt_now - mn_now; |
1115 | ((WT)timers [i])->at += ev_rt_now - mn_now; |
1024 | } |
1116 | } |
1025 | |
1117 | |
1026 | mn_now = rt_now; |
1118 | mn_now = ev_rt_now; |
1027 | } |
1119 | } |
1028 | } |
1120 | } |
1029 | |
1121 | |
1030 | void |
1122 | void |
1031 | ev_ref (EV_P) |
1123 | ev_ref (EV_P) |
… | |
… | |
1063 | /* update fd-related kernel structures */ |
1155 | /* update fd-related kernel structures */ |
1064 | fd_reify (EV_A); |
1156 | fd_reify (EV_A); |
1065 | |
1157 | |
1066 | /* calculate blocking time */ |
1158 | /* calculate blocking time */ |
1067 | |
1159 | |
1068 | /* we only need this for !monotonic clockor timers, but as we basically |
1160 | /* we only need this for !monotonic clock or timers, but as we basically |
1069 | always have timers, we just calculate it always */ |
1161 | always have timers, we just calculate it always */ |
1070 | #if EV_USE_MONOTONIC |
1162 | #if EV_USE_MONOTONIC |
1071 | if (expect_true (have_monotonic)) |
1163 | if (expect_true (have_monotonic)) |
1072 | time_update_monotonic (EV_A); |
1164 | time_update_monotonic (EV_A); |
1073 | else |
1165 | else |
1074 | #endif |
1166 | #endif |
1075 | { |
1167 | { |
1076 | rt_now = ev_time (); |
1168 | ev_rt_now = ev_time (); |
1077 | mn_now = rt_now; |
1169 | mn_now = ev_rt_now; |
1078 | } |
1170 | } |
1079 | |
1171 | |
1080 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
1172 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
1081 | block = 0.; |
1173 | block = 0.; |
1082 | else |
1174 | else |
… | |
… | |
1089 | if (block > to) block = to; |
1181 | if (block > to) block = to; |
1090 | } |
1182 | } |
1091 | |
1183 | |
1092 | if (periodiccnt) |
1184 | if (periodiccnt) |
1093 | { |
1185 | { |
1094 | ev_tstamp to = ((WT)periodics [0])->at - rt_now + method_fudge; |
1186 | ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + method_fudge; |
1095 | if (block > to) block = to; |
1187 | if (block > to) block = to; |
1096 | } |
1188 | } |
1097 | |
1189 | |
1098 | if (block < 0.) block = 0.; |
1190 | if (block < 0.) block = 0.; |
1099 | } |
1191 | } |
1100 | |
1192 | |
1101 | method_poll (EV_A_ block); |
1193 | method_poll (EV_A_ block); |
1102 | |
1194 | |
1103 | /* update rt_now, do magic */ |
1195 | /* update ev_rt_now, do magic */ |
1104 | time_update (EV_A); |
1196 | time_update (EV_A); |
1105 | |
1197 | |
1106 | /* queue pending timers and reschedule them */ |
1198 | /* queue pending timers and reschedule them */ |
1107 | timers_reify (EV_A); /* relative timers called last */ |
1199 | timers_reify (EV_A); /* relative timers called last */ |
1108 | periodics_reify (EV_A); /* absolute timers called first */ |
1200 | periodics_reify (EV_A); /* absolute timers called first */ |
1109 | |
1201 | |
1110 | /* queue idle watchers unless io or timers are pending */ |
1202 | /* queue idle watchers unless io or timers are pending */ |
1111 | if (!pendingcnt) |
1203 | if (idlecnt && !any_pending (EV_A)) |
1112 | queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); |
1204 | queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); |
1113 | |
1205 | |
1114 | /* queue check watchers, to be executed first */ |
1206 | /* queue check watchers, to be executed first */ |
1115 | if (checkcnt) |
1207 | if (checkcnt) |
1116 | queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); |
1208 | queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); |
… | |
… | |
1191 | return; |
1283 | return; |
1192 | |
1284 | |
1193 | assert (("ev_io_start called with negative fd", fd >= 0)); |
1285 | assert (("ev_io_start called with negative fd", fd >= 0)); |
1194 | |
1286 | |
1195 | ev_start (EV_A_ (W)w, 1); |
1287 | ev_start (EV_A_ (W)w, 1); |
1196 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
1288 | array_needsize (ANFD, anfds, anfdmax, fd + 1, anfds_init); |
1197 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
1289 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
1198 | |
1290 | |
1199 | fd_change (EV_A_ fd); |
1291 | fd_change (EV_A_ fd); |
1200 | } |
1292 | } |
1201 | |
1293 | |
… | |
… | |
1204 | { |
1296 | { |
1205 | ev_clear_pending (EV_A_ (W)w); |
1297 | ev_clear_pending (EV_A_ (W)w); |
1206 | if (!ev_is_active (w)) |
1298 | if (!ev_is_active (w)) |
1207 | return; |
1299 | return; |
1208 | |
1300 | |
|
|
1301 | assert (("ev_io_start called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax)); |
|
|
1302 | |
1209 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
1303 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
1210 | ev_stop (EV_A_ (W)w); |
1304 | ev_stop (EV_A_ (W)w); |
1211 | |
1305 | |
1212 | fd_change (EV_A_ w->fd); |
1306 | fd_change (EV_A_ w->fd); |
1213 | } |
1307 | } |
… | |
… | |
1221 | ((WT)w)->at += mn_now; |
1315 | ((WT)w)->at += mn_now; |
1222 | |
1316 | |
1223 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
1317 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
1224 | |
1318 | |
1225 | ev_start (EV_A_ (W)w, ++timercnt); |
1319 | ev_start (EV_A_ (W)w, ++timercnt); |
1226 | array_needsize (timers, timermax, timercnt, ); |
1320 | array_needsize (struct ev_timer *, timers, timermax, timercnt, (void)); |
1227 | timers [timercnt - 1] = w; |
1321 | timers [timercnt - 1] = w; |
1228 | upheap ((WT *)timers, timercnt - 1); |
1322 | upheap ((WT *)timers, timercnt - 1); |
1229 | |
1323 | |
1230 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1324 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1231 | } |
1325 | } |
… | |
… | |
1243 | { |
1337 | { |
1244 | timers [((W)w)->active - 1] = timers [timercnt]; |
1338 | timers [((W)w)->active - 1] = timers [timercnt]; |
1245 | downheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1339 | downheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1246 | } |
1340 | } |
1247 | |
1341 | |
1248 | ((WT)w)->at = w->repeat; |
1342 | ((WT)w)->at -= mn_now; |
1249 | |
1343 | |
1250 | ev_stop (EV_A_ (W)w); |
1344 | ev_stop (EV_A_ (W)w); |
1251 | } |
1345 | } |
1252 | |
1346 | |
1253 | void |
1347 | void |
1254 | ev_timer_again (EV_P_ struct ev_timer *w) |
1348 | ev_timer_again (EV_P_ struct ev_timer *w) |
1255 | { |
1349 | { |
1256 | if (ev_is_active (w)) |
1350 | if (ev_is_active (w)) |
1257 | { |
1351 | { |
1258 | if (w->repeat) |
1352 | if (w->repeat) |
1259 | { |
|
|
1260 | ((WT)w)->at = mn_now + w->repeat; |
|
|
1261 | downheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1353 | adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1, mn_now + w->repeat); |
1262 | } |
|
|
1263 | else |
1354 | else |
1264 | ev_timer_stop (EV_A_ w); |
1355 | ev_timer_stop (EV_A_ w); |
1265 | } |
1356 | } |
1266 | else if (w->repeat) |
1357 | else if (w->repeat) |
1267 | ev_timer_start (EV_A_ w); |
1358 | ev_timer_start (EV_A_ w); |
… | |
… | |
1271 | ev_periodic_start (EV_P_ struct ev_periodic *w) |
1362 | ev_periodic_start (EV_P_ struct ev_periodic *w) |
1272 | { |
1363 | { |
1273 | if (ev_is_active (w)) |
1364 | if (ev_is_active (w)) |
1274 | return; |
1365 | return; |
1275 | |
1366 | |
|
|
1367 | if (w->reschedule_cb) |
|
|
1368 | ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); |
|
|
1369 | else if (w->interval) |
|
|
1370 | { |
1276 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
1371 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
1277 | |
|
|
1278 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1372 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1279 | if (w->interval) |
|
|
1280 | ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1373 | ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; |
|
|
1374 | } |
1281 | |
1375 | |
1282 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1376 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1283 | array_needsize (periodics, periodicmax, periodiccnt, ); |
1377 | array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void)); |
1284 | periodics [periodiccnt - 1] = w; |
1378 | periodics [periodiccnt - 1] = w; |
1285 | upheap ((WT *)periodics, periodiccnt - 1); |
1379 | upheap ((WT *)periodics, periodiccnt - 1); |
1286 | |
1380 | |
1287 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1381 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1288 | } |
1382 | } |
… | |
… | |
1304 | |
1398 | |
1305 | ev_stop (EV_A_ (W)w); |
1399 | ev_stop (EV_A_ (W)w); |
1306 | } |
1400 | } |
1307 | |
1401 | |
1308 | void |
1402 | void |
|
|
1403 | ev_periodic_again (EV_P_ struct ev_periodic *w) |
|
|
1404 | { |
|
|
1405 | /* TODO: use adjustheap and recalculation */ |
|
|
1406 | ev_periodic_stop (EV_A_ w); |
|
|
1407 | ev_periodic_start (EV_A_ w); |
|
|
1408 | } |
|
|
1409 | |
|
|
1410 | void |
1309 | ev_idle_start (EV_P_ struct ev_idle *w) |
1411 | ev_idle_start (EV_P_ struct ev_idle *w) |
1310 | { |
1412 | { |
1311 | if (ev_is_active (w)) |
1413 | if (ev_is_active (w)) |
1312 | return; |
1414 | return; |
1313 | |
1415 | |
1314 | ev_start (EV_A_ (W)w, ++idlecnt); |
1416 | ev_start (EV_A_ (W)w, ++idlecnt); |
1315 | array_needsize (idles, idlemax, idlecnt, ); |
1417 | array_needsize (struct ev_idle *, idles, idlemax, idlecnt, (void)); |
1316 | idles [idlecnt - 1] = w; |
1418 | idles [idlecnt - 1] = w; |
1317 | } |
1419 | } |
1318 | |
1420 | |
1319 | void |
1421 | void |
1320 | ev_idle_stop (EV_P_ struct ev_idle *w) |
1422 | ev_idle_stop (EV_P_ struct ev_idle *w) |
… | |
… | |
1332 | { |
1434 | { |
1333 | if (ev_is_active (w)) |
1435 | if (ev_is_active (w)) |
1334 | return; |
1436 | return; |
1335 | |
1437 | |
1336 | ev_start (EV_A_ (W)w, ++preparecnt); |
1438 | ev_start (EV_A_ (W)w, ++preparecnt); |
1337 | array_needsize (prepares, preparemax, preparecnt, ); |
1439 | array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, (void)); |
1338 | prepares [preparecnt - 1] = w; |
1440 | prepares [preparecnt - 1] = w; |
1339 | } |
1441 | } |
1340 | |
1442 | |
1341 | void |
1443 | void |
1342 | ev_prepare_stop (EV_P_ struct ev_prepare *w) |
1444 | ev_prepare_stop (EV_P_ struct ev_prepare *w) |
… | |
… | |
1354 | { |
1456 | { |
1355 | if (ev_is_active (w)) |
1457 | if (ev_is_active (w)) |
1356 | return; |
1458 | return; |
1357 | |
1459 | |
1358 | ev_start (EV_A_ (W)w, ++checkcnt); |
1460 | ev_start (EV_A_ (W)w, ++checkcnt); |
1359 | array_needsize (checks, checkmax, checkcnt, ); |
1461 | array_needsize (struct ev_check *, checks, checkmax, checkcnt, (void)); |
1360 | checks [checkcnt - 1] = w; |
1462 | checks [checkcnt - 1] = w; |
1361 | } |
1463 | } |
1362 | |
1464 | |
1363 | void |
1465 | void |
1364 | ev_check_stop (EV_P_ struct ev_check *w) |
1466 | ev_check_stop (EV_P_ struct ev_check *w) |
… | |
… | |
1385 | return; |
1487 | return; |
1386 | |
1488 | |
1387 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
1489 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
1388 | |
1490 | |
1389 | ev_start (EV_A_ (W)w, 1); |
1491 | ev_start (EV_A_ (W)w, 1); |
1390 | array_needsize (signals, signalmax, w->signum, signals_init); |
1492 | array_needsize (ANSIG, signals, signalmax, w->signum, signals_init); |
1391 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1493 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1392 | |
1494 | |
1393 | if (!((WL)w)->next) |
1495 | if (!((WL)w)->next) |
1394 | { |
1496 | { |
1395 | #if WIN32 |
1497 | #if WIN32 |
… | |
… | |
1478 | } |
1580 | } |
1479 | |
1581 | |
1480 | void |
1582 | void |
1481 | ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1583 | ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1482 | { |
1584 | { |
1483 | struct ev_once *once = ev_malloc (sizeof (struct ev_once)); |
1585 | struct ev_once *once = (struct ev_once *)ev_malloc (sizeof (struct ev_once)); |
1484 | |
1586 | |
1485 | if (!once) |
1587 | if (!once) |
1486 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
1588 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
1487 | else |
1589 | else |
1488 | { |
1590 | { |
1489 | once->cb = cb; |
1591 | once->cb = cb; |
1490 | once->arg = arg; |
1592 | once->arg = arg; |
1491 | |
1593 | |
1492 | ev_watcher_init (&once->io, once_cb_io); |
1594 | ev_init (&once->io, once_cb_io); |
1493 | if (fd >= 0) |
1595 | if (fd >= 0) |
1494 | { |
1596 | { |
1495 | ev_io_set (&once->io, fd, events); |
1597 | ev_io_set (&once->io, fd, events); |
1496 | ev_io_start (EV_A_ &once->io); |
1598 | ev_io_start (EV_A_ &once->io); |
1497 | } |
1599 | } |
1498 | |
1600 | |
1499 | ev_watcher_init (&once->to, once_cb_to); |
1601 | ev_init (&once->to, once_cb_to); |
1500 | if (timeout >= 0.) |
1602 | if (timeout >= 0.) |
1501 | { |
1603 | { |
1502 | ev_timer_set (&once->to, timeout, 0.); |
1604 | ev_timer_set (&once->to, timeout, 0.); |
1503 | ev_timer_start (EV_A_ &once->to); |
1605 | ev_timer_start (EV_A_ &once->to); |
1504 | } |
1606 | } |
1505 | } |
1607 | } |
1506 | } |
1608 | } |
1507 | |
1609 | |
|
|
1610 | #ifdef __cplusplus |
|
|
1611 | } |
|
|
1612 | #endif |
|
|
1613 | |