… | |
… | |
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 | |
|
|
140 | static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
|
|
141 | |
118 | /*****************************************************************************/ |
142 | /*****************************************************************************/ |
119 | |
143 | |
120 | typedef struct |
144 | typedef struct |
121 | { |
145 | { |
122 | struct ev_watcher_list *head; |
146 | struct ev_watcher_list *head; |
… | |
… | |
128 | { |
152 | { |
129 | W w; |
153 | W w; |
130 | int events; |
154 | int events; |
131 | } ANPENDING; |
155 | } ANPENDING; |
132 | |
156 | |
133 | #ifdef EV_MULTIPLICITY |
157 | #if EV_MULTIPLICITY |
|
|
158 | |
134 | struct ev_loop |
159 | struct ev_loop |
135 | { |
160 | { |
136 | # define VAR(name,decl) decl |
161 | # define VAR(name,decl) decl; |
137 | # include "ev_vars.h" |
162 | # include "ev_vars.h" |
138 | }; |
163 | }; |
|
|
164 | # undef VAR |
|
|
165 | # include "ev_wrap.h" |
|
|
166 | |
139 | #else |
167 | #else |
|
|
168 | |
140 | # define VAR(name,decl) static decl |
169 | # define VAR(name,decl) static decl; |
141 | # include "ev_vars.h" |
170 | # include "ev_vars.h" |
142 | #endif |
|
|
143 | #undef VAR |
171 | # undef VAR |
|
|
172 | |
|
|
173 | #endif |
144 | |
174 | |
145 | /*****************************************************************************/ |
175 | /*****************************************************************************/ |
146 | |
176 | |
147 | inline ev_tstamp |
177 | inline ev_tstamp |
148 | ev_time (void) |
178 | ev_time (void) |
… | |
… | |
330 | fd_kill (EV_A_ fd); |
360 | fd_kill (EV_A_ fd); |
331 | return; |
361 | return; |
332 | } |
362 | } |
333 | } |
363 | } |
334 | |
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 | |
335 | /*****************************************************************************/ |
380 | /*****************************************************************************/ |
336 | |
381 | |
337 | static void |
382 | static void |
338 | upheap (WT *timers, int k) |
383 | upheap (WT *heap, int k) |
339 | { |
384 | { |
340 | WT w = timers [k]; |
385 | WT w = heap [k]; |
341 | |
386 | |
342 | while (k && timers [k >> 1]->at > w->at) |
387 | while (k && heap [k >> 1]->at > w->at) |
343 | { |
388 | { |
344 | timers [k] = timers [k >> 1]; |
389 | heap [k] = heap [k >> 1]; |
345 | timers [k]->active = k + 1; |
390 | heap [k]->active = k + 1; |
346 | k >>= 1; |
391 | k >>= 1; |
347 | } |
392 | } |
348 | |
393 | |
349 | timers [k] = w; |
394 | heap [k] = w; |
350 | timers [k]->active = k + 1; |
395 | heap [k]->active = k + 1; |
351 | |
396 | |
352 | } |
397 | } |
353 | |
398 | |
354 | static void |
399 | static void |
355 | downheap (WT *timers, int N, int k) |
400 | downheap (WT *heap, int N, int k) |
356 | { |
401 | { |
357 | WT w = timers [k]; |
402 | WT w = heap [k]; |
358 | |
403 | |
359 | while (k < (N >> 1)) |
404 | while (k < (N >> 1)) |
360 | { |
405 | { |
361 | int j = k << 1; |
406 | int j = k << 1; |
362 | |
407 | |
363 | if (j + 1 < N && timers [j]->at > timers [j + 1]->at) |
408 | if (j + 1 < N && heap [j]->at > heap [j + 1]->at) |
364 | ++j; |
409 | ++j; |
365 | |
410 | |
366 | if (w->at <= timers [j]->at) |
411 | if (w->at <= heap [j]->at) |
367 | break; |
412 | break; |
368 | |
413 | |
369 | timers [k] = timers [j]; |
414 | heap [k] = heap [j]; |
370 | timers [k]->active = k + 1; |
415 | heap [k]->active = k + 1; |
371 | k = j; |
416 | k = j; |
372 | } |
417 | } |
373 | |
418 | |
374 | timers [k] = w; |
419 | heap [k] = w; |
375 | timers [k]->active = k + 1; |
420 | heap [k]->active = k + 1; |
376 | } |
421 | } |
377 | |
422 | |
378 | /*****************************************************************************/ |
423 | /*****************************************************************************/ |
379 | |
424 | |
380 | typedef struct |
425 | typedef struct |
… | |
… | |
386 | static ANSIG *signals; |
431 | static ANSIG *signals; |
387 | static int signalmax; |
432 | static int signalmax; |
388 | |
433 | |
389 | static int sigpipe [2]; |
434 | static int sigpipe [2]; |
390 | static sig_atomic_t volatile gotsig; |
435 | static sig_atomic_t volatile gotsig; |
|
|
436 | static struct ev_io sigev; |
391 | |
437 | |
392 | static void |
438 | static void |
393 | signals_init (ANSIG *base, int count) |
439 | signals_init (ANSIG *base, int count) |
394 | { |
440 | { |
395 | while (count--) |
441 | while (count--) |
… | |
… | |
445 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
491 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
446 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
492 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
447 | #endif |
493 | #endif |
448 | |
494 | |
449 | ev_io_set (&sigev, sigpipe [0], EV_READ); |
495 | ev_io_set (&sigev, sigpipe [0], EV_READ); |
450 | ev_io_start (&sigev); |
496 | ev_io_start (EV_A_ &sigev); |
451 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
497 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
452 | } |
498 | } |
453 | |
499 | |
454 | /*****************************************************************************/ |
500 | /*****************************************************************************/ |
455 | |
501 | |
456 | #ifndef WIN32 |
502 | #ifndef WIN32 |
|
|
503 | |
|
|
504 | static struct ev_child *childs [PID_HASHSIZE]; |
|
|
505 | static struct ev_signal childev; |
457 | |
506 | |
458 | #ifndef WCONTINUED |
507 | #ifndef WCONTINUED |
459 | # define WCONTINUED 0 |
508 | # define WCONTINUED 0 |
460 | #endif |
509 | #endif |
461 | |
510 | |
… | |
… | |
497 | # include "ev_kqueue.c" |
546 | # include "ev_kqueue.c" |
498 | #endif |
547 | #endif |
499 | #if EV_USE_EPOLL |
548 | #if EV_USE_EPOLL |
500 | # include "ev_epoll.c" |
549 | # include "ev_epoll.c" |
501 | #endif |
550 | #endif |
502 | #if EV_USEV_POLL |
551 | #if EV_USE_POLL |
503 | # include "ev_poll.c" |
552 | # include "ev_poll.c" |
504 | #endif |
553 | #endif |
505 | #if EV_USE_SELECT |
554 | #if EV_USE_SELECT |
506 | # include "ev_select.c" |
555 | # include "ev_select.c" |
507 | #endif |
556 | #endif |
… | |
… | |
534 | ev_method (EV_P) |
583 | ev_method (EV_P) |
535 | { |
584 | { |
536 | return method; |
585 | return method; |
537 | } |
586 | } |
538 | |
587 | |
539 | int |
588 | static void |
540 | ev_init (EV_P_ int methods) |
589 | loop_init (EV_P_ int methods) |
541 | { |
590 | { |
542 | #ifdef EV_MULTIPLICITY |
|
|
543 | memset (loop, 0, sizeof (struct ev_loop)); |
|
|
544 | #endif |
|
|
545 | |
|
|
546 | if (!method) |
591 | if (!method) |
547 | { |
592 | { |
548 | #if EV_USE_MONOTONIC |
593 | #if EV_USE_MONOTONIC |
549 | { |
594 | { |
550 | struct timespec ts; |
595 | struct timespec ts; |
… | |
… | |
554 | #endif |
599 | #endif |
555 | |
600 | |
556 | rt_now = ev_time (); |
601 | rt_now = ev_time (); |
557 | mn_now = get_clock (); |
602 | mn_now = get_clock (); |
558 | now_floor = mn_now; |
603 | now_floor = mn_now; |
559 | diff = rt_now - mn_now; |
604 | rtmn_diff = rt_now - mn_now; |
560 | |
|
|
561 | if (pipe (sigpipe)) |
|
|
562 | return 0; |
|
|
563 | |
605 | |
564 | if (methods == EVMETHOD_AUTO) |
606 | if (methods == EVMETHOD_AUTO) |
565 | if (!enable_secure () && getenv ("LIBmethodS")) |
607 | if (!enable_secure () && getenv ("LIBEV_METHODS")) |
566 | methods = atoi (getenv ("LIBmethodS")); |
608 | methods = atoi (getenv ("LIBEV_METHODS")); |
567 | else |
609 | else |
568 | methods = EVMETHOD_ANY; |
610 | methods = EVMETHOD_ANY; |
569 | |
611 | |
570 | method = 0; |
612 | method = 0; |
571 | #if EV_USE_KQUEUE |
613 | #if EV_USE_KQUEUE |
572 | if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); |
614 | if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); |
573 | #endif |
615 | #endif |
574 | #if EV_USE_EPOLL |
616 | #if EV_USE_EPOLL |
575 | if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); |
617 | if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); |
576 | #endif |
618 | #endif |
577 | #if EV_USEV_POLL |
619 | #if EV_USE_POLL |
578 | if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); |
620 | if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); |
579 | #endif |
621 | #endif |
580 | #if EV_USE_SELECT |
622 | #if EV_USE_SELECT |
581 | if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); |
623 | if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); |
582 | #endif |
624 | #endif |
|
|
625 | } |
|
|
626 | } |
583 | |
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 | |
584 | if (method) |
715 | if (ev_method (EV_A)) |
585 | { |
716 | { |
586 | ev_watcher_init (&sigev, sigcb); |
717 | ev_watcher_init (&sigev, sigcb); |
587 | ev_set_priority (&sigev, EV_MAXPRI); |
718 | ev_set_priority (&sigev, EV_MAXPRI); |
588 | siginit (EV_A); |
719 | siginit (EV_A); |
589 | |
720 | |
… | |
… | |
592 | ev_set_priority (&childev, EV_MAXPRI); |
723 | ev_set_priority (&childev, EV_MAXPRI); |
593 | ev_signal_start (EV_A_ &childev); |
724 | ev_signal_start (EV_A_ &childev); |
594 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
725 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
595 | #endif |
726 | #endif |
596 | } |
727 | } |
|
|
728 | else |
|
|
729 | default_loop = 0; |
597 | } |
730 | } |
598 | |
731 | |
599 | return method; |
732 | return default_loop; |
600 | } |
733 | } |
601 | |
734 | |
602 | /*****************************************************************************/ |
|
|
603 | |
|
|
604 | void |
735 | void |
605 | ev_fork_prepare (void) |
736 | ev_default_destroy (void) |
606 | { |
737 | { |
607 | /* nop */ |
738 | #if EV_MULTIPLICITY |
608 | } |
739 | struct ev_loop *loop = default_loop; |
609 | |
|
|
610 | void |
|
|
611 | ev_fork_parent (void) |
|
|
612 | { |
|
|
613 | /* nop */ |
|
|
614 | } |
|
|
615 | |
|
|
616 | void |
|
|
617 | ev_fork_child (void) |
|
|
618 | { |
|
|
619 | #if EV_USE_EPOLL |
|
|
620 | if (method == EVMETHOD_EPOLL) |
|
|
621 | epoll_postfork_child (); |
|
|
622 | #endif |
740 | #endif |
623 | |
741 | |
|
|
742 | ev_ref (EV_A); /* child watcher */ |
|
|
743 | ev_signal_stop (EV_A_ &childev); |
|
|
744 | |
|
|
745 | ev_ref (EV_A); /* signal watcher */ |
624 | 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); |
625 | close (sigpipe [0]); |
764 | close (sigpipe [0]); |
626 | close (sigpipe [1]); |
765 | close (sigpipe [1]); |
627 | pipe (sigpipe); |
766 | pipe (sigpipe); |
|
|
767 | |
|
|
768 | ev_ref (EV_A); /* signal watcher */ |
628 | siginit (); |
769 | siginit (EV_A); |
629 | } |
770 | } |
630 | |
771 | |
631 | /*****************************************************************************/ |
772 | /*****************************************************************************/ |
632 | |
773 | |
633 | static void |
774 | static void |
… | |
… | |
652 | timers_reify (EV_P) |
793 | timers_reify (EV_P) |
653 | { |
794 | { |
654 | while (timercnt && timers [0]->at <= mn_now) |
795 | while (timercnt && timers [0]->at <= mn_now) |
655 | { |
796 | { |
656 | 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))); |
657 | |
800 | |
658 | /* first reschedule or stop timer */ |
801 | /* first reschedule or stop timer */ |
659 | if (w->repeat) |
802 | if (w->repeat) |
660 | { |
803 | { |
661 | 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.)); |
… | |
… | |
663 | downheap ((WT *)timers, timercnt, 0); |
806 | downheap ((WT *)timers, timercnt, 0); |
664 | } |
807 | } |
665 | else |
808 | else |
666 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
809 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
667 | |
810 | |
668 | event ((W)w, EV_TIMEOUT); |
811 | event (EV_A_ (W)w, EV_TIMEOUT); |
669 | } |
812 | } |
670 | } |
813 | } |
671 | |
814 | |
672 | static void |
815 | static void |
673 | periodics_reify (EV_P) |
816 | periodics_reify (EV_P) |
674 | { |
817 | { |
675 | while (periodiccnt && periodics [0]->at <= rt_now) |
818 | while (periodiccnt && periodics [0]->at <= rt_now) |
676 | { |
819 | { |
677 | 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))); |
678 | |
823 | |
679 | /* first reschedule or stop timer */ |
824 | /* first reschedule or stop timer */ |
680 | if (w->interval) |
825 | if (w->interval) |
681 | { |
826 | { |
682 | 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; |
… | |
… | |
689 | event (EV_A_ (W)w, EV_PERIODIC); |
834 | event (EV_A_ (W)w, EV_PERIODIC); |
690 | } |
835 | } |
691 | } |
836 | } |
692 | |
837 | |
693 | static void |
838 | static void |
694 | periodics_reschedule (EV_P_ ev_tstamp diff) |
839 | periodics_reschedule (EV_P) |
695 | { |
840 | { |
696 | int i; |
841 | int i; |
697 | |
842 | |
698 | /* adjust periodics after time jump */ |
843 | /* adjust periodics after time jump */ |
699 | for (i = 0; i < periodiccnt; ++i) |
844 | for (i = 0; i < periodiccnt; ++i) |
… | |
… | |
720 | { |
865 | { |
721 | mn_now = get_clock (); |
866 | mn_now = get_clock (); |
722 | |
867 | |
723 | if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) |
868 | if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) |
724 | { |
869 | { |
725 | rt_now = mn_now + diff; |
870 | rt_now = rtmn_diff + mn_now; |
726 | return 0; |
871 | return 0; |
727 | } |
872 | } |
728 | else |
873 | else |
729 | { |
874 | { |
730 | now_floor = mn_now; |
875 | now_floor = mn_now; |
… | |
… | |
741 | #if EV_USE_MONOTONIC |
886 | #if EV_USE_MONOTONIC |
742 | if (expect_true (have_monotonic)) |
887 | if (expect_true (have_monotonic)) |
743 | { |
888 | { |
744 | if (time_update_monotonic (EV_A)) |
889 | if (time_update_monotonic (EV_A)) |
745 | { |
890 | { |
746 | ev_tstamp odiff = diff; |
891 | ev_tstamp odiff = rtmn_diff; |
747 | |
892 | |
748 | 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 */ |
749 | { |
894 | { |
750 | diff = rt_now - mn_now; |
895 | rtmn_diff = rt_now - mn_now; |
751 | |
896 | |
752 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
897 | if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) |
753 | return; /* all is well */ |
898 | return; /* all is well */ |
754 | |
899 | |
755 | rt_now = ev_time (); |
900 | rt_now = ev_time (); |
756 | mn_now = get_clock (); |
901 | mn_now = get_clock (); |
757 | now_floor = mn_now; |
902 | now_floor = mn_now; |
758 | } |
903 | } |
759 | |
904 | |
760 | periodics_reschedule (EV_A_ diff - odiff); |
905 | periodics_reschedule (EV_A); |
761 | /* 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) */ |
762 | } |
908 | } |
763 | } |
909 | } |
764 | else |
910 | else |
765 | #endif |
911 | #endif |
766 | { |
912 | { |
767 | rt_now = ev_time (); |
913 | rt_now = ev_time (); |
768 | |
914 | |
769 | 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)) |
770 | { |
916 | { |
771 | periodics_reschedule (EV_A_ rt_now - mn_now); |
917 | periodics_reschedule (EV_A); |
772 | |
918 | |
773 | /* 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 */ |
774 | for (i = 0; i < timercnt; ++i) |
920 | for (i = 0; i < timercnt; ++i) |
775 | timers [i]->at += diff; |
921 | timers [i]->at += rt_now - mn_now; |
776 | } |
922 | } |
777 | |
923 | |
778 | mn_now = rt_now; |
924 | mn_now = rt_now; |
779 | } |
925 | } |
780 | } |
926 | } |
… | |
… | |
1043 | } |
1189 | } |
1044 | |
1190 | |
1045 | ev_stop (EV_A_ (W)w); |
1191 | ev_stop (EV_A_ (W)w); |
1046 | } |
1192 | } |
1047 | |
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 | |
1048 | #ifndef SA_RESTART |
1260 | #ifndef SA_RESTART |
1049 | # define SA_RESTART 0 |
1261 | # define SA_RESTART 0 |
1050 | #endif |
1262 | #endif |
1051 | |
1263 | |
1052 | void |
1264 | void |
1053 | ev_signal_start (EV_P_ struct ev_signal *w) |
1265 | ev_signal_start (EV_P_ struct ev_signal *w) |
1054 | { |
1266 | { |
|
|
1267 | #if EV_MULTIPLICITY |
|
|
1268 | assert (("signal watchers are only supported in the default loop", loop == default_loop)); |
|
|
1269 | #endif |
1055 | if (ev_is_active (w)) |
1270 | if (ev_is_active (w)) |
1056 | return; |
1271 | return; |
1057 | |
1272 | |
1058 | 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)); |
1059 | |
1274 | |
… | |
… | |
1084 | if (!signals [w->signum - 1].head) |
1299 | if (!signals [w->signum - 1].head) |
1085 | signal (w->signum, SIG_DFL); |
1300 | signal (w->signum, SIG_DFL); |
1086 | } |
1301 | } |
1087 | |
1302 | |
1088 | void |
1303 | void |
1089 | ev_idle_start (EV_P_ struct ev_idle *w) |
|
|
1090 | { |
|
|
1091 | if (ev_is_active (w)) |
|
|
1092 | return; |
|
|
1093 | |
|
|
1094 | ev_start (EV_A_ (W)w, ++idlecnt); |
|
|
1095 | array_needsize (idles, idlemax, idlecnt, ); |
|
|
1096 | idles [idlecnt - 1] = w; |
|
|
1097 | } |
|
|
1098 | |
|
|
1099 | void |
|
|
1100 | ev_idle_stop (EV_P_ struct ev_idle *w) |
|
|
1101 | { |
|
|
1102 | ev_clear_pending (EV_A_ (W)w); |
|
|
1103 | if (ev_is_active (w)) |
|
|
1104 | return; |
|
|
1105 | |
|
|
1106 | idles [w->active - 1] = idles [--idlecnt]; |
|
|
1107 | ev_stop (EV_A_ (W)w); |
|
|
1108 | } |
|
|
1109 | |
|
|
1110 | void |
|
|
1111 | ev_prepare_start (EV_P_ struct ev_prepare *w) |
|
|
1112 | { |
|
|
1113 | if (ev_is_active (w)) |
|
|
1114 | return; |
|
|
1115 | |
|
|
1116 | ev_start (EV_A_ (W)w, ++preparecnt); |
|
|
1117 | array_needsize (prepares, preparemax, preparecnt, ); |
|
|
1118 | prepares [preparecnt - 1] = w; |
|
|
1119 | } |
|
|
1120 | |
|
|
1121 | void |
|
|
1122 | ev_prepare_stop (EV_P_ struct ev_prepare *w) |
|
|
1123 | { |
|
|
1124 | ev_clear_pending (EV_A_ (W)w); |
|
|
1125 | if (ev_is_active (w)) |
|
|
1126 | return; |
|
|
1127 | |
|
|
1128 | prepares [w->active - 1] = prepares [--preparecnt]; |
|
|
1129 | ev_stop (EV_A_ (W)w); |
|
|
1130 | } |
|
|
1131 | |
|
|
1132 | void |
|
|
1133 | ev_check_start (EV_P_ struct ev_check *w) |
|
|
1134 | { |
|
|
1135 | if (ev_is_active (w)) |
|
|
1136 | return; |
|
|
1137 | |
|
|
1138 | ev_start (EV_A_ (W)w, ++checkcnt); |
|
|
1139 | array_needsize (checks, checkmax, checkcnt, ); |
|
|
1140 | checks [checkcnt - 1] = w; |
|
|
1141 | } |
|
|
1142 | |
|
|
1143 | void |
|
|
1144 | ev_check_stop (EV_P_ struct ev_check *w) |
|
|
1145 | { |
|
|
1146 | ev_clear_pending (EV_A_ (W)w); |
|
|
1147 | if (ev_is_active (w)) |
|
|
1148 | return; |
|
|
1149 | |
|
|
1150 | checks [w->active - 1] = checks [--checkcnt]; |
|
|
1151 | ev_stop (EV_A_ (W)w); |
|
|
1152 | } |
|
|
1153 | |
|
|
1154 | void |
|
|
1155 | ev_child_start (EV_P_ struct ev_child *w) |
1304 | ev_child_start (EV_P_ struct ev_child *w) |
1156 | { |
1305 | { |
|
|
1306 | #if EV_MULTIPLICITY |
|
|
1307 | assert (("child watchers are only supported in the default loop", loop == default_loop)); |
|
|
1308 | #endif |
1157 | if (ev_is_active (w)) |
1309 | if (ev_is_active (w)) |
1158 | return; |
1310 | return; |
1159 | |
1311 | |
1160 | ev_start (EV_A_ (W)w, 1); |
1312 | ev_start (EV_A_ (W)w, 1); |
1161 | wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1313 | wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
… | |
… | |
1233 | ev_timer_start (EV_A_ &once->to); |
1385 | ev_timer_start (EV_A_ &once->to); |
1234 | } |
1386 | } |
1235 | } |
1387 | } |
1236 | } |
1388 | } |
1237 | |
1389 | |
1238 | /*****************************************************************************/ |
|
|
1239 | |
|
|
1240 | #if 0 |
|
|
1241 | |
|
|
1242 | struct ev_io wio; |
|
|
1243 | |
|
|
1244 | static void |
|
|
1245 | sin_cb (struct ev_io *w, int revents) |
|
|
1246 | { |
|
|
1247 | fprintf (stderr, "sin %d, revents %d\n", w->fd, revents); |
|
|
1248 | } |
|
|
1249 | |
|
|
1250 | static void |
|
|
1251 | ocb (struct ev_timer *w, int revents) |
|
|
1252 | { |
|
|
1253 | //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data); |
|
|
1254 | ev_timer_stop (w); |
|
|
1255 | ev_timer_start (w); |
|
|
1256 | } |
|
|
1257 | |
|
|
1258 | static void |
|
|
1259 | scb (struct ev_signal *w, int revents) |
|
|
1260 | { |
|
|
1261 | fprintf (stderr, "signal %x,%d\n", revents, w->signum); |
|
|
1262 | ev_io_stop (&wio); |
|
|
1263 | ev_io_start (&wio); |
|
|
1264 | } |
|
|
1265 | |
|
|
1266 | static void |
|
|
1267 | gcb (struct ev_signal *w, int revents) |
|
|
1268 | { |
|
|
1269 | fprintf (stderr, "generic %x\n", revents); |
|
|
1270 | |
|
|
1271 | } |
|
|
1272 | |
|
|
1273 | int main (void) |
|
|
1274 | { |
|
|
1275 | ev_init (0); |
|
|
1276 | |
|
|
1277 | ev_io_init (&wio, sin_cb, 0, EV_READ); |
|
|
1278 | ev_io_start (&wio); |
|
|
1279 | |
|
|
1280 | struct ev_timer t[10000]; |
|
|
1281 | |
|
|
1282 | #if 0 |
|
|
1283 | int i; |
|
|
1284 | for (i = 0; i < 10000; ++i) |
|
|
1285 | { |
|
|
1286 | struct ev_timer *w = t + i; |
|
|
1287 | ev_watcher_init (w, ocb, i); |
|
|
1288 | ev_timer_init_abs (w, ocb, drand48 (), 0.99775533); |
|
|
1289 | ev_timer_start (w); |
|
|
1290 | if (drand48 () < 0.5) |
|
|
1291 | ev_timer_stop (w); |
|
|
1292 | } |
|
|
1293 | #endif |
|
|
1294 | |
|
|
1295 | struct ev_timer t1; |
|
|
1296 | ev_timer_init (&t1, ocb, 5, 10); |
|
|
1297 | ev_timer_start (&t1); |
|
|
1298 | |
|
|
1299 | struct ev_signal sig; |
|
|
1300 | ev_signal_init (&sig, scb, SIGQUIT); |
|
|
1301 | ev_signal_start (&sig); |
|
|
1302 | |
|
|
1303 | struct ev_check cw; |
|
|
1304 | ev_check_init (&cw, gcb); |
|
|
1305 | ev_check_start (&cw); |
|
|
1306 | |
|
|
1307 | struct ev_idle iw; |
|
|
1308 | ev_idle_init (&iw, gcb); |
|
|
1309 | ev_idle_start (&iw); |
|
|
1310 | |
|
|
1311 | ev_loop (0); |
|
|
1312 | |
|
|
1313 | return 0; |
|
|
1314 | } |
|
|
1315 | |
|
|
1316 | #endif |
|
|
1317 | |
|
|
1318 | |
|
|
1319 | |
|
|
1320 | |
|
|