… | |
… | |
54 | |
54 | |
55 | #endif |
55 | #endif |
56 | |
56 | |
57 | #include <math.h> |
57 | #include <math.h> |
58 | #include <stdlib.h> |
58 | #include <stdlib.h> |
59 | #include <unistd.h> |
|
|
60 | #include <fcntl.h> |
59 | #include <fcntl.h> |
61 | #include <signal.h> |
|
|
62 | #include <stddef.h> |
60 | #include <stddef.h> |
63 | |
61 | |
64 | #include <stdio.h> |
62 | #include <stdio.h> |
65 | |
63 | |
66 | #include <assert.h> |
64 | #include <assert.h> |
67 | #include <errno.h> |
65 | #include <errno.h> |
68 | #include <sys/types.h> |
66 | #include <sys/types.h> |
|
|
67 | #include <time.h> |
|
|
68 | |
|
|
69 | #include <signal.h> |
|
|
70 | |
69 | #ifndef WIN32 |
71 | #ifndef WIN32 |
|
|
72 | # include <unistd.h> |
|
|
73 | # include <sys/time.h> |
70 | # include <sys/wait.h> |
74 | # include <sys/wait.h> |
71 | #endif |
75 | #endif |
72 | #include <sys/time.h> |
|
|
73 | #include <time.h> |
|
|
74 | |
|
|
75 | /**/ |
76 | /**/ |
76 | |
77 | |
77 | #ifndef EV_USE_MONOTONIC |
78 | #ifndef EV_USE_MONOTONIC |
78 | # define EV_USE_MONOTONIC 1 |
79 | # define EV_USE_MONOTONIC 1 |
79 | #endif |
80 | #endif |
… | |
… | |
94 | # define EV_USE_KQUEUE 0 |
95 | # define EV_USE_KQUEUE 0 |
95 | #endif |
96 | #endif |
96 | |
97 | |
97 | #ifndef EV_USE_WIN32 |
98 | #ifndef EV_USE_WIN32 |
98 | # ifdef WIN32 |
99 | # ifdef WIN32 |
|
|
100 | # define EV_USE_WIN32 0 /* it does not exist, use select */ |
|
|
101 | # undef EV_USE_SELECT |
99 | # define EV_USE_WIN32 1 |
102 | # define EV_USE_SELECT 1 |
100 | # else |
103 | # else |
101 | # define EV_USE_WIN32 0 |
104 | # define EV_USE_WIN32 0 |
102 | # endif |
105 | # endif |
103 | #endif |
106 | #endif |
104 | |
107 | |
… | |
… | |
123 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
126 | #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) */ |
127 | #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 */ |
128 | #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 */ |
129 | /*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */ |
127 | |
130 | |
|
|
131 | #ifdef EV_H |
|
|
132 | # include EV_H |
|
|
133 | #else |
128 | #include "ev.h" |
134 | # include "ev.h" |
|
|
135 | #endif |
129 | |
136 | |
130 | #if __GNUC__ >= 3 |
137 | #if __GNUC__ >= 3 |
131 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
138 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
132 | # define inline inline |
139 | # define inline inline |
133 | #else |
140 | #else |
… | |
… | |
145 | typedef struct ev_watcher_list *WL; |
152 | typedef struct ev_watcher_list *WL; |
146 | typedef struct ev_watcher_time *WT; |
153 | typedef struct ev_watcher_time *WT; |
147 | |
154 | |
148 | static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
155 | static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
149 | |
156 | |
150 | #if WIN32 |
157 | #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 | |
158 | |
156 | /*****************************************************************************/ |
159 | /*****************************************************************************/ |
157 | |
160 | |
158 | static void (*syserr_cb)(const char *msg); |
161 | static void (*syserr_cb)(const char *msg); |
159 | |
162 | |
… | |
… | |
216 | int events; |
219 | int events; |
217 | } ANPENDING; |
220 | } ANPENDING; |
218 | |
221 | |
219 | #if EV_MULTIPLICITY |
222 | #if EV_MULTIPLICITY |
220 | |
223 | |
221 | struct ev_loop |
224 | struct ev_loop |
222 | { |
225 | { |
223 | # define VAR(name,decl) decl; |
226 | #define VAR(name,decl) decl; |
224 | # include "ev_vars.h" |
227 | #include "ev_vars.h" |
225 | }; |
|
|
226 | # undef VAR |
228 | #undef VAR |
|
|
229 | }; |
227 | # include "ev_wrap.h" |
230 | #include "ev_wrap.h" |
|
|
231 | |
|
|
232 | struct ev_loop default_loop_struct; |
|
|
233 | static struct ev_loop *default_loop; |
228 | |
234 | |
229 | #else |
235 | #else |
230 | |
236 | |
231 | # define VAR(name,decl) static decl; |
237 | #define VAR(name,decl) static decl; |
232 | # include "ev_vars.h" |
238 | #include "ev_vars.h" |
233 | # undef VAR |
239 | #undef VAR |
|
|
240 | |
|
|
241 | static int default_loop; |
234 | |
242 | |
235 | #endif |
243 | #endif |
236 | |
244 | |
237 | /*****************************************************************************/ |
245 | /*****************************************************************************/ |
238 | |
246 | |
… | |
… | |
269 | ev_now (EV_P) |
277 | ev_now (EV_P) |
270 | { |
278 | { |
271 | return rt_now; |
279 | return rt_now; |
272 | } |
280 | } |
273 | |
281 | |
274 | #define array_roundsize(base,n) ((n) | 4 & ~3) |
282 | #define array_roundsize(type,n) ((n) | 4 & ~3) |
275 | |
283 | |
276 | #define array_needsize(base,cur,cnt,init) \ |
284 | #define array_needsize(type,base,cur,cnt,init) \ |
277 | if (expect_false ((cnt) > cur)) \ |
285 | if (expect_false ((cnt) > cur)) \ |
278 | { \ |
286 | { \ |
279 | int newcnt = cur; \ |
287 | int newcnt = cur; \ |
280 | do \ |
288 | do \ |
281 | { \ |
289 | { \ |
282 | newcnt = array_roundsize (base, newcnt << 1); \ |
290 | newcnt = array_roundsize (type, newcnt << 1); \ |
283 | } \ |
291 | } \ |
284 | while ((cnt) > newcnt); \ |
292 | while ((cnt) > newcnt); \ |
285 | \ |
293 | \ |
286 | base = ev_realloc (base, sizeof (*base) * (newcnt)); \ |
294 | base = (type *)ev_realloc (base, sizeof (type) * (newcnt));\ |
287 | init (base + cur, newcnt - cur); \ |
295 | init (base + cur, newcnt - cur); \ |
288 | cur = newcnt; \ |
296 | cur = newcnt; \ |
289 | } |
297 | } |
290 | |
298 | |
291 | #define array_slim(stem) \ |
299 | #define array_slim(type,stem) \ |
292 | if (stem ## max < array_roundsize (stem ## cnt >> 2)) \ |
300 | if (stem ## max < array_roundsize (stem ## cnt >> 2)) \ |
293 | { \ |
301 | { \ |
294 | stem ## max = array_roundsize (stem ## cnt >> 1); \ |
302 | stem ## max = array_roundsize (stem ## cnt >> 1); \ |
295 | base = ev_realloc (base, sizeof (*base) * (stem ## max)); \ |
303 | base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\ |
296 | fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ |
304 | fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ |
297 | } |
305 | } |
|
|
306 | |
|
|
307 | /* microsoft's pseudo-c is quite far from C as the rest of the world and the standard knows it */ |
|
|
308 | /* bringing us everlasting joy in form of stupid extra macros that are not required in C */ |
|
|
309 | #define array_free_microshit(stem) \ |
|
|
310 | ev_free (stem ## s); stem ## cnt = stem ## max = 0; |
298 | |
311 | |
299 | #define array_free(stem, idx) \ |
312 | #define array_free(stem, idx) \ |
300 | ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; |
313 | ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; |
301 | |
314 | |
302 | /*****************************************************************************/ |
315 | /*****************************************************************************/ |
… | |
… | |
312 | |
325 | |
313 | ++base; |
326 | ++base; |
314 | } |
327 | } |
315 | } |
328 | } |
316 | |
329 | |
317 | static void |
330 | void |
318 | event (EV_P_ W w, int events) |
331 | ev_feed_event (EV_P_ void *w, int revents) |
319 | { |
332 | { |
|
|
333 | W w_ = (W)w; |
|
|
334 | |
320 | if (w->pending) |
335 | if (w_->pending) |
321 | { |
336 | { |
322 | pendings [ABSPRI (w)][w->pending - 1].events |= events; |
337 | pendings [ABSPRI (w_)][w_->pending - 1].events |= revents; |
323 | return; |
338 | return; |
324 | } |
339 | } |
325 | |
340 | |
326 | w->pending = ++pendingcnt [ABSPRI (w)]; |
341 | w_->pending = ++pendingcnt [ABSPRI (w_)]; |
327 | array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], ); |
342 | array_needsize (ANPENDING, pendings [ABSPRI (w_)], pendingmax [ABSPRI (w_)], pendingcnt [ABSPRI (w_)], (void)); |
328 | pendings [ABSPRI (w)][w->pending - 1].w = w; |
343 | pendings [ABSPRI (w_)][w_->pending - 1].w = w_; |
329 | pendings [ABSPRI (w)][w->pending - 1].events = events; |
344 | pendings [ABSPRI (w_)][w_->pending - 1].events = revents; |
330 | } |
345 | } |
331 | |
346 | |
332 | static void |
347 | static void |
333 | queue_events (EV_P_ W *events, int eventcnt, int type) |
348 | queue_events (EV_P_ W *events, int eventcnt, int type) |
334 | { |
349 | { |
335 | int i; |
350 | int i; |
336 | |
351 | |
337 | for (i = 0; i < eventcnt; ++i) |
352 | for (i = 0; i < eventcnt; ++i) |
338 | event (EV_A_ events [i], type); |
353 | ev_feed_event (EV_A_ events [i], type); |
339 | } |
354 | } |
340 | |
355 | |
341 | static void |
356 | inline void |
342 | fd_event (EV_P_ int fd, int events) |
357 | fd_event (EV_P_ int fd, int revents) |
343 | { |
358 | { |
344 | ANFD *anfd = anfds + fd; |
359 | ANFD *anfd = anfds + fd; |
345 | struct ev_io *w; |
360 | struct ev_io *w; |
346 | |
361 | |
347 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
362 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
348 | { |
363 | { |
349 | int ev = w->events & events; |
364 | int ev = w->events & revents; |
350 | |
365 | |
351 | if (ev) |
366 | if (ev) |
352 | event (EV_A_ (W)w, ev); |
367 | ev_feed_event (EV_A_ (W)w, ev); |
353 | } |
368 | } |
|
|
369 | } |
|
|
370 | |
|
|
371 | void |
|
|
372 | ev_feed_fd_event (EV_P_ int fd, int revents) |
|
|
373 | { |
|
|
374 | fd_event (EV_A_ fd, revents); |
354 | } |
375 | } |
355 | |
376 | |
356 | /*****************************************************************************/ |
377 | /*****************************************************************************/ |
357 | |
378 | |
358 | static void |
379 | static void |
… | |
… | |
387 | return; |
408 | return; |
388 | |
409 | |
389 | anfds [fd].reify = 1; |
410 | anfds [fd].reify = 1; |
390 | |
411 | |
391 | ++fdchangecnt; |
412 | ++fdchangecnt; |
392 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
413 | array_needsize (int, fdchanges, fdchangemax, fdchangecnt, (void)); |
393 | fdchanges [fdchangecnt - 1] = fd; |
414 | fdchanges [fdchangecnt - 1] = fd; |
394 | } |
415 | } |
395 | |
416 | |
396 | static void |
417 | static void |
397 | fd_kill (EV_P_ int fd) |
418 | fd_kill (EV_P_ int fd) |
… | |
… | |
399 | struct ev_io *w; |
420 | struct ev_io *w; |
400 | |
421 | |
401 | while ((w = (struct ev_io *)anfds [fd].head)) |
422 | while ((w = (struct ev_io *)anfds [fd].head)) |
402 | { |
423 | { |
403 | ev_io_stop (EV_A_ w); |
424 | ev_io_stop (EV_A_ w); |
404 | event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); |
425 | ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); |
405 | } |
426 | } |
|
|
427 | } |
|
|
428 | |
|
|
429 | static int |
|
|
430 | fd_valid (int fd) |
|
|
431 | { |
|
|
432 | #ifdef WIN32 |
|
|
433 | return !!win32_get_osfhandle (fd); |
|
|
434 | #else |
|
|
435 | return fcntl (fd, F_GETFD) != -1; |
|
|
436 | #endif |
406 | } |
437 | } |
407 | |
438 | |
408 | /* called on EBADF to verify fds */ |
439 | /* called on EBADF to verify fds */ |
409 | static void |
440 | static void |
410 | fd_ebadf (EV_P) |
441 | fd_ebadf (EV_P) |
411 | { |
442 | { |
412 | int fd; |
443 | int fd; |
413 | |
444 | |
414 | for (fd = 0; fd < anfdmax; ++fd) |
445 | for (fd = 0; fd < anfdmax; ++fd) |
415 | if (anfds [fd].events) |
446 | if (anfds [fd].events) |
416 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
447 | if (!fd_valid (fd) == -1 && errno == EBADF) |
417 | fd_kill (EV_A_ fd); |
448 | fd_kill (EV_A_ fd); |
418 | } |
449 | } |
419 | |
450 | |
420 | /* called on ENOMEM in select/poll to kill some fds and retry */ |
451 | /* called on ENOMEM in select/poll to kill some fds and retry */ |
421 | static void |
452 | static void |
… | |
… | |
527 | |
558 | |
528 | if (!gotsig) |
559 | if (!gotsig) |
529 | { |
560 | { |
530 | int old_errno = errno; |
561 | int old_errno = errno; |
531 | gotsig = 1; |
562 | gotsig = 1; |
|
|
563 | #ifdef WIN32 |
|
|
564 | send (sigpipe [1], &signum, 1, MSG_DONTWAIT); |
|
|
565 | #else |
532 | write (sigpipe [1], &signum, 1); |
566 | write (sigpipe [1], &signum, 1); |
|
|
567 | #endif |
533 | errno = old_errno; |
568 | errno = old_errno; |
534 | } |
569 | } |
535 | } |
570 | } |
536 | |
571 | |
|
|
572 | void |
|
|
573 | ev_feed_signal_event (EV_P_ int signum) |
|
|
574 | { |
|
|
575 | WL w; |
|
|
576 | |
|
|
577 | #if EV_MULTIPLICITY |
|
|
578 | assert (("feeding signal events is only supported in the default loop", loop == default_loop)); |
|
|
579 | #endif |
|
|
580 | |
|
|
581 | --signum; |
|
|
582 | |
|
|
583 | if (signum < 0 || signum >= signalmax) |
|
|
584 | return; |
|
|
585 | |
|
|
586 | signals [signum].gotsig = 0; |
|
|
587 | |
|
|
588 | for (w = signals [signum].head; w; w = w->next) |
|
|
589 | ev_feed_event (EV_A_ (W)w, EV_SIGNAL); |
|
|
590 | } |
|
|
591 | |
537 | static void |
592 | static void |
538 | sigcb (EV_P_ struct ev_io *iow, int revents) |
593 | sigcb (EV_P_ struct ev_io *iow, int revents) |
539 | { |
594 | { |
540 | WL w; |
|
|
541 | int signum; |
595 | int signum; |
542 | |
596 | |
|
|
597 | #ifdef WIN32 |
|
|
598 | recv (sigpipe [0], &revents, 1, MSG_DONTWAIT); |
|
|
599 | #else |
543 | read (sigpipe [0], &revents, 1); |
600 | read (sigpipe [0], &revents, 1); |
|
|
601 | #endif |
544 | gotsig = 0; |
602 | gotsig = 0; |
545 | |
603 | |
546 | for (signum = signalmax; signum--; ) |
604 | for (signum = signalmax; signum--; ) |
547 | if (signals [signum].gotsig) |
605 | if (signals [signum].gotsig) |
548 | { |
606 | 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 | } |
607 | } |
555 | |
608 | |
556 | static void |
609 | static void |
557 | siginit (EV_P) |
610 | siginit (EV_P) |
558 | { |
611 | { |
… | |
… | |
570 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
623 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
571 | } |
624 | } |
572 | |
625 | |
573 | /*****************************************************************************/ |
626 | /*****************************************************************************/ |
574 | |
627 | |
|
|
628 | static struct ev_child *childs [PID_HASHSIZE]; |
|
|
629 | |
575 | #ifndef WIN32 |
630 | #ifndef WIN32 |
576 | |
631 | |
577 | static struct ev_child *childs [PID_HASHSIZE]; |
|
|
578 | static struct ev_signal childev; |
632 | static struct ev_signal childev; |
579 | |
633 | |
580 | #ifndef WCONTINUED |
634 | #ifndef WCONTINUED |
581 | # define WCONTINUED 0 |
635 | # define WCONTINUED 0 |
582 | #endif |
636 | #endif |
… | |
… | |
590 | if (w->pid == pid || !w->pid) |
644 | if (w->pid == pid || !w->pid) |
591 | { |
645 | { |
592 | ev_priority (w) = ev_priority (sw); /* need to do it *now* */ |
646 | ev_priority (w) = ev_priority (sw); /* need to do it *now* */ |
593 | w->rpid = pid; |
647 | w->rpid = pid; |
594 | w->rstatus = status; |
648 | w->rstatus = status; |
595 | event (EV_A_ (W)w, EV_CHILD); |
649 | ev_feed_event (EV_A_ (W)w, EV_CHILD); |
596 | } |
650 | } |
597 | } |
651 | } |
598 | |
652 | |
599 | static void |
653 | static void |
600 | childcb (EV_P_ struct ev_signal *sw, int revents) |
654 | childcb (EV_P_ struct ev_signal *sw, int revents) |
… | |
… | |
602 | int pid, status; |
656 | int pid, status; |
603 | |
657 | |
604 | if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) |
658 | if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) |
605 | { |
659 | { |
606 | /* make sure we are called again until all childs have been reaped */ |
660 | /* make sure we are called again until all childs have been reaped */ |
607 | event (EV_A_ (W)sw, EV_SIGNAL); |
661 | ev_feed_event (EV_A_ (W)sw, EV_SIGNAL); |
608 | |
662 | |
609 | child_reap (EV_A_ sw, pid, pid, status); |
663 | 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 */ |
664 | child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */ |
611 | } |
665 | } |
612 | } |
666 | } |
… | |
… | |
697 | #endif |
751 | #endif |
698 | #if EV_USE_SELECT |
752 | #if EV_USE_SELECT |
699 | if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); |
753 | if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); |
700 | #endif |
754 | #endif |
701 | |
755 | |
702 | ev_watcher_init (&sigev, sigcb); |
756 | ev_init (&sigev, sigcb); |
703 | ev_set_priority (&sigev, EV_MAXPRI); |
757 | ev_set_priority (&sigev, EV_MAXPRI); |
704 | } |
758 | } |
705 | } |
759 | } |
706 | |
760 | |
707 | void |
761 | void |
… | |
… | |
726 | #endif |
780 | #endif |
727 | |
781 | |
728 | for (i = NUMPRI; i--; ) |
782 | for (i = NUMPRI; i--; ) |
729 | array_free (pending, [i]); |
783 | array_free (pending, [i]); |
730 | |
784 | |
|
|
785 | /* have to use the microsoft-never-gets-it-right macro */ |
731 | array_free (fdchange, ); |
786 | array_free_microshit (fdchange); |
732 | array_free (timer, ); |
787 | array_free_microshit (timer); |
733 | array_free (periodic, ); |
788 | array_free_microshit (periodic); |
734 | array_free (idle, ); |
789 | array_free_microshit (idle); |
735 | array_free (prepare, ); |
790 | array_free_microshit (prepare); |
736 | array_free (check, ); |
791 | array_free_microshit (check); |
737 | |
792 | |
738 | method = 0; |
793 | method = 0; |
739 | } |
794 | } |
740 | |
795 | |
741 | static void |
796 | static void |
… | |
… | |
796 | } |
851 | } |
797 | |
852 | |
798 | #endif |
853 | #endif |
799 | |
854 | |
800 | #if EV_MULTIPLICITY |
855 | #if EV_MULTIPLICITY |
801 | struct ev_loop default_loop_struct; |
|
|
802 | static struct ev_loop *default_loop; |
|
|
803 | |
|
|
804 | struct ev_loop * |
856 | struct ev_loop * |
805 | #else |
857 | #else |
806 | static int default_loop; |
|
|
807 | |
|
|
808 | int |
858 | int |
809 | #endif |
859 | #endif |
810 | ev_default_loop (int methods) |
860 | ev_default_loop (int methods) |
811 | { |
861 | { |
812 | if (sigpipe [0] == sigpipe [1]) |
862 | if (sigpipe [0] == sigpipe [1]) |
… | |
… | |
846 | { |
896 | { |
847 | #if EV_MULTIPLICITY |
897 | #if EV_MULTIPLICITY |
848 | struct ev_loop *loop = default_loop; |
898 | struct ev_loop *loop = default_loop; |
849 | #endif |
899 | #endif |
850 | |
900 | |
|
|
901 | #ifndef WIN32 |
851 | ev_ref (EV_A); /* child watcher */ |
902 | ev_ref (EV_A); /* child watcher */ |
852 | ev_signal_stop (EV_A_ &childev); |
903 | ev_signal_stop (EV_A_ &childev); |
|
|
904 | #endif |
853 | |
905 | |
854 | ev_ref (EV_A); /* signal watcher */ |
906 | ev_ref (EV_A); /* signal watcher */ |
855 | ev_io_stop (EV_A_ &sigev); |
907 | ev_io_stop (EV_A_ &sigev); |
856 | |
908 | |
857 | close (sigpipe [0]); sigpipe [0] = 0; |
909 | close (sigpipe [0]); sigpipe [0] = 0; |
… | |
… | |
870 | if (method) |
922 | if (method) |
871 | postfork = 1; |
923 | postfork = 1; |
872 | } |
924 | } |
873 | |
925 | |
874 | /*****************************************************************************/ |
926 | /*****************************************************************************/ |
|
|
927 | |
|
|
928 | static int |
|
|
929 | any_pending (EV_P) |
|
|
930 | { |
|
|
931 | int pri; |
|
|
932 | |
|
|
933 | for (pri = NUMPRI; pri--; ) |
|
|
934 | if (pendingcnt [pri]) |
|
|
935 | return 1; |
|
|
936 | |
|
|
937 | return 0; |
|
|
938 | } |
875 | |
939 | |
876 | static void |
940 | static void |
877 | call_pending (EV_P) |
941 | call_pending (EV_P) |
878 | { |
942 | { |
879 | int pri; |
943 | int pri; |
… | |
… | |
884 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
948 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
885 | |
949 | |
886 | if (p->w) |
950 | if (p->w) |
887 | { |
951 | { |
888 | p->w->pending = 0; |
952 | p->w->pending = 0; |
889 | p->w->cb (EV_A_ p->w, p->events); |
953 | EV_CB_INVOKE (p->w, p->events); |
890 | } |
954 | } |
891 | } |
955 | } |
892 | } |
956 | } |
893 | |
957 | |
894 | static void |
958 | static void |
… | |
… | |
908 | downheap ((WT *)timers, timercnt, 0); |
972 | downheap ((WT *)timers, timercnt, 0); |
909 | } |
973 | } |
910 | else |
974 | else |
911 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
975 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
912 | |
976 | |
913 | event (EV_A_ (W)w, EV_TIMEOUT); |
977 | ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); |
914 | } |
978 | } |
915 | } |
979 | } |
916 | |
980 | |
917 | static void |
981 | static void |
918 | periodics_reify (EV_P) |
982 | periodics_reify (EV_P) |
… | |
… | |
922 | struct ev_periodic *w = periodics [0]; |
986 | struct ev_periodic *w = periodics [0]; |
923 | |
987 | |
924 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
988 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
925 | |
989 | |
926 | /* first reschedule or stop timer */ |
990 | /* first reschedule or stop timer */ |
|
|
991 | if (w->reschedule_cb) |
|
|
992 | { |
|
|
993 | ev_tstamp at = ((WT)w)->at = w->reschedule_cb (w, rt_now + 0.0001); |
|
|
994 | |
|
|
995 | assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > rt_now)); |
|
|
996 | downheap ((WT *)periodics, periodiccnt, 0); |
|
|
997 | } |
927 | if (w->interval) |
998 | else if (w->interval) |
928 | { |
999 | { |
929 | ((WT)w)->at += floor ((rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval; |
1000 | ((WT)w)->at += floor ((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)); |
1001 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > rt_now)); |
931 | downheap ((WT *)periodics, periodiccnt, 0); |
1002 | downheap ((WT *)periodics, periodiccnt, 0); |
932 | } |
1003 | } |
933 | else |
1004 | else |
934 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
1005 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
935 | |
1006 | |
936 | event (EV_A_ (W)w, EV_PERIODIC); |
1007 | ev_feed_event (EV_A_ (W)w, EV_PERIODIC); |
937 | } |
1008 | } |
938 | } |
1009 | } |
939 | |
1010 | |
940 | static void |
1011 | static void |
941 | periodics_reschedule (EV_P) |
1012 | periodics_reschedule (EV_P) |
… | |
… | |
945 | /* adjust periodics after time jump */ |
1016 | /* adjust periodics after time jump */ |
946 | for (i = 0; i < periodiccnt; ++i) |
1017 | for (i = 0; i < periodiccnt; ++i) |
947 | { |
1018 | { |
948 | struct ev_periodic *w = periodics [i]; |
1019 | struct ev_periodic *w = periodics [i]; |
949 | |
1020 | |
|
|
1021 | if (w->reschedule_cb) |
|
|
1022 | ((WT)w)->at = w->reschedule_cb (w, rt_now); |
950 | if (w->interval) |
1023 | else if (w->interval) |
951 | { |
|
|
952 | ev_tstamp diff = ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1024 | ((WT)w)->at += ceil ((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 | } |
1025 | } |
|
|
1026 | |
|
|
1027 | /* now rebuild the heap */ |
|
|
1028 | for (i = periodiccnt >> 1; i--; ) |
|
|
1029 | downheap ((WT *)periodics, periodiccnt, i); |
963 | } |
1030 | } |
964 | |
1031 | |
965 | inline int |
1032 | inline int |
966 | time_update_monotonic (EV_P) |
1033 | time_update_monotonic (EV_P) |
967 | { |
1034 | { |
… | |
… | |
1063 | /* update fd-related kernel structures */ |
1130 | /* update fd-related kernel structures */ |
1064 | fd_reify (EV_A); |
1131 | fd_reify (EV_A); |
1065 | |
1132 | |
1066 | /* calculate blocking time */ |
1133 | /* calculate blocking time */ |
1067 | |
1134 | |
1068 | /* we only need this for !monotonic clockor timers, but as we basically |
1135 | /* we only need this for !monotonic clock or timers, but as we basically |
1069 | always have timers, we just calculate it always */ |
1136 | always have timers, we just calculate it always */ |
1070 | #if EV_USE_MONOTONIC |
1137 | #if EV_USE_MONOTONIC |
1071 | if (expect_true (have_monotonic)) |
1138 | if (expect_true (have_monotonic)) |
1072 | time_update_monotonic (EV_A); |
1139 | time_update_monotonic (EV_A); |
1073 | else |
1140 | else |
… | |
… | |
1106 | /* queue pending timers and reschedule them */ |
1173 | /* queue pending timers and reschedule them */ |
1107 | timers_reify (EV_A); /* relative timers called last */ |
1174 | timers_reify (EV_A); /* relative timers called last */ |
1108 | periodics_reify (EV_A); /* absolute timers called first */ |
1175 | periodics_reify (EV_A); /* absolute timers called first */ |
1109 | |
1176 | |
1110 | /* queue idle watchers unless io or timers are pending */ |
1177 | /* queue idle watchers unless io or timers are pending */ |
1111 | if (!pendingcnt) |
1178 | if (idlecnt && !any_pending (EV_A)) |
1112 | queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); |
1179 | queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); |
1113 | |
1180 | |
1114 | /* queue check watchers, to be executed first */ |
1181 | /* queue check watchers, to be executed first */ |
1115 | if (checkcnt) |
1182 | if (checkcnt) |
1116 | queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); |
1183 | queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); |
… | |
… | |
1191 | return; |
1258 | return; |
1192 | |
1259 | |
1193 | assert (("ev_io_start called with negative fd", fd >= 0)); |
1260 | assert (("ev_io_start called with negative fd", fd >= 0)); |
1194 | |
1261 | |
1195 | ev_start (EV_A_ (W)w, 1); |
1262 | ev_start (EV_A_ (W)w, 1); |
1196 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
1263 | array_needsize (ANFD, anfds, anfdmax, fd + 1, anfds_init); |
1197 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
1264 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
1198 | |
1265 | |
1199 | fd_change (EV_A_ fd); |
1266 | fd_change (EV_A_ fd); |
1200 | } |
1267 | } |
1201 | |
1268 | |
… | |
… | |
1221 | ((WT)w)->at += mn_now; |
1288 | ((WT)w)->at += mn_now; |
1222 | |
1289 | |
1223 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
1290 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
1224 | |
1291 | |
1225 | ev_start (EV_A_ (W)w, ++timercnt); |
1292 | ev_start (EV_A_ (W)w, ++timercnt); |
1226 | array_needsize (timers, timermax, timercnt, ); |
1293 | array_needsize (struct ev_timer *, timers, timermax, timercnt, (void)); |
1227 | timers [timercnt - 1] = w; |
1294 | timers [timercnt - 1] = w; |
1228 | upheap ((WT *)timers, timercnt - 1); |
1295 | upheap ((WT *)timers, timercnt - 1); |
1229 | |
1296 | |
1230 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1297 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1231 | } |
1298 | } |
… | |
… | |
1271 | ev_periodic_start (EV_P_ struct ev_periodic *w) |
1338 | ev_periodic_start (EV_P_ struct ev_periodic *w) |
1272 | { |
1339 | { |
1273 | if (ev_is_active (w)) |
1340 | if (ev_is_active (w)) |
1274 | return; |
1341 | return; |
1275 | |
1342 | |
|
|
1343 | if (w->reschedule_cb) |
|
|
1344 | ((WT)w)->at = w->reschedule_cb (w, rt_now); |
|
|
1345 | else if (w->interval) |
|
|
1346 | { |
1276 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
1347 | 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 */ |
1348 | /* 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; |
1349 | ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; |
|
|
1350 | } |
1281 | |
1351 | |
1282 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1352 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1283 | array_needsize (periodics, periodicmax, periodiccnt, ); |
1353 | array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void)); |
1284 | periodics [periodiccnt - 1] = w; |
1354 | periodics [periodiccnt - 1] = w; |
1285 | upheap ((WT *)periodics, periodiccnt - 1); |
1355 | upheap ((WT *)periodics, periodiccnt - 1); |
1286 | |
1356 | |
1287 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1357 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1288 | } |
1358 | } |
… | |
… | |
1304 | |
1374 | |
1305 | ev_stop (EV_A_ (W)w); |
1375 | ev_stop (EV_A_ (W)w); |
1306 | } |
1376 | } |
1307 | |
1377 | |
1308 | void |
1378 | void |
|
|
1379 | ev_periodic_again (EV_P_ struct ev_periodic *w) |
|
|
1380 | { |
|
|
1381 | ev_periodic_stop (EV_A_ w); |
|
|
1382 | ev_periodic_start (EV_A_ w); |
|
|
1383 | } |
|
|
1384 | |
|
|
1385 | void |
1309 | ev_idle_start (EV_P_ struct ev_idle *w) |
1386 | ev_idle_start (EV_P_ struct ev_idle *w) |
1310 | { |
1387 | { |
1311 | if (ev_is_active (w)) |
1388 | if (ev_is_active (w)) |
1312 | return; |
1389 | return; |
1313 | |
1390 | |
1314 | ev_start (EV_A_ (W)w, ++idlecnt); |
1391 | ev_start (EV_A_ (W)w, ++idlecnt); |
1315 | array_needsize (idles, idlemax, idlecnt, ); |
1392 | array_needsize (struct ev_idle *, idles, idlemax, idlecnt, (void)); |
1316 | idles [idlecnt - 1] = w; |
1393 | idles [idlecnt - 1] = w; |
1317 | } |
1394 | } |
1318 | |
1395 | |
1319 | void |
1396 | void |
1320 | ev_idle_stop (EV_P_ struct ev_idle *w) |
1397 | ev_idle_stop (EV_P_ struct ev_idle *w) |
… | |
… | |
1332 | { |
1409 | { |
1333 | if (ev_is_active (w)) |
1410 | if (ev_is_active (w)) |
1334 | return; |
1411 | return; |
1335 | |
1412 | |
1336 | ev_start (EV_A_ (W)w, ++preparecnt); |
1413 | ev_start (EV_A_ (W)w, ++preparecnt); |
1337 | array_needsize (prepares, preparemax, preparecnt, ); |
1414 | array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, (void)); |
1338 | prepares [preparecnt - 1] = w; |
1415 | prepares [preparecnt - 1] = w; |
1339 | } |
1416 | } |
1340 | |
1417 | |
1341 | void |
1418 | void |
1342 | ev_prepare_stop (EV_P_ struct ev_prepare *w) |
1419 | ev_prepare_stop (EV_P_ struct ev_prepare *w) |
… | |
… | |
1354 | { |
1431 | { |
1355 | if (ev_is_active (w)) |
1432 | if (ev_is_active (w)) |
1356 | return; |
1433 | return; |
1357 | |
1434 | |
1358 | ev_start (EV_A_ (W)w, ++checkcnt); |
1435 | ev_start (EV_A_ (W)w, ++checkcnt); |
1359 | array_needsize (checks, checkmax, checkcnt, ); |
1436 | array_needsize (struct ev_check *, checks, checkmax, checkcnt, (void)); |
1360 | checks [checkcnt - 1] = w; |
1437 | checks [checkcnt - 1] = w; |
1361 | } |
1438 | } |
1362 | |
1439 | |
1363 | void |
1440 | void |
1364 | ev_check_stop (EV_P_ struct ev_check *w) |
1441 | ev_check_stop (EV_P_ struct ev_check *w) |
… | |
… | |
1385 | return; |
1462 | return; |
1386 | |
1463 | |
1387 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
1464 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
1388 | |
1465 | |
1389 | ev_start (EV_A_ (W)w, 1); |
1466 | ev_start (EV_A_ (W)w, 1); |
1390 | array_needsize (signals, signalmax, w->signum, signals_init); |
1467 | array_needsize (ANSIG, signals, signalmax, w->signum, signals_init); |
1391 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1468 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1392 | |
1469 | |
1393 | if (!((WL)w)->next) |
1470 | if (!((WL)w)->next) |
1394 | { |
1471 | { |
1395 | #if WIN32 |
1472 | #if WIN32 |
… | |
… | |
1478 | } |
1555 | } |
1479 | |
1556 | |
1480 | void |
1557 | void |
1481 | ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1558 | ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1482 | { |
1559 | { |
1483 | struct ev_once *once = ev_malloc (sizeof (struct ev_once)); |
1560 | struct ev_once *once = (struct ev_once *)ev_malloc (sizeof (struct ev_once)); |
1484 | |
1561 | |
1485 | if (!once) |
1562 | if (!once) |
1486 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
1563 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
1487 | else |
1564 | else |
1488 | { |
1565 | { |
1489 | once->cb = cb; |
1566 | once->cb = cb; |
1490 | once->arg = arg; |
1567 | once->arg = arg; |
1491 | |
1568 | |
1492 | ev_watcher_init (&once->io, once_cb_io); |
1569 | ev_init (&once->io, once_cb_io); |
1493 | if (fd >= 0) |
1570 | if (fd >= 0) |
1494 | { |
1571 | { |
1495 | ev_io_set (&once->io, fd, events); |
1572 | ev_io_set (&once->io, fd, events); |
1496 | ev_io_start (EV_A_ &once->io); |
1573 | ev_io_start (EV_A_ &once->io); |
1497 | } |
1574 | } |
1498 | |
1575 | |
1499 | ev_watcher_init (&once->to, once_cb_to); |
1576 | ev_init (&once->to, once_cb_to); |
1500 | if (timeout >= 0.) |
1577 | if (timeout >= 0.) |
1501 | { |
1578 | { |
1502 | ev_timer_set (&once->to, timeout, 0.); |
1579 | ev_timer_set (&once->to, timeout, 0.); |
1503 | ev_timer_start (EV_A_ &once->to); |
1580 | ev_timer_start (EV_A_ &once->to); |
1504 | } |
1581 | } |