… | |
… | |
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_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; |
|
|
238 | #define ev_rt_now ((loop)->ev_rt_now) |
222 | # define VAR(name,decl) decl; |
239 | #define VAR(name,decl) decl; |
223 | # include "ev_vars.h" |
240 | #include "ev_vars.h" |
224 | }; |
|
|
225 | # undef VAR |
241 | #undef VAR |
|
|
242 | }; |
226 | # include "ev_wrap.h" |
243 | #include "ev_wrap.h" |
|
|
244 | |
|
|
245 | struct ev_loop default_loop_struct; |
|
|
246 | static struct ev_loop *default_loop; |
227 | |
247 | |
228 | #else |
248 | #else |
229 | |
249 | |
|
|
250 | ev_tstamp ev_rt_now; |
230 | # define VAR(name,decl) static decl; |
251 | #define VAR(name,decl) static decl; |
231 | # include "ev_vars.h" |
252 | #include "ev_vars.h" |
232 | # undef VAR |
253 | #undef VAR |
|
|
254 | |
|
|
255 | static int default_loop; |
233 | |
256 | |
234 | #endif |
257 | #endif |
235 | |
258 | |
236 | /*****************************************************************************/ |
259 | /*****************************************************************************/ |
237 | |
260 | |
238 | inline ev_tstamp |
261 | ev_tstamp |
239 | ev_time (void) |
262 | ev_time (void) |
240 | { |
263 | { |
241 | #if EV_USE_REALTIME |
264 | #if EV_USE_REALTIME |
242 | struct timespec ts; |
265 | struct timespec ts; |
243 | clock_gettime (CLOCK_REALTIME, &ts); |
266 | clock_gettime (CLOCK_REALTIME, &ts); |
… | |
… | |
262 | #endif |
285 | #endif |
263 | |
286 | |
264 | return ev_time (); |
287 | return ev_time (); |
265 | } |
288 | } |
266 | |
289 | |
|
|
290 | #if EV_MULTIPLICITY |
267 | ev_tstamp |
291 | ev_tstamp |
268 | ev_now (EV_P) |
292 | ev_now (EV_P) |
269 | { |
293 | { |
270 | return rt_now; |
294 | return ev_rt_now; |
271 | } |
295 | } |
|
|
296 | #endif |
272 | |
297 | |
273 | #define array_roundsize(type,n) ((n) | 4 & ~3) |
298 | #define array_roundsize(type,n) ((n) | 4 & ~3) |
274 | |
299 | |
275 | #define array_needsize(type,base,cur,cnt,init) \ |
300 | #define array_needsize(type,base,cur,cnt,init) \ |
276 | if (expect_false ((cnt) > cur)) \ |
301 | if (expect_false ((cnt) > cur)) \ |
… | |
… | |
509 | |
534 | |
510 | heap [k] = w; |
535 | heap [k] = w; |
511 | ((W)heap [k])->active = k + 1; |
536 | ((W)heap [k])->active = k + 1; |
512 | } |
537 | } |
513 | |
538 | |
|
|
539 | inline void |
|
|
540 | adjustheap (WT *heap, int N, int k) |
|
|
541 | { |
|
|
542 | upheap (heap, k); |
|
|
543 | downheap (heap, N, k); |
|
|
544 | } |
|
|
545 | |
514 | /*****************************************************************************/ |
546 | /*****************************************************************************/ |
515 | |
547 | |
516 | typedef struct |
548 | typedef struct |
517 | { |
549 | { |
518 | WL head; |
550 | WL head; |
… | |
… | |
561 | } |
593 | } |
562 | |
594 | |
563 | void |
595 | void |
564 | ev_feed_signal_event (EV_P_ int signum) |
596 | ev_feed_signal_event (EV_P_ int signum) |
565 | { |
597 | { |
|
|
598 | WL w; |
|
|
599 | |
566 | #if EV_MULTIPLICITY |
600 | #if EV_MULTIPLICITY |
567 | assert (("feeding signal events is only supported in the default loop", loop == default_loop)); |
601 | assert (("feeding signal events is only supported in the default loop", loop == default_loop)); |
568 | #endif |
602 | #endif |
569 | |
603 | |
570 | --signum; |
604 | --signum; |
… | |
… | |
579 | } |
613 | } |
580 | |
614 | |
581 | static void |
615 | static void |
582 | sigcb (EV_P_ struct ev_io *iow, int revents) |
616 | sigcb (EV_P_ struct ev_io *iow, int revents) |
583 | { |
617 | { |
584 | WL w; |
|
|
585 | int signum; |
618 | int signum; |
586 | |
619 | |
587 | #ifdef WIN32 |
620 | #ifdef WIN32 |
588 | recv (sigpipe [0], &revents, 1, MSG_DONTWAIT); |
621 | recv (sigpipe [0], &revents, 1, MSG_DONTWAIT); |
589 | #else |
622 | #else |
… | |
… | |
591 | #endif |
624 | #endif |
592 | gotsig = 0; |
625 | gotsig = 0; |
593 | |
626 | |
594 | for (signum = signalmax; signum--; ) |
627 | for (signum = signalmax; signum--; ) |
595 | if (signals [signum].gotsig) |
628 | if (signals [signum].gotsig) |
596 | sigevent (EV_A_ signum + 1); |
629 | ev_feed_signal_event (EV_A_ signum + 1); |
597 | } |
630 | } |
598 | |
631 | |
599 | static void |
632 | static void |
600 | siginit (EV_P) |
633 | siginit (EV_P) |
601 | { |
634 | { |
… | |
… | |
713 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
746 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
714 | have_monotonic = 1; |
747 | have_monotonic = 1; |
715 | } |
748 | } |
716 | #endif |
749 | #endif |
717 | |
750 | |
718 | rt_now = ev_time (); |
751 | ev_rt_now = ev_time (); |
719 | mn_now = get_clock (); |
752 | mn_now = get_clock (); |
720 | now_floor = mn_now; |
753 | now_floor = mn_now; |
721 | rtmn_diff = rt_now - mn_now; |
754 | rtmn_diff = ev_rt_now - mn_now; |
722 | |
755 | |
723 | if (methods == EVMETHOD_AUTO) |
756 | if (methods == EVMETHOD_AUTO) |
724 | if (!enable_secure () && getenv ("LIBEV_METHODS")) |
757 | if (!enable_secure () && getenv ("LIBEV_METHODS")) |
725 | methods = atoi (getenv ("LIBEV_METHODS")); |
758 | methods = atoi (getenv ("LIBEV_METHODS")); |
726 | else |
759 | else |
… | |
… | |
741 | #endif |
774 | #endif |
742 | #if EV_USE_SELECT |
775 | #if EV_USE_SELECT |
743 | if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); |
776 | if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); |
744 | #endif |
777 | #endif |
745 | |
778 | |
746 | ev_watcher_init (&sigev, sigcb); |
779 | ev_init (&sigev, sigcb); |
747 | ev_set_priority (&sigev, EV_MAXPRI); |
780 | ev_set_priority (&sigev, EV_MAXPRI); |
748 | } |
781 | } |
749 | } |
782 | } |
750 | |
783 | |
751 | void |
784 | void |
… | |
… | |
773 | array_free (pending, [i]); |
806 | array_free (pending, [i]); |
774 | |
807 | |
775 | /* have to use the microsoft-never-gets-it-right macro */ |
808 | /* have to use the microsoft-never-gets-it-right macro */ |
776 | array_free_microshit (fdchange); |
809 | array_free_microshit (fdchange); |
777 | array_free_microshit (timer); |
810 | array_free_microshit (timer); |
|
|
811 | #if EV_PERIODICS |
778 | array_free_microshit (periodic); |
812 | array_free_microshit (periodic); |
|
|
813 | #endif |
779 | array_free_microshit (idle); |
814 | array_free_microshit (idle); |
780 | array_free_microshit (prepare); |
815 | array_free_microshit (prepare); |
781 | array_free_microshit (check); |
816 | array_free_microshit (check); |
782 | |
817 | |
783 | method = 0; |
818 | method = 0; |
… | |
… | |
841 | } |
876 | } |
842 | |
877 | |
843 | #endif |
878 | #endif |
844 | |
879 | |
845 | #if EV_MULTIPLICITY |
880 | #if EV_MULTIPLICITY |
846 | struct ev_loop default_loop_struct; |
|
|
847 | static struct ev_loop *default_loop; |
|
|
848 | |
|
|
849 | struct ev_loop * |
881 | struct ev_loop * |
850 | #else |
882 | #else |
851 | static int default_loop; |
|
|
852 | |
|
|
853 | int |
883 | int |
854 | #endif |
884 | #endif |
855 | ev_default_loop (int methods) |
885 | ev_default_loop (int methods) |
856 | { |
886 | { |
857 | if (sigpipe [0] == sigpipe [1]) |
887 | if (sigpipe [0] == sigpipe [1]) |
… | |
… | |
943 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
973 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
944 | |
974 | |
945 | if (p->w) |
975 | if (p->w) |
946 | { |
976 | { |
947 | p->w->pending = 0; |
977 | p->w->pending = 0; |
948 | p->w->cb (EV_A_ p->w, p->events); |
978 | EV_CB_INVOKE (p->w, p->events); |
949 | } |
979 | } |
950 | } |
980 | } |
951 | } |
981 | } |
952 | |
982 | |
953 | static void |
983 | static void |
… | |
… | |
961 | |
991 | |
962 | /* first reschedule or stop timer */ |
992 | /* first reschedule or stop timer */ |
963 | if (w->repeat) |
993 | if (w->repeat) |
964 | { |
994 | { |
965 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
995 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
|
|
996 | |
966 | ((WT)w)->at = mn_now + w->repeat; |
997 | ((WT)w)->at += w->repeat; |
|
|
998 | if (((WT)w)->at < mn_now) |
|
|
999 | ((WT)w)->at = mn_now; |
|
|
1000 | |
967 | downheap ((WT *)timers, timercnt, 0); |
1001 | downheap ((WT *)timers, timercnt, 0); |
968 | } |
1002 | } |
969 | else |
1003 | else |
970 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
1004 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
971 | |
1005 | |
972 | ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); |
1006 | ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); |
973 | } |
1007 | } |
974 | } |
1008 | } |
975 | |
1009 | |
|
|
1010 | #if EV_PERIODICS |
976 | static void |
1011 | static void |
977 | periodics_reify (EV_P) |
1012 | periodics_reify (EV_P) |
978 | { |
1013 | { |
979 | while (periodiccnt && ((WT)periodics [0])->at <= rt_now) |
1014 | while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) |
980 | { |
1015 | { |
981 | struct ev_periodic *w = periodics [0]; |
1016 | struct ev_periodic *w = periodics [0]; |
982 | |
1017 | |
983 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
1018 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
984 | |
1019 | |
985 | /* first reschedule or stop timer */ |
1020 | /* first reschedule or stop timer */ |
986 | if (w->reschedule_cb) |
1021 | if (w->reschedule_cb) |
987 | { |
1022 | { |
988 | ev_tstamp at = ((WT)w)->at = w->reschedule_cb (w, rt_now + 0.0001); |
1023 | ev_tstamp at = ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + 0.0001); |
989 | |
1024 | |
990 | assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > rt_now)); |
1025 | assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now)); |
991 | downheap ((WT *)periodics, periodiccnt, 0); |
1026 | downheap ((WT *)periodics, periodiccnt, 0); |
992 | } |
1027 | } |
993 | else if (w->interval) |
1028 | else if (w->interval) |
994 | { |
1029 | { |
995 | ((WT)w)->at += floor ((rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval; |
1030 | ((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)); |
1031 | 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); |
1032 | downheap ((WT *)periodics, periodiccnt, 0); |
998 | } |
1033 | } |
999 | else |
1034 | else |
1000 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
1035 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
1001 | |
1036 | |
… | |
… | |
1012 | for (i = 0; i < periodiccnt; ++i) |
1047 | for (i = 0; i < periodiccnt; ++i) |
1013 | { |
1048 | { |
1014 | struct ev_periodic *w = periodics [i]; |
1049 | struct ev_periodic *w = periodics [i]; |
1015 | |
1050 | |
1016 | if (w->reschedule_cb) |
1051 | if (w->reschedule_cb) |
1017 | ((WT)w)->at = w->reschedule_cb (w, rt_now); |
1052 | ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); |
1018 | else if (w->interval) |
1053 | else if (w->interval) |
1019 | ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1054 | ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1020 | } |
1055 | } |
1021 | |
1056 | |
1022 | /* now rebuild the heap */ |
1057 | /* now rebuild the heap */ |
1023 | for (i = periodiccnt >> 1; i--; ) |
1058 | for (i = periodiccnt >> 1; i--; ) |
1024 | downheap ((WT *)periodics, periodiccnt, i); |
1059 | downheap ((WT *)periodics, periodiccnt, i); |
1025 | } |
1060 | } |
|
|
1061 | #endif |
1026 | |
1062 | |
1027 | inline int |
1063 | inline int |
1028 | time_update_monotonic (EV_P) |
1064 | time_update_monotonic (EV_P) |
1029 | { |
1065 | { |
1030 | mn_now = get_clock (); |
1066 | mn_now = get_clock (); |
1031 | |
1067 | |
1032 | if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) |
1068 | if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) |
1033 | { |
1069 | { |
1034 | rt_now = rtmn_diff + mn_now; |
1070 | ev_rt_now = rtmn_diff + mn_now; |
1035 | return 0; |
1071 | return 0; |
1036 | } |
1072 | } |
1037 | else |
1073 | else |
1038 | { |
1074 | { |
1039 | now_floor = mn_now; |
1075 | now_floor = mn_now; |
1040 | rt_now = ev_time (); |
1076 | ev_rt_now = ev_time (); |
1041 | return 1; |
1077 | return 1; |
1042 | } |
1078 | } |
1043 | } |
1079 | } |
1044 | |
1080 | |
1045 | static void |
1081 | static void |
… | |
… | |
1054 | { |
1090 | { |
1055 | ev_tstamp odiff = rtmn_diff; |
1091 | ev_tstamp odiff = rtmn_diff; |
1056 | |
1092 | |
1057 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
1093 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
1058 | { |
1094 | { |
1059 | rtmn_diff = rt_now - mn_now; |
1095 | rtmn_diff = ev_rt_now - mn_now; |
1060 | |
1096 | |
1061 | if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) |
1097 | if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) |
1062 | return; /* all is well */ |
1098 | return; /* all is well */ |
1063 | |
1099 | |
1064 | rt_now = ev_time (); |
1100 | ev_rt_now = ev_time (); |
1065 | mn_now = get_clock (); |
1101 | mn_now = get_clock (); |
1066 | now_floor = mn_now; |
1102 | now_floor = mn_now; |
1067 | } |
1103 | } |
1068 | |
1104 | |
|
|
1105 | # if EV_PERIODICS |
1069 | periodics_reschedule (EV_A); |
1106 | periodics_reschedule (EV_A); |
|
|
1107 | # endif |
1070 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
1108 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
1071 | /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ |
1109 | /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ |
1072 | } |
1110 | } |
1073 | } |
1111 | } |
1074 | else |
1112 | else |
1075 | #endif |
1113 | #endif |
1076 | { |
1114 | { |
1077 | rt_now = ev_time (); |
1115 | ev_rt_now = ev_time (); |
1078 | |
1116 | |
1079 | if (expect_false (mn_now > rt_now || mn_now < rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
1117 | if (expect_false (mn_now > ev_rt_now || mn_now < ev_rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
1080 | { |
1118 | { |
|
|
1119 | #if EV_PERIODICS |
1081 | periodics_reschedule (EV_A); |
1120 | periodics_reschedule (EV_A); |
|
|
1121 | #endif |
1082 | |
1122 | |
1083 | /* adjust timers. this is easy, as the offset is the same for all */ |
1123 | /* adjust timers. this is easy, as the offset is the same for all */ |
1084 | for (i = 0; i < timercnt; ++i) |
1124 | for (i = 0; i < timercnt; ++i) |
1085 | ((WT)timers [i])->at += rt_now - mn_now; |
1125 | ((WT)timers [i])->at += ev_rt_now - mn_now; |
1086 | } |
1126 | } |
1087 | |
1127 | |
1088 | mn_now = rt_now; |
1128 | mn_now = ev_rt_now; |
1089 | } |
1129 | } |
1090 | } |
1130 | } |
1091 | |
1131 | |
1092 | void |
1132 | void |
1093 | ev_ref (EV_P) |
1133 | ev_ref (EV_P) |
… | |
… | |
1133 | if (expect_true (have_monotonic)) |
1173 | if (expect_true (have_monotonic)) |
1134 | time_update_monotonic (EV_A); |
1174 | time_update_monotonic (EV_A); |
1135 | else |
1175 | else |
1136 | #endif |
1176 | #endif |
1137 | { |
1177 | { |
1138 | rt_now = ev_time (); |
1178 | ev_rt_now = ev_time (); |
1139 | mn_now = rt_now; |
1179 | mn_now = ev_rt_now; |
1140 | } |
1180 | } |
1141 | |
1181 | |
1142 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
1182 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
1143 | block = 0.; |
1183 | block = 0.; |
1144 | else |
1184 | else |
… | |
… | |
1149 | { |
1189 | { |
1150 | ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge; |
1190 | ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge; |
1151 | if (block > to) block = to; |
1191 | if (block > to) block = to; |
1152 | } |
1192 | } |
1153 | |
1193 | |
|
|
1194 | #if EV_PERIODICS |
1154 | if (periodiccnt) |
1195 | if (periodiccnt) |
1155 | { |
1196 | { |
1156 | ev_tstamp to = ((WT)periodics [0])->at - rt_now + method_fudge; |
1197 | ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + method_fudge; |
1157 | if (block > to) block = to; |
1198 | if (block > to) block = to; |
1158 | } |
1199 | } |
|
|
1200 | #endif |
1159 | |
1201 | |
1160 | if (block < 0.) block = 0.; |
1202 | if (block < 0.) block = 0.; |
1161 | } |
1203 | } |
1162 | |
1204 | |
1163 | method_poll (EV_A_ block); |
1205 | method_poll (EV_A_ block); |
1164 | |
1206 | |
1165 | /* update rt_now, do magic */ |
1207 | /* update ev_rt_now, do magic */ |
1166 | time_update (EV_A); |
1208 | time_update (EV_A); |
1167 | |
1209 | |
1168 | /* queue pending timers and reschedule them */ |
1210 | /* queue pending timers and reschedule them */ |
1169 | timers_reify (EV_A); /* relative timers called last */ |
1211 | timers_reify (EV_A); /* relative timers called last */ |
|
|
1212 | #if EV_PERIODICS |
1170 | periodics_reify (EV_A); /* absolute timers called first */ |
1213 | periodics_reify (EV_A); /* absolute timers called first */ |
|
|
1214 | #endif |
1171 | |
1215 | |
1172 | /* queue idle watchers unless io or timers are pending */ |
1216 | /* queue idle watchers unless io or timers are pending */ |
1173 | if (idlecnt && !any_pending (EV_A)) |
1217 | if (idlecnt && !any_pending (EV_A)) |
1174 | queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); |
1218 | queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); |
1175 | |
1219 | |
… | |
… | |
1266 | { |
1310 | { |
1267 | ev_clear_pending (EV_A_ (W)w); |
1311 | ev_clear_pending (EV_A_ (W)w); |
1268 | if (!ev_is_active (w)) |
1312 | if (!ev_is_active (w)) |
1269 | return; |
1313 | return; |
1270 | |
1314 | |
|
|
1315 | assert (("ev_io_start called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax)); |
|
|
1316 | |
1271 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
1317 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
1272 | ev_stop (EV_A_ (W)w); |
1318 | ev_stop (EV_A_ (W)w); |
1273 | |
1319 | |
1274 | fd_change (EV_A_ w->fd); |
1320 | fd_change (EV_A_ w->fd); |
1275 | } |
1321 | } |
… | |
… | |
1302 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1348 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1303 | |
1349 | |
1304 | if (((W)w)->active < timercnt--) |
1350 | if (((W)w)->active < timercnt--) |
1305 | { |
1351 | { |
1306 | timers [((W)w)->active - 1] = timers [timercnt]; |
1352 | timers [((W)w)->active - 1] = timers [timercnt]; |
1307 | downheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1353 | adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1308 | } |
1354 | } |
1309 | |
1355 | |
1310 | ((WT)w)->at = w->repeat; |
1356 | ((WT)w)->at -= mn_now; |
1311 | |
1357 | |
1312 | ev_stop (EV_A_ (W)w); |
1358 | ev_stop (EV_A_ (W)w); |
1313 | } |
1359 | } |
1314 | |
1360 | |
1315 | void |
1361 | void |
… | |
… | |
1318 | if (ev_is_active (w)) |
1364 | if (ev_is_active (w)) |
1319 | { |
1365 | { |
1320 | if (w->repeat) |
1366 | if (w->repeat) |
1321 | { |
1367 | { |
1322 | ((WT)w)->at = mn_now + w->repeat; |
1368 | ((WT)w)->at = mn_now + w->repeat; |
1323 | downheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1369 | adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1324 | } |
1370 | } |
1325 | else |
1371 | else |
1326 | ev_timer_stop (EV_A_ w); |
1372 | ev_timer_stop (EV_A_ w); |
1327 | } |
1373 | } |
1328 | else if (w->repeat) |
1374 | else if (w->repeat) |
1329 | ev_timer_start (EV_A_ w); |
1375 | ev_timer_start (EV_A_ w); |
1330 | } |
1376 | } |
1331 | |
1377 | |
|
|
1378 | #if EV_PERIODICS |
1332 | void |
1379 | void |
1333 | ev_periodic_start (EV_P_ struct ev_periodic *w) |
1380 | ev_periodic_start (EV_P_ struct ev_periodic *w) |
1334 | { |
1381 | { |
1335 | if (ev_is_active (w)) |
1382 | if (ev_is_active (w)) |
1336 | return; |
1383 | return; |
1337 | |
1384 | |
1338 | if (w->reschedule_cb) |
1385 | if (w->reschedule_cb) |
1339 | ((WT)w)->at = w->reschedule_cb (w, rt_now); |
1386 | ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); |
1340 | else if (w->interval) |
1387 | else if (w->interval) |
1341 | { |
1388 | { |
1342 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
1389 | 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 */ |
1390 | /* 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; |
1391 | ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1345 | } |
1392 | } |
1346 | |
1393 | |
1347 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1394 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1348 | array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void)); |
1395 | array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void)); |
1349 | periodics [periodiccnt - 1] = w; |
1396 | periodics [periodiccnt - 1] = w; |
… | |
… | |
1362 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1409 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1363 | |
1410 | |
1364 | if (((W)w)->active < periodiccnt--) |
1411 | if (((W)w)->active < periodiccnt--) |
1365 | { |
1412 | { |
1366 | periodics [((W)w)->active - 1] = periodics [periodiccnt]; |
1413 | periodics [((W)w)->active - 1] = periodics [periodiccnt]; |
1367 | downheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1); |
1414 | adjustheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1); |
1368 | } |
1415 | } |
1369 | |
1416 | |
1370 | ev_stop (EV_A_ (W)w); |
1417 | ev_stop (EV_A_ (W)w); |
1371 | } |
1418 | } |
1372 | |
1419 | |
1373 | void |
1420 | void |
1374 | ev_periodic_again (EV_P_ struct ev_periodic *w) |
1421 | ev_periodic_again (EV_P_ struct ev_periodic *w) |
1375 | { |
1422 | { |
|
|
1423 | /* TODO: use adjustheap and recalculation */ |
1376 | ev_periodic_stop (EV_A_ w); |
1424 | ev_periodic_stop (EV_A_ w); |
1377 | ev_periodic_start (EV_A_ w); |
1425 | ev_periodic_start (EV_A_ w); |
1378 | } |
1426 | } |
|
|
1427 | #endif |
1379 | |
1428 | |
1380 | void |
1429 | void |
1381 | ev_idle_start (EV_P_ struct ev_idle *w) |
1430 | ev_idle_start (EV_P_ struct ev_idle *w) |
1382 | { |
1431 | { |
1383 | if (ev_is_active (w)) |
1432 | if (ev_is_active (w)) |
… | |
… | |
1390 | |
1439 | |
1391 | void |
1440 | void |
1392 | ev_idle_stop (EV_P_ struct ev_idle *w) |
1441 | ev_idle_stop (EV_P_ struct ev_idle *w) |
1393 | { |
1442 | { |
1394 | ev_clear_pending (EV_A_ (W)w); |
1443 | ev_clear_pending (EV_A_ (W)w); |
1395 | if (ev_is_active (w)) |
1444 | if (!ev_is_active (w)) |
1396 | return; |
1445 | return; |
1397 | |
1446 | |
1398 | idles [((W)w)->active - 1] = idles [--idlecnt]; |
1447 | idles [((W)w)->active - 1] = idles [--idlecnt]; |
1399 | ev_stop (EV_A_ (W)w); |
1448 | ev_stop (EV_A_ (W)w); |
1400 | } |
1449 | } |
… | |
… | |
1412 | |
1461 | |
1413 | void |
1462 | void |
1414 | ev_prepare_stop (EV_P_ struct ev_prepare *w) |
1463 | ev_prepare_stop (EV_P_ struct ev_prepare *w) |
1415 | { |
1464 | { |
1416 | ev_clear_pending (EV_A_ (W)w); |
1465 | ev_clear_pending (EV_A_ (W)w); |
1417 | if (ev_is_active (w)) |
1466 | if (!ev_is_active (w)) |
1418 | return; |
1467 | return; |
1419 | |
1468 | |
1420 | prepares [((W)w)->active - 1] = prepares [--preparecnt]; |
1469 | prepares [((W)w)->active - 1] = prepares [--preparecnt]; |
1421 | ev_stop (EV_A_ (W)w); |
1470 | ev_stop (EV_A_ (W)w); |
1422 | } |
1471 | } |
… | |
… | |
1434 | |
1483 | |
1435 | void |
1484 | void |
1436 | ev_check_stop (EV_P_ struct ev_check *w) |
1485 | ev_check_stop (EV_P_ struct ev_check *w) |
1437 | { |
1486 | { |
1438 | ev_clear_pending (EV_A_ (W)w); |
1487 | ev_clear_pending (EV_A_ (W)w); |
1439 | if (ev_is_active (w)) |
1488 | if (!ev_is_active (w)) |
1440 | return; |
1489 | return; |
1441 | |
1490 | |
1442 | checks [((W)w)->active - 1] = checks [--checkcnt]; |
1491 | checks [((W)w)->active - 1] = checks [--checkcnt]; |
1443 | ev_stop (EV_A_ (W)w); |
1492 | ev_stop (EV_A_ (W)w); |
1444 | } |
1493 | } |
… | |
… | |
1505 | |
1554 | |
1506 | void |
1555 | void |
1507 | ev_child_stop (EV_P_ struct ev_child *w) |
1556 | ev_child_stop (EV_P_ struct ev_child *w) |
1508 | { |
1557 | { |
1509 | ev_clear_pending (EV_A_ (W)w); |
1558 | ev_clear_pending (EV_A_ (W)w); |
1510 | if (ev_is_active (w)) |
1559 | if (!ev_is_active (w)) |
1511 | return; |
1560 | return; |
1512 | |
1561 | |
1513 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1562 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1514 | ev_stop (EV_A_ (W)w); |
1563 | ev_stop (EV_A_ (W)w); |
1515 | } |
1564 | } |
… | |
… | |
1559 | else |
1608 | else |
1560 | { |
1609 | { |
1561 | once->cb = cb; |
1610 | once->cb = cb; |
1562 | once->arg = arg; |
1611 | once->arg = arg; |
1563 | |
1612 | |
1564 | ev_watcher_init (&once->io, once_cb_io); |
1613 | ev_init (&once->io, once_cb_io); |
1565 | if (fd >= 0) |
1614 | if (fd >= 0) |
1566 | { |
1615 | { |
1567 | ev_io_set (&once->io, fd, events); |
1616 | ev_io_set (&once->io, fd, events); |
1568 | ev_io_start (EV_A_ &once->io); |
1617 | ev_io_start (EV_A_ &once->io); |
1569 | } |
1618 | } |
1570 | |
1619 | |
1571 | ev_watcher_init (&once->to, once_cb_to); |
1620 | ev_init (&once->to, once_cb_to); |
1572 | if (timeout >= 0.) |
1621 | if (timeout >= 0.) |
1573 | { |
1622 | { |
1574 | ev_timer_set (&once->to, timeout, 0.); |
1623 | ev_timer_set (&once->to, timeout, 0.); |
1575 | ev_timer_start (EV_A_ &once->to); |
1624 | ev_timer_start (EV_A_ &once->to); |
1576 | } |
1625 | } |
1577 | } |
1626 | } |
1578 | } |
1627 | } |
1579 | |
1628 | |
|
|
1629 | #ifdef __cplusplus |
|
|
1630 | } |
|
|
1631 | #endif |
|
|
1632 | |