… | |
… | |
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 | #ifndef EV_STANDALONE |
31 | #ifndef EV_STANDALONE |
32 | # include "config.h" |
32 | # include "config.h" |
|
|
33 | |
|
|
34 | # if HAVE_CLOCK_GETTIME |
|
|
35 | # define EV_USE_MONOTONIC 1 |
|
|
36 | # define EV_USE_REALTIME 1 |
|
|
37 | # endif |
|
|
38 | |
|
|
39 | # if HAVE_SELECT && HAVE_SYS_SELECT_H |
|
|
40 | # define EV_USE_SELECT 1 |
|
|
41 | # endif |
|
|
42 | |
|
|
43 | # if HAVE_POLL && HAVE_POLL_H |
|
|
44 | # define EV_USE_POLL 1 |
|
|
45 | # endif |
|
|
46 | |
|
|
47 | # if HAVE_EPOLL && HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H |
|
|
48 | # define EV_USE_EPOLL 1 |
|
|
49 | # endif |
|
|
50 | |
|
|
51 | # if HAVE_KQUEUE && HAVE_WORKING_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H |
|
|
52 | # define EV_USE_KQUEUE 1 |
|
|
53 | # endif |
|
|
54 | |
33 | #endif |
55 | #endif |
34 | |
56 | |
35 | #include <math.h> |
57 | #include <math.h> |
36 | #include <stdlib.h> |
58 | #include <stdlib.h> |
37 | #include <unistd.h> |
59 | #include <unistd.h> |
… | |
… | |
58 | |
80 | |
59 | #ifndef EV_USE_SELECT |
81 | #ifndef EV_USE_SELECT |
60 | # define EV_USE_SELECT 1 |
82 | # define EV_USE_SELECT 1 |
61 | #endif |
83 | #endif |
62 | |
84 | |
63 | #ifndef EV_USEV_POLL |
85 | #ifndef EV_USE_POLL |
64 | # define EV_USEV_POLL 0 /* poll is usually slower than select, and not as well tested */ |
86 | # define EV_USE_POLL 0 /* poll is usually slower than select, and not as well tested */ |
65 | #endif |
87 | #endif |
66 | |
88 | |
67 | #ifndef EV_USE_EPOLL |
89 | #ifndef EV_USE_EPOLL |
68 | # define EV_USE_EPOLL 0 |
90 | # define EV_USE_EPOLL 0 |
69 | #endif |
91 | #endif |
… | |
… | |
113 | |
135 | |
114 | typedef struct ev_watcher *W; |
136 | typedef struct ev_watcher *W; |
115 | typedef struct ev_watcher_list *WL; |
137 | typedef struct ev_watcher_list *WL; |
116 | typedef struct ev_watcher_time *WT; |
138 | typedef struct ev_watcher_time *WT; |
117 | |
139 | |
118 | static ev_tstamp now_floor, mn_now, diff; /* monotonic clock */ |
140 | static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
119 | static ev_tstamp rt_now; |
|
|
120 | static int method; |
|
|
121 | |
141 | |
122 | static int have_monotonic; /* runtime */ |
142 | /*****************************************************************************/ |
123 | |
143 | |
124 | static ev_tstamp method_fudge; /* stupid epoll-returns-early bug */ |
144 | typedef struct |
125 | static void (*method_modify)(EV_P_ int fd, int oev, int nev); |
145 | { |
126 | static void (*method_poll)(EV_P_ ev_tstamp timeout); |
146 | struct ev_watcher_list *head; |
|
|
147 | unsigned char events; |
|
|
148 | unsigned char reify; |
|
|
149 | } ANFD; |
127 | |
150 | |
128 | static int activecnt; /* number of active events */ |
151 | typedef struct |
|
|
152 | { |
|
|
153 | W w; |
|
|
154 | int events; |
|
|
155 | } ANPENDING; |
129 | |
156 | |
130 | #if EV_USE_SELECT |
157 | #if EV_MULTIPLICITY |
131 | static unsigned char *vec_ri, *vec_ro, *vec_wi, *vec_wo; |
|
|
132 | static int vec_max; |
|
|
133 | #endif |
|
|
134 | |
158 | |
135 | #if EV_USEV_POLL |
159 | struct ev_loop |
136 | static struct pollfd *polls; |
160 | { |
137 | static int pollmax, pollcnt; |
161 | # define VAR(name,decl) decl; |
138 | static int *pollidxs; /* maps fds into structure indices */ |
162 | # include "ev_vars.h" |
139 | static int pollidxmax; |
163 | }; |
140 | #endif |
164 | # undef VAR |
|
|
165 | # include "ev_wrap.h" |
141 | |
166 | |
142 | #if EV_USE_EPOLL |
167 | #else |
143 | static int epoll_fd = -1; |
|
|
144 | |
168 | |
145 | static struct epoll_event *events; |
169 | # define VAR(name,decl) static decl; |
146 | static int eventmax; |
170 | # include "ev_vars.h" |
147 | #endif |
171 | # undef VAR |
148 | |
172 | |
149 | #if EV_USE_KQUEUE |
|
|
150 | static int kqueue_fd; |
|
|
151 | static struct kevent *kqueue_changes; |
|
|
152 | static int kqueue_changemax, kqueue_changecnt; |
|
|
153 | static struct kevent *kqueue_events; |
|
|
154 | static int kqueue_eventmax; |
|
|
155 | #endif |
173 | #endif |
156 | |
174 | |
157 | /*****************************************************************************/ |
175 | /*****************************************************************************/ |
158 | |
176 | |
159 | inline ev_tstamp |
177 | inline ev_tstamp |
… | |
… | |
208 | cur = newcnt; \ |
226 | cur = newcnt; \ |
209 | } |
227 | } |
210 | |
228 | |
211 | /*****************************************************************************/ |
229 | /*****************************************************************************/ |
212 | |
230 | |
213 | typedef struct |
|
|
214 | { |
|
|
215 | struct ev_watcher_list *head; |
|
|
216 | unsigned char events; |
|
|
217 | unsigned char reify; |
|
|
218 | } ANFD; |
|
|
219 | |
|
|
220 | static ANFD *anfds; |
|
|
221 | static int anfdmax; |
|
|
222 | |
|
|
223 | static void |
231 | static void |
224 | anfds_init (ANFD *base, int count) |
232 | anfds_init (ANFD *base, int count) |
225 | { |
233 | { |
226 | while (count--) |
234 | while (count--) |
227 | { |
235 | { |
… | |
… | |
230 | base->reify = 0; |
238 | base->reify = 0; |
231 | |
239 | |
232 | ++base; |
240 | ++base; |
233 | } |
241 | } |
234 | } |
242 | } |
235 | |
|
|
236 | typedef struct |
|
|
237 | { |
|
|
238 | W w; |
|
|
239 | int events; |
|
|
240 | } ANPENDING; |
|
|
241 | |
|
|
242 | static ANPENDING *pendings [NUMPRI]; |
|
|
243 | static int pendingmax [NUMPRI], pendingcnt [NUMPRI]; |
|
|
244 | |
243 | |
245 | static void |
244 | static void |
246 | event (EV_P_ W w, int events) |
245 | event (EV_P_ W w, int events) |
247 | { |
246 | { |
248 | if (w->pending) |
247 | if (w->pending) |
… | |
… | |
280 | event (EV_A_ (W)w, ev); |
279 | event (EV_A_ (W)w, ev); |
281 | } |
280 | } |
282 | } |
281 | } |
283 | |
282 | |
284 | /*****************************************************************************/ |
283 | /*****************************************************************************/ |
285 | |
|
|
286 | static int *fdchanges; |
|
|
287 | static int fdchangemax, fdchangecnt; |
|
|
288 | |
284 | |
289 | static void |
285 | static void |
290 | fd_reify (EV_P) |
286 | fd_reify (EV_P) |
291 | { |
287 | { |
292 | int i; |
288 | int i; |
… | |
… | |
364 | fd_kill (EV_A_ fd); |
360 | fd_kill (EV_A_ fd); |
365 | return; |
361 | return; |
366 | } |
362 | } |
367 | } |
363 | } |
368 | |
364 | |
|
|
365 | /* susually called after fork if method needs to re-arm all fds from scratch */ |
|
|
366 | static void |
|
|
367 | fd_rearm_all (EV_P) |
|
|
368 | { |
|
|
369 | int fd; |
|
|
370 | |
|
|
371 | /* this should be highly optimised to not do anything but set a flag */ |
|
|
372 | for (fd = 0; fd < anfdmax; ++fd) |
|
|
373 | if (anfds [fd].events) |
|
|
374 | { |
|
|
375 | anfds [fd].events = 0; |
|
|
376 | fd_change (EV_A_ fd); |
|
|
377 | } |
|
|
378 | } |
|
|
379 | |
369 | /*****************************************************************************/ |
380 | /*****************************************************************************/ |
370 | |
381 | |
371 | static struct ev_timer **timers; |
|
|
372 | static int timermax, timercnt; |
|
|
373 | |
|
|
374 | static struct ev_periodic **periodics; |
|
|
375 | static int periodicmax, periodiccnt; |
|
|
376 | |
|
|
377 | static void |
382 | static void |
378 | upheap (WT *timers, int k) |
383 | upheap (WT *heap, int k) |
379 | { |
384 | { |
380 | WT w = timers [k]; |
385 | WT w = heap [k]; |
381 | |
386 | |
382 | while (k && timers [k >> 1]->at > w->at) |
387 | while (k && heap [k >> 1]->at > w->at) |
383 | { |
388 | { |
384 | timers [k] = timers [k >> 1]; |
389 | heap [k] = heap [k >> 1]; |
385 | timers [k]->active = k + 1; |
390 | heap [k]->active = k + 1; |
386 | k >>= 1; |
391 | k >>= 1; |
387 | } |
392 | } |
388 | |
393 | |
389 | timers [k] = w; |
394 | heap [k] = w; |
390 | timers [k]->active = k + 1; |
395 | heap [k]->active = k + 1; |
391 | |
396 | |
392 | } |
397 | } |
393 | |
398 | |
394 | static void |
399 | static void |
395 | downheap (WT *timers, int N, int k) |
400 | downheap (WT *heap, int N, int k) |
396 | { |
401 | { |
397 | WT w = timers [k]; |
402 | WT w = heap [k]; |
398 | |
403 | |
399 | while (k < (N >> 1)) |
404 | while (k < (N >> 1)) |
400 | { |
405 | { |
401 | int j = k << 1; |
406 | int j = k << 1; |
402 | |
407 | |
403 | if (j + 1 < N && timers [j]->at > timers [j + 1]->at) |
408 | if (j + 1 < N && heap [j]->at > heap [j + 1]->at) |
404 | ++j; |
409 | ++j; |
405 | |
410 | |
406 | if (w->at <= timers [j]->at) |
411 | if (w->at <= heap [j]->at) |
407 | break; |
412 | break; |
408 | |
413 | |
409 | timers [k] = timers [j]; |
414 | heap [k] = heap [j]; |
410 | timers [k]->active = k + 1; |
415 | heap [k]->active = k + 1; |
411 | k = j; |
416 | k = j; |
412 | } |
417 | } |
413 | |
418 | |
414 | timers [k] = w; |
419 | heap [k] = w; |
415 | timers [k]->active = k + 1; |
420 | heap [k]->active = k + 1; |
416 | } |
421 | } |
417 | |
422 | |
418 | /*****************************************************************************/ |
423 | /*****************************************************************************/ |
419 | |
424 | |
420 | typedef struct |
425 | typedef struct |
… | |
… | |
486 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
491 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
487 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
492 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
488 | #endif |
493 | #endif |
489 | |
494 | |
490 | ev_io_set (&sigev, sigpipe [0], EV_READ); |
495 | ev_io_set (&sigev, sigpipe [0], EV_READ); |
491 | ev_io_start (&sigev); |
496 | ev_io_start (EV_A_ &sigev); |
492 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
497 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
493 | } |
498 | } |
494 | |
499 | |
495 | /*****************************************************************************/ |
500 | /*****************************************************************************/ |
496 | |
501 | |
497 | static struct ev_idle **idles; |
502 | #ifndef WIN32 |
498 | static int idlemax, idlecnt; |
|
|
499 | |
|
|
500 | static struct ev_prepare **prepares; |
|
|
501 | static int preparemax, preparecnt; |
|
|
502 | |
|
|
503 | static struct ev_check **checks; |
|
|
504 | static int checkmax, checkcnt; |
|
|
505 | |
|
|
506 | /*****************************************************************************/ |
|
|
507 | |
503 | |
508 | static struct ev_child *childs [PID_HASHSIZE]; |
504 | static struct ev_child *childs [PID_HASHSIZE]; |
509 | static struct ev_signal childev; |
505 | static struct ev_signal childev; |
510 | |
|
|
511 | #ifndef WIN32 |
|
|
512 | |
506 | |
513 | #ifndef WCONTINUED |
507 | #ifndef WCONTINUED |
514 | # define WCONTINUED 0 |
508 | # define WCONTINUED 0 |
515 | #endif |
509 | #endif |
516 | |
510 | |
… | |
… | |
552 | # include "ev_kqueue.c" |
546 | # include "ev_kqueue.c" |
553 | #endif |
547 | #endif |
554 | #if EV_USE_EPOLL |
548 | #if EV_USE_EPOLL |
555 | # include "ev_epoll.c" |
549 | # include "ev_epoll.c" |
556 | #endif |
550 | #endif |
557 | #if EV_USEV_POLL |
551 | #if EV_USE_POLL |
558 | # include "ev_poll.c" |
552 | # include "ev_poll.c" |
559 | #endif |
553 | #endif |
560 | #if EV_USE_SELECT |
554 | #if EV_USE_SELECT |
561 | # include "ev_select.c" |
555 | # include "ev_select.c" |
562 | #endif |
556 | #endif |
… | |
… | |
589 | ev_method (EV_P) |
583 | ev_method (EV_P) |
590 | { |
584 | { |
591 | return method; |
585 | return method; |
592 | } |
586 | } |
593 | |
587 | |
594 | int |
588 | static void |
595 | ev_init (EV_P_ int methods) |
589 | loop_init (EV_P_ int methods) |
596 | { |
590 | { |
597 | if (!method) |
591 | if (!method) |
598 | { |
592 | { |
599 | #if EV_USE_MONOTONIC |
593 | #if EV_USE_MONOTONIC |
600 | { |
594 | { |
… | |
… | |
605 | #endif |
599 | #endif |
606 | |
600 | |
607 | rt_now = ev_time (); |
601 | rt_now = ev_time (); |
608 | mn_now = get_clock (); |
602 | mn_now = get_clock (); |
609 | now_floor = mn_now; |
603 | now_floor = mn_now; |
610 | diff = rt_now - mn_now; |
604 | rtmn_diff = rt_now - mn_now; |
611 | |
|
|
612 | if (pipe (sigpipe)) |
|
|
613 | return 0; |
|
|
614 | |
605 | |
615 | if (methods == EVMETHOD_AUTO) |
606 | if (methods == EVMETHOD_AUTO) |
616 | if (!enable_secure () && getenv ("LIBmethodS")) |
607 | if (!enable_secure () && getenv ("LIBEV_METHODS")) |
617 | methods = atoi (getenv ("LIBmethodS")); |
608 | methods = atoi (getenv ("LIBEV_METHODS")); |
618 | else |
609 | else |
619 | methods = EVMETHOD_ANY; |
610 | methods = EVMETHOD_ANY; |
620 | |
611 | |
621 | method = 0; |
612 | method = 0; |
622 | #if EV_USE_KQUEUE |
613 | #if EV_USE_KQUEUE |
623 | if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); |
614 | if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); |
624 | #endif |
615 | #endif |
625 | #if EV_USE_EPOLL |
616 | #if EV_USE_EPOLL |
626 | if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); |
617 | if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); |
627 | #endif |
618 | #endif |
628 | #if EV_USEV_POLL |
619 | #if EV_USE_POLL |
629 | if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); |
620 | if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); |
630 | #endif |
621 | #endif |
631 | #if EV_USE_SELECT |
622 | #if EV_USE_SELECT |
632 | if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); |
623 | if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); |
633 | #endif |
624 | #endif |
|
|
625 | } |
|
|
626 | } |
634 | |
627 | |
|
|
628 | void |
|
|
629 | loop_destroy (EV_P) |
|
|
630 | { |
|
|
631 | #if EV_USE_KQUEUE |
|
|
632 | if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); |
|
|
633 | #endif |
|
|
634 | #if EV_USE_EPOLL |
|
|
635 | if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A); |
|
|
636 | #endif |
|
|
637 | #if EV_USE_POLL |
|
|
638 | if (method == EVMETHOD_POLL ) poll_destroy (EV_A); |
|
|
639 | #endif |
|
|
640 | #if EV_USE_SELECT |
|
|
641 | if (method == EVMETHOD_SELECT) select_destroy (EV_A); |
|
|
642 | #endif |
|
|
643 | |
|
|
644 | method = 0; |
|
|
645 | /*TODO*/ |
|
|
646 | } |
|
|
647 | |
|
|
648 | void |
|
|
649 | loop_fork (EV_P) |
|
|
650 | { |
|
|
651 | /*TODO*/ |
|
|
652 | #if EV_USE_EPOLL |
|
|
653 | if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A); |
|
|
654 | #endif |
|
|
655 | #if EV_USE_KQUEUE |
|
|
656 | if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A); |
|
|
657 | #endif |
|
|
658 | } |
|
|
659 | |
|
|
660 | #if EV_MULTIPLICITY |
|
|
661 | struct ev_loop * |
|
|
662 | ev_loop_new (int methods) |
|
|
663 | { |
|
|
664 | struct ev_loop *loop = (struct ev_loop *)calloc (1, sizeof (struct ev_loop)); |
|
|
665 | |
|
|
666 | loop_init (EV_A_ methods); |
|
|
667 | |
|
|
668 | if (ev_method (EV_A)) |
|
|
669 | return loop; |
|
|
670 | |
|
|
671 | return 0; |
|
|
672 | } |
|
|
673 | |
|
|
674 | void |
|
|
675 | ev_loop_destroy (EV_P) |
|
|
676 | { |
|
|
677 | loop_destroy (EV_A); |
|
|
678 | free (loop); |
|
|
679 | } |
|
|
680 | |
|
|
681 | void |
|
|
682 | ev_loop_fork (EV_P) |
|
|
683 | { |
|
|
684 | loop_fork (EV_A); |
|
|
685 | } |
|
|
686 | |
|
|
687 | #endif |
|
|
688 | |
|
|
689 | #if EV_MULTIPLICITY |
|
|
690 | struct ev_loop default_loop_struct; |
|
|
691 | static struct ev_loop *default_loop; |
|
|
692 | |
|
|
693 | struct ev_loop * |
|
|
694 | #else |
|
|
695 | static int default_loop; |
|
|
696 | |
|
|
697 | int |
|
|
698 | #endif |
|
|
699 | ev_default_loop (int methods) |
|
|
700 | { |
|
|
701 | if (sigpipe [0] == sigpipe [1]) |
|
|
702 | if (pipe (sigpipe)) |
|
|
703 | return 0; |
|
|
704 | |
|
|
705 | if (!default_loop) |
|
|
706 | { |
|
|
707 | #if EV_MULTIPLICITY |
|
|
708 | struct ev_loop *loop = default_loop = &default_loop_struct; |
|
|
709 | #else |
|
|
710 | default_loop = 1; |
|
|
711 | #endif |
|
|
712 | |
|
|
713 | loop_init (EV_A_ methods); |
|
|
714 | |
635 | if (method) |
715 | if (ev_method (EV_A)) |
636 | { |
716 | { |
637 | ev_watcher_init (&sigev, sigcb); |
717 | ev_watcher_init (&sigev, sigcb); |
638 | ev_set_priority (&sigev, EV_MAXPRI); |
718 | ev_set_priority (&sigev, EV_MAXPRI); |
639 | siginit (EV_A); |
719 | siginit (EV_A); |
640 | |
720 | |
… | |
… | |
643 | ev_set_priority (&childev, EV_MAXPRI); |
723 | ev_set_priority (&childev, EV_MAXPRI); |
644 | ev_signal_start (EV_A_ &childev); |
724 | ev_signal_start (EV_A_ &childev); |
645 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
725 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
646 | #endif |
726 | #endif |
647 | } |
727 | } |
|
|
728 | else |
|
|
729 | default_loop = 0; |
648 | } |
730 | } |
649 | |
731 | |
650 | return method; |
732 | return default_loop; |
651 | } |
733 | } |
652 | |
734 | |
653 | /*****************************************************************************/ |
|
|
654 | |
|
|
655 | void |
735 | void |
656 | ev_fork_prepare (void) |
736 | ev_default_destroy (void) |
657 | { |
737 | { |
658 | /* nop */ |
738 | #if EV_MULTIPLICITY |
659 | } |
739 | struct ev_loop *loop = default_loop; |
660 | |
|
|
661 | void |
|
|
662 | ev_fork_parent (void) |
|
|
663 | { |
|
|
664 | /* nop */ |
|
|
665 | } |
|
|
666 | |
|
|
667 | void |
|
|
668 | ev_fork_child (void) |
|
|
669 | { |
|
|
670 | #if EV_USE_EPOLL |
|
|
671 | if (method == EVMETHOD_EPOLL) |
|
|
672 | epoll_postfork_child (); |
|
|
673 | #endif |
740 | #endif |
674 | |
741 | |
|
|
742 | ev_ref (EV_A); /* child watcher */ |
|
|
743 | ev_signal_stop (EV_A_ &childev); |
|
|
744 | |
|
|
745 | ev_ref (EV_A); /* signal watcher */ |
675 | ev_io_stop (&sigev); |
746 | ev_io_stop (EV_A_ &sigev); |
|
|
747 | |
|
|
748 | close (sigpipe [0]); sigpipe [0] = 0; |
|
|
749 | close (sigpipe [1]); sigpipe [1] = 0; |
|
|
750 | |
|
|
751 | loop_destroy (EV_A); |
|
|
752 | } |
|
|
753 | |
|
|
754 | void |
|
|
755 | ev_default_fork (void) |
|
|
756 | { |
|
|
757 | #if EV_MULTIPLICITY |
|
|
758 | struct ev_loop *loop = default_loop; |
|
|
759 | #endif |
|
|
760 | |
|
|
761 | loop_fork (EV_A); |
|
|
762 | |
|
|
763 | ev_io_stop (EV_A_ &sigev); |
676 | close (sigpipe [0]); |
764 | close (sigpipe [0]); |
677 | close (sigpipe [1]); |
765 | close (sigpipe [1]); |
678 | pipe (sigpipe); |
766 | pipe (sigpipe); |
|
|
767 | |
|
|
768 | ev_ref (EV_A); /* signal watcher */ |
679 | siginit (); |
769 | siginit (EV_A); |
680 | } |
770 | } |
681 | |
771 | |
682 | /*****************************************************************************/ |
772 | /*****************************************************************************/ |
683 | |
773 | |
684 | static void |
774 | static void |
… | |
… | |
703 | timers_reify (EV_P) |
793 | timers_reify (EV_P) |
704 | { |
794 | { |
705 | while (timercnt && timers [0]->at <= mn_now) |
795 | while (timercnt && timers [0]->at <= mn_now) |
706 | { |
796 | { |
707 | struct ev_timer *w = timers [0]; |
797 | struct ev_timer *w = timers [0]; |
|
|
798 | |
|
|
799 | assert (("inactive timer on timer heap detected", ev_is_active (w))); |
708 | |
800 | |
709 | /* first reschedule or stop timer */ |
801 | /* first reschedule or stop timer */ |
710 | if (w->repeat) |
802 | if (w->repeat) |
711 | { |
803 | { |
712 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
804 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
… | |
… | |
714 | downheap ((WT *)timers, timercnt, 0); |
806 | downheap ((WT *)timers, timercnt, 0); |
715 | } |
807 | } |
716 | else |
808 | else |
717 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
809 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
718 | |
810 | |
719 | event ((W)w, EV_TIMEOUT); |
811 | event (EV_A_ (W)w, EV_TIMEOUT); |
720 | } |
812 | } |
721 | } |
813 | } |
722 | |
814 | |
723 | static void |
815 | static void |
724 | periodics_reify (EV_P) |
816 | periodics_reify (EV_P) |
725 | { |
817 | { |
726 | while (periodiccnt && periodics [0]->at <= rt_now) |
818 | while (periodiccnt && periodics [0]->at <= rt_now) |
727 | { |
819 | { |
728 | struct ev_periodic *w = periodics [0]; |
820 | struct ev_periodic *w = periodics [0]; |
|
|
821 | |
|
|
822 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
729 | |
823 | |
730 | /* first reschedule or stop timer */ |
824 | /* first reschedule or stop timer */ |
731 | if (w->interval) |
825 | if (w->interval) |
732 | { |
826 | { |
733 | w->at += floor ((rt_now - w->at) / w->interval + 1.) * w->interval; |
827 | w->at += floor ((rt_now - w->at) / w->interval + 1.) * w->interval; |
… | |
… | |
740 | event (EV_A_ (W)w, EV_PERIODIC); |
834 | event (EV_A_ (W)w, EV_PERIODIC); |
741 | } |
835 | } |
742 | } |
836 | } |
743 | |
837 | |
744 | static void |
838 | static void |
745 | periodics_reschedule (EV_P_ ev_tstamp diff) |
839 | periodics_reschedule (EV_P) |
746 | { |
840 | { |
747 | int i; |
841 | int i; |
748 | |
842 | |
749 | /* adjust periodics after time jump */ |
843 | /* adjust periodics after time jump */ |
750 | for (i = 0; i < periodiccnt; ++i) |
844 | for (i = 0; i < periodiccnt; ++i) |
… | |
… | |
771 | { |
865 | { |
772 | mn_now = get_clock (); |
866 | mn_now = get_clock (); |
773 | |
867 | |
774 | if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) |
868 | if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) |
775 | { |
869 | { |
776 | rt_now = mn_now + diff; |
870 | rt_now = rtmn_diff + mn_now; |
777 | return 0; |
871 | return 0; |
778 | } |
872 | } |
779 | else |
873 | else |
780 | { |
874 | { |
781 | now_floor = mn_now; |
875 | now_floor = mn_now; |
… | |
… | |
792 | #if EV_USE_MONOTONIC |
886 | #if EV_USE_MONOTONIC |
793 | if (expect_true (have_monotonic)) |
887 | if (expect_true (have_monotonic)) |
794 | { |
888 | { |
795 | if (time_update_monotonic (EV_A)) |
889 | if (time_update_monotonic (EV_A)) |
796 | { |
890 | { |
797 | ev_tstamp odiff = diff; |
891 | ev_tstamp odiff = rtmn_diff; |
798 | |
892 | |
799 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
893 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
800 | { |
894 | { |
801 | diff = rt_now - mn_now; |
895 | rtmn_diff = rt_now - mn_now; |
802 | |
896 | |
803 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
897 | if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) |
804 | return; /* all is well */ |
898 | return; /* all is well */ |
805 | |
899 | |
806 | rt_now = ev_time (); |
900 | rt_now = ev_time (); |
807 | mn_now = get_clock (); |
901 | mn_now = get_clock (); |
808 | now_floor = mn_now; |
902 | now_floor = mn_now; |
809 | } |
903 | } |
810 | |
904 | |
811 | periodics_reschedule (EV_A_ diff - odiff); |
905 | periodics_reschedule (EV_A); |
812 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
906 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
|
|
907 | /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ |
813 | } |
908 | } |
814 | } |
909 | } |
815 | else |
910 | else |
816 | #endif |
911 | #endif |
817 | { |
912 | { |
818 | rt_now = ev_time (); |
913 | rt_now = ev_time (); |
819 | |
914 | |
820 | if (expect_false (mn_now > rt_now || mn_now < rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
915 | if (expect_false (mn_now > rt_now || mn_now < rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
821 | { |
916 | { |
822 | periodics_reschedule (EV_A_ rt_now - mn_now); |
917 | periodics_reschedule (EV_A); |
823 | |
918 | |
824 | /* adjust timers. this is easy, as the offset is the same for all */ |
919 | /* adjust timers. this is easy, as the offset is the same for all */ |
825 | for (i = 0; i < timercnt; ++i) |
920 | for (i = 0; i < timercnt; ++i) |
826 | timers [i]->at += diff; |
921 | timers [i]->at += rt_now - mn_now; |
827 | } |
922 | } |
828 | |
923 | |
829 | mn_now = rt_now; |
924 | mn_now = rt_now; |
830 | } |
925 | } |
831 | } |
926 | } |
… | |
… | |
1094 | } |
1189 | } |
1095 | |
1190 | |
1096 | ev_stop (EV_A_ (W)w); |
1191 | ev_stop (EV_A_ (W)w); |
1097 | } |
1192 | } |
1098 | |
1193 | |
|
|
1194 | void |
|
|
1195 | ev_idle_start (EV_P_ struct ev_idle *w) |
|
|
1196 | { |
|
|
1197 | if (ev_is_active (w)) |
|
|
1198 | return; |
|
|
1199 | |
|
|
1200 | ev_start (EV_A_ (W)w, ++idlecnt); |
|
|
1201 | array_needsize (idles, idlemax, idlecnt, ); |
|
|
1202 | idles [idlecnt - 1] = w; |
|
|
1203 | } |
|
|
1204 | |
|
|
1205 | void |
|
|
1206 | ev_idle_stop (EV_P_ struct ev_idle *w) |
|
|
1207 | { |
|
|
1208 | ev_clear_pending (EV_A_ (W)w); |
|
|
1209 | if (ev_is_active (w)) |
|
|
1210 | return; |
|
|
1211 | |
|
|
1212 | idles [w->active - 1] = idles [--idlecnt]; |
|
|
1213 | ev_stop (EV_A_ (W)w); |
|
|
1214 | } |
|
|
1215 | |
|
|
1216 | void |
|
|
1217 | ev_prepare_start (EV_P_ struct ev_prepare *w) |
|
|
1218 | { |
|
|
1219 | if (ev_is_active (w)) |
|
|
1220 | return; |
|
|
1221 | |
|
|
1222 | ev_start (EV_A_ (W)w, ++preparecnt); |
|
|
1223 | array_needsize (prepares, preparemax, preparecnt, ); |
|
|
1224 | prepares [preparecnt - 1] = w; |
|
|
1225 | } |
|
|
1226 | |
|
|
1227 | void |
|
|
1228 | ev_prepare_stop (EV_P_ struct ev_prepare *w) |
|
|
1229 | { |
|
|
1230 | ev_clear_pending (EV_A_ (W)w); |
|
|
1231 | if (ev_is_active (w)) |
|
|
1232 | return; |
|
|
1233 | |
|
|
1234 | prepares [w->active - 1] = prepares [--preparecnt]; |
|
|
1235 | ev_stop (EV_A_ (W)w); |
|
|
1236 | } |
|
|
1237 | |
|
|
1238 | void |
|
|
1239 | ev_check_start (EV_P_ struct ev_check *w) |
|
|
1240 | { |
|
|
1241 | if (ev_is_active (w)) |
|
|
1242 | return; |
|
|
1243 | |
|
|
1244 | ev_start (EV_A_ (W)w, ++checkcnt); |
|
|
1245 | array_needsize (checks, checkmax, checkcnt, ); |
|
|
1246 | checks [checkcnt - 1] = w; |
|
|
1247 | } |
|
|
1248 | |
|
|
1249 | void |
|
|
1250 | ev_check_stop (EV_P_ struct ev_check *w) |
|
|
1251 | { |
|
|
1252 | ev_clear_pending (EV_A_ (W)w); |
|
|
1253 | if (ev_is_active (w)) |
|
|
1254 | return; |
|
|
1255 | |
|
|
1256 | checks [w->active - 1] = checks [--checkcnt]; |
|
|
1257 | ev_stop (EV_A_ (W)w); |
|
|
1258 | } |
|
|
1259 | |
1099 | #ifndef SA_RESTART |
1260 | #ifndef SA_RESTART |
1100 | # define SA_RESTART 0 |
1261 | # define SA_RESTART 0 |
1101 | #endif |
1262 | #endif |
1102 | |
1263 | |
1103 | void |
1264 | void |
1104 | ev_signal_start (EV_P_ struct ev_signal *w) |
1265 | ev_signal_start (EV_P_ struct ev_signal *w) |
1105 | { |
1266 | { |
|
|
1267 | #if EV_MULTIPLICITY |
|
|
1268 | assert (("signal watchers are only supported in the default loop", loop == default_loop)); |
|
|
1269 | #endif |
1106 | if (ev_is_active (w)) |
1270 | if (ev_is_active (w)) |
1107 | return; |
1271 | return; |
1108 | |
1272 | |
1109 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
1273 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
1110 | |
1274 | |
… | |
… | |
1135 | if (!signals [w->signum - 1].head) |
1299 | if (!signals [w->signum - 1].head) |
1136 | signal (w->signum, SIG_DFL); |
1300 | signal (w->signum, SIG_DFL); |
1137 | } |
1301 | } |
1138 | |
1302 | |
1139 | void |
1303 | void |
1140 | ev_idle_start (EV_P_ struct ev_idle *w) |
|
|
1141 | { |
|
|
1142 | if (ev_is_active (w)) |
|
|
1143 | return; |
|
|
1144 | |
|
|
1145 | ev_start (EV_A_ (W)w, ++idlecnt); |
|
|
1146 | array_needsize (idles, idlemax, idlecnt, ); |
|
|
1147 | idles [idlecnt - 1] = w; |
|
|
1148 | } |
|
|
1149 | |
|
|
1150 | void |
|
|
1151 | ev_idle_stop (EV_P_ struct ev_idle *w) |
|
|
1152 | { |
|
|
1153 | ev_clear_pending (EV_A_ (W)w); |
|
|
1154 | if (ev_is_active (w)) |
|
|
1155 | return; |
|
|
1156 | |
|
|
1157 | idles [w->active - 1] = idles [--idlecnt]; |
|
|
1158 | ev_stop (EV_A_ (W)w); |
|
|
1159 | } |
|
|
1160 | |
|
|
1161 | void |
|
|
1162 | ev_prepare_start (EV_P_ struct ev_prepare *w) |
|
|
1163 | { |
|
|
1164 | if (ev_is_active (w)) |
|
|
1165 | return; |
|
|
1166 | |
|
|
1167 | ev_start (EV_A_ (W)w, ++preparecnt); |
|
|
1168 | array_needsize (prepares, preparemax, preparecnt, ); |
|
|
1169 | prepares [preparecnt - 1] = w; |
|
|
1170 | } |
|
|
1171 | |
|
|
1172 | void |
|
|
1173 | ev_prepare_stop (EV_P_ struct ev_prepare *w) |
|
|
1174 | { |
|
|
1175 | ev_clear_pending (EV_A_ (W)w); |
|
|
1176 | if (ev_is_active (w)) |
|
|
1177 | return; |
|
|
1178 | |
|
|
1179 | prepares [w->active - 1] = prepares [--preparecnt]; |
|
|
1180 | ev_stop (EV_A_ (W)w); |
|
|
1181 | } |
|
|
1182 | |
|
|
1183 | void |
|
|
1184 | ev_check_start (EV_P_ struct ev_check *w) |
|
|
1185 | { |
|
|
1186 | if (ev_is_active (w)) |
|
|
1187 | return; |
|
|
1188 | |
|
|
1189 | ev_start (EV_A_ (W)w, ++checkcnt); |
|
|
1190 | array_needsize (checks, checkmax, checkcnt, ); |
|
|
1191 | checks [checkcnt - 1] = w; |
|
|
1192 | } |
|
|
1193 | |
|
|
1194 | void |
|
|
1195 | ev_check_stop (EV_P_ struct ev_check *w) |
|
|
1196 | { |
|
|
1197 | ev_clear_pending (EV_A_ (W)w); |
|
|
1198 | if (ev_is_active (w)) |
|
|
1199 | return; |
|
|
1200 | |
|
|
1201 | checks [w->active - 1] = checks [--checkcnt]; |
|
|
1202 | ev_stop (EV_A_ (W)w); |
|
|
1203 | } |
|
|
1204 | |
|
|
1205 | void |
|
|
1206 | ev_child_start (EV_P_ struct ev_child *w) |
1304 | ev_child_start (EV_P_ struct ev_child *w) |
1207 | { |
1305 | { |
|
|
1306 | #if EV_MULTIPLICITY |
|
|
1307 | assert (("child watchers are only supported in the default loop", loop == default_loop)); |
|
|
1308 | #endif |
1208 | if (ev_is_active (w)) |
1309 | if (ev_is_active (w)) |
1209 | return; |
1310 | return; |
1210 | |
1311 | |
1211 | ev_start (EV_A_ (W)w, 1); |
1312 | ev_start (EV_A_ (W)w, 1); |
1212 | wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1313 | wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
… | |
… | |
1284 | ev_timer_start (EV_A_ &once->to); |
1385 | ev_timer_start (EV_A_ &once->to); |
1285 | } |
1386 | } |
1286 | } |
1387 | } |
1287 | } |
1388 | } |
1288 | |
1389 | |
1289 | /*****************************************************************************/ |
|
|
1290 | |
|
|
1291 | #if 0 |
|
|
1292 | |
|
|
1293 | struct ev_io wio; |
|
|
1294 | |
|
|
1295 | static void |
|
|
1296 | sin_cb (struct ev_io *w, int revents) |
|
|
1297 | { |
|
|
1298 | fprintf (stderr, "sin %d, revents %d\n", w->fd, revents); |
|
|
1299 | } |
|
|
1300 | |
|
|
1301 | static void |
|
|
1302 | ocb (struct ev_timer *w, int revents) |
|
|
1303 | { |
|
|
1304 | //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data); |
|
|
1305 | ev_timer_stop (w); |
|
|
1306 | ev_timer_start (w); |
|
|
1307 | } |
|
|
1308 | |
|
|
1309 | static void |
|
|
1310 | scb (struct ev_signal *w, int revents) |
|
|
1311 | { |
|
|
1312 | fprintf (stderr, "signal %x,%d\n", revents, w->signum); |
|
|
1313 | ev_io_stop (&wio); |
|
|
1314 | ev_io_start (&wio); |
|
|
1315 | } |
|
|
1316 | |
|
|
1317 | static void |
|
|
1318 | gcb (struct ev_signal *w, int revents) |
|
|
1319 | { |
|
|
1320 | fprintf (stderr, "generic %x\n", revents); |
|
|
1321 | |
|
|
1322 | } |
|
|
1323 | |
|
|
1324 | int main (void) |
|
|
1325 | { |
|
|
1326 | ev_init (0); |
|
|
1327 | |
|
|
1328 | ev_io_init (&wio, sin_cb, 0, EV_READ); |
|
|
1329 | ev_io_start (&wio); |
|
|
1330 | |
|
|
1331 | struct ev_timer t[10000]; |
|
|
1332 | |
|
|
1333 | #if 0 |
|
|
1334 | int i; |
|
|
1335 | for (i = 0; i < 10000; ++i) |
|
|
1336 | { |
|
|
1337 | struct ev_timer *w = t + i; |
|
|
1338 | ev_watcher_init (w, ocb, i); |
|
|
1339 | ev_timer_init_abs (w, ocb, drand48 (), 0.99775533); |
|
|
1340 | ev_timer_start (w); |
|
|
1341 | if (drand48 () < 0.5) |
|
|
1342 | ev_timer_stop (w); |
|
|
1343 | } |
|
|
1344 | #endif |
|
|
1345 | |
|
|
1346 | struct ev_timer t1; |
|
|
1347 | ev_timer_init (&t1, ocb, 5, 10); |
|
|
1348 | ev_timer_start (&t1); |
|
|
1349 | |
|
|
1350 | struct ev_signal sig; |
|
|
1351 | ev_signal_init (&sig, scb, SIGQUIT); |
|
|
1352 | ev_signal_start (&sig); |
|
|
1353 | |
|
|
1354 | struct ev_check cw; |
|
|
1355 | ev_check_init (&cw, gcb); |
|
|
1356 | ev_check_start (&cw); |
|
|
1357 | |
|
|
1358 | struct ev_idle iw; |
|
|
1359 | ev_idle_init (&iw, gcb); |
|
|
1360 | ev_idle_start (&iw); |
|
|
1361 | |
|
|
1362 | ev_loop (0); |
|
|
1363 | |
|
|
1364 | return 0; |
|
|
1365 | } |
|
|
1366 | |
|
|
1367 | #endif |
|
|
1368 | |
|
|
1369 | |
|
|
1370 | |
|
|
1371 | |
|
|