… | |
… | |
26 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
26 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
27 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
30 | */ |
30 | */ |
31 | #ifndef EV_EMBED |
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 |
70 | |
92 | |
71 | #ifndef EV_USE_KQUEUE |
93 | #ifndef EV_USE_KQUEUE |
72 | # define EV_USE_KQUEUE 0 |
94 | # define EV_USE_KQUEUE 0 |
|
|
95 | #endif |
|
|
96 | |
|
|
97 | #ifndef EV_USE_WIN32 |
|
|
98 | # ifdef WIN32 |
|
|
99 | # define EV_USE_WIN32 1 |
|
|
100 | # else |
|
|
101 | # define EV_USE_WIN32 0 |
|
|
102 | # endif |
73 | #endif |
103 | #endif |
74 | |
104 | |
75 | #ifndef EV_USE_REALTIME |
105 | #ifndef EV_USE_REALTIME |
76 | # define EV_USE_REALTIME 1 |
106 | # define EV_USE_REALTIME 1 |
77 | #endif |
107 | #endif |
… | |
… | |
93 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
123 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
94 | #define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detect time jumps) */ |
124 | #define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detect time jumps) */ |
95 | #define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */ |
125 | #define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */ |
96 | /*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */ |
126 | /*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */ |
97 | |
127 | |
98 | #ifndef EV_EMBED |
|
|
99 | # include "ev.h" |
128 | #include "ev.h" |
100 | #endif |
|
|
101 | |
129 | |
102 | #if __GNUC__ >= 3 |
130 | #if __GNUC__ >= 3 |
103 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
131 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
104 | # define inline inline |
132 | # define inline inline |
105 | #else |
133 | #else |
… | |
… | |
329 | |
357 | |
330 | /* called on ENOMEM in select/poll to kill some fds and retry */ |
358 | /* called on ENOMEM in select/poll to kill some fds and retry */ |
331 | static void |
359 | static void |
332 | fd_enomem (EV_P) |
360 | fd_enomem (EV_P) |
333 | { |
361 | { |
334 | int fd = anfdmax; |
362 | int fd; |
335 | |
363 | |
336 | while (fd--) |
364 | for (fd = anfdmax; fd--; ) |
337 | if (anfds [fd].events) |
365 | if (anfds [fd].events) |
338 | { |
366 | { |
339 | close (fd); |
367 | close (fd); |
340 | fd_kill (EV_A_ fd); |
368 | fd_kill (EV_A_ fd); |
341 | return; |
369 | return; |
… | |
… | |
351 | /* this should be highly optimised to not do anything but set a flag */ |
379 | /* this should be highly optimised to not do anything but set a flag */ |
352 | for (fd = 0; fd < anfdmax; ++fd) |
380 | for (fd = 0; fd < anfdmax; ++fd) |
353 | if (anfds [fd].events) |
381 | if (anfds [fd].events) |
354 | { |
382 | { |
355 | anfds [fd].events = 0; |
383 | anfds [fd].events = 0; |
356 | fd_change (fd); |
384 | fd_change (EV_A_ fd); |
357 | } |
385 | } |
358 | } |
386 | } |
359 | |
387 | |
360 | /*****************************************************************************/ |
388 | /*****************************************************************************/ |
361 | |
389 | |
… | |
… | |
365 | WT w = heap [k]; |
393 | WT w = heap [k]; |
366 | |
394 | |
367 | while (k && heap [k >> 1]->at > w->at) |
395 | while (k && heap [k >> 1]->at > w->at) |
368 | { |
396 | { |
369 | heap [k] = heap [k >> 1]; |
397 | heap [k] = heap [k >> 1]; |
370 | heap [k]->active = k + 1; |
398 | ((W)heap [k])->active = k + 1; |
371 | k >>= 1; |
399 | k >>= 1; |
372 | } |
400 | } |
373 | |
401 | |
374 | heap [k] = w; |
402 | heap [k] = w; |
375 | heap [k]->active = k + 1; |
403 | ((W)heap [k])->active = k + 1; |
376 | |
404 | |
377 | } |
405 | } |
378 | |
406 | |
379 | static void |
407 | static void |
380 | downheap (WT *heap, int N, int k) |
408 | downheap (WT *heap, int N, int k) |
… | |
… | |
390 | |
418 | |
391 | if (w->at <= heap [j]->at) |
419 | if (w->at <= heap [j]->at) |
392 | break; |
420 | break; |
393 | |
421 | |
394 | heap [k] = heap [j]; |
422 | heap [k] = heap [j]; |
395 | heap [k]->active = k + 1; |
423 | ((W)heap [k])->active = k + 1; |
396 | k = j; |
424 | k = j; |
397 | } |
425 | } |
398 | |
426 | |
399 | heap [k] = w; |
427 | heap [k] = w; |
400 | heap [k]->active = k + 1; |
428 | ((W)heap [k])->active = k + 1; |
401 | } |
429 | } |
402 | |
430 | |
403 | /*****************************************************************************/ |
431 | /*****************************************************************************/ |
404 | |
432 | |
405 | typedef struct |
433 | typedef struct |
… | |
… | |
411 | static ANSIG *signals; |
439 | static ANSIG *signals; |
412 | static int signalmax; |
440 | static int signalmax; |
413 | |
441 | |
414 | static int sigpipe [2]; |
442 | static int sigpipe [2]; |
415 | static sig_atomic_t volatile gotsig; |
443 | static sig_atomic_t volatile gotsig; |
|
|
444 | static struct ev_io sigev; |
416 | |
445 | |
417 | static void |
446 | static void |
418 | signals_init (ANSIG *base, int count) |
447 | signals_init (ANSIG *base, int count) |
419 | { |
448 | { |
420 | while (count--) |
449 | while (count--) |
… | |
… | |
478 | |
507 | |
479 | /*****************************************************************************/ |
508 | /*****************************************************************************/ |
480 | |
509 | |
481 | #ifndef WIN32 |
510 | #ifndef WIN32 |
482 | |
511 | |
|
|
512 | static struct ev_child *childs [PID_HASHSIZE]; |
|
|
513 | static struct ev_signal childev; |
|
|
514 | |
483 | #ifndef WCONTINUED |
515 | #ifndef WCONTINUED |
484 | # define WCONTINUED 0 |
516 | # define WCONTINUED 0 |
485 | #endif |
517 | #endif |
486 | |
518 | |
487 | static void |
519 | static void |
… | |
… | |
490 | struct ev_child *w; |
522 | struct ev_child *w; |
491 | |
523 | |
492 | for (w = (struct ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (struct ev_child *)((WL)w)->next) |
524 | for (w = (struct ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (struct ev_child *)((WL)w)->next) |
493 | if (w->pid == pid || !w->pid) |
525 | if (w->pid == pid || !w->pid) |
494 | { |
526 | { |
495 | w->priority = sw->priority; /* need to do it *now* */ |
527 | ev_priority (w) = ev_priority (sw); /* need to do it *now* */ |
496 | w->rpid = pid; |
528 | w->rpid = pid; |
497 | w->rstatus = status; |
529 | w->rstatus = status; |
498 | event (EV_A_ (W)w, EV_CHILD); |
530 | event (EV_A_ (W)w, EV_CHILD); |
499 | } |
531 | } |
500 | } |
532 | } |
501 | |
533 | |
502 | static void |
534 | static void |
… | |
… | |
522 | # include "ev_kqueue.c" |
554 | # include "ev_kqueue.c" |
523 | #endif |
555 | #endif |
524 | #if EV_USE_EPOLL |
556 | #if EV_USE_EPOLL |
525 | # include "ev_epoll.c" |
557 | # include "ev_epoll.c" |
526 | #endif |
558 | #endif |
527 | #if EV_USEV_POLL |
559 | #if EV_USE_POLL |
528 | # include "ev_poll.c" |
560 | # include "ev_poll.c" |
529 | #endif |
561 | #endif |
530 | #if EV_USE_SELECT |
562 | #if EV_USE_SELECT |
531 | # include "ev_select.c" |
563 | # include "ev_select.c" |
532 | #endif |
564 | #endif |
… | |
… | |
584 | methods = atoi (getenv ("LIBEV_METHODS")); |
616 | methods = atoi (getenv ("LIBEV_METHODS")); |
585 | else |
617 | else |
586 | methods = EVMETHOD_ANY; |
618 | methods = EVMETHOD_ANY; |
587 | |
619 | |
588 | method = 0; |
620 | method = 0; |
|
|
621 | #if EV_USE_WIN32 |
|
|
622 | if (!method && (methods & EVMETHOD_WIN32 )) method = win32_init (EV_A_ methods); |
|
|
623 | #endif |
589 | #if EV_USE_KQUEUE |
624 | #if EV_USE_KQUEUE |
590 | if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); |
625 | if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); |
591 | #endif |
626 | #endif |
592 | #if EV_USE_EPOLL |
627 | #if EV_USE_EPOLL |
593 | if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); |
628 | if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); |
594 | #endif |
629 | #endif |
595 | #if EV_USEV_POLL |
630 | #if EV_USE_POLL |
596 | if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); |
631 | if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); |
597 | #endif |
632 | #endif |
598 | #if EV_USE_SELECT |
633 | #if EV_USE_SELECT |
599 | if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); |
634 | if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); |
600 | #endif |
635 | #endif |
… | |
… | |
602 | } |
637 | } |
603 | |
638 | |
604 | void |
639 | void |
605 | loop_destroy (EV_P) |
640 | loop_destroy (EV_P) |
606 | { |
641 | { |
|
|
642 | #if EV_USE_WIN32 |
|
|
643 | if (method == EVMETHOD_WIN32 ) win32_destroy (EV_A); |
|
|
644 | #endif |
607 | #if EV_USE_KQUEUE |
645 | #if EV_USE_KQUEUE |
608 | if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); |
646 | if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); |
609 | #endif |
647 | #endif |
610 | #if EV_USE_EPOLL |
648 | #if EV_USE_EPOLL |
611 | if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A); |
649 | if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A); |
612 | #endif |
650 | #endif |
613 | #if EV_USEV_POLL |
651 | #if EV_USE_POLL |
614 | if (method == EVMETHOD_POLL ) poll_destroy (EV_A); |
652 | if (method == EVMETHOD_POLL ) poll_destroy (EV_A); |
615 | #endif |
653 | #endif |
616 | #if EV_USE_SELECT |
654 | #if EV_USE_SELECT |
617 | if (method == EVMETHOD_SELECT) select_destroy (EV_A); |
655 | if (method == EVMETHOD_SELECT) select_destroy (EV_A); |
618 | #endif |
656 | #endif |
… | |
… | |
639 | { |
677 | { |
640 | struct ev_loop *loop = (struct ev_loop *)calloc (1, sizeof (struct ev_loop)); |
678 | struct ev_loop *loop = (struct ev_loop *)calloc (1, sizeof (struct ev_loop)); |
641 | |
679 | |
642 | loop_init (EV_A_ methods); |
680 | loop_init (EV_A_ methods); |
643 | |
681 | |
644 | if (ev_methods (EV_A)) |
682 | if (ev_method (EV_A)) |
645 | return loop; |
683 | return loop; |
646 | |
684 | |
647 | return 0; |
685 | return 0; |
648 | } |
686 | } |
649 | |
687 | |
… | |
… | |
726 | |
764 | |
727 | loop_destroy (EV_A); |
765 | loop_destroy (EV_A); |
728 | } |
766 | } |
729 | |
767 | |
730 | void |
768 | void |
731 | ev_default_fork (EV_P) |
769 | ev_default_fork (void) |
732 | { |
770 | { |
|
|
771 | #if EV_MULTIPLICITY |
|
|
772 | struct ev_loop *loop = default_loop; |
|
|
773 | #endif |
|
|
774 | |
733 | loop_fork (EV_A); |
775 | loop_fork (EV_A); |
734 | |
776 | |
735 | ev_io_stop (EV_A_ &sigev); |
777 | ev_io_stop (EV_A_ &sigev); |
736 | close (sigpipe [0]); |
778 | close (sigpipe [0]); |
737 | close (sigpipe [1]); |
779 | close (sigpipe [1]); |
… | |
… | |
754 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
796 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
755 | |
797 | |
756 | if (p->w) |
798 | if (p->w) |
757 | { |
799 | { |
758 | p->w->pending = 0; |
800 | p->w->pending = 0; |
|
|
801 | |
759 | p->w->cb (EV_A_ p->w, p->events); |
802 | (*(void (**)(EV_P_ W, int))&p->w->cb) (EV_A_ p->w, p->events); |
760 | } |
803 | } |
761 | } |
804 | } |
762 | } |
805 | } |
763 | |
806 | |
764 | static void |
807 | static void |
765 | timers_reify (EV_P) |
808 | timers_reify (EV_P) |
766 | { |
809 | { |
767 | while (timercnt && timers [0]->at <= mn_now) |
810 | while (timercnt && ((WT)timers [0])->at <= mn_now) |
768 | { |
811 | { |
769 | struct ev_timer *w = timers [0]; |
812 | struct ev_timer *w = timers [0]; |
|
|
813 | |
|
|
814 | assert (("inactive timer on timer heap detected", ev_is_active (w))); |
770 | |
815 | |
771 | /* first reschedule or stop timer */ |
816 | /* first reschedule or stop timer */ |
772 | if (w->repeat) |
817 | if (w->repeat) |
773 | { |
818 | { |
774 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
819 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
775 | w->at = mn_now + w->repeat; |
820 | ((WT)w)->at = mn_now + w->repeat; |
776 | downheap ((WT *)timers, timercnt, 0); |
821 | downheap ((WT *)timers, timercnt, 0); |
777 | } |
822 | } |
778 | else |
823 | else |
779 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
824 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
780 | |
825 | |
… | |
… | |
783 | } |
828 | } |
784 | |
829 | |
785 | static void |
830 | static void |
786 | periodics_reify (EV_P) |
831 | periodics_reify (EV_P) |
787 | { |
832 | { |
788 | while (periodiccnt && periodics [0]->at <= rt_now) |
833 | while (periodiccnt && ((WT)periodics [0])->at <= rt_now) |
789 | { |
834 | { |
790 | struct ev_periodic *w = periodics [0]; |
835 | struct ev_periodic *w = periodics [0]; |
|
|
836 | |
|
|
837 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
791 | |
838 | |
792 | /* first reschedule or stop timer */ |
839 | /* first reschedule or stop timer */ |
793 | if (w->interval) |
840 | if (w->interval) |
794 | { |
841 | { |
795 | w->at += floor ((rt_now - w->at) / w->interval + 1.) * w->interval; |
842 | ((WT)w)->at += floor ((rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval; |
796 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > rt_now)); |
843 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > rt_now)); |
797 | downheap ((WT *)periodics, periodiccnt, 0); |
844 | downheap ((WT *)periodics, periodiccnt, 0); |
798 | } |
845 | } |
799 | else |
846 | else |
800 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
847 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
801 | |
848 | |
… | |
… | |
813 | { |
860 | { |
814 | struct ev_periodic *w = periodics [i]; |
861 | struct ev_periodic *w = periodics [i]; |
815 | |
862 | |
816 | if (w->interval) |
863 | if (w->interval) |
817 | { |
864 | { |
818 | ev_tstamp diff = ceil ((rt_now - w->at) / w->interval) * w->interval; |
865 | ev_tstamp diff = ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; |
819 | |
866 | |
820 | if (fabs (diff) >= 1e-4) |
867 | if (fabs (diff) >= 1e-4) |
821 | { |
868 | { |
822 | ev_periodic_stop (EV_A_ w); |
869 | ev_periodic_stop (EV_A_ w); |
823 | ev_periodic_start (EV_A_ w); |
870 | ev_periodic_start (EV_A_ w); |
… | |
… | |
884 | { |
931 | { |
885 | periodics_reschedule (EV_A); |
932 | periodics_reschedule (EV_A); |
886 | |
933 | |
887 | /* adjust timers. this is easy, as the offset is the same for all */ |
934 | /* adjust timers. this is easy, as the offset is the same for all */ |
888 | for (i = 0; i < timercnt; ++i) |
935 | for (i = 0; i < timercnt; ++i) |
889 | timers [i]->at += rt_now - mn_now; |
936 | ((WT)timers [i])->at += rt_now - mn_now; |
890 | } |
937 | } |
891 | |
938 | |
892 | mn_now = rt_now; |
939 | mn_now = rt_now; |
893 | } |
940 | } |
894 | } |
941 | } |
… | |
… | |
945 | { |
992 | { |
946 | block = MAX_BLOCKTIME; |
993 | block = MAX_BLOCKTIME; |
947 | |
994 | |
948 | if (timercnt) |
995 | if (timercnt) |
949 | { |
996 | { |
950 | ev_tstamp to = timers [0]->at - mn_now + method_fudge; |
997 | ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge; |
951 | if (block > to) block = to; |
998 | if (block > to) block = to; |
952 | } |
999 | } |
953 | |
1000 | |
954 | if (periodiccnt) |
1001 | if (periodiccnt) |
955 | { |
1002 | { |
956 | ev_tstamp to = periodics [0]->at - rt_now + method_fudge; |
1003 | ev_tstamp to = ((WT)periodics [0])->at - rt_now + method_fudge; |
957 | if (block > to) block = to; |
1004 | if (block > to) block = to; |
958 | } |
1005 | } |
959 | |
1006 | |
960 | if (block < 0.) block = 0.; |
1007 | if (block < 0.) block = 0.; |
961 | } |
1008 | } |
… | |
… | |
1078 | ev_timer_start (EV_P_ struct ev_timer *w) |
1125 | ev_timer_start (EV_P_ struct ev_timer *w) |
1079 | { |
1126 | { |
1080 | if (ev_is_active (w)) |
1127 | if (ev_is_active (w)) |
1081 | return; |
1128 | return; |
1082 | |
1129 | |
1083 | w->at += mn_now; |
1130 | ((WT)w)->at += mn_now; |
1084 | |
1131 | |
1085 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
1132 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
1086 | |
1133 | |
1087 | ev_start (EV_A_ (W)w, ++timercnt); |
1134 | ev_start (EV_A_ (W)w, ++timercnt); |
1088 | array_needsize (timers, timermax, timercnt, ); |
1135 | array_needsize (timers, timermax, timercnt, ); |
1089 | timers [timercnt - 1] = w; |
1136 | timers [timercnt - 1] = w; |
1090 | upheap ((WT *)timers, timercnt - 1); |
1137 | upheap ((WT *)timers, timercnt - 1); |
|
|
1138 | |
|
|
1139 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1091 | } |
1140 | } |
1092 | |
1141 | |
1093 | void |
1142 | void |
1094 | ev_timer_stop (EV_P_ struct ev_timer *w) |
1143 | ev_timer_stop (EV_P_ struct ev_timer *w) |
1095 | { |
1144 | { |
1096 | ev_clear_pending (EV_A_ (W)w); |
1145 | ev_clear_pending (EV_A_ (W)w); |
1097 | if (!ev_is_active (w)) |
1146 | if (!ev_is_active (w)) |
1098 | return; |
1147 | return; |
1099 | |
1148 | |
|
|
1149 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
|
|
1150 | |
1100 | if (w->active < timercnt--) |
1151 | if (((W)w)->active < timercnt--) |
1101 | { |
1152 | { |
1102 | timers [w->active - 1] = timers [timercnt]; |
1153 | timers [((W)w)->active - 1] = timers [timercnt]; |
1103 | downheap ((WT *)timers, timercnt, w->active - 1); |
1154 | downheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1104 | } |
1155 | } |
1105 | |
1156 | |
1106 | w->at = w->repeat; |
1157 | ((WT)w)->at = w->repeat; |
1107 | |
1158 | |
1108 | ev_stop (EV_A_ (W)w); |
1159 | ev_stop (EV_A_ (W)w); |
1109 | } |
1160 | } |
1110 | |
1161 | |
1111 | void |
1162 | void |
… | |
… | |
1113 | { |
1164 | { |
1114 | if (ev_is_active (w)) |
1165 | if (ev_is_active (w)) |
1115 | { |
1166 | { |
1116 | if (w->repeat) |
1167 | if (w->repeat) |
1117 | { |
1168 | { |
1118 | w->at = mn_now + w->repeat; |
1169 | ((WT)w)->at = mn_now + w->repeat; |
1119 | downheap ((WT *)timers, timercnt, w->active - 1); |
1170 | downheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1120 | } |
1171 | } |
1121 | else |
1172 | else |
1122 | ev_timer_stop (EV_A_ w); |
1173 | ev_timer_stop (EV_A_ w); |
1123 | } |
1174 | } |
1124 | else if (w->repeat) |
1175 | else if (w->repeat) |
… | |
… | |
1133 | |
1184 | |
1134 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
1185 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
1135 | |
1186 | |
1136 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1187 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1137 | if (w->interval) |
1188 | if (w->interval) |
1138 | w->at += ceil ((rt_now - w->at) / w->interval) * w->interval; |
1189 | ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1139 | |
1190 | |
1140 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1191 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1141 | array_needsize (periodics, periodicmax, periodiccnt, ); |
1192 | array_needsize (periodics, periodicmax, periodiccnt, ); |
1142 | periodics [periodiccnt - 1] = w; |
1193 | periodics [periodiccnt - 1] = w; |
1143 | upheap ((WT *)periodics, periodiccnt - 1); |
1194 | upheap ((WT *)periodics, periodiccnt - 1); |
|
|
1195 | |
|
|
1196 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1144 | } |
1197 | } |
1145 | |
1198 | |
1146 | void |
1199 | void |
1147 | ev_periodic_stop (EV_P_ struct ev_periodic *w) |
1200 | ev_periodic_stop (EV_P_ struct ev_periodic *w) |
1148 | { |
1201 | { |
1149 | ev_clear_pending (EV_A_ (W)w); |
1202 | ev_clear_pending (EV_A_ (W)w); |
1150 | if (!ev_is_active (w)) |
1203 | if (!ev_is_active (w)) |
1151 | return; |
1204 | return; |
1152 | |
1205 | |
|
|
1206 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
|
|
1207 | |
1153 | if (w->active < periodiccnt--) |
1208 | if (((W)w)->active < periodiccnt--) |
1154 | { |
1209 | { |
1155 | periodics [w->active - 1] = periodics [periodiccnt]; |
1210 | periodics [((W)w)->active - 1] = periodics [periodiccnt]; |
1156 | downheap ((WT *)periodics, periodiccnt, w->active - 1); |
1211 | downheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1); |
1157 | } |
1212 | } |
1158 | |
1213 | |
1159 | ev_stop (EV_A_ (W)w); |
1214 | ev_stop (EV_A_ (W)w); |
1160 | } |
1215 | } |
1161 | |
1216 | |
… | |
… | |
1175 | { |
1230 | { |
1176 | ev_clear_pending (EV_A_ (W)w); |
1231 | ev_clear_pending (EV_A_ (W)w); |
1177 | if (ev_is_active (w)) |
1232 | if (ev_is_active (w)) |
1178 | return; |
1233 | return; |
1179 | |
1234 | |
1180 | idles [w->active - 1] = idles [--idlecnt]; |
1235 | idles [((W)w)->active - 1] = idles [--idlecnt]; |
1181 | ev_stop (EV_A_ (W)w); |
1236 | ev_stop (EV_A_ (W)w); |
1182 | } |
1237 | } |
1183 | |
1238 | |
1184 | void |
1239 | void |
1185 | ev_prepare_start (EV_P_ struct ev_prepare *w) |
1240 | ev_prepare_start (EV_P_ struct ev_prepare *w) |
… | |
… | |
1197 | { |
1252 | { |
1198 | ev_clear_pending (EV_A_ (W)w); |
1253 | ev_clear_pending (EV_A_ (W)w); |
1199 | if (ev_is_active (w)) |
1254 | if (ev_is_active (w)) |
1200 | return; |
1255 | return; |
1201 | |
1256 | |
1202 | prepares [w->active - 1] = prepares [--preparecnt]; |
1257 | prepares [((W)w)->active - 1] = prepares [--preparecnt]; |
1203 | ev_stop (EV_A_ (W)w); |
1258 | ev_stop (EV_A_ (W)w); |
1204 | } |
1259 | } |
1205 | |
1260 | |
1206 | void |
1261 | void |
1207 | ev_check_start (EV_P_ struct ev_check *w) |
1262 | ev_check_start (EV_P_ struct ev_check *w) |
… | |
… | |
1219 | { |
1274 | { |
1220 | ev_clear_pending (EV_A_ (W)w); |
1275 | ev_clear_pending (EV_A_ (W)w); |
1221 | if (ev_is_active (w)) |
1276 | if (ev_is_active (w)) |
1222 | return; |
1277 | return; |
1223 | |
1278 | |
1224 | checks [w->active - 1] = checks [--checkcnt]; |
1279 | checks [((W)w)->active - 1] = checks [--checkcnt]; |
1225 | ev_stop (EV_A_ (W)w); |
1280 | ev_stop (EV_A_ (W)w); |
1226 | } |
1281 | } |
1227 | |
1282 | |
1228 | #ifndef SA_RESTART |
1283 | #ifndef SA_RESTART |
1229 | # define SA_RESTART 0 |
1284 | # define SA_RESTART 0 |
… | |
… | |
1242 | |
1297 | |
1243 | ev_start (EV_A_ (W)w, 1); |
1298 | ev_start (EV_A_ (W)w, 1); |
1244 | array_needsize (signals, signalmax, w->signum, signals_init); |
1299 | array_needsize (signals, signalmax, w->signum, signals_init); |
1245 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1300 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1246 | |
1301 | |
1247 | if (!w->next) |
1302 | if (!((WL)w)->next) |
1248 | { |
1303 | { |
1249 | struct sigaction sa; |
1304 | struct sigaction sa; |
1250 | sa.sa_handler = sighandler; |
1305 | sa.sa_handler = sighandler; |
1251 | sigfillset (&sa.sa_mask); |
1306 | sigfillset (&sa.sa_mask); |
1252 | sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ |
1307 | sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ |