… | |
… | |
216 | # include <sys/inotify.h> |
216 | # include <sys/inotify.h> |
217 | #endif |
217 | #endif |
218 | |
218 | |
219 | /**/ |
219 | /**/ |
220 | |
220 | |
|
|
221 | /* |
|
|
222 | * This is used to avoid floating point rounding problems. |
|
|
223 | * It is added to ev_rt_now when scheduling periodics |
|
|
224 | * to ensure progress, time-wise, even when rounding |
|
|
225 | * errors are against us. |
|
|
226 | * This value is good at least till the year 4000 |
|
|
227 | * and intervals up to 20 years. |
|
|
228 | * Better solutions welcome. |
|
|
229 | */ |
|
|
230 | #define TIME_EPSILON 0.0001220703125 /* 1/8192 */ |
|
|
231 | |
221 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
232 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
222 | #define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */ |
233 | #define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */ |
223 | /*#define CLEANUP_INTERVAL (MAX_BLOCKTIME * 5.) /* how often to try to free memory and re-check fds */ |
234 | /*#define CLEANUP_INTERVAL (MAX_BLOCKTIME * 5.) /* how often to try to free memory and re-check fds, TODO */ |
224 | |
235 | |
225 | #if __GNUC__ >= 3 |
236 | #if __GNUC__ >= 3 |
226 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
237 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
227 | # define inline_size static inline /* inline for codesize */ |
|
|
228 | # if EV_MINIMAL |
|
|
229 | # define noinline __attribute__ ((noinline)) |
238 | # define noinline __attribute__ ((noinline)) |
230 | # define inline_speed static noinline |
|
|
231 | # else |
|
|
232 | # define noinline |
|
|
233 | # define inline_speed static inline |
|
|
234 | # endif |
|
|
235 | #else |
239 | #else |
236 | # define expect(expr,value) (expr) |
240 | # define expect(expr,value) (expr) |
237 | # define inline_speed static |
|
|
238 | # define inline_size static |
|
|
239 | # define noinline |
241 | # define noinline |
|
|
242 | # if __STDC_VERSION__ < 199901L |
|
|
243 | # define inline |
|
|
244 | # endif |
240 | #endif |
245 | #endif |
241 | |
246 | |
242 | #define expect_false(expr) expect ((expr) != 0, 0) |
247 | #define expect_false(expr) expect ((expr) != 0, 0) |
243 | #define expect_true(expr) expect ((expr) != 0, 1) |
248 | #define expect_true(expr) expect ((expr) != 0, 1) |
|
|
249 | #define inline_size static inline |
|
|
250 | |
|
|
251 | #if EV_MINIMAL |
|
|
252 | # define inline_speed static noinline |
|
|
253 | #else |
|
|
254 | # define inline_speed static inline |
|
|
255 | #endif |
244 | |
256 | |
245 | #define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) |
257 | #define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) |
246 | #define ABSPRI(w) ((w)->priority - EV_MINPRI) |
258 | #define ABSPRI(w) (((W)w)->priority - EV_MINPRI) |
247 | |
259 | |
248 | #define EMPTY0 /* required for microsofts broken pseudo-c compiler */ |
260 | #define EMPTY /* required for microsofts broken pseudo-c compiler */ |
249 | #define EMPTY2(a,b) /* used to suppress some warnings */ |
261 | #define EMPTY2(a,b) /* used to suppress some warnings */ |
250 | |
262 | |
251 | typedef ev_watcher *W; |
263 | typedef ev_watcher *W; |
252 | typedef ev_watcher_list *WL; |
264 | typedef ev_watcher_list *WL; |
253 | typedef ev_watcher_time *WT; |
265 | typedef ev_watcher_time *WT; |
… | |
… | |
396 | { |
408 | { |
397 | return ev_rt_now; |
409 | return ev_rt_now; |
398 | } |
410 | } |
399 | #endif |
411 | #endif |
400 | |
412 | |
401 | #define array_roundsize(type,n) (((n) | 4) & ~3) |
413 | int inline_size |
|
|
414 | array_nextsize (int elem, int cur, int cnt) |
|
|
415 | { |
|
|
416 | int ncur = cur + 1; |
|
|
417 | |
|
|
418 | do |
|
|
419 | ncur <<= 1; |
|
|
420 | while (cnt > ncur); |
|
|
421 | |
|
|
422 | /* if size > 4096, round to 4096 - 4 * longs to accomodate malloc overhead */ |
|
|
423 | if (elem * ncur > 4096) |
|
|
424 | { |
|
|
425 | ncur *= elem; |
|
|
426 | ncur = (ncur + elem + 4095 + sizeof (void *) * 4) & ~4095; |
|
|
427 | ncur = ncur - sizeof (void *) * 4; |
|
|
428 | ncur /= elem; |
|
|
429 | } |
|
|
430 | |
|
|
431 | return ncur; |
|
|
432 | } |
|
|
433 | |
|
|
434 | static noinline void * |
|
|
435 | array_realloc (int elem, void *base, int *cur, int cnt) |
|
|
436 | { |
|
|
437 | *cur = array_nextsize (elem, *cur, cnt); |
|
|
438 | return ev_realloc (base, elem * *cur); |
|
|
439 | } |
402 | |
440 | |
403 | #define array_needsize(type,base,cur,cnt,init) \ |
441 | #define array_needsize(type,base,cur,cnt,init) \ |
404 | if (expect_false ((cnt) > cur)) \ |
442 | if (expect_false ((cnt) > (cur))) \ |
405 | { \ |
443 | { \ |
406 | int newcnt = cur; \ |
444 | int ocur_ = (cur); \ |
407 | do \ |
445 | (base) = (type *)array_realloc \ |
408 | { \ |
446 | (sizeof (type), (base), &(cur), (cnt)); \ |
409 | newcnt = array_roundsize (type, newcnt << 1); \ |
447 | init ((base) + (ocur_), (cur) - ocur_); \ |
410 | } \ |
|
|
411 | while ((cnt) > newcnt); \ |
|
|
412 | \ |
|
|
413 | base = (type *)ev_realloc (base, sizeof (type) * (newcnt));\ |
|
|
414 | init (base + cur, newcnt - cur); \ |
|
|
415 | cur = newcnt; \ |
|
|
416 | } |
448 | } |
417 | |
449 | |
|
|
450 | #if 0 |
418 | #define array_slim(type,stem) \ |
451 | #define array_slim(type,stem) \ |
419 | if (stem ## max < array_roundsize (stem ## cnt >> 2)) \ |
452 | if (stem ## max < array_roundsize (stem ## cnt >> 2)) \ |
420 | { \ |
453 | { \ |
421 | stem ## max = array_roundsize (stem ## cnt >> 1); \ |
454 | stem ## max = array_roundsize (stem ## cnt >> 1); \ |
422 | base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\ |
455 | base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\ |
423 | fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ |
456 | fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ |
424 | } |
457 | } |
|
|
458 | #endif |
425 | |
459 | |
426 | #define array_free(stem, idx) \ |
460 | #define array_free(stem, idx) \ |
427 | ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; |
461 | ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; |
428 | |
462 | |
429 | /*****************************************************************************/ |
463 | /*****************************************************************************/ |
430 | |
464 | |
431 | void noinline |
465 | void noinline |
432 | ev_feed_event (EV_P_ void *w, int revents) |
466 | ev_feed_event (EV_P_ void *w, int revents) |
433 | { |
467 | { |
434 | W w_ = (W)w; |
468 | W w_ = (W)w; |
|
|
469 | int pri = ABSPRI (w_); |
435 | |
470 | |
436 | if (expect_false (w_->pending)) |
471 | if (expect_false (w_->pending)) |
|
|
472 | pendings [pri][w_->pending - 1].events |= revents; |
|
|
473 | else |
437 | { |
474 | { |
|
|
475 | w_->pending = ++pendingcnt [pri]; |
|
|
476 | array_needsize (ANPENDING, pendings [pri], pendingmax [pri], w_->pending, EMPTY2); |
|
|
477 | pendings [pri][w_->pending - 1].w = w_; |
438 | pendings [ABSPRI (w_)][w_->pending - 1].events |= revents; |
478 | pendings [pri][w_->pending - 1].events = revents; |
439 | return; |
|
|
440 | } |
479 | } |
441 | |
|
|
442 | w_->pending = ++pendingcnt [ABSPRI (w_)]; |
|
|
443 | array_needsize (ANPENDING, pendings [ABSPRI (w_)], pendingmax [ABSPRI (w_)], pendingcnt [ABSPRI (w_)], EMPTY2); |
|
|
444 | pendings [ABSPRI (w_)][w_->pending - 1].w = w_; |
|
|
445 | pendings [ABSPRI (w_)][w_->pending - 1].events = revents; |
|
|
446 | } |
480 | } |
447 | |
481 | |
448 | void inline_size |
482 | void inline_size |
449 | queue_events (EV_P_ W *events, int eventcnt, int type) |
483 | queue_events (EV_P_ W *events, int eventcnt, int type) |
450 | { |
484 | { |
… | |
… | |
485 | } |
519 | } |
486 | |
520 | |
487 | void |
521 | void |
488 | ev_feed_fd_event (EV_P_ int fd, int revents) |
522 | ev_feed_fd_event (EV_P_ int fd, int revents) |
489 | { |
523 | { |
|
|
524 | if (fd >= 0 && fd < anfdmax) |
490 | fd_event (EV_A_ fd, revents); |
525 | fd_event (EV_A_ fd, revents); |
491 | } |
526 | } |
492 | |
527 | |
493 | void inline_size |
528 | void inline_size |
494 | fd_reify (EV_P) |
529 | fd_reify (EV_P) |
495 | { |
530 | { |
… | |
… | |
725 | for (signum = signalmax; signum--; ) |
760 | for (signum = signalmax; signum--; ) |
726 | if (signals [signum].gotsig) |
761 | if (signals [signum].gotsig) |
727 | ev_feed_signal_event (EV_A_ signum + 1); |
762 | ev_feed_signal_event (EV_A_ signum + 1); |
728 | } |
763 | } |
729 | |
764 | |
730 | void inline_size |
765 | void inline_speed |
731 | fd_intern (int fd) |
766 | fd_intern (int fd) |
732 | { |
767 | { |
733 | #ifdef _WIN32 |
768 | #ifdef _WIN32 |
734 | int arg = 1; |
769 | int arg = 1; |
735 | ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg); |
770 | ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg); |
… | |
… | |
764 | ev_child *w; |
799 | ev_child *w; |
765 | |
800 | |
766 | for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next) |
801 | for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next) |
767 | if (w->pid == pid || !w->pid) |
802 | if (w->pid == pid || !w->pid) |
768 | { |
803 | { |
769 | ev_priority (w) = ev_priority (sw); /* need to do it *now* */ |
804 | ev_set_priority (w, ev_priority (sw)); /* need to do it *now* */ |
770 | w->rpid = pid; |
805 | w->rpid = pid; |
771 | w->rstatus = status; |
806 | w->rstatus = status; |
772 | ev_feed_event (EV_A_ (W)w, EV_CHILD); |
807 | ev_feed_event (EV_A_ (W)w, EV_CHILD); |
773 | } |
808 | } |
774 | } |
809 | } |
775 | |
810 | |
776 | #ifndef WCONTINUED |
811 | #ifndef WCONTINUED |
… | |
… | |
886 | ev_backend (EV_P) |
921 | ev_backend (EV_P) |
887 | { |
922 | { |
888 | return backend; |
923 | return backend; |
889 | } |
924 | } |
890 | |
925 | |
|
|
926 | unsigned int |
|
|
927 | ev_loop_count (EV_P) |
|
|
928 | { |
|
|
929 | return loop_count; |
|
|
930 | } |
|
|
931 | |
891 | static void noinline |
932 | static void noinline |
892 | loop_init (EV_P_ unsigned int flags) |
933 | loop_init (EV_P_ unsigned int flags) |
893 | { |
934 | { |
894 | if (!backend) |
935 | if (!backend) |
895 | { |
936 | { |
… | |
… | |
975 | #if EV_USE_SELECT |
1016 | #if EV_USE_SELECT |
976 | if (backend == EVBACKEND_SELECT) select_destroy (EV_A); |
1017 | if (backend == EVBACKEND_SELECT) select_destroy (EV_A); |
977 | #endif |
1018 | #endif |
978 | |
1019 | |
979 | for (i = NUMPRI; i--; ) |
1020 | for (i = NUMPRI; i--; ) |
|
|
1021 | { |
980 | array_free (pending, [i]); |
1022 | array_free (pending, [i]); |
|
|
1023 | #if EV_IDLE_ENABLE |
|
|
1024 | array_free (idle, [i]); |
|
|
1025 | #endif |
|
|
1026 | } |
981 | |
1027 | |
982 | /* have to use the microsoft-never-gets-it-right macro */ |
1028 | /* have to use the microsoft-never-gets-it-right macro */ |
983 | array_free (fdchange, EMPTY0); |
1029 | array_free (fdchange, EMPTY); |
984 | array_free (timer, EMPTY0); |
1030 | array_free (timer, EMPTY); |
985 | #if EV_PERIODIC_ENABLE |
1031 | #if EV_PERIODIC_ENABLE |
986 | array_free (periodic, EMPTY0); |
1032 | array_free (periodic, EMPTY); |
987 | #endif |
1033 | #endif |
988 | array_free (idle, EMPTY0); |
|
|
989 | array_free (prepare, EMPTY0); |
1034 | array_free (prepare, EMPTY); |
990 | array_free (check, EMPTY0); |
1035 | array_free (check, EMPTY); |
991 | |
1036 | |
992 | backend = 0; |
1037 | backend = 0; |
993 | } |
1038 | } |
994 | |
1039 | |
995 | void inline_size infy_fork (EV_P); |
1040 | void inline_size infy_fork (EV_P); |
… | |
… | |
1131 | postfork = 1; |
1176 | postfork = 1; |
1132 | } |
1177 | } |
1133 | |
1178 | |
1134 | /*****************************************************************************/ |
1179 | /*****************************************************************************/ |
1135 | |
1180 | |
1136 | int inline_size |
1181 | void |
1137 | any_pending (EV_P) |
1182 | ev_invoke (EV_P_ void *w, int revents) |
1138 | { |
1183 | { |
1139 | int pri; |
1184 | EV_CB_INVOKE ((W)w, revents); |
1140 | |
|
|
1141 | for (pri = NUMPRI; pri--; ) |
|
|
1142 | if (pendingcnt [pri]) |
|
|
1143 | return 1; |
|
|
1144 | |
|
|
1145 | return 0; |
|
|
1146 | } |
1185 | } |
1147 | |
1186 | |
1148 | void inline_speed |
1187 | void inline_speed |
1149 | call_pending (EV_P) |
1188 | call_pending (EV_P) |
1150 | { |
1189 | { |
… | |
… | |
1203 | /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/ |
1242 | /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/ |
1204 | |
1243 | |
1205 | /* first reschedule or stop timer */ |
1244 | /* first reschedule or stop timer */ |
1206 | if (w->reschedule_cb) |
1245 | if (w->reschedule_cb) |
1207 | { |
1246 | { |
1208 | ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + 0.0001); |
1247 | ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + TIME_EPSILON); |
1209 | assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now)); |
1248 | assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now)); |
1210 | downheap ((WT *)periodics, periodiccnt, 0); |
1249 | downheap ((WT *)periodics, periodiccnt, 0); |
1211 | } |
1250 | } |
1212 | else if (w->interval) |
1251 | else if (w->interval) |
1213 | { |
1252 | { |
1214 | ((WT)w)->at += floor ((ev_rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval; |
1253 | ((WT)w)->at = w->offset + floor ((ev_rt_now + TIME_EPSILON - w->offset) / w->interval + 1.) * w->interval; |
1215 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > ev_rt_now)); |
1254 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > ev_rt_now)); |
1216 | downheap ((WT *)periodics, periodiccnt, 0); |
1255 | downheap ((WT *)periodics, periodiccnt, 0); |
1217 | } |
1256 | } |
1218 | else |
1257 | else |
1219 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
1258 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
… | |
… | |
1233 | ev_periodic *w = periodics [i]; |
1272 | ev_periodic *w = periodics [i]; |
1234 | |
1273 | |
1235 | if (w->reschedule_cb) |
1274 | if (w->reschedule_cb) |
1236 | ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); |
1275 | ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); |
1237 | else if (w->interval) |
1276 | else if (w->interval) |
1238 | ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1277 | ((WT)w)->at = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; |
1239 | } |
1278 | } |
1240 | |
1279 | |
1241 | /* now rebuild the heap */ |
1280 | /* now rebuild the heap */ |
1242 | for (i = periodiccnt >> 1; i--; ) |
1281 | for (i = periodiccnt >> 1; i--; ) |
1243 | downheap ((WT *)periodics, periodiccnt, i); |
1282 | downheap ((WT *)periodics, periodiccnt, i); |
|
|
1283 | } |
|
|
1284 | #endif |
|
|
1285 | |
|
|
1286 | #if EV_IDLE_ENABLE |
|
|
1287 | void inline_size |
|
|
1288 | idle_reify (EV_P) |
|
|
1289 | { |
|
|
1290 | if (expect_false (idleall)) |
|
|
1291 | { |
|
|
1292 | int pri; |
|
|
1293 | |
|
|
1294 | for (pri = NUMPRI; pri--; ) |
|
|
1295 | { |
|
|
1296 | if (pendingcnt [pri]) |
|
|
1297 | break; |
|
|
1298 | |
|
|
1299 | if (idlecnt [pri]) |
|
|
1300 | { |
|
|
1301 | queue_events (EV_A_ (W *)idles [pri], idlecnt [pri], EV_IDLE); |
|
|
1302 | break; |
|
|
1303 | } |
|
|
1304 | } |
|
|
1305 | } |
1244 | } |
1306 | } |
1245 | #endif |
1307 | #endif |
1246 | |
1308 | |
1247 | int inline_size |
1309 | int inline_size |
1248 | time_update_monotonic (EV_P) |
1310 | time_update_monotonic (EV_P) |
… | |
… | |
1342 | ? EVUNLOOP_ONE |
1404 | ? EVUNLOOP_ONE |
1343 | : EVUNLOOP_CANCEL; |
1405 | : EVUNLOOP_CANCEL; |
1344 | |
1406 | |
1345 | call_pending (EV_A); /* in case we recurse, ensure ordering stays nice and clean */ |
1407 | call_pending (EV_A); /* in case we recurse, ensure ordering stays nice and clean */ |
1346 | |
1408 | |
1347 | for (;;) |
1409 | do |
1348 | { |
1410 | { |
1349 | #ifndef _WIN32 |
1411 | #ifndef _WIN32 |
1350 | if (expect_false (curpid)) /* penalise the forking check even more */ |
1412 | if (expect_false (curpid)) /* penalise the forking check even more */ |
1351 | if (expect_false (getpid () != curpid)) |
1413 | if (expect_false (getpid () != curpid)) |
1352 | { |
1414 | { |
… | |
… | |
1363 | queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK); |
1425 | queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK); |
1364 | call_pending (EV_A); |
1426 | call_pending (EV_A); |
1365 | } |
1427 | } |
1366 | #endif |
1428 | #endif |
1367 | |
1429 | |
1368 | /* queue check watchers (and execute them) */ |
1430 | /* queue prepare watchers (and execute them) */ |
1369 | if (expect_false (preparecnt)) |
1431 | if (expect_false (preparecnt)) |
1370 | { |
1432 | { |
1371 | queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); |
1433 | queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); |
1372 | call_pending (EV_A); |
1434 | call_pending (EV_A); |
1373 | } |
1435 | } |
… | |
… | |
1384 | |
1446 | |
1385 | /* calculate blocking time */ |
1447 | /* calculate blocking time */ |
1386 | { |
1448 | { |
1387 | ev_tstamp block; |
1449 | ev_tstamp block; |
1388 | |
1450 | |
1389 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
1451 | if (expect_false (flags & EVLOOP_NONBLOCK || idleall || !activecnt)) |
1390 | block = 0.; /* do not block at all */ |
1452 | block = 0.; /* do not block at all */ |
1391 | else |
1453 | else |
1392 | { |
1454 | { |
1393 | /* update time to cancel out callback processing overhead */ |
1455 | /* update time to cancel out callback processing overhead */ |
1394 | #if EV_USE_MONOTONIC |
1456 | #if EV_USE_MONOTONIC |
… | |
… | |
1418 | #endif |
1480 | #endif |
1419 | |
1481 | |
1420 | if (expect_false (block < 0.)) block = 0.; |
1482 | if (expect_false (block < 0.)) block = 0.; |
1421 | } |
1483 | } |
1422 | |
1484 | |
|
|
1485 | ++loop_count; |
1423 | backend_poll (EV_A_ block); |
1486 | backend_poll (EV_A_ block); |
1424 | } |
1487 | } |
1425 | |
1488 | |
1426 | /* update ev_rt_now, do magic */ |
1489 | /* update ev_rt_now, do magic */ |
1427 | time_update (EV_A); |
1490 | time_update (EV_A); |
… | |
… | |
1430 | timers_reify (EV_A); /* relative timers called last */ |
1493 | timers_reify (EV_A); /* relative timers called last */ |
1431 | #if EV_PERIODIC_ENABLE |
1494 | #if EV_PERIODIC_ENABLE |
1432 | periodics_reify (EV_A); /* absolute timers called first */ |
1495 | periodics_reify (EV_A); /* absolute timers called first */ |
1433 | #endif |
1496 | #endif |
1434 | |
1497 | |
|
|
1498 | #if EV_IDLE_ENABLE |
1435 | /* queue idle watchers unless other events are pending */ |
1499 | /* queue idle watchers unless other events are pending */ |
1436 | if (idlecnt && !any_pending (EV_A)) |
1500 | idle_reify (EV_A); |
1437 | queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); |
1501 | #endif |
1438 | |
1502 | |
1439 | /* queue check watchers, to be executed first */ |
1503 | /* queue check watchers, to be executed first */ |
1440 | if (expect_false (checkcnt)) |
1504 | if (expect_false (checkcnt)) |
1441 | queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); |
1505 | queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); |
1442 | |
1506 | |
1443 | call_pending (EV_A); |
1507 | call_pending (EV_A); |
1444 | |
1508 | |
1445 | if (expect_false (loop_done)) |
|
|
1446 | break; |
|
|
1447 | } |
1509 | } |
|
|
1510 | while (expect_true (activecnt && !loop_done)); |
1448 | |
1511 | |
1449 | if (loop_done == EVUNLOOP_ONE) |
1512 | if (loop_done == EVUNLOOP_ONE) |
1450 | loop_done = EVUNLOOP_CANCEL; |
1513 | loop_done = EVUNLOOP_CANCEL; |
1451 | } |
1514 | } |
1452 | |
1515 | |
… | |
… | |
1479 | head = &(*head)->next; |
1542 | head = &(*head)->next; |
1480 | } |
1543 | } |
1481 | } |
1544 | } |
1482 | |
1545 | |
1483 | void inline_speed |
1546 | void inline_speed |
1484 | ev_clear_pending (EV_P_ W w) |
1547 | clear_pending (EV_P_ W w) |
1485 | { |
1548 | { |
1486 | if (w->pending) |
1549 | if (w->pending) |
1487 | { |
1550 | { |
1488 | pendings [ABSPRI (w)][w->pending - 1].w = 0; |
1551 | pendings [ABSPRI (w)][w->pending - 1].w = 0; |
1489 | w->pending = 0; |
1552 | w->pending = 0; |
1490 | } |
1553 | } |
1491 | } |
1554 | } |
1492 | |
1555 | |
|
|
1556 | int |
|
|
1557 | ev_clear_pending (EV_P_ void *w) |
|
|
1558 | { |
|
|
1559 | W w_ = (W)w; |
|
|
1560 | int pending = w_->pending; |
|
|
1561 | |
|
|
1562 | if (expect_true (pending)) |
|
|
1563 | { |
|
|
1564 | ANPENDING *p = pendings [ABSPRI (w_)] + pending - 1; |
|
|
1565 | w_->pending = 0; |
|
|
1566 | p->w = 0; |
|
|
1567 | return p->events; |
|
|
1568 | } |
|
|
1569 | else |
|
|
1570 | return 0; |
|
|
1571 | } |
|
|
1572 | |
|
|
1573 | void inline_size |
|
|
1574 | pri_adjust (EV_P_ W w) |
|
|
1575 | { |
|
|
1576 | int pri = w->priority; |
|
|
1577 | pri = pri < EV_MINPRI ? EV_MINPRI : pri; |
|
|
1578 | pri = pri > EV_MAXPRI ? EV_MAXPRI : pri; |
|
|
1579 | w->priority = pri; |
|
|
1580 | } |
|
|
1581 | |
1493 | void inline_speed |
1582 | void inline_speed |
1494 | ev_start (EV_P_ W w, int active) |
1583 | ev_start (EV_P_ W w, int active) |
1495 | { |
1584 | { |
1496 | if (w->priority < EV_MINPRI) w->priority = EV_MINPRI; |
1585 | pri_adjust (EV_A_ w); |
1497 | if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI; |
|
|
1498 | |
|
|
1499 | w->active = active; |
1586 | w->active = active; |
1500 | ev_ref (EV_A); |
1587 | ev_ref (EV_A); |
1501 | } |
1588 | } |
1502 | |
1589 | |
1503 | void inline_size |
1590 | void inline_size |
… | |
… | |
1507 | w->active = 0; |
1594 | w->active = 0; |
1508 | } |
1595 | } |
1509 | |
1596 | |
1510 | /*****************************************************************************/ |
1597 | /*****************************************************************************/ |
1511 | |
1598 | |
1512 | void |
1599 | void noinline |
1513 | ev_io_start (EV_P_ ev_io *w) |
1600 | ev_io_start (EV_P_ ev_io *w) |
1514 | { |
1601 | { |
1515 | int fd = w->fd; |
1602 | int fd = w->fd; |
1516 | |
1603 | |
1517 | if (expect_false (ev_is_active (w))) |
1604 | if (expect_false (ev_is_active (w))) |
… | |
… | |
1524 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
1611 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
1525 | |
1612 | |
1526 | fd_change (EV_A_ fd); |
1613 | fd_change (EV_A_ fd); |
1527 | } |
1614 | } |
1528 | |
1615 | |
1529 | void |
1616 | void noinline |
1530 | ev_io_stop (EV_P_ ev_io *w) |
1617 | ev_io_stop (EV_P_ ev_io *w) |
1531 | { |
1618 | { |
1532 | ev_clear_pending (EV_A_ (W)w); |
1619 | clear_pending (EV_A_ (W)w); |
1533 | if (expect_false (!ev_is_active (w))) |
1620 | if (expect_false (!ev_is_active (w))) |
1534 | return; |
1621 | return; |
1535 | |
1622 | |
1536 | assert (("ev_io_start called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax)); |
1623 | assert (("ev_io_start called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax)); |
1537 | |
1624 | |
… | |
… | |
1539 | ev_stop (EV_A_ (W)w); |
1626 | ev_stop (EV_A_ (W)w); |
1540 | |
1627 | |
1541 | fd_change (EV_A_ w->fd); |
1628 | fd_change (EV_A_ w->fd); |
1542 | } |
1629 | } |
1543 | |
1630 | |
1544 | void |
1631 | void noinline |
1545 | ev_timer_start (EV_P_ ev_timer *w) |
1632 | ev_timer_start (EV_P_ ev_timer *w) |
1546 | { |
1633 | { |
1547 | if (expect_false (ev_is_active (w))) |
1634 | if (expect_false (ev_is_active (w))) |
1548 | return; |
1635 | return; |
1549 | |
1636 | |
… | |
… | |
1557 | upheap ((WT *)timers, timercnt - 1); |
1644 | upheap ((WT *)timers, timercnt - 1); |
1558 | |
1645 | |
1559 | /*assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));*/ |
1646 | /*assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));*/ |
1560 | } |
1647 | } |
1561 | |
1648 | |
1562 | void |
1649 | void noinline |
1563 | ev_timer_stop (EV_P_ ev_timer *w) |
1650 | ev_timer_stop (EV_P_ ev_timer *w) |
1564 | { |
1651 | { |
1565 | ev_clear_pending (EV_A_ (W)w); |
1652 | clear_pending (EV_A_ (W)w); |
1566 | if (expect_false (!ev_is_active (w))) |
1653 | if (expect_false (!ev_is_active (w))) |
1567 | return; |
1654 | return; |
1568 | |
1655 | |
1569 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1656 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1570 | |
1657 | |
… | |
… | |
1581 | ((WT)w)->at -= mn_now; |
1668 | ((WT)w)->at -= mn_now; |
1582 | |
1669 | |
1583 | ev_stop (EV_A_ (W)w); |
1670 | ev_stop (EV_A_ (W)w); |
1584 | } |
1671 | } |
1585 | |
1672 | |
1586 | void |
1673 | void noinline |
1587 | ev_timer_again (EV_P_ ev_timer *w) |
1674 | ev_timer_again (EV_P_ ev_timer *w) |
1588 | { |
1675 | { |
1589 | if (ev_is_active (w)) |
1676 | if (ev_is_active (w)) |
1590 | { |
1677 | { |
1591 | if (w->repeat) |
1678 | if (w->repeat) |
… | |
… | |
1602 | ev_timer_start (EV_A_ w); |
1689 | ev_timer_start (EV_A_ w); |
1603 | } |
1690 | } |
1604 | } |
1691 | } |
1605 | |
1692 | |
1606 | #if EV_PERIODIC_ENABLE |
1693 | #if EV_PERIODIC_ENABLE |
1607 | void |
1694 | void noinline |
1608 | ev_periodic_start (EV_P_ ev_periodic *w) |
1695 | ev_periodic_start (EV_P_ ev_periodic *w) |
1609 | { |
1696 | { |
1610 | if (expect_false (ev_is_active (w))) |
1697 | if (expect_false (ev_is_active (w))) |
1611 | return; |
1698 | return; |
1612 | |
1699 | |
… | |
… | |
1614 | ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); |
1701 | ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); |
1615 | else if (w->interval) |
1702 | else if (w->interval) |
1616 | { |
1703 | { |
1617 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
1704 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
1618 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1705 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1619 | ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1706 | ((WT)w)->at = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; |
1620 | } |
1707 | } |
|
|
1708 | else |
|
|
1709 | ((WT)w)->at = w->offset; |
1621 | |
1710 | |
1622 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1711 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1623 | array_needsize (ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2); |
1712 | array_needsize (ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2); |
1624 | periodics [periodiccnt - 1] = w; |
1713 | periodics [periodiccnt - 1] = w; |
1625 | upheap ((WT *)periodics, periodiccnt - 1); |
1714 | upheap ((WT *)periodics, periodiccnt - 1); |
1626 | |
1715 | |
1627 | /*assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));*/ |
1716 | /*assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));*/ |
1628 | } |
1717 | } |
1629 | |
1718 | |
1630 | void |
1719 | void noinline |
1631 | ev_periodic_stop (EV_P_ ev_periodic *w) |
1720 | ev_periodic_stop (EV_P_ ev_periodic *w) |
1632 | { |
1721 | { |
1633 | ev_clear_pending (EV_A_ (W)w); |
1722 | clear_pending (EV_A_ (W)w); |
1634 | if (expect_false (!ev_is_active (w))) |
1723 | if (expect_false (!ev_is_active (w))) |
1635 | return; |
1724 | return; |
1636 | |
1725 | |
1637 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1726 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1638 | |
1727 | |
… | |
… | |
1647 | } |
1736 | } |
1648 | |
1737 | |
1649 | ev_stop (EV_A_ (W)w); |
1738 | ev_stop (EV_A_ (W)w); |
1650 | } |
1739 | } |
1651 | |
1740 | |
1652 | void |
1741 | void noinline |
1653 | ev_periodic_again (EV_P_ ev_periodic *w) |
1742 | ev_periodic_again (EV_P_ ev_periodic *w) |
1654 | { |
1743 | { |
1655 | /* TODO: use adjustheap and recalculation */ |
1744 | /* TODO: use adjustheap and recalculation */ |
1656 | ev_periodic_stop (EV_A_ w); |
1745 | ev_periodic_stop (EV_A_ w); |
1657 | ev_periodic_start (EV_A_ w); |
1746 | ev_periodic_start (EV_A_ w); |
… | |
… | |
1660 | |
1749 | |
1661 | #ifndef SA_RESTART |
1750 | #ifndef SA_RESTART |
1662 | # define SA_RESTART 0 |
1751 | # define SA_RESTART 0 |
1663 | #endif |
1752 | #endif |
1664 | |
1753 | |
1665 | void |
1754 | void noinline |
1666 | ev_signal_start (EV_P_ ev_signal *w) |
1755 | ev_signal_start (EV_P_ ev_signal *w) |
1667 | { |
1756 | { |
1668 | #if EV_MULTIPLICITY |
1757 | #if EV_MULTIPLICITY |
1669 | assert (("signal watchers are only supported in the default loop", loop == ev_default_loop_ptr)); |
1758 | assert (("signal watchers are only supported in the default loop", loop == ev_default_loop_ptr)); |
1670 | #endif |
1759 | #endif |
… | |
… | |
1689 | sigaction (w->signum, &sa, 0); |
1778 | sigaction (w->signum, &sa, 0); |
1690 | #endif |
1779 | #endif |
1691 | } |
1780 | } |
1692 | } |
1781 | } |
1693 | |
1782 | |
1694 | void |
1783 | void noinline |
1695 | ev_signal_stop (EV_P_ ev_signal *w) |
1784 | ev_signal_stop (EV_P_ ev_signal *w) |
1696 | { |
1785 | { |
1697 | ev_clear_pending (EV_A_ (W)w); |
1786 | clear_pending (EV_A_ (W)w); |
1698 | if (expect_false (!ev_is_active (w))) |
1787 | if (expect_false (!ev_is_active (w))) |
1699 | return; |
1788 | return; |
1700 | |
1789 | |
1701 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
1790 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
1702 | ev_stop (EV_A_ (W)w); |
1791 | ev_stop (EV_A_ (W)w); |
… | |
… | |
1719 | } |
1808 | } |
1720 | |
1809 | |
1721 | void |
1810 | void |
1722 | ev_child_stop (EV_P_ ev_child *w) |
1811 | ev_child_stop (EV_P_ ev_child *w) |
1723 | { |
1812 | { |
1724 | ev_clear_pending (EV_A_ (W)w); |
1813 | clear_pending (EV_A_ (W)w); |
1725 | if (expect_false (!ev_is_active (w))) |
1814 | if (expect_false (!ev_is_active (w))) |
1726 | return; |
1815 | return; |
1727 | |
1816 | |
1728 | wlist_del ((WL *)&childs [w->pid & (EV_PID_HASHSIZE - 1)], (WL)w); |
1817 | wlist_del ((WL *)&childs [w->pid & (EV_PID_HASHSIZE - 1)], (WL)w); |
1729 | ev_stop (EV_A_ (W)w); |
1818 | ev_stop (EV_A_ (W)w); |
… | |
… | |
1965 | } |
2054 | } |
1966 | |
2055 | |
1967 | void |
2056 | void |
1968 | ev_stat_stop (EV_P_ ev_stat *w) |
2057 | ev_stat_stop (EV_P_ ev_stat *w) |
1969 | { |
2058 | { |
1970 | ev_clear_pending (EV_A_ (W)w); |
2059 | clear_pending (EV_A_ (W)w); |
1971 | if (expect_false (!ev_is_active (w))) |
2060 | if (expect_false (!ev_is_active (w))) |
1972 | return; |
2061 | return; |
1973 | |
2062 | |
1974 | #if EV_USE_INOTIFY |
2063 | #if EV_USE_INOTIFY |
1975 | infy_del (EV_A_ w); |
2064 | infy_del (EV_A_ w); |
… | |
… | |
1978 | |
2067 | |
1979 | ev_stop (EV_A_ (W)w); |
2068 | ev_stop (EV_A_ (W)w); |
1980 | } |
2069 | } |
1981 | #endif |
2070 | #endif |
1982 | |
2071 | |
|
|
2072 | #if EV_IDLE_ENABLE |
1983 | void |
2073 | void |
1984 | ev_idle_start (EV_P_ ev_idle *w) |
2074 | ev_idle_start (EV_P_ ev_idle *w) |
1985 | { |
2075 | { |
1986 | if (expect_false (ev_is_active (w))) |
2076 | if (expect_false (ev_is_active (w))) |
1987 | return; |
2077 | return; |
1988 | |
2078 | |
|
|
2079 | pri_adjust (EV_A_ (W)w); |
|
|
2080 | |
|
|
2081 | { |
|
|
2082 | int active = ++idlecnt [ABSPRI (w)]; |
|
|
2083 | |
|
|
2084 | ++idleall; |
1989 | ev_start (EV_A_ (W)w, ++idlecnt); |
2085 | ev_start (EV_A_ (W)w, active); |
|
|
2086 | |
1990 | array_needsize (ev_idle *, idles, idlemax, idlecnt, EMPTY2); |
2087 | array_needsize (ev_idle *, idles [ABSPRI (w)], idlemax [ABSPRI (w)], active, EMPTY2); |
1991 | idles [idlecnt - 1] = w; |
2088 | idles [ABSPRI (w)][active - 1] = w; |
|
|
2089 | } |
1992 | } |
2090 | } |
1993 | |
2091 | |
1994 | void |
2092 | void |
1995 | ev_idle_stop (EV_P_ ev_idle *w) |
2093 | ev_idle_stop (EV_P_ ev_idle *w) |
1996 | { |
2094 | { |
1997 | ev_clear_pending (EV_A_ (W)w); |
2095 | clear_pending (EV_A_ (W)w); |
1998 | if (expect_false (!ev_is_active (w))) |
2096 | if (expect_false (!ev_is_active (w))) |
1999 | return; |
2097 | return; |
2000 | |
2098 | |
2001 | { |
2099 | { |
2002 | int active = ((W)w)->active; |
2100 | int active = ((W)w)->active; |
2003 | idles [active - 1] = idles [--idlecnt]; |
2101 | |
|
|
2102 | idles [ABSPRI (w)][active - 1] = idles [ABSPRI (w)][--idlecnt [ABSPRI (w)]]; |
2004 | ((W)idles [active - 1])->active = active; |
2103 | ((W)idles [ABSPRI (w)][active - 1])->active = active; |
|
|
2104 | |
|
|
2105 | ev_stop (EV_A_ (W)w); |
|
|
2106 | --idleall; |
2005 | } |
2107 | } |
2006 | |
|
|
2007 | ev_stop (EV_A_ (W)w); |
|
|
2008 | } |
2108 | } |
|
|
2109 | #endif |
2009 | |
2110 | |
2010 | void |
2111 | void |
2011 | ev_prepare_start (EV_P_ ev_prepare *w) |
2112 | ev_prepare_start (EV_P_ ev_prepare *w) |
2012 | { |
2113 | { |
2013 | if (expect_false (ev_is_active (w))) |
2114 | if (expect_false (ev_is_active (w))) |
… | |
… | |
2019 | } |
2120 | } |
2020 | |
2121 | |
2021 | void |
2122 | void |
2022 | ev_prepare_stop (EV_P_ ev_prepare *w) |
2123 | ev_prepare_stop (EV_P_ ev_prepare *w) |
2023 | { |
2124 | { |
2024 | ev_clear_pending (EV_A_ (W)w); |
2125 | clear_pending (EV_A_ (W)w); |
2025 | if (expect_false (!ev_is_active (w))) |
2126 | if (expect_false (!ev_is_active (w))) |
2026 | return; |
2127 | return; |
2027 | |
2128 | |
2028 | { |
2129 | { |
2029 | int active = ((W)w)->active; |
2130 | int active = ((W)w)->active; |
… | |
… | |
2046 | } |
2147 | } |
2047 | |
2148 | |
2048 | void |
2149 | void |
2049 | ev_check_stop (EV_P_ ev_check *w) |
2150 | ev_check_stop (EV_P_ ev_check *w) |
2050 | { |
2151 | { |
2051 | ev_clear_pending (EV_A_ (W)w); |
2152 | clear_pending (EV_A_ (W)w); |
2052 | if (expect_false (!ev_is_active (w))) |
2153 | if (expect_false (!ev_is_active (w))) |
2053 | return; |
2154 | return; |
2054 | |
2155 | |
2055 | { |
2156 | { |
2056 | int active = ((W)w)->active; |
2157 | int active = ((W)w)->active; |
… | |
… | |
2098 | } |
2199 | } |
2099 | |
2200 | |
2100 | void |
2201 | void |
2101 | ev_embed_stop (EV_P_ ev_embed *w) |
2202 | ev_embed_stop (EV_P_ ev_embed *w) |
2102 | { |
2203 | { |
2103 | ev_clear_pending (EV_A_ (W)w); |
2204 | clear_pending (EV_A_ (W)w); |
2104 | if (expect_false (!ev_is_active (w))) |
2205 | if (expect_false (!ev_is_active (w))) |
2105 | return; |
2206 | return; |
2106 | |
2207 | |
2107 | ev_io_stop (EV_A_ &w->io); |
2208 | ev_io_stop (EV_A_ &w->io); |
2108 | |
2209 | |
… | |
… | |
2123 | } |
2224 | } |
2124 | |
2225 | |
2125 | void |
2226 | void |
2126 | ev_fork_stop (EV_P_ ev_fork *w) |
2227 | ev_fork_stop (EV_P_ ev_fork *w) |
2127 | { |
2228 | { |
2128 | ev_clear_pending (EV_A_ (W)w); |
2229 | clear_pending (EV_A_ (W)w); |
2129 | if (expect_false (!ev_is_active (w))) |
2230 | if (expect_false (!ev_is_active (w))) |
2130 | return; |
2231 | return; |
2131 | |
2232 | |
2132 | { |
2233 | { |
2133 | int active = ((W)w)->active; |
2234 | int active = ((W)w)->active; |