… | |
… | |
26 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
26 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
27 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
30 | */ |
30 | */ |
|
|
31 | |
|
|
32 | #ifdef __cplusplus |
|
|
33 | extern "C" { |
|
|
34 | #endif |
|
|
35 | |
31 | #ifndef EV_STANDALONE |
36 | #ifndef EV_STANDALONE |
32 | # include "config.h" |
37 | # include "config.h" |
33 | |
38 | |
34 | # if HAVE_CLOCK_GETTIME |
39 | # if HAVE_CLOCK_GETTIME |
|
|
40 | # ifndef EV_USE_MONOTONIC |
35 | # define EV_USE_MONOTONIC 1 |
41 | # define EV_USE_MONOTONIC 1 |
|
|
42 | # endif |
|
|
43 | # ifndef EV_USE_REALTIME |
36 | # define EV_USE_REALTIME 1 |
44 | # define EV_USE_REALTIME 1 |
|
|
45 | # endif |
37 | # endif |
46 | # endif |
38 | |
47 | |
39 | # if HAVE_SELECT && HAVE_SYS_SELECT_H |
48 | # if HAVE_SELECT && HAVE_SYS_SELECT_H && !defined (EV_USE_SELECT) |
40 | # define EV_USE_SELECT 1 |
49 | # define EV_USE_SELECT 1 |
41 | # endif |
50 | # endif |
42 | |
51 | |
43 | # if HAVE_POLL && HAVE_POLL_H |
52 | # if HAVE_POLL && HAVE_POLL_H && !defined (EV_USE_POLL) |
44 | # define EV_USE_POLL 1 |
53 | # define EV_USE_POLL 1 |
45 | # endif |
54 | # endif |
46 | |
55 | |
47 | # if HAVE_EPOLL && HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H |
56 | # if HAVE_EPOLL && HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H && !defined (EV_USE_EPOLL) |
48 | # define EV_USE_EPOLL 1 |
57 | # define EV_USE_EPOLL 1 |
49 | # endif |
58 | # endif |
50 | |
59 | |
51 | # if HAVE_KQUEUE && HAVE_WORKING_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H |
60 | # if HAVE_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H && !defined (EV_USE_KQUEUE) |
52 | # define EV_USE_KQUEUE 1 |
61 | # define EV_USE_KQUEUE 1 |
53 | # endif |
62 | # endif |
54 | |
63 | |
55 | #endif |
64 | #endif |
56 | |
65 | |
… | |
… | |
126 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
135 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
127 | #define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detect time jumps) */ |
136 | #define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detect time jumps) */ |
128 | #define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */ |
137 | #define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */ |
129 | /*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */ |
138 | /*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */ |
130 | |
139 | |
|
|
140 | #ifdef EV_H |
|
|
141 | # include EV_H |
|
|
142 | #else |
131 | #include "ev.h" |
143 | # include "ev.h" |
|
|
144 | #endif |
132 | |
145 | |
133 | #if __GNUC__ >= 3 |
146 | #if __GNUC__ >= 3 |
134 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
147 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
135 | # define inline inline |
148 | # define inline inline |
136 | #else |
149 | #else |
… | |
… | |
148 | typedef struct ev_watcher_list *WL; |
161 | typedef struct ev_watcher_list *WL; |
149 | typedef struct ev_watcher_time *WT; |
162 | typedef struct ev_watcher_time *WT; |
150 | |
163 | |
151 | static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
164 | static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
152 | |
165 | |
|
|
166 | #ifdef WIN32 |
153 | #include "ev_win32.c" |
167 | # include "ev_win32.c" |
|
|
168 | #endif |
154 | |
169 | |
155 | /*****************************************************************************/ |
170 | /*****************************************************************************/ |
156 | |
171 | |
157 | static void (*syserr_cb)(const char *msg); |
172 | static void (*syserr_cb)(const char *msg); |
158 | |
173 | |
… | |
… | |
215 | int events; |
230 | int events; |
216 | } ANPENDING; |
231 | } ANPENDING; |
217 | |
232 | |
218 | #if EV_MULTIPLICITY |
233 | #if EV_MULTIPLICITY |
219 | |
234 | |
220 | struct ev_loop |
235 | struct ev_loop |
221 | { |
236 | { |
|
|
237 | ev_tstamp ev_rt_now; |
222 | # define VAR(name,decl) decl; |
238 | #define VAR(name,decl) decl; |
223 | # include "ev_vars.h" |
239 | #include "ev_vars.h" |
224 | }; |
|
|
225 | # undef VAR |
240 | #undef VAR |
|
|
241 | }; |
226 | # include "ev_wrap.h" |
242 | #include "ev_wrap.h" |
|
|
243 | |
|
|
244 | struct ev_loop default_loop_struct; |
|
|
245 | static struct ev_loop *default_loop; |
227 | |
246 | |
228 | #else |
247 | #else |
229 | |
248 | |
|
|
249 | ev_tstamp ev_rt_now; |
230 | # define VAR(name,decl) static decl; |
250 | #define VAR(name,decl) static decl; |
231 | # include "ev_vars.h" |
251 | #include "ev_vars.h" |
232 | # undef VAR |
252 | #undef VAR |
|
|
253 | |
|
|
254 | static int default_loop; |
233 | |
255 | |
234 | #endif |
256 | #endif |
235 | |
257 | |
236 | /*****************************************************************************/ |
258 | /*****************************************************************************/ |
237 | |
259 | |
238 | inline ev_tstamp |
260 | ev_tstamp |
239 | ev_time (void) |
261 | ev_time (void) |
240 | { |
262 | { |
241 | #if EV_USE_REALTIME |
263 | #if EV_USE_REALTIME |
242 | struct timespec ts; |
264 | struct timespec ts; |
243 | clock_gettime (CLOCK_REALTIME, &ts); |
265 | clock_gettime (CLOCK_REALTIME, &ts); |
… | |
… | |
262 | #endif |
284 | #endif |
263 | |
285 | |
264 | return ev_time (); |
286 | return ev_time (); |
265 | } |
287 | } |
266 | |
288 | |
|
|
289 | #if EV_MULTIPLICITY |
267 | ev_tstamp |
290 | ev_tstamp |
268 | ev_now (EV_P) |
291 | ev_now (EV_P) |
269 | { |
292 | { |
270 | return rt_now; |
293 | return ev_rt_now; |
271 | } |
294 | } |
|
|
295 | #endif |
272 | |
296 | |
273 | #define array_roundsize(type,n) ((n) | 4 & ~3) |
297 | #define array_roundsize(type,n) ((n) | 4 & ~3) |
274 | |
298 | |
275 | #define array_needsize(type,base,cur,cnt,init) \ |
299 | #define array_needsize(type,base,cur,cnt,init) \ |
276 | if (expect_false ((cnt) > cur)) \ |
300 | if (expect_false ((cnt) > cur)) \ |
… | |
… | |
509 | |
533 | |
510 | heap [k] = w; |
534 | heap [k] = w; |
511 | ((W)heap [k])->active = k + 1; |
535 | ((W)heap [k])->active = k + 1; |
512 | } |
536 | } |
513 | |
537 | |
|
|
538 | inline void |
|
|
539 | adjustheap (WT *heap, int N, int k, ev_tstamp at) |
|
|
540 | { |
|
|
541 | ev_tstamp old_at = heap [k]->at; |
|
|
542 | heap [k]->at = at; |
|
|
543 | |
|
|
544 | if (old_at < at) |
|
|
545 | downheap (heap, N, k); |
|
|
546 | else |
|
|
547 | upheap (heap, k); |
|
|
548 | } |
|
|
549 | |
514 | /*****************************************************************************/ |
550 | /*****************************************************************************/ |
515 | |
551 | |
516 | typedef struct |
552 | typedef struct |
517 | { |
553 | { |
518 | WL head; |
554 | WL head; |
… | |
… | |
561 | } |
597 | } |
562 | |
598 | |
563 | void |
599 | void |
564 | ev_feed_signal_event (EV_P_ int signum) |
600 | ev_feed_signal_event (EV_P_ int signum) |
565 | { |
601 | { |
|
|
602 | WL w; |
|
|
603 | |
566 | #if EV_MULTIPLICITY |
604 | #if EV_MULTIPLICITY |
567 | assert (("feeding signal events is only supported in the default loop", loop == default_loop)); |
605 | assert (("feeding signal events is only supported in the default loop", loop == default_loop)); |
568 | #endif |
606 | #endif |
569 | |
607 | |
570 | --signum; |
608 | --signum; |
… | |
… | |
579 | } |
617 | } |
580 | |
618 | |
581 | static void |
619 | static void |
582 | sigcb (EV_P_ struct ev_io *iow, int revents) |
620 | sigcb (EV_P_ struct ev_io *iow, int revents) |
583 | { |
621 | { |
584 | WL w; |
|
|
585 | int signum; |
622 | int signum; |
586 | |
623 | |
587 | #ifdef WIN32 |
624 | #ifdef WIN32 |
588 | recv (sigpipe [0], &revents, 1, MSG_DONTWAIT); |
625 | recv (sigpipe [0], &revents, 1, MSG_DONTWAIT); |
589 | #else |
626 | #else |
… | |
… | |
591 | #endif |
628 | #endif |
592 | gotsig = 0; |
629 | gotsig = 0; |
593 | |
630 | |
594 | for (signum = signalmax; signum--; ) |
631 | for (signum = signalmax; signum--; ) |
595 | if (signals [signum].gotsig) |
632 | if (signals [signum].gotsig) |
596 | sigevent (EV_A_ signum + 1); |
633 | ev_feed_signal_event (EV_A_ signum + 1); |
597 | } |
634 | } |
598 | |
635 | |
599 | static void |
636 | static void |
600 | siginit (EV_P) |
637 | siginit (EV_P) |
601 | { |
638 | { |
… | |
… | |
713 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
750 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
714 | have_monotonic = 1; |
751 | have_monotonic = 1; |
715 | } |
752 | } |
716 | #endif |
753 | #endif |
717 | |
754 | |
718 | rt_now = ev_time (); |
755 | ev_rt_now = ev_time (); |
719 | mn_now = get_clock (); |
756 | mn_now = get_clock (); |
720 | now_floor = mn_now; |
757 | now_floor = mn_now; |
721 | rtmn_diff = rt_now - mn_now; |
758 | rtmn_diff = ev_rt_now - mn_now; |
722 | |
759 | |
723 | if (methods == EVMETHOD_AUTO) |
760 | if (methods == EVMETHOD_AUTO) |
724 | if (!enable_secure () && getenv ("LIBEV_METHODS")) |
761 | if (!enable_secure () && getenv ("LIBEV_METHODS")) |
725 | methods = atoi (getenv ("LIBEV_METHODS")); |
762 | methods = atoi (getenv ("LIBEV_METHODS")); |
726 | else |
763 | else |
… | |
… | |
741 | #endif |
778 | #endif |
742 | #if EV_USE_SELECT |
779 | #if EV_USE_SELECT |
743 | if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); |
780 | if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); |
744 | #endif |
781 | #endif |
745 | |
782 | |
746 | ev_watcher_init (&sigev, sigcb); |
783 | ev_init (&sigev, sigcb); |
747 | ev_set_priority (&sigev, EV_MAXPRI); |
784 | ev_set_priority (&sigev, EV_MAXPRI); |
748 | } |
785 | } |
749 | } |
786 | } |
750 | |
787 | |
751 | void |
788 | void |
… | |
… | |
773 | array_free (pending, [i]); |
810 | array_free (pending, [i]); |
774 | |
811 | |
775 | /* have to use the microsoft-never-gets-it-right macro */ |
812 | /* have to use the microsoft-never-gets-it-right macro */ |
776 | array_free_microshit (fdchange); |
813 | array_free_microshit (fdchange); |
777 | array_free_microshit (timer); |
814 | array_free_microshit (timer); |
|
|
815 | #if EV_PERIODICS |
778 | array_free_microshit (periodic); |
816 | array_free_microshit (periodic); |
|
|
817 | #endif |
779 | array_free_microshit (idle); |
818 | array_free_microshit (idle); |
780 | array_free_microshit (prepare); |
819 | array_free_microshit (prepare); |
781 | array_free_microshit (check); |
820 | array_free_microshit (check); |
782 | |
821 | |
783 | method = 0; |
822 | method = 0; |
… | |
… | |
841 | } |
880 | } |
842 | |
881 | |
843 | #endif |
882 | #endif |
844 | |
883 | |
845 | #if EV_MULTIPLICITY |
884 | #if EV_MULTIPLICITY |
846 | struct ev_loop default_loop_struct; |
|
|
847 | static struct ev_loop *default_loop; |
|
|
848 | |
|
|
849 | struct ev_loop * |
885 | struct ev_loop * |
850 | #else |
886 | #else |
851 | static int default_loop; |
|
|
852 | |
|
|
853 | int |
887 | int |
854 | #endif |
888 | #endif |
855 | ev_default_loop (int methods) |
889 | ev_default_loop (int methods) |
856 | { |
890 | { |
857 | if (sigpipe [0] == sigpipe [1]) |
891 | if (sigpipe [0] == sigpipe [1]) |
… | |
… | |
943 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
977 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
944 | |
978 | |
945 | if (p->w) |
979 | if (p->w) |
946 | { |
980 | { |
947 | p->w->pending = 0; |
981 | p->w->pending = 0; |
948 | p->w->cb (EV_A_ p->w, p->events); |
982 | EV_CB_INVOKE (p->w, p->events); |
949 | } |
983 | } |
950 | } |
984 | } |
951 | } |
985 | } |
952 | |
986 | |
953 | static void |
987 | static void |
… | |
… | |
961 | |
995 | |
962 | /* first reschedule or stop timer */ |
996 | /* first reschedule or stop timer */ |
963 | if (w->repeat) |
997 | if (w->repeat) |
964 | { |
998 | { |
965 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
999 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
|
|
1000 | |
966 | ((WT)w)->at = mn_now + w->repeat; |
1001 | ((WT)w)->at += w->repeat; |
|
|
1002 | if (((WT)w)->at < mn_now) |
|
|
1003 | ((WT)w)->at = mn_now; |
|
|
1004 | |
967 | downheap ((WT *)timers, timercnt, 0); |
1005 | downheap ((WT *)timers, timercnt, 0); |
968 | } |
1006 | } |
969 | else |
1007 | else |
970 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
1008 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
971 | |
1009 | |
972 | ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); |
1010 | ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); |
973 | } |
1011 | } |
974 | } |
1012 | } |
975 | |
1013 | |
|
|
1014 | #if EV_PERIODICS |
976 | static void |
1015 | static void |
977 | periodics_reify (EV_P) |
1016 | periodics_reify (EV_P) |
978 | { |
1017 | { |
979 | while (periodiccnt && ((WT)periodics [0])->at <= rt_now) |
1018 | while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) |
980 | { |
1019 | { |
981 | struct ev_periodic *w = periodics [0]; |
1020 | struct ev_periodic *w = periodics [0]; |
982 | |
1021 | |
983 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
1022 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
984 | |
1023 | |
985 | /* first reschedule or stop timer */ |
1024 | /* first reschedule or stop timer */ |
986 | if (w->reschedule_cb) |
1025 | if (w->reschedule_cb) |
987 | { |
1026 | { |
988 | ev_tstamp at = ((WT)w)->at = w->reschedule_cb (w, rt_now + 0.0001); |
1027 | ev_tstamp at = ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + 0.0001); |
989 | |
1028 | |
990 | assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > rt_now)); |
1029 | assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now)); |
991 | downheap ((WT *)periodics, periodiccnt, 0); |
1030 | downheap ((WT *)periodics, periodiccnt, 0); |
992 | } |
1031 | } |
993 | else if (w->interval) |
1032 | else if (w->interval) |
994 | { |
1033 | { |
995 | ((WT)w)->at += floor ((rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval; |
1034 | ((WT)w)->at += floor ((ev_rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval; |
996 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > rt_now)); |
1035 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > ev_rt_now)); |
997 | downheap ((WT *)periodics, periodiccnt, 0); |
1036 | downheap ((WT *)periodics, periodiccnt, 0); |
998 | } |
1037 | } |
999 | else |
1038 | else |
1000 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
1039 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
1001 | |
1040 | |
… | |
… | |
1012 | for (i = 0; i < periodiccnt; ++i) |
1051 | for (i = 0; i < periodiccnt; ++i) |
1013 | { |
1052 | { |
1014 | struct ev_periodic *w = periodics [i]; |
1053 | struct ev_periodic *w = periodics [i]; |
1015 | |
1054 | |
1016 | if (w->reschedule_cb) |
1055 | if (w->reschedule_cb) |
1017 | ((WT)w)->at = w->reschedule_cb (w, rt_now); |
1056 | ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); |
1018 | else if (w->interval) |
1057 | else if (w->interval) |
1019 | ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1058 | ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1020 | } |
1059 | } |
1021 | |
1060 | |
1022 | /* now rebuild the heap */ |
1061 | /* now rebuild the heap */ |
1023 | for (i = periodiccnt >> 1; i--; ) |
1062 | for (i = periodiccnt >> 1; i--; ) |
1024 | downheap ((WT *)periodics, periodiccnt, i); |
1063 | downheap ((WT *)periodics, periodiccnt, i); |
1025 | } |
1064 | } |
|
|
1065 | #endif |
1026 | |
1066 | |
1027 | inline int |
1067 | inline int |
1028 | time_update_monotonic (EV_P) |
1068 | time_update_monotonic (EV_P) |
1029 | { |
1069 | { |
1030 | mn_now = get_clock (); |
1070 | mn_now = get_clock (); |
1031 | |
1071 | |
1032 | if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) |
1072 | if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) |
1033 | { |
1073 | { |
1034 | rt_now = rtmn_diff + mn_now; |
1074 | ev_rt_now = rtmn_diff + mn_now; |
1035 | return 0; |
1075 | return 0; |
1036 | } |
1076 | } |
1037 | else |
1077 | else |
1038 | { |
1078 | { |
1039 | now_floor = mn_now; |
1079 | now_floor = mn_now; |
1040 | rt_now = ev_time (); |
1080 | ev_rt_now = ev_time (); |
1041 | return 1; |
1081 | return 1; |
1042 | } |
1082 | } |
1043 | } |
1083 | } |
1044 | |
1084 | |
1045 | static void |
1085 | static void |
… | |
… | |
1054 | { |
1094 | { |
1055 | ev_tstamp odiff = rtmn_diff; |
1095 | ev_tstamp odiff = rtmn_diff; |
1056 | |
1096 | |
1057 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
1097 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
1058 | { |
1098 | { |
1059 | rtmn_diff = rt_now - mn_now; |
1099 | rtmn_diff = ev_rt_now - mn_now; |
1060 | |
1100 | |
1061 | if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) |
1101 | if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) |
1062 | return; /* all is well */ |
1102 | return; /* all is well */ |
1063 | |
1103 | |
1064 | rt_now = ev_time (); |
1104 | ev_rt_now = ev_time (); |
1065 | mn_now = get_clock (); |
1105 | mn_now = get_clock (); |
1066 | now_floor = mn_now; |
1106 | now_floor = mn_now; |
1067 | } |
1107 | } |
1068 | |
1108 | |
|
|
1109 | # if EV_PERIODICS |
1069 | periodics_reschedule (EV_A); |
1110 | periodics_reschedule (EV_A); |
|
|
1111 | # endif |
1070 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
1112 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
1071 | /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ |
1113 | /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ |
1072 | } |
1114 | } |
1073 | } |
1115 | } |
1074 | else |
1116 | else |
1075 | #endif |
1117 | #endif |
1076 | { |
1118 | { |
1077 | rt_now = ev_time (); |
1119 | ev_rt_now = ev_time (); |
1078 | |
1120 | |
1079 | if (expect_false (mn_now > rt_now || mn_now < rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
1121 | if (expect_false (mn_now > ev_rt_now || mn_now < ev_rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
1080 | { |
1122 | { |
|
|
1123 | #if EV_PERIODICS |
1081 | periodics_reschedule (EV_A); |
1124 | periodics_reschedule (EV_A); |
|
|
1125 | #endif |
1082 | |
1126 | |
1083 | /* adjust timers. this is easy, as the offset is the same for all */ |
1127 | /* adjust timers. this is easy, as the offset is the same for all */ |
1084 | for (i = 0; i < timercnt; ++i) |
1128 | for (i = 0; i < timercnt; ++i) |
1085 | ((WT)timers [i])->at += rt_now - mn_now; |
1129 | ((WT)timers [i])->at += ev_rt_now - mn_now; |
1086 | } |
1130 | } |
1087 | |
1131 | |
1088 | mn_now = rt_now; |
1132 | mn_now = ev_rt_now; |
1089 | } |
1133 | } |
1090 | } |
1134 | } |
1091 | |
1135 | |
1092 | void |
1136 | void |
1093 | ev_ref (EV_P) |
1137 | ev_ref (EV_P) |
… | |
… | |
1133 | if (expect_true (have_monotonic)) |
1177 | if (expect_true (have_monotonic)) |
1134 | time_update_monotonic (EV_A); |
1178 | time_update_monotonic (EV_A); |
1135 | else |
1179 | else |
1136 | #endif |
1180 | #endif |
1137 | { |
1181 | { |
1138 | rt_now = ev_time (); |
1182 | ev_rt_now = ev_time (); |
1139 | mn_now = rt_now; |
1183 | mn_now = ev_rt_now; |
1140 | } |
1184 | } |
1141 | |
1185 | |
1142 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
1186 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
1143 | block = 0.; |
1187 | block = 0.; |
1144 | else |
1188 | else |
… | |
… | |
1149 | { |
1193 | { |
1150 | ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge; |
1194 | ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge; |
1151 | if (block > to) block = to; |
1195 | if (block > to) block = to; |
1152 | } |
1196 | } |
1153 | |
1197 | |
|
|
1198 | #if EV_PERIODICS |
1154 | if (periodiccnt) |
1199 | if (periodiccnt) |
1155 | { |
1200 | { |
1156 | ev_tstamp to = ((WT)periodics [0])->at - rt_now + method_fudge; |
1201 | ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + method_fudge; |
1157 | if (block > to) block = to; |
1202 | if (block > to) block = to; |
1158 | } |
1203 | } |
|
|
1204 | #endif |
1159 | |
1205 | |
1160 | if (block < 0.) block = 0.; |
1206 | if (block < 0.) block = 0.; |
1161 | } |
1207 | } |
1162 | |
1208 | |
1163 | method_poll (EV_A_ block); |
1209 | method_poll (EV_A_ block); |
1164 | |
1210 | |
1165 | /* update rt_now, do magic */ |
1211 | /* update ev_rt_now, do magic */ |
1166 | time_update (EV_A); |
1212 | time_update (EV_A); |
1167 | |
1213 | |
1168 | /* queue pending timers and reschedule them */ |
1214 | /* queue pending timers and reschedule them */ |
1169 | timers_reify (EV_A); /* relative timers called last */ |
1215 | timers_reify (EV_A); /* relative timers called last */ |
|
|
1216 | #if EV_PERIODICS |
1170 | periodics_reify (EV_A); /* absolute timers called first */ |
1217 | periodics_reify (EV_A); /* absolute timers called first */ |
|
|
1218 | #endif |
1171 | |
1219 | |
1172 | /* queue idle watchers unless io or timers are pending */ |
1220 | /* queue idle watchers unless io or timers are pending */ |
1173 | if (idlecnt && !any_pending (EV_A)) |
1221 | if (idlecnt && !any_pending (EV_A)) |
1174 | queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); |
1222 | queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); |
1175 | |
1223 | |
… | |
… | |
1266 | { |
1314 | { |
1267 | ev_clear_pending (EV_A_ (W)w); |
1315 | ev_clear_pending (EV_A_ (W)w); |
1268 | if (!ev_is_active (w)) |
1316 | if (!ev_is_active (w)) |
1269 | return; |
1317 | return; |
1270 | |
1318 | |
|
|
1319 | assert (("ev_io_start called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax)); |
|
|
1320 | |
1271 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
1321 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
1272 | ev_stop (EV_A_ (W)w); |
1322 | ev_stop (EV_A_ (W)w); |
1273 | |
1323 | |
1274 | fd_change (EV_A_ w->fd); |
1324 | fd_change (EV_A_ w->fd); |
1275 | } |
1325 | } |
… | |
… | |
1305 | { |
1355 | { |
1306 | timers [((W)w)->active - 1] = timers [timercnt]; |
1356 | timers [((W)w)->active - 1] = timers [timercnt]; |
1307 | downheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1357 | downheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1308 | } |
1358 | } |
1309 | |
1359 | |
1310 | ((WT)w)->at = w->repeat; |
1360 | ((WT)w)->at -= mn_now; |
1311 | |
1361 | |
1312 | ev_stop (EV_A_ (W)w); |
1362 | ev_stop (EV_A_ (W)w); |
1313 | } |
1363 | } |
1314 | |
1364 | |
1315 | void |
1365 | void |
1316 | ev_timer_again (EV_P_ struct ev_timer *w) |
1366 | ev_timer_again (EV_P_ struct ev_timer *w) |
1317 | { |
1367 | { |
1318 | if (ev_is_active (w)) |
1368 | if (ev_is_active (w)) |
1319 | { |
1369 | { |
1320 | if (w->repeat) |
1370 | if (w->repeat) |
1321 | { |
|
|
1322 | ((WT)w)->at = mn_now + w->repeat; |
|
|
1323 | downheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1371 | adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1, mn_now + w->repeat); |
1324 | } |
|
|
1325 | else |
1372 | else |
1326 | ev_timer_stop (EV_A_ w); |
1373 | ev_timer_stop (EV_A_ w); |
1327 | } |
1374 | } |
1328 | else if (w->repeat) |
1375 | else if (w->repeat) |
1329 | ev_timer_start (EV_A_ w); |
1376 | ev_timer_start (EV_A_ w); |
1330 | } |
1377 | } |
1331 | |
1378 | |
|
|
1379 | #if EV_PERIODICS |
1332 | void |
1380 | void |
1333 | ev_periodic_start (EV_P_ struct ev_periodic *w) |
1381 | ev_periodic_start (EV_P_ struct ev_periodic *w) |
1334 | { |
1382 | { |
1335 | if (ev_is_active (w)) |
1383 | if (ev_is_active (w)) |
1336 | return; |
1384 | return; |
1337 | |
1385 | |
1338 | if (w->reschedule_cb) |
1386 | if (w->reschedule_cb) |
1339 | ((WT)w)->at = w->reschedule_cb (w, rt_now); |
1387 | ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); |
1340 | else if (w->interval) |
1388 | else if (w->interval) |
1341 | { |
1389 | { |
1342 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
1390 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
1343 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1391 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1344 | ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1392 | ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1345 | } |
1393 | } |
1346 | |
1394 | |
1347 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1395 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1348 | array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void)); |
1396 | array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void)); |
1349 | periodics [periodiccnt - 1] = w; |
1397 | periodics [periodiccnt - 1] = w; |
… | |
… | |
1371 | } |
1419 | } |
1372 | |
1420 | |
1373 | void |
1421 | void |
1374 | ev_periodic_again (EV_P_ struct ev_periodic *w) |
1422 | ev_periodic_again (EV_P_ struct ev_periodic *w) |
1375 | { |
1423 | { |
|
|
1424 | /* TODO: use adjustheap and recalculation */ |
1376 | ev_periodic_stop (EV_A_ w); |
1425 | ev_periodic_stop (EV_A_ w); |
1377 | ev_periodic_start (EV_A_ w); |
1426 | ev_periodic_start (EV_A_ w); |
1378 | } |
1427 | } |
|
|
1428 | #endif |
1379 | |
1429 | |
1380 | void |
1430 | void |
1381 | ev_idle_start (EV_P_ struct ev_idle *w) |
1431 | ev_idle_start (EV_P_ struct ev_idle *w) |
1382 | { |
1432 | { |
1383 | if (ev_is_active (w)) |
1433 | if (ev_is_active (w)) |
… | |
… | |
1434 | |
1484 | |
1435 | void |
1485 | void |
1436 | ev_check_stop (EV_P_ struct ev_check *w) |
1486 | ev_check_stop (EV_P_ struct ev_check *w) |
1437 | { |
1487 | { |
1438 | ev_clear_pending (EV_A_ (W)w); |
1488 | ev_clear_pending (EV_A_ (W)w); |
1439 | if (ev_is_active (w)) |
1489 | if (!ev_is_active (w)) |
1440 | return; |
1490 | return; |
1441 | |
1491 | |
1442 | checks [((W)w)->active - 1] = checks [--checkcnt]; |
1492 | checks [((W)w)->active - 1] = checks [--checkcnt]; |
1443 | ev_stop (EV_A_ (W)w); |
1493 | ev_stop (EV_A_ (W)w); |
1444 | } |
1494 | } |
… | |
… | |
1505 | |
1555 | |
1506 | void |
1556 | void |
1507 | ev_child_stop (EV_P_ struct ev_child *w) |
1557 | ev_child_stop (EV_P_ struct ev_child *w) |
1508 | { |
1558 | { |
1509 | ev_clear_pending (EV_A_ (W)w); |
1559 | ev_clear_pending (EV_A_ (W)w); |
1510 | if (ev_is_active (w)) |
1560 | if (!ev_is_active (w)) |
1511 | return; |
1561 | return; |
1512 | |
1562 | |
1513 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1563 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1514 | ev_stop (EV_A_ (W)w); |
1564 | ev_stop (EV_A_ (W)w); |
1515 | } |
1565 | } |
… | |
… | |
1559 | else |
1609 | else |
1560 | { |
1610 | { |
1561 | once->cb = cb; |
1611 | once->cb = cb; |
1562 | once->arg = arg; |
1612 | once->arg = arg; |
1563 | |
1613 | |
1564 | ev_watcher_init (&once->io, once_cb_io); |
1614 | ev_init (&once->io, once_cb_io); |
1565 | if (fd >= 0) |
1615 | if (fd >= 0) |
1566 | { |
1616 | { |
1567 | ev_io_set (&once->io, fd, events); |
1617 | ev_io_set (&once->io, fd, events); |
1568 | ev_io_start (EV_A_ &once->io); |
1618 | ev_io_start (EV_A_ &once->io); |
1569 | } |
1619 | } |
1570 | |
1620 | |
1571 | ev_watcher_init (&once->to, once_cb_to); |
1621 | ev_init (&once->to, once_cb_to); |
1572 | if (timeout >= 0.) |
1622 | if (timeout >= 0.) |
1573 | { |
1623 | { |
1574 | ev_timer_set (&once->to, timeout, 0.); |
1624 | ev_timer_set (&once->to, timeout, 0.); |
1575 | ev_timer_start (EV_A_ &once->to); |
1625 | ev_timer_start (EV_A_ &once->to); |
1576 | } |
1626 | } |
1577 | } |
1627 | } |
1578 | } |
1628 | } |
1579 | |
1629 | |
|
|
1630 | #ifdef __cplusplus |
|
|
1631 | } |
|
|
1632 | #endif |
|
|
1633 | |