… | |
… | |
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 | |
… | |
… | |
249 | |
253 | |
250 | #endif |
254 | #endif |
251 | |
255 | |
252 | /*****************************************************************************/ |
256 | /*****************************************************************************/ |
253 | |
257 | |
254 | inline ev_tstamp |
258 | ev_tstamp |
255 | ev_time (void) |
259 | ev_time (void) |
256 | { |
260 | { |
257 | #if EV_USE_REALTIME |
261 | #if EV_USE_REALTIME |
258 | struct timespec ts; |
262 | struct timespec ts; |
259 | clock_gettime (CLOCK_REALTIME, &ts); |
263 | clock_gettime (CLOCK_REALTIME, &ts); |
… | |
… | |
804 | array_free (pending, [i]); |
808 | array_free (pending, [i]); |
805 | |
809 | |
806 | /* have to use the microsoft-never-gets-it-right macro */ |
810 | /* have to use the microsoft-never-gets-it-right macro */ |
807 | array_free_microshit (fdchange); |
811 | array_free_microshit (fdchange); |
808 | array_free_microshit (timer); |
812 | array_free_microshit (timer); |
|
|
813 | #if EV_PERIODICS |
809 | array_free_microshit (periodic); |
814 | array_free_microshit (periodic); |
|
|
815 | #endif |
810 | array_free_microshit (idle); |
816 | array_free_microshit (idle); |
811 | array_free_microshit (prepare); |
817 | array_free_microshit (prepare); |
812 | array_free_microshit (check); |
818 | array_free_microshit (check); |
813 | |
819 | |
814 | method = 0; |
820 | method = 0; |
… | |
… | |
987 | |
993 | |
988 | /* first reschedule or stop timer */ |
994 | /* first reschedule or stop timer */ |
989 | if (w->repeat) |
995 | if (w->repeat) |
990 | { |
996 | { |
991 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
997 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
|
|
998 | |
992 | ((WT)w)->at = mn_now + w->repeat; |
999 | ((WT)w)->at += w->repeat; |
|
|
1000 | if (((WT)w)->at < mn_now) |
|
|
1001 | ((WT)w)->at = mn_now; |
|
|
1002 | |
993 | downheap ((WT *)timers, timercnt, 0); |
1003 | downheap ((WT *)timers, timercnt, 0); |
994 | } |
1004 | } |
995 | else |
1005 | else |
996 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
1006 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
997 | |
1007 | |
998 | ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); |
1008 | ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); |
999 | } |
1009 | } |
1000 | } |
1010 | } |
1001 | |
1011 | |
|
|
1012 | #if EV_PERIODICS |
1002 | static void |
1013 | static void |
1003 | periodics_reify (EV_P) |
1014 | periodics_reify (EV_P) |
1004 | { |
1015 | { |
1005 | while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) |
1016 | while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) |
1006 | { |
1017 | { |
… | |
… | |
1047 | |
1058 | |
1048 | /* now rebuild the heap */ |
1059 | /* now rebuild the heap */ |
1049 | for (i = periodiccnt >> 1; i--; ) |
1060 | for (i = periodiccnt >> 1; i--; ) |
1050 | downheap ((WT *)periodics, periodiccnt, i); |
1061 | downheap ((WT *)periodics, periodiccnt, i); |
1051 | } |
1062 | } |
|
|
1063 | #endif |
1052 | |
1064 | |
1053 | inline int |
1065 | inline int |
1054 | time_update_monotonic (EV_P) |
1066 | time_update_monotonic (EV_P) |
1055 | { |
1067 | { |
1056 | mn_now = get_clock (); |
1068 | mn_now = get_clock (); |
… | |
… | |
1090 | ev_rt_now = ev_time (); |
1102 | ev_rt_now = ev_time (); |
1091 | mn_now = get_clock (); |
1103 | mn_now = get_clock (); |
1092 | now_floor = mn_now; |
1104 | now_floor = mn_now; |
1093 | } |
1105 | } |
1094 | |
1106 | |
|
|
1107 | # if EV_PERIODICS |
1095 | periodics_reschedule (EV_A); |
1108 | periodics_reschedule (EV_A); |
|
|
1109 | # endif |
1096 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
1110 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
1097 | /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ |
1111 | /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ |
1098 | } |
1112 | } |
1099 | } |
1113 | } |
1100 | else |
1114 | else |
… | |
… | |
1102 | { |
1116 | { |
1103 | ev_rt_now = ev_time (); |
1117 | ev_rt_now = ev_time (); |
1104 | |
1118 | |
1105 | if (expect_false (mn_now > ev_rt_now || mn_now < ev_rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
1119 | if (expect_false (mn_now > ev_rt_now || mn_now < ev_rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
1106 | { |
1120 | { |
|
|
1121 | #if EV_PERIODICS |
1107 | periodics_reschedule (EV_A); |
1122 | periodics_reschedule (EV_A); |
|
|
1123 | #endif |
1108 | |
1124 | |
1109 | /* adjust timers. this is easy, as the offset is the same for all */ |
1125 | /* adjust timers. this is easy, as the offset is the same for all */ |
1110 | for (i = 0; i < timercnt; ++i) |
1126 | for (i = 0; i < timercnt; ++i) |
1111 | ((WT)timers [i])->at += ev_rt_now - mn_now; |
1127 | ((WT)timers [i])->at += ev_rt_now - mn_now; |
1112 | } |
1128 | } |
… | |
… | |
1175 | { |
1191 | { |
1176 | ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge; |
1192 | ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge; |
1177 | if (block > to) block = to; |
1193 | if (block > to) block = to; |
1178 | } |
1194 | } |
1179 | |
1195 | |
|
|
1196 | #if EV_PERIODICS |
1180 | if (periodiccnt) |
1197 | if (periodiccnt) |
1181 | { |
1198 | { |
1182 | ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + method_fudge; |
1199 | ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + method_fudge; |
1183 | if (block > to) block = to; |
1200 | if (block > to) block = to; |
1184 | } |
1201 | } |
|
|
1202 | #endif |
1185 | |
1203 | |
1186 | if (block < 0.) block = 0.; |
1204 | if (block < 0.) block = 0.; |
1187 | } |
1205 | } |
1188 | |
1206 | |
1189 | method_poll (EV_A_ block); |
1207 | method_poll (EV_A_ block); |
… | |
… | |
1191 | /* update ev_rt_now, do magic */ |
1209 | /* update ev_rt_now, do magic */ |
1192 | time_update (EV_A); |
1210 | time_update (EV_A); |
1193 | |
1211 | |
1194 | /* queue pending timers and reschedule them */ |
1212 | /* queue pending timers and reschedule them */ |
1195 | timers_reify (EV_A); /* relative timers called last */ |
1213 | timers_reify (EV_A); /* relative timers called last */ |
|
|
1214 | #if EV_PERIODICS |
1196 | periodics_reify (EV_A); /* absolute timers called first */ |
1215 | periodics_reify (EV_A); /* absolute timers called first */ |
|
|
1216 | #endif |
1197 | |
1217 | |
1198 | /* queue idle watchers unless io or timers are pending */ |
1218 | /* queue idle watchers unless io or timers are pending */ |
1199 | if (idlecnt && !any_pending (EV_A)) |
1219 | if (idlecnt && !any_pending (EV_A)) |
1200 | queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); |
1220 | queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); |
1201 | |
1221 | |
… | |
… | |
1292 | { |
1312 | { |
1293 | ev_clear_pending (EV_A_ (W)w); |
1313 | ev_clear_pending (EV_A_ (W)w); |
1294 | if (!ev_is_active (w)) |
1314 | if (!ev_is_active (w)) |
1295 | return; |
1315 | return; |
1296 | |
1316 | |
|
|
1317 | assert (("ev_io_start called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax)); |
|
|
1318 | |
1297 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
1319 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
1298 | ev_stop (EV_A_ (W)w); |
1320 | ev_stop (EV_A_ (W)w); |
1299 | |
1321 | |
1300 | fd_change (EV_A_ w->fd); |
1322 | fd_change (EV_A_ w->fd); |
1301 | } |
1323 | } |
… | |
… | |
1331 | { |
1353 | { |
1332 | timers [((W)w)->active - 1] = timers [timercnt]; |
1354 | timers [((W)w)->active - 1] = timers [timercnt]; |
1333 | downheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1355 | downheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1334 | } |
1356 | } |
1335 | |
1357 | |
1336 | ((WT)w)->at = w->repeat; |
1358 | ((WT)w)->at -= mn_now; |
1337 | |
1359 | |
1338 | ev_stop (EV_A_ (W)w); |
1360 | ev_stop (EV_A_ (W)w); |
1339 | } |
1361 | } |
1340 | |
1362 | |
1341 | void |
1363 | void |
… | |
… | |
1350 | } |
1372 | } |
1351 | else if (w->repeat) |
1373 | else if (w->repeat) |
1352 | ev_timer_start (EV_A_ w); |
1374 | ev_timer_start (EV_A_ w); |
1353 | } |
1375 | } |
1354 | |
1376 | |
|
|
1377 | #if EV_PERIODICS |
1355 | void |
1378 | void |
1356 | ev_periodic_start (EV_P_ struct ev_periodic *w) |
1379 | ev_periodic_start (EV_P_ struct ev_periodic *w) |
1357 | { |
1380 | { |
1358 | if (ev_is_active (w)) |
1381 | if (ev_is_active (w)) |
1359 | return; |
1382 | return; |
… | |
… | |
1398 | { |
1421 | { |
1399 | /* TODO: use adjustheap and recalculation */ |
1422 | /* TODO: use adjustheap and recalculation */ |
1400 | ev_periodic_stop (EV_A_ w); |
1423 | ev_periodic_stop (EV_A_ w); |
1401 | ev_periodic_start (EV_A_ w); |
1424 | ev_periodic_start (EV_A_ w); |
1402 | } |
1425 | } |
|
|
1426 | #endif |
1403 | |
1427 | |
1404 | void |
1428 | void |
1405 | ev_idle_start (EV_P_ struct ev_idle *w) |
1429 | ev_idle_start (EV_P_ struct ev_idle *w) |
1406 | { |
1430 | { |
1407 | if (ev_is_active (w)) |
1431 | if (ev_is_active (w)) |
… | |
… | |
1458 | |
1482 | |
1459 | void |
1483 | void |
1460 | ev_check_stop (EV_P_ struct ev_check *w) |
1484 | ev_check_stop (EV_P_ struct ev_check *w) |
1461 | { |
1485 | { |
1462 | ev_clear_pending (EV_A_ (W)w); |
1486 | ev_clear_pending (EV_A_ (W)w); |
1463 | if (ev_is_active (w)) |
1487 | if (!ev_is_active (w)) |
1464 | return; |
1488 | return; |
1465 | |
1489 | |
1466 | checks [((W)w)->active - 1] = checks [--checkcnt]; |
1490 | checks [((W)w)->active - 1] = checks [--checkcnt]; |
1467 | ev_stop (EV_A_ (W)w); |
1491 | ev_stop (EV_A_ (W)w); |
1468 | } |
1492 | } |
… | |
… | |
1529 | |
1553 | |
1530 | void |
1554 | void |
1531 | ev_child_stop (EV_P_ struct ev_child *w) |
1555 | ev_child_stop (EV_P_ struct ev_child *w) |
1532 | { |
1556 | { |
1533 | ev_clear_pending (EV_A_ (W)w); |
1557 | ev_clear_pending (EV_A_ (W)w); |
1534 | if (ev_is_active (w)) |
1558 | if (!ev_is_active (w)) |
1535 | return; |
1559 | return; |
1536 | |
1560 | |
1537 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1561 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1538 | ev_stop (EV_A_ (W)w); |
1562 | ev_stop (EV_A_ (W)w); |
1539 | } |
1563 | } |