… | |
… | |
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 | |
… | |
… | |
227 | #if EV_MULTIPLICITY |
233 | #if EV_MULTIPLICITY |
228 | |
234 | |
229 | struct ev_loop |
235 | struct ev_loop |
230 | { |
236 | { |
231 | ev_tstamp ev_rt_now; |
237 | ev_tstamp ev_rt_now; |
|
|
238 | #define ev_rt_now ((loop)->ev_rt_now) |
232 | #define VAR(name,decl) decl; |
239 | #define VAR(name,decl) decl; |
233 | #include "ev_vars.h" |
240 | #include "ev_vars.h" |
234 | #undef VAR |
241 | #undef VAR |
235 | }; |
242 | }; |
236 | #include "ev_wrap.h" |
243 | #include "ev_wrap.h" |
… | |
… | |
249 | |
256 | |
250 | #endif |
257 | #endif |
251 | |
258 | |
252 | /*****************************************************************************/ |
259 | /*****************************************************************************/ |
253 | |
260 | |
254 | inline ev_tstamp |
261 | ev_tstamp |
255 | ev_time (void) |
262 | ev_time (void) |
256 | { |
263 | { |
257 | #if EV_USE_REALTIME |
264 | #if EV_USE_REALTIME |
258 | struct timespec ts; |
265 | struct timespec ts; |
259 | clock_gettime (CLOCK_REALTIME, &ts); |
266 | clock_gettime (CLOCK_REALTIME, &ts); |
… | |
… | |
528 | heap [k] = w; |
535 | heap [k] = w; |
529 | ((W)heap [k])->active = k + 1; |
536 | ((W)heap [k])->active = k + 1; |
530 | } |
537 | } |
531 | |
538 | |
532 | inline void |
539 | inline void |
533 | adjustheap (WT *heap, int N, int k, ev_tstamp at) |
540 | adjustheap (WT *heap, int N, int k) |
534 | { |
541 | { |
535 | ev_tstamp old_at = heap [k]->at; |
542 | upheap (heap, k); |
536 | heap [k]->at = at; |
|
|
537 | |
|
|
538 | if (old_at < at) |
|
|
539 | downheap (heap, N, k); |
543 | downheap (heap, N, k); |
540 | else |
|
|
541 | upheap (heap, k); |
|
|
542 | } |
544 | } |
543 | |
545 | |
544 | /*****************************************************************************/ |
546 | /*****************************************************************************/ |
545 | |
547 | |
546 | typedef struct |
548 | typedef struct |
… | |
… | |
804 | array_free (pending, [i]); |
806 | array_free (pending, [i]); |
805 | |
807 | |
806 | /* have to use the microsoft-never-gets-it-right macro */ |
808 | /* have to use the microsoft-never-gets-it-right macro */ |
807 | array_free_microshit (fdchange); |
809 | array_free_microshit (fdchange); |
808 | array_free_microshit (timer); |
810 | array_free_microshit (timer); |
|
|
811 | #if EV_PERIODICS |
809 | array_free_microshit (periodic); |
812 | array_free_microshit (periodic); |
|
|
813 | #endif |
810 | array_free_microshit (idle); |
814 | array_free_microshit (idle); |
811 | array_free_microshit (prepare); |
815 | array_free_microshit (prepare); |
812 | array_free_microshit (check); |
816 | array_free_microshit (check); |
813 | |
817 | |
814 | method = 0; |
818 | method = 0; |
… | |
… | |
987 | |
991 | |
988 | /* first reschedule or stop timer */ |
992 | /* first reschedule or stop timer */ |
989 | if (w->repeat) |
993 | if (w->repeat) |
990 | { |
994 | { |
991 | 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 | |
992 | ((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 | |
993 | downheap ((WT *)timers, timercnt, 0); |
1001 | downheap ((WT *)timers, timercnt, 0); |
994 | } |
1002 | } |
995 | else |
1003 | else |
996 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
1004 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
997 | |
1005 | |
998 | ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); |
1006 | ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); |
999 | } |
1007 | } |
1000 | } |
1008 | } |
1001 | |
1009 | |
|
|
1010 | #if EV_PERIODICS |
1002 | static void |
1011 | static void |
1003 | periodics_reify (EV_P) |
1012 | periodics_reify (EV_P) |
1004 | { |
1013 | { |
1005 | while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) |
1014 | while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) |
1006 | { |
1015 | { |
… | |
… | |
1047 | |
1056 | |
1048 | /* now rebuild the heap */ |
1057 | /* now rebuild the heap */ |
1049 | for (i = periodiccnt >> 1; i--; ) |
1058 | for (i = periodiccnt >> 1; i--; ) |
1050 | downheap ((WT *)periodics, periodiccnt, i); |
1059 | downheap ((WT *)periodics, periodiccnt, i); |
1051 | } |
1060 | } |
|
|
1061 | #endif |
1052 | |
1062 | |
1053 | inline int |
1063 | inline int |
1054 | time_update_monotonic (EV_P) |
1064 | time_update_monotonic (EV_P) |
1055 | { |
1065 | { |
1056 | mn_now = get_clock (); |
1066 | mn_now = get_clock (); |
… | |
… | |
1090 | ev_rt_now = ev_time (); |
1100 | ev_rt_now = ev_time (); |
1091 | mn_now = get_clock (); |
1101 | mn_now = get_clock (); |
1092 | now_floor = mn_now; |
1102 | now_floor = mn_now; |
1093 | } |
1103 | } |
1094 | |
1104 | |
|
|
1105 | # if EV_PERIODICS |
1095 | periodics_reschedule (EV_A); |
1106 | periodics_reschedule (EV_A); |
|
|
1107 | # endif |
1096 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
1108 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
1097 | /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ |
1109 | /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ |
1098 | } |
1110 | } |
1099 | } |
1111 | } |
1100 | else |
1112 | else |
… | |
… | |
1102 | { |
1114 | { |
1103 | ev_rt_now = ev_time (); |
1115 | ev_rt_now = ev_time (); |
1104 | |
1116 | |
1105 | if (expect_false (mn_now > ev_rt_now || mn_now < ev_rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
1117 | if (expect_false (mn_now > ev_rt_now || mn_now < ev_rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
1106 | { |
1118 | { |
|
|
1119 | #if EV_PERIODICS |
1107 | periodics_reschedule (EV_A); |
1120 | periodics_reschedule (EV_A); |
|
|
1121 | #endif |
1108 | |
1122 | |
1109 | /* 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 */ |
1110 | for (i = 0; i < timercnt; ++i) |
1124 | for (i = 0; i < timercnt; ++i) |
1111 | ((WT)timers [i])->at += ev_rt_now - mn_now; |
1125 | ((WT)timers [i])->at += ev_rt_now - mn_now; |
1112 | } |
1126 | } |
… | |
… | |
1175 | { |
1189 | { |
1176 | ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge; |
1190 | ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge; |
1177 | if (block > to) block = to; |
1191 | if (block > to) block = to; |
1178 | } |
1192 | } |
1179 | |
1193 | |
|
|
1194 | #if EV_PERIODICS |
1180 | if (periodiccnt) |
1195 | if (periodiccnt) |
1181 | { |
1196 | { |
1182 | ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + method_fudge; |
1197 | ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + method_fudge; |
1183 | if (block > to) block = to; |
1198 | if (block > to) block = to; |
1184 | } |
1199 | } |
|
|
1200 | #endif |
1185 | |
1201 | |
1186 | if (block < 0.) block = 0.; |
1202 | if (block < 0.) block = 0.; |
1187 | } |
1203 | } |
1188 | |
1204 | |
1189 | method_poll (EV_A_ block); |
1205 | method_poll (EV_A_ block); |
… | |
… | |
1191 | /* update ev_rt_now, do magic */ |
1207 | /* update ev_rt_now, do magic */ |
1192 | time_update (EV_A); |
1208 | time_update (EV_A); |
1193 | |
1209 | |
1194 | /* queue pending timers and reschedule them */ |
1210 | /* queue pending timers and reschedule them */ |
1195 | timers_reify (EV_A); /* relative timers called last */ |
1211 | timers_reify (EV_A); /* relative timers called last */ |
|
|
1212 | #if EV_PERIODICS |
1196 | periodics_reify (EV_A); /* absolute timers called first */ |
1213 | periodics_reify (EV_A); /* absolute timers called first */ |
|
|
1214 | #endif |
1197 | |
1215 | |
1198 | /* queue idle watchers unless io or timers are pending */ |
1216 | /* queue idle watchers unless io or timers are pending */ |
1199 | if (idlecnt && !any_pending (EV_A)) |
1217 | if (idlecnt && !any_pending (EV_A)) |
1200 | queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); |
1218 | queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); |
1201 | |
1219 | |
… | |
… | |
1292 | { |
1310 | { |
1293 | ev_clear_pending (EV_A_ (W)w); |
1311 | ev_clear_pending (EV_A_ (W)w); |
1294 | if (!ev_is_active (w)) |
1312 | if (!ev_is_active (w)) |
1295 | return; |
1313 | return; |
1296 | |
1314 | |
|
|
1315 | assert (("ev_io_start called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax)); |
|
|
1316 | |
1297 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
1317 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
1298 | ev_stop (EV_A_ (W)w); |
1318 | ev_stop (EV_A_ (W)w); |
1299 | |
1319 | |
1300 | fd_change (EV_A_ w->fd); |
1320 | fd_change (EV_A_ w->fd); |
1301 | } |
1321 | } |
… | |
… | |
1328 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1348 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1329 | |
1349 | |
1330 | if (((W)w)->active < timercnt--) |
1350 | if (((W)w)->active < timercnt--) |
1331 | { |
1351 | { |
1332 | timers [((W)w)->active - 1] = timers [timercnt]; |
1352 | timers [((W)w)->active - 1] = timers [timercnt]; |
1333 | downheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1353 | adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1334 | } |
1354 | } |
1335 | |
1355 | |
1336 | ((WT)w)->at = w->repeat; |
1356 | ((WT)w)->at -= mn_now; |
1337 | |
1357 | |
1338 | ev_stop (EV_A_ (W)w); |
1358 | ev_stop (EV_A_ (W)w); |
1339 | } |
1359 | } |
1340 | |
1360 | |
1341 | void |
1361 | void |
1342 | ev_timer_again (EV_P_ struct ev_timer *w) |
1362 | ev_timer_again (EV_P_ struct ev_timer *w) |
1343 | { |
1363 | { |
1344 | if (ev_is_active (w)) |
1364 | if (ev_is_active (w)) |
1345 | { |
1365 | { |
1346 | if (w->repeat) |
1366 | if (w->repeat) |
|
|
1367 | { |
|
|
1368 | ((WT)w)->at = mn_now + w->repeat; |
1347 | adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1, mn_now + w->repeat); |
1369 | adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
|
|
1370 | } |
1348 | else |
1371 | else |
1349 | ev_timer_stop (EV_A_ w); |
1372 | ev_timer_stop (EV_A_ w); |
1350 | } |
1373 | } |
1351 | else if (w->repeat) |
1374 | else if (w->repeat) |
1352 | ev_timer_start (EV_A_ w); |
1375 | ev_timer_start (EV_A_ w); |
1353 | } |
1376 | } |
1354 | |
1377 | |
|
|
1378 | #if EV_PERIODICS |
1355 | void |
1379 | void |
1356 | ev_periodic_start (EV_P_ struct ev_periodic *w) |
1380 | ev_periodic_start (EV_P_ struct ev_periodic *w) |
1357 | { |
1381 | { |
1358 | if (ev_is_active (w)) |
1382 | if (ev_is_active (w)) |
1359 | return; |
1383 | return; |
… | |
… | |
1385 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1409 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1386 | |
1410 | |
1387 | if (((W)w)->active < periodiccnt--) |
1411 | if (((W)w)->active < periodiccnt--) |
1388 | { |
1412 | { |
1389 | periodics [((W)w)->active - 1] = periodics [periodiccnt]; |
1413 | periodics [((W)w)->active - 1] = periodics [periodiccnt]; |
1390 | downheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1); |
1414 | adjustheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1); |
1391 | } |
1415 | } |
1392 | |
1416 | |
1393 | ev_stop (EV_A_ (W)w); |
1417 | ev_stop (EV_A_ (W)w); |
1394 | } |
1418 | } |
1395 | |
1419 | |
… | |
… | |
1398 | { |
1422 | { |
1399 | /* TODO: use adjustheap and recalculation */ |
1423 | /* TODO: use adjustheap and recalculation */ |
1400 | ev_periodic_stop (EV_A_ w); |
1424 | ev_periodic_stop (EV_A_ w); |
1401 | ev_periodic_start (EV_A_ w); |
1425 | ev_periodic_start (EV_A_ w); |
1402 | } |
1426 | } |
|
|
1427 | #endif |
1403 | |
1428 | |
1404 | void |
1429 | void |
1405 | ev_idle_start (EV_P_ struct ev_idle *w) |
1430 | ev_idle_start (EV_P_ struct ev_idle *w) |
1406 | { |
1431 | { |
1407 | if (ev_is_active (w)) |
1432 | if (ev_is_active (w)) |
… | |
… | |
1458 | |
1483 | |
1459 | void |
1484 | void |
1460 | ev_check_stop (EV_P_ struct ev_check *w) |
1485 | ev_check_stop (EV_P_ struct ev_check *w) |
1461 | { |
1486 | { |
1462 | ev_clear_pending (EV_A_ (W)w); |
1487 | ev_clear_pending (EV_A_ (W)w); |
1463 | if (ev_is_active (w)) |
1488 | if (!ev_is_active (w)) |
1464 | return; |
1489 | return; |
1465 | |
1490 | |
1466 | checks [((W)w)->active - 1] = checks [--checkcnt]; |
1491 | checks [((W)w)->active - 1] = checks [--checkcnt]; |
1467 | ev_stop (EV_A_ (W)w); |
1492 | ev_stop (EV_A_ (W)w); |
1468 | } |
1493 | } |
… | |
… | |
1529 | |
1554 | |
1530 | void |
1555 | void |
1531 | ev_child_stop (EV_P_ struct ev_child *w) |
1556 | ev_child_stop (EV_P_ struct ev_child *w) |
1532 | { |
1557 | { |
1533 | ev_clear_pending (EV_A_ (W)w); |
1558 | ev_clear_pending (EV_A_ (W)w); |
1534 | if (ev_is_active (w)) |
1559 | if (!ev_is_active (w)) |
1535 | return; |
1560 | return; |
1536 | |
1561 | |
1537 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1562 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1538 | ev_stop (EV_A_ (W)w); |
1563 | ev_stop (EV_A_ (W)w); |
1539 | } |
1564 | } |