… | |
… | |
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 | |
… | |
… | |
152 | typedef struct ev_watcher_list *WL; |
161 | typedef struct ev_watcher_list *WL; |
153 | typedef struct ev_watcher_time *WT; |
162 | typedef struct ev_watcher_time *WT; |
154 | |
163 | |
155 | static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
164 | static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
156 | |
165 | |
|
|
166 | #ifdef WIN32 |
157 | #include "ev_win32.c" |
167 | # include "ev_win32.c" |
|
|
168 | #endif |
158 | |
169 | |
159 | /*****************************************************************************/ |
170 | /*****************************************************************************/ |
160 | |
171 | |
161 | static void (*syserr_cb)(const char *msg); |
172 | static void (*syserr_cb)(const char *msg); |
162 | |
173 | |
… | |
… | |
221 | |
232 | |
222 | #if EV_MULTIPLICITY |
233 | #if EV_MULTIPLICITY |
223 | |
234 | |
224 | struct ev_loop |
235 | struct ev_loop |
225 | { |
236 | { |
|
|
237 | ev_tstamp ev_rt_now; |
226 | #define VAR(name,decl) decl; |
238 | #define VAR(name,decl) decl; |
227 | #include "ev_vars.h" |
239 | #include "ev_vars.h" |
228 | #undef VAR |
240 | #undef VAR |
229 | }; |
241 | }; |
230 | #include "ev_wrap.h" |
242 | #include "ev_wrap.h" |
… | |
… | |
232 | struct ev_loop default_loop_struct; |
244 | struct ev_loop default_loop_struct; |
233 | static struct ev_loop *default_loop; |
245 | static struct ev_loop *default_loop; |
234 | |
246 | |
235 | #else |
247 | #else |
236 | |
248 | |
|
|
249 | ev_tstamp ev_rt_now; |
237 | #define VAR(name,decl) static decl; |
250 | #define VAR(name,decl) static decl; |
238 | #include "ev_vars.h" |
251 | #include "ev_vars.h" |
239 | #undef VAR |
252 | #undef VAR |
240 | |
253 | |
241 | static int default_loop; |
254 | static int default_loop; |
242 | |
255 | |
243 | #endif |
256 | #endif |
244 | |
257 | |
245 | /*****************************************************************************/ |
258 | /*****************************************************************************/ |
246 | |
259 | |
247 | inline ev_tstamp |
260 | ev_tstamp |
248 | ev_time (void) |
261 | ev_time (void) |
249 | { |
262 | { |
250 | #if EV_USE_REALTIME |
263 | #if EV_USE_REALTIME |
251 | struct timespec ts; |
264 | struct timespec ts; |
252 | clock_gettime (CLOCK_REALTIME, &ts); |
265 | clock_gettime (CLOCK_REALTIME, &ts); |
… | |
… | |
271 | #endif |
284 | #endif |
272 | |
285 | |
273 | return ev_time (); |
286 | return ev_time (); |
274 | } |
287 | } |
275 | |
288 | |
|
|
289 | #if EV_MULTIPLICITY |
276 | ev_tstamp |
290 | ev_tstamp |
277 | ev_now (EV_P) |
291 | ev_now (EV_P) |
278 | { |
292 | { |
279 | return rt_now; |
293 | return ev_rt_now; |
280 | } |
294 | } |
|
|
295 | #endif |
281 | |
296 | |
282 | #define array_roundsize(type,n) ((n) | 4 & ~3) |
297 | #define array_roundsize(type,n) ((n) | 4 & ~3) |
283 | |
298 | |
284 | #define array_needsize(type,base,cur,cnt,init) \ |
299 | #define array_needsize(type,base,cur,cnt,init) \ |
285 | if (expect_false ((cnt) > cur)) \ |
300 | if (expect_false ((cnt) > cur)) \ |
… | |
… | |
518 | |
533 | |
519 | heap [k] = w; |
534 | heap [k] = w; |
520 | ((W)heap [k])->active = k + 1; |
535 | ((W)heap [k])->active = k + 1; |
521 | } |
536 | } |
522 | |
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 | |
523 | /*****************************************************************************/ |
550 | /*****************************************************************************/ |
524 | |
551 | |
525 | typedef struct |
552 | typedef struct |
526 | { |
553 | { |
527 | WL head; |
554 | WL head; |
… | |
… | |
723 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
750 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
724 | have_monotonic = 1; |
751 | have_monotonic = 1; |
725 | } |
752 | } |
726 | #endif |
753 | #endif |
727 | |
754 | |
728 | rt_now = ev_time (); |
755 | ev_rt_now = ev_time (); |
729 | mn_now = get_clock (); |
756 | mn_now = get_clock (); |
730 | now_floor = mn_now; |
757 | now_floor = mn_now; |
731 | rtmn_diff = rt_now - mn_now; |
758 | rtmn_diff = ev_rt_now - mn_now; |
732 | |
759 | |
733 | if (methods == EVMETHOD_AUTO) |
760 | if (methods == EVMETHOD_AUTO) |
734 | if (!enable_secure () && getenv ("LIBEV_METHODS")) |
761 | if (!enable_secure () && getenv ("LIBEV_METHODS")) |
735 | methods = atoi (getenv ("LIBEV_METHODS")); |
762 | methods = atoi (getenv ("LIBEV_METHODS")); |
736 | else |
763 | else |
… | |
… | |
751 | #endif |
778 | #endif |
752 | #if EV_USE_SELECT |
779 | #if EV_USE_SELECT |
753 | if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); |
780 | if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); |
754 | #endif |
781 | #endif |
755 | |
782 | |
756 | ev_watcher_init (&sigev, sigcb); |
783 | ev_init (&sigev, sigcb); |
757 | ev_set_priority (&sigev, EV_MAXPRI); |
784 | ev_set_priority (&sigev, EV_MAXPRI); |
758 | } |
785 | } |
759 | } |
786 | } |
760 | |
787 | |
761 | void |
788 | void |
… | |
… | |
783 | array_free (pending, [i]); |
810 | array_free (pending, [i]); |
784 | |
811 | |
785 | /* have to use the microsoft-never-gets-it-right macro */ |
812 | /* have to use the microsoft-never-gets-it-right macro */ |
786 | array_free_microshit (fdchange); |
813 | array_free_microshit (fdchange); |
787 | array_free_microshit (timer); |
814 | array_free_microshit (timer); |
|
|
815 | #if EV_PERIODICS |
788 | array_free_microshit (periodic); |
816 | array_free_microshit (periodic); |
|
|
817 | #endif |
789 | array_free_microshit (idle); |
818 | array_free_microshit (idle); |
790 | array_free_microshit (prepare); |
819 | array_free_microshit (prepare); |
791 | array_free_microshit (check); |
820 | array_free_microshit (check); |
792 | |
821 | |
793 | method = 0; |
822 | method = 0; |
… | |
… | |
966 | |
995 | |
967 | /* first reschedule or stop timer */ |
996 | /* first reschedule or stop timer */ |
968 | if (w->repeat) |
997 | if (w->repeat) |
969 | { |
998 | { |
970 | 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 | |
971 | ((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 | |
972 | downheap ((WT *)timers, timercnt, 0); |
1005 | downheap ((WT *)timers, timercnt, 0); |
973 | } |
1006 | } |
974 | else |
1007 | else |
975 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
1008 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
976 | |
1009 | |
977 | ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); |
1010 | ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); |
978 | } |
1011 | } |
979 | } |
1012 | } |
980 | |
1013 | |
|
|
1014 | #if EV_PERIODICS |
981 | static void |
1015 | static void |
982 | periodics_reify (EV_P) |
1016 | periodics_reify (EV_P) |
983 | { |
1017 | { |
984 | while (periodiccnt && ((WT)periodics [0])->at <= rt_now) |
1018 | while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) |
985 | { |
1019 | { |
986 | struct ev_periodic *w = periodics [0]; |
1020 | struct ev_periodic *w = periodics [0]; |
987 | |
1021 | |
988 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
1022 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
989 | |
1023 | |
990 | /* first reschedule or stop timer */ |
1024 | /* first reschedule or stop timer */ |
991 | if (w->reschedule_cb) |
1025 | if (w->reschedule_cb) |
992 | { |
1026 | { |
993 | 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); |
994 | |
1028 | |
995 | 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)); |
996 | downheap ((WT *)periodics, periodiccnt, 0); |
1030 | downheap ((WT *)periodics, periodiccnt, 0); |
997 | } |
1031 | } |
998 | else if (w->interval) |
1032 | else if (w->interval) |
999 | { |
1033 | { |
1000 | ((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; |
1001 | 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)); |
1002 | downheap ((WT *)periodics, periodiccnt, 0); |
1036 | downheap ((WT *)periodics, periodiccnt, 0); |
1003 | } |
1037 | } |
1004 | else |
1038 | else |
1005 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
1039 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
1006 | |
1040 | |
… | |
… | |
1017 | for (i = 0; i < periodiccnt; ++i) |
1051 | for (i = 0; i < periodiccnt; ++i) |
1018 | { |
1052 | { |
1019 | struct ev_periodic *w = periodics [i]; |
1053 | struct ev_periodic *w = periodics [i]; |
1020 | |
1054 | |
1021 | if (w->reschedule_cb) |
1055 | if (w->reschedule_cb) |
1022 | ((WT)w)->at = w->reschedule_cb (w, rt_now); |
1056 | ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); |
1023 | else if (w->interval) |
1057 | else if (w->interval) |
1024 | ((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; |
1025 | } |
1059 | } |
1026 | |
1060 | |
1027 | /* now rebuild the heap */ |
1061 | /* now rebuild the heap */ |
1028 | for (i = periodiccnt >> 1; i--; ) |
1062 | for (i = periodiccnt >> 1; i--; ) |
1029 | downheap ((WT *)periodics, periodiccnt, i); |
1063 | downheap ((WT *)periodics, periodiccnt, i); |
1030 | } |
1064 | } |
|
|
1065 | #endif |
1031 | |
1066 | |
1032 | inline int |
1067 | inline int |
1033 | time_update_monotonic (EV_P) |
1068 | time_update_monotonic (EV_P) |
1034 | { |
1069 | { |
1035 | mn_now = get_clock (); |
1070 | mn_now = get_clock (); |
1036 | |
1071 | |
1037 | if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) |
1072 | if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) |
1038 | { |
1073 | { |
1039 | rt_now = rtmn_diff + mn_now; |
1074 | ev_rt_now = rtmn_diff + mn_now; |
1040 | return 0; |
1075 | return 0; |
1041 | } |
1076 | } |
1042 | else |
1077 | else |
1043 | { |
1078 | { |
1044 | now_floor = mn_now; |
1079 | now_floor = mn_now; |
1045 | rt_now = ev_time (); |
1080 | ev_rt_now = ev_time (); |
1046 | return 1; |
1081 | return 1; |
1047 | } |
1082 | } |
1048 | } |
1083 | } |
1049 | |
1084 | |
1050 | static void |
1085 | static void |
… | |
… | |
1059 | { |
1094 | { |
1060 | ev_tstamp odiff = rtmn_diff; |
1095 | ev_tstamp odiff = rtmn_diff; |
1061 | |
1096 | |
1062 | 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 */ |
1063 | { |
1098 | { |
1064 | rtmn_diff = rt_now - mn_now; |
1099 | rtmn_diff = ev_rt_now - mn_now; |
1065 | |
1100 | |
1066 | if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) |
1101 | if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) |
1067 | return; /* all is well */ |
1102 | return; /* all is well */ |
1068 | |
1103 | |
1069 | rt_now = ev_time (); |
1104 | ev_rt_now = ev_time (); |
1070 | mn_now = get_clock (); |
1105 | mn_now = get_clock (); |
1071 | now_floor = mn_now; |
1106 | now_floor = mn_now; |
1072 | } |
1107 | } |
1073 | |
1108 | |
|
|
1109 | # if EV_PERIODICS |
1074 | periodics_reschedule (EV_A); |
1110 | periodics_reschedule (EV_A); |
|
|
1111 | # endif |
1075 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
1112 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
1076 | /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ |
1113 | /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ |
1077 | } |
1114 | } |
1078 | } |
1115 | } |
1079 | else |
1116 | else |
1080 | #endif |
1117 | #endif |
1081 | { |
1118 | { |
1082 | rt_now = ev_time (); |
1119 | ev_rt_now = ev_time (); |
1083 | |
1120 | |
1084 | 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)) |
1085 | { |
1122 | { |
|
|
1123 | #if EV_PERIODICS |
1086 | periodics_reschedule (EV_A); |
1124 | periodics_reschedule (EV_A); |
|
|
1125 | #endif |
1087 | |
1126 | |
1088 | /* 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 */ |
1089 | for (i = 0; i < timercnt; ++i) |
1128 | for (i = 0; i < timercnt; ++i) |
1090 | ((WT)timers [i])->at += rt_now - mn_now; |
1129 | ((WT)timers [i])->at += ev_rt_now - mn_now; |
1091 | } |
1130 | } |
1092 | |
1131 | |
1093 | mn_now = rt_now; |
1132 | mn_now = ev_rt_now; |
1094 | } |
1133 | } |
1095 | } |
1134 | } |
1096 | |
1135 | |
1097 | void |
1136 | void |
1098 | ev_ref (EV_P) |
1137 | ev_ref (EV_P) |
… | |
… | |
1138 | if (expect_true (have_monotonic)) |
1177 | if (expect_true (have_monotonic)) |
1139 | time_update_monotonic (EV_A); |
1178 | time_update_monotonic (EV_A); |
1140 | else |
1179 | else |
1141 | #endif |
1180 | #endif |
1142 | { |
1181 | { |
1143 | rt_now = ev_time (); |
1182 | ev_rt_now = ev_time (); |
1144 | mn_now = rt_now; |
1183 | mn_now = ev_rt_now; |
1145 | } |
1184 | } |
1146 | |
1185 | |
1147 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
1186 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
1148 | block = 0.; |
1187 | block = 0.; |
1149 | else |
1188 | else |
… | |
… | |
1154 | { |
1193 | { |
1155 | ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge; |
1194 | ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge; |
1156 | if (block > to) block = to; |
1195 | if (block > to) block = to; |
1157 | } |
1196 | } |
1158 | |
1197 | |
|
|
1198 | #if EV_PERIODICS |
1159 | if (periodiccnt) |
1199 | if (periodiccnt) |
1160 | { |
1200 | { |
1161 | ev_tstamp to = ((WT)periodics [0])->at - rt_now + method_fudge; |
1201 | ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + method_fudge; |
1162 | if (block > to) block = to; |
1202 | if (block > to) block = to; |
1163 | } |
1203 | } |
|
|
1204 | #endif |
1164 | |
1205 | |
1165 | if (block < 0.) block = 0.; |
1206 | if (block < 0.) block = 0.; |
1166 | } |
1207 | } |
1167 | |
1208 | |
1168 | method_poll (EV_A_ block); |
1209 | method_poll (EV_A_ block); |
1169 | |
1210 | |
1170 | /* update rt_now, do magic */ |
1211 | /* update ev_rt_now, do magic */ |
1171 | time_update (EV_A); |
1212 | time_update (EV_A); |
1172 | |
1213 | |
1173 | /* queue pending timers and reschedule them */ |
1214 | /* queue pending timers and reschedule them */ |
1174 | timers_reify (EV_A); /* relative timers called last */ |
1215 | timers_reify (EV_A); /* relative timers called last */ |
|
|
1216 | #if EV_PERIODICS |
1175 | periodics_reify (EV_A); /* absolute timers called first */ |
1217 | periodics_reify (EV_A); /* absolute timers called first */ |
|
|
1218 | #endif |
1176 | |
1219 | |
1177 | /* queue idle watchers unless io or timers are pending */ |
1220 | /* queue idle watchers unless io or timers are pending */ |
1178 | if (idlecnt && !any_pending (EV_A)) |
1221 | if (idlecnt && !any_pending (EV_A)) |
1179 | queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); |
1222 | queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); |
1180 | |
1223 | |
… | |
… | |
1271 | { |
1314 | { |
1272 | ev_clear_pending (EV_A_ (W)w); |
1315 | ev_clear_pending (EV_A_ (W)w); |
1273 | if (!ev_is_active (w)) |
1316 | if (!ev_is_active (w)) |
1274 | return; |
1317 | return; |
1275 | |
1318 | |
|
|
1319 | assert (("ev_io_start called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax)); |
|
|
1320 | |
1276 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
1321 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
1277 | ev_stop (EV_A_ (W)w); |
1322 | ev_stop (EV_A_ (W)w); |
1278 | |
1323 | |
1279 | fd_change (EV_A_ w->fd); |
1324 | fd_change (EV_A_ w->fd); |
1280 | } |
1325 | } |
… | |
… | |
1310 | { |
1355 | { |
1311 | timers [((W)w)->active - 1] = timers [timercnt]; |
1356 | timers [((W)w)->active - 1] = timers [timercnt]; |
1312 | downheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1357 | downheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1313 | } |
1358 | } |
1314 | |
1359 | |
1315 | ((WT)w)->at = w->repeat; |
1360 | ((WT)w)->at -= mn_now; |
1316 | |
1361 | |
1317 | ev_stop (EV_A_ (W)w); |
1362 | ev_stop (EV_A_ (W)w); |
1318 | } |
1363 | } |
1319 | |
1364 | |
1320 | void |
1365 | void |
1321 | ev_timer_again (EV_P_ struct ev_timer *w) |
1366 | ev_timer_again (EV_P_ struct ev_timer *w) |
1322 | { |
1367 | { |
1323 | if (ev_is_active (w)) |
1368 | if (ev_is_active (w)) |
1324 | { |
1369 | { |
1325 | if (w->repeat) |
1370 | if (w->repeat) |
1326 | { |
|
|
1327 | ((WT)w)->at = mn_now + w->repeat; |
|
|
1328 | downheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1371 | adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1, mn_now + w->repeat); |
1329 | } |
|
|
1330 | else |
1372 | else |
1331 | ev_timer_stop (EV_A_ w); |
1373 | ev_timer_stop (EV_A_ w); |
1332 | } |
1374 | } |
1333 | else if (w->repeat) |
1375 | else if (w->repeat) |
1334 | ev_timer_start (EV_A_ w); |
1376 | ev_timer_start (EV_A_ w); |
1335 | } |
1377 | } |
1336 | |
1378 | |
|
|
1379 | #if EV_PERIODICS |
1337 | void |
1380 | void |
1338 | ev_periodic_start (EV_P_ struct ev_periodic *w) |
1381 | ev_periodic_start (EV_P_ struct ev_periodic *w) |
1339 | { |
1382 | { |
1340 | if (ev_is_active (w)) |
1383 | if (ev_is_active (w)) |
1341 | return; |
1384 | return; |
1342 | |
1385 | |
1343 | if (w->reschedule_cb) |
1386 | if (w->reschedule_cb) |
1344 | ((WT)w)->at = w->reschedule_cb (w, rt_now); |
1387 | ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); |
1345 | else if (w->interval) |
1388 | else if (w->interval) |
1346 | { |
1389 | { |
1347 | 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.)); |
1348 | /* 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 */ |
1349 | ((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; |
1350 | } |
1393 | } |
1351 | |
1394 | |
1352 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1395 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1353 | array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void)); |
1396 | array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void)); |
1354 | periodics [periodiccnt - 1] = w; |
1397 | periodics [periodiccnt - 1] = w; |
… | |
… | |
1376 | } |
1419 | } |
1377 | |
1420 | |
1378 | void |
1421 | void |
1379 | ev_periodic_again (EV_P_ struct ev_periodic *w) |
1422 | ev_periodic_again (EV_P_ struct ev_periodic *w) |
1380 | { |
1423 | { |
|
|
1424 | /* TODO: use adjustheap and recalculation */ |
1381 | ev_periodic_stop (EV_A_ w); |
1425 | ev_periodic_stop (EV_A_ w); |
1382 | ev_periodic_start (EV_A_ w); |
1426 | ev_periodic_start (EV_A_ w); |
1383 | } |
1427 | } |
|
|
1428 | #endif |
1384 | |
1429 | |
1385 | void |
1430 | void |
1386 | ev_idle_start (EV_P_ struct ev_idle *w) |
1431 | ev_idle_start (EV_P_ struct ev_idle *w) |
1387 | { |
1432 | { |
1388 | if (ev_is_active (w)) |
1433 | if (ev_is_active (w)) |
… | |
… | |
1439 | |
1484 | |
1440 | void |
1485 | void |
1441 | ev_check_stop (EV_P_ struct ev_check *w) |
1486 | ev_check_stop (EV_P_ struct ev_check *w) |
1442 | { |
1487 | { |
1443 | ev_clear_pending (EV_A_ (W)w); |
1488 | ev_clear_pending (EV_A_ (W)w); |
1444 | if (ev_is_active (w)) |
1489 | if (!ev_is_active (w)) |
1445 | return; |
1490 | return; |
1446 | |
1491 | |
1447 | checks [((W)w)->active - 1] = checks [--checkcnt]; |
1492 | checks [((W)w)->active - 1] = checks [--checkcnt]; |
1448 | ev_stop (EV_A_ (W)w); |
1493 | ev_stop (EV_A_ (W)w); |
1449 | } |
1494 | } |
… | |
… | |
1510 | |
1555 | |
1511 | void |
1556 | void |
1512 | ev_child_stop (EV_P_ struct ev_child *w) |
1557 | ev_child_stop (EV_P_ struct ev_child *w) |
1513 | { |
1558 | { |
1514 | ev_clear_pending (EV_A_ (W)w); |
1559 | ev_clear_pending (EV_A_ (W)w); |
1515 | if (ev_is_active (w)) |
1560 | if (!ev_is_active (w)) |
1516 | return; |
1561 | return; |
1517 | |
1562 | |
1518 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1563 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1519 | ev_stop (EV_A_ (W)w); |
1564 | ev_stop (EV_A_ (W)w); |
1520 | } |
1565 | } |
… | |
… | |
1564 | else |
1609 | else |
1565 | { |
1610 | { |
1566 | once->cb = cb; |
1611 | once->cb = cb; |
1567 | once->arg = arg; |
1612 | once->arg = arg; |
1568 | |
1613 | |
1569 | ev_watcher_init (&once->io, once_cb_io); |
1614 | ev_init (&once->io, once_cb_io); |
1570 | if (fd >= 0) |
1615 | if (fd >= 0) |
1571 | { |
1616 | { |
1572 | ev_io_set (&once->io, fd, events); |
1617 | ev_io_set (&once->io, fd, events); |
1573 | ev_io_start (EV_A_ &once->io); |
1618 | ev_io_start (EV_A_ &once->io); |
1574 | } |
1619 | } |
1575 | |
1620 | |
1576 | ev_watcher_init (&once->to, once_cb_to); |
1621 | ev_init (&once->to, once_cb_to); |
1577 | if (timeout >= 0.) |
1622 | if (timeout >= 0.) |
1578 | { |
1623 | { |
1579 | ev_timer_set (&once->to, timeout, 0.); |
1624 | ev_timer_set (&once->to, timeout, 0.); |
1580 | ev_timer_start (EV_A_ &once->to); |
1625 | ev_timer_start (EV_A_ &once->to); |
1581 | } |
1626 | } |
1582 | } |
1627 | } |
1583 | } |
1628 | } |
1584 | |
1629 | |
|
|
1630 | #ifdef __cplusplus |
|
|
1631 | } |
|
|
1632 | #endif |
|
|
1633 | |