… | |
… | |
35 | |
35 | |
36 | #ifndef EV_STANDALONE |
36 | #ifndef EV_STANDALONE |
37 | # include "config.h" |
37 | # include "config.h" |
38 | |
38 | |
39 | # if HAVE_CLOCK_GETTIME |
39 | # if HAVE_CLOCK_GETTIME |
|
|
40 | # ifndef EV_USE_MONOTONIC |
40 | # define EV_USE_MONOTONIC 1 |
41 | # define EV_USE_MONOTONIC 1 |
|
|
42 | # endif |
|
|
43 | # ifndef EV_USE_REALTIME |
41 | # define EV_USE_REALTIME 1 |
44 | # define EV_USE_REALTIME 1 |
|
|
45 | # endif |
42 | # endif |
46 | # endif |
43 | |
47 | |
44 | # if HAVE_SELECT && HAVE_SYS_SELECT_H |
48 | # if HAVE_SELECT && HAVE_SYS_SELECT_H && !defined (EV_USE_SELECT) |
45 | # define EV_USE_SELECT 1 |
49 | # define EV_USE_SELECT 1 |
46 | # endif |
50 | # endif |
47 | |
51 | |
48 | # if HAVE_POLL && HAVE_POLL_H |
52 | # if HAVE_POLL && HAVE_POLL_H && !defined (EV_USE_POLL) |
49 | # define EV_USE_POLL 1 |
53 | # define EV_USE_POLL 1 |
50 | # endif |
54 | # endif |
51 | |
55 | |
52 | # 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) |
53 | # define EV_USE_EPOLL 1 |
57 | # define EV_USE_EPOLL 1 |
54 | # endif |
58 | # endif |
55 | |
59 | |
56 | # if HAVE_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) |
57 | # define EV_USE_KQUEUE 1 |
61 | # define EV_USE_KQUEUE 1 |
58 | # endif |
62 | # endif |
59 | |
63 | |
60 | #endif |
64 | #endif |
61 | |
65 | |
… | |
… | |
157 | typedef struct ev_watcher_list *WL; |
161 | typedef struct ev_watcher_list *WL; |
158 | typedef struct ev_watcher_time *WT; |
162 | typedef struct ev_watcher_time *WT; |
159 | |
163 | |
160 | static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
164 | static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
161 | |
165 | |
|
|
166 | #ifdef WIN32 |
162 | #include "ev_win32.c" |
167 | # include "ev_win32.c" |
|
|
168 | #endif |
163 | |
169 | |
164 | /*****************************************************************************/ |
170 | /*****************************************************************************/ |
165 | |
171 | |
166 | static void (*syserr_cb)(const char *msg); |
172 | static void (*syserr_cb)(const char *msg); |
167 | |
173 | |
… | |
… | |
249 | |
255 | |
250 | #endif |
256 | #endif |
251 | |
257 | |
252 | /*****************************************************************************/ |
258 | /*****************************************************************************/ |
253 | |
259 | |
254 | inline ev_tstamp |
260 | ev_tstamp |
255 | ev_time (void) |
261 | ev_time (void) |
256 | { |
262 | { |
257 | #if EV_USE_REALTIME |
263 | #if EV_USE_REALTIME |
258 | struct timespec ts; |
264 | struct timespec ts; |
259 | clock_gettime (CLOCK_REALTIME, &ts); |
265 | clock_gettime (CLOCK_REALTIME, &ts); |
… | |
… | |
804 | array_free (pending, [i]); |
810 | array_free (pending, [i]); |
805 | |
811 | |
806 | /* have to use the microsoft-never-gets-it-right macro */ |
812 | /* have to use the microsoft-never-gets-it-right macro */ |
807 | array_free_microshit (fdchange); |
813 | array_free_microshit (fdchange); |
808 | array_free_microshit (timer); |
814 | array_free_microshit (timer); |
|
|
815 | #if EV_PERIODICS |
809 | array_free_microshit (periodic); |
816 | array_free_microshit (periodic); |
|
|
817 | #endif |
810 | array_free_microshit (idle); |
818 | array_free_microshit (idle); |
811 | array_free_microshit (prepare); |
819 | array_free_microshit (prepare); |
812 | array_free_microshit (check); |
820 | array_free_microshit (check); |
813 | |
821 | |
814 | method = 0; |
822 | method = 0; |
… | |
… | |
1001 | |
1009 | |
1002 | ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); |
1010 | ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); |
1003 | } |
1011 | } |
1004 | } |
1012 | } |
1005 | |
1013 | |
|
|
1014 | #if EV_PERIODICS |
1006 | static void |
1015 | static void |
1007 | periodics_reify (EV_P) |
1016 | periodics_reify (EV_P) |
1008 | { |
1017 | { |
1009 | while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) |
1018 | while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) |
1010 | { |
1019 | { |
… | |
… | |
1051 | |
1060 | |
1052 | /* now rebuild the heap */ |
1061 | /* now rebuild the heap */ |
1053 | for (i = periodiccnt >> 1; i--; ) |
1062 | for (i = periodiccnt >> 1; i--; ) |
1054 | downheap ((WT *)periodics, periodiccnt, i); |
1063 | downheap ((WT *)periodics, periodiccnt, i); |
1055 | } |
1064 | } |
|
|
1065 | #endif |
1056 | |
1066 | |
1057 | inline int |
1067 | inline int |
1058 | time_update_monotonic (EV_P) |
1068 | time_update_monotonic (EV_P) |
1059 | { |
1069 | { |
1060 | mn_now = get_clock (); |
1070 | mn_now = get_clock (); |
… | |
… | |
1094 | ev_rt_now = ev_time (); |
1104 | ev_rt_now = ev_time (); |
1095 | mn_now = get_clock (); |
1105 | mn_now = get_clock (); |
1096 | now_floor = mn_now; |
1106 | now_floor = mn_now; |
1097 | } |
1107 | } |
1098 | |
1108 | |
|
|
1109 | # if EV_PERIODICS |
1099 | periodics_reschedule (EV_A); |
1110 | periodics_reschedule (EV_A); |
|
|
1111 | # endif |
1100 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
1112 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
1101 | /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ |
1113 | /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ |
1102 | } |
1114 | } |
1103 | } |
1115 | } |
1104 | else |
1116 | else |
… | |
… | |
1106 | { |
1118 | { |
1107 | ev_rt_now = ev_time (); |
1119 | ev_rt_now = ev_time (); |
1108 | |
1120 | |
1109 | if (expect_false (mn_now > ev_rt_now || mn_now < ev_rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
1121 | if (expect_false (mn_now > ev_rt_now || mn_now < ev_rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
1110 | { |
1122 | { |
|
|
1123 | #if EV_PERIODICS |
1111 | periodics_reschedule (EV_A); |
1124 | periodics_reschedule (EV_A); |
|
|
1125 | #endif |
1112 | |
1126 | |
1113 | /* 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 */ |
1114 | for (i = 0; i < timercnt; ++i) |
1128 | for (i = 0; i < timercnt; ++i) |
1115 | ((WT)timers [i])->at += ev_rt_now - mn_now; |
1129 | ((WT)timers [i])->at += ev_rt_now - mn_now; |
1116 | } |
1130 | } |
… | |
… | |
1179 | { |
1193 | { |
1180 | ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge; |
1194 | ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge; |
1181 | if (block > to) block = to; |
1195 | if (block > to) block = to; |
1182 | } |
1196 | } |
1183 | |
1197 | |
|
|
1198 | #if EV_PERIODICS |
1184 | if (periodiccnt) |
1199 | if (periodiccnt) |
1185 | { |
1200 | { |
1186 | ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + method_fudge; |
1201 | ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + method_fudge; |
1187 | if (block > to) block = to; |
1202 | if (block > to) block = to; |
1188 | } |
1203 | } |
|
|
1204 | #endif |
1189 | |
1205 | |
1190 | if (block < 0.) block = 0.; |
1206 | if (block < 0.) block = 0.; |
1191 | } |
1207 | } |
1192 | |
1208 | |
1193 | method_poll (EV_A_ block); |
1209 | method_poll (EV_A_ block); |
… | |
… | |
1195 | /* update ev_rt_now, do magic */ |
1211 | /* update ev_rt_now, do magic */ |
1196 | time_update (EV_A); |
1212 | time_update (EV_A); |
1197 | |
1213 | |
1198 | /* queue pending timers and reschedule them */ |
1214 | /* queue pending timers and reschedule them */ |
1199 | timers_reify (EV_A); /* relative timers called last */ |
1215 | timers_reify (EV_A); /* relative timers called last */ |
|
|
1216 | #if EV_PERIODICS |
1200 | periodics_reify (EV_A); /* absolute timers called first */ |
1217 | periodics_reify (EV_A); /* absolute timers called first */ |
|
|
1218 | #endif |
1201 | |
1219 | |
1202 | /* queue idle watchers unless io or timers are pending */ |
1220 | /* queue idle watchers unless io or timers are pending */ |
1203 | if (idlecnt && !any_pending (EV_A)) |
1221 | if (idlecnt && !any_pending (EV_A)) |
1204 | queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); |
1222 | queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); |
1205 | |
1223 | |
… | |
… | |
1356 | } |
1374 | } |
1357 | else if (w->repeat) |
1375 | else if (w->repeat) |
1358 | ev_timer_start (EV_A_ w); |
1376 | ev_timer_start (EV_A_ w); |
1359 | } |
1377 | } |
1360 | |
1378 | |
|
|
1379 | #if EV_PERIODICS |
1361 | void |
1380 | void |
1362 | ev_periodic_start (EV_P_ struct ev_periodic *w) |
1381 | ev_periodic_start (EV_P_ struct ev_periodic *w) |
1363 | { |
1382 | { |
1364 | if (ev_is_active (w)) |
1383 | if (ev_is_active (w)) |
1365 | return; |
1384 | return; |
… | |
… | |
1404 | { |
1423 | { |
1405 | /* TODO: use adjustheap and recalculation */ |
1424 | /* TODO: use adjustheap and recalculation */ |
1406 | ev_periodic_stop (EV_A_ w); |
1425 | ev_periodic_stop (EV_A_ w); |
1407 | ev_periodic_start (EV_A_ w); |
1426 | ev_periodic_start (EV_A_ w); |
1408 | } |
1427 | } |
|
|
1428 | #endif |
1409 | |
1429 | |
1410 | void |
1430 | void |
1411 | ev_idle_start (EV_P_ struct ev_idle *w) |
1431 | ev_idle_start (EV_P_ struct ev_idle *w) |
1412 | { |
1432 | { |
1413 | if (ev_is_active (w)) |
1433 | if (ev_is_active (w)) |
… | |
… | |
1464 | |
1484 | |
1465 | void |
1485 | void |
1466 | ev_check_stop (EV_P_ struct ev_check *w) |
1486 | ev_check_stop (EV_P_ struct ev_check *w) |
1467 | { |
1487 | { |
1468 | ev_clear_pending (EV_A_ (W)w); |
1488 | ev_clear_pending (EV_A_ (W)w); |
1469 | if (ev_is_active (w)) |
1489 | if (!ev_is_active (w)) |
1470 | return; |
1490 | return; |
1471 | |
1491 | |
1472 | checks [((W)w)->active - 1] = checks [--checkcnt]; |
1492 | checks [((W)w)->active - 1] = checks [--checkcnt]; |
1473 | ev_stop (EV_A_ (W)w); |
1493 | ev_stop (EV_A_ (W)w); |
1474 | } |
1494 | } |
… | |
… | |
1535 | |
1555 | |
1536 | void |
1556 | void |
1537 | ev_child_stop (EV_P_ struct ev_child *w) |
1557 | ev_child_stop (EV_P_ struct ev_child *w) |
1538 | { |
1558 | { |
1539 | ev_clear_pending (EV_A_ (W)w); |
1559 | ev_clear_pending (EV_A_ (W)w); |
1540 | if (ev_is_active (w)) |
1560 | if (!ev_is_active (w)) |
1541 | return; |
1561 | return; |
1542 | |
1562 | |
1543 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1563 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1544 | ev_stop (EV_A_ (W)w); |
1564 | ev_stop (EV_A_ (W)w); |
1545 | } |
1565 | } |