… | |
… | |
271 | #endif |
271 | #endif |
272 | |
272 | |
273 | return ev_time (); |
273 | return ev_time (); |
274 | } |
274 | } |
275 | |
275 | |
|
|
276 | #if EV_MULTIPLICITY |
276 | ev_tstamp |
277 | ev_tstamp |
277 | ev_now (EV_P) |
278 | ev_now (EV_P) |
278 | { |
279 | { |
279 | return rt_now; |
280 | return ev_rt_now; |
280 | } |
281 | } |
|
|
282 | #endif |
281 | |
283 | |
282 | #define array_roundsize(type,n) ((n) | 4 & ~3) |
284 | #define array_roundsize(type,n) ((n) | 4 & ~3) |
283 | |
285 | |
284 | #define array_needsize(type,base,cur,cnt,init) \ |
286 | #define array_needsize(type,base,cur,cnt,init) \ |
285 | if (expect_false ((cnt) > cur)) \ |
287 | if (expect_false ((cnt) > cur)) \ |
… | |
… | |
735 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
737 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
736 | have_monotonic = 1; |
738 | have_monotonic = 1; |
737 | } |
739 | } |
738 | #endif |
740 | #endif |
739 | |
741 | |
740 | rt_now = ev_time (); |
742 | ev_rt_now = ev_time (); |
741 | mn_now = get_clock (); |
743 | mn_now = get_clock (); |
742 | now_floor = mn_now; |
744 | now_floor = mn_now; |
743 | rtmn_diff = rt_now - mn_now; |
745 | rtmn_diff = ev_rt_now - mn_now; |
744 | |
746 | |
745 | if (methods == EVMETHOD_AUTO) |
747 | if (methods == EVMETHOD_AUTO) |
746 | if (!enable_secure () && getenv ("LIBEV_METHODS")) |
748 | if (!enable_secure () && getenv ("LIBEV_METHODS")) |
747 | methods = atoi (getenv ("LIBEV_METHODS")); |
749 | methods = atoi (getenv ("LIBEV_METHODS")); |
748 | else |
750 | else |
… | |
… | |
991 | } |
993 | } |
992 | |
994 | |
993 | static void |
995 | static void |
994 | periodics_reify (EV_P) |
996 | periodics_reify (EV_P) |
995 | { |
997 | { |
996 | while (periodiccnt && ((WT)periodics [0])->at <= rt_now) |
998 | while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) |
997 | { |
999 | { |
998 | struct ev_periodic *w = periodics [0]; |
1000 | struct ev_periodic *w = periodics [0]; |
999 | |
1001 | |
1000 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
1002 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
1001 | |
1003 | |
1002 | /* first reschedule or stop timer */ |
1004 | /* first reschedule or stop timer */ |
1003 | if (w->reschedule_cb) |
1005 | if (w->reschedule_cb) |
1004 | { |
1006 | { |
1005 | ev_tstamp at = ((WT)w)->at = w->reschedule_cb (w, rt_now + 0.0001); |
1007 | ev_tstamp at = ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + 0.0001); |
1006 | |
1008 | |
1007 | assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > rt_now)); |
1009 | assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now)); |
1008 | downheap ((WT *)periodics, periodiccnt, 0); |
1010 | downheap ((WT *)periodics, periodiccnt, 0); |
1009 | } |
1011 | } |
1010 | else if (w->interval) |
1012 | else if (w->interval) |
1011 | { |
1013 | { |
1012 | ((WT)w)->at += floor ((rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval; |
1014 | ((WT)w)->at += floor ((ev_rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval; |
1013 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > rt_now)); |
1015 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > ev_rt_now)); |
1014 | downheap ((WT *)periodics, periodiccnt, 0); |
1016 | downheap ((WT *)periodics, periodiccnt, 0); |
1015 | } |
1017 | } |
1016 | else |
1018 | else |
1017 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
1019 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
1018 | |
1020 | |
… | |
… | |
1029 | for (i = 0; i < periodiccnt; ++i) |
1031 | for (i = 0; i < periodiccnt; ++i) |
1030 | { |
1032 | { |
1031 | struct ev_periodic *w = periodics [i]; |
1033 | struct ev_periodic *w = periodics [i]; |
1032 | |
1034 | |
1033 | if (w->reschedule_cb) |
1035 | if (w->reschedule_cb) |
1034 | ((WT)w)->at = w->reschedule_cb (w, rt_now); |
1036 | ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); |
1035 | else if (w->interval) |
1037 | else if (w->interval) |
1036 | ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1038 | ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1037 | } |
1039 | } |
1038 | |
1040 | |
1039 | /* now rebuild the heap */ |
1041 | /* now rebuild the heap */ |
1040 | for (i = periodiccnt >> 1; i--; ) |
1042 | for (i = periodiccnt >> 1; i--; ) |
1041 | downheap ((WT *)periodics, periodiccnt, i); |
1043 | downheap ((WT *)periodics, periodiccnt, i); |
… | |
… | |
1046 | { |
1048 | { |
1047 | mn_now = get_clock (); |
1049 | mn_now = get_clock (); |
1048 | |
1050 | |
1049 | if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) |
1051 | if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) |
1050 | { |
1052 | { |
1051 | rt_now = rtmn_diff + mn_now; |
1053 | ev_rt_now = rtmn_diff + mn_now; |
1052 | return 0; |
1054 | return 0; |
1053 | } |
1055 | } |
1054 | else |
1056 | else |
1055 | { |
1057 | { |
1056 | now_floor = mn_now; |
1058 | now_floor = mn_now; |
1057 | rt_now = ev_time (); |
1059 | ev_rt_now = ev_time (); |
1058 | return 1; |
1060 | return 1; |
1059 | } |
1061 | } |
1060 | } |
1062 | } |
1061 | |
1063 | |
1062 | static void |
1064 | static void |
… | |
… | |
1071 | { |
1073 | { |
1072 | ev_tstamp odiff = rtmn_diff; |
1074 | ev_tstamp odiff = rtmn_diff; |
1073 | |
1075 | |
1074 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
1076 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
1075 | { |
1077 | { |
1076 | rtmn_diff = rt_now - mn_now; |
1078 | rtmn_diff = ev_rt_now - mn_now; |
1077 | |
1079 | |
1078 | if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) |
1080 | if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) |
1079 | return; /* all is well */ |
1081 | return; /* all is well */ |
1080 | |
1082 | |
1081 | rt_now = ev_time (); |
1083 | ev_rt_now = ev_time (); |
1082 | mn_now = get_clock (); |
1084 | mn_now = get_clock (); |
1083 | now_floor = mn_now; |
1085 | now_floor = mn_now; |
1084 | } |
1086 | } |
1085 | |
1087 | |
1086 | periodics_reschedule (EV_A); |
1088 | periodics_reschedule (EV_A); |
… | |
… | |
1089 | } |
1091 | } |
1090 | } |
1092 | } |
1091 | else |
1093 | else |
1092 | #endif |
1094 | #endif |
1093 | { |
1095 | { |
1094 | rt_now = ev_time (); |
1096 | ev_rt_now = ev_time (); |
1095 | |
1097 | |
1096 | if (expect_false (mn_now > rt_now || mn_now < rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
1098 | if (expect_false (mn_now > ev_rt_now || mn_now < ev_rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
1097 | { |
1099 | { |
1098 | periodics_reschedule (EV_A); |
1100 | periodics_reschedule (EV_A); |
1099 | |
1101 | |
1100 | /* adjust timers. this is easy, as the offset is the same for all */ |
1102 | /* adjust timers. this is easy, as the offset is the same for all */ |
1101 | for (i = 0; i < timercnt; ++i) |
1103 | for (i = 0; i < timercnt; ++i) |
1102 | ((WT)timers [i])->at += rt_now - mn_now; |
1104 | ((WT)timers [i])->at += ev_rt_now - mn_now; |
1103 | } |
1105 | } |
1104 | |
1106 | |
1105 | mn_now = rt_now; |
1107 | mn_now = ev_rt_now; |
1106 | } |
1108 | } |
1107 | } |
1109 | } |
1108 | |
1110 | |
1109 | void |
1111 | void |
1110 | ev_ref (EV_P) |
1112 | ev_ref (EV_P) |
… | |
… | |
1150 | if (expect_true (have_monotonic)) |
1152 | if (expect_true (have_monotonic)) |
1151 | time_update_monotonic (EV_A); |
1153 | time_update_monotonic (EV_A); |
1152 | else |
1154 | else |
1153 | #endif |
1155 | #endif |
1154 | { |
1156 | { |
1155 | rt_now = ev_time (); |
1157 | ev_rt_now = ev_time (); |
1156 | mn_now = rt_now; |
1158 | mn_now = ev_rt_now; |
1157 | } |
1159 | } |
1158 | |
1160 | |
1159 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
1161 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
1160 | block = 0.; |
1162 | block = 0.; |
1161 | else |
1163 | else |
… | |
… | |
1168 | if (block > to) block = to; |
1170 | if (block > to) block = to; |
1169 | } |
1171 | } |
1170 | |
1172 | |
1171 | if (periodiccnt) |
1173 | if (periodiccnt) |
1172 | { |
1174 | { |
1173 | ev_tstamp to = ((WT)periodics [0])->at - rt_now + method_fudge; |
1175 | ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + method_fudge; |
1174 | if (block > to) block = to; |
1176 | if (block > to) block = to; |
1175 | } |
1177 | } |
1176 | |
1178 | |
1177 | if (block < 0.) block = 0.; |
1179 | if (block < 0.) block = 0.; |
1178 | } |
1180 | } |
1179 | |
1181 | |
1180 | method_poll (EV_A_ block); |
1182 | method_poll (EV_A_ block); |
1181 | |
1183 | |
1182 | /* update rt_now, do magic */ |
1184 | /* update ev_rt_now, do magic */ |
1183 | time_update (EV_A); |
1185 | time_update (EV_A); |
1184 | |
1186 | |
1185 | /* queue pending timers and reschedule them */ |
1187 | /* queue pending timers and reschedule them */ |
1186 | timers_reify (EV_A); /* relative timers called last */ |
1188 | timers_reify (EV_A); /* relative timers called last */ |
1187 | periodics_reify (EV_A); /* absolute timers called first */ |
1189 | periodics_reify (EV_A); /* absolute timers called first */ |
… | |
… | |
1348 | { |
1350 | { |
1349 | if (ev_is_active (w)) |
1351 | if (ev_is_active (w)) |
1350 | return; |
1352 | return; |
1351 | |
1353 | |
1352 | if (w->reschedule_cb) |
1354 | if (w->reschedule_cb) |
1353 | ((WT)w)->at = w->reschedule_cb (w, rt_now); |
1355 | ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); |
1354 | else if (w->interval) |
1356 | else if (w->interval) |
1355 | { |
1357 | { |
1356 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
1358 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
1357 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1359 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1358 | ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1360 | ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1359 | } |
1361 | } |
1360 | |
1362 | |
1361 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1363 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1362 | array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void)); |
1364 | array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void)); |
1363 | periodics [periodiccnt - 1] = w; |
1365 | periodics [periodiccnt - 1] = w; |