… | |
… | |
222 | #define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */ |
222 | #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 */ |
223 | /*#define CLEANUP_INTERVAL (MAX_BLOCKTIME * 5.) /* how often to try to free memory and re-check fds */ |
224 | |
224 | |
225 | #if __GNUC__ >= 3 |
225 | #if __GNUC__ >= 3 |
226 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
226 | # 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)) |
227 | # 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 |
228 | #else |
236 | # define expect(expr,value) (expr) |
229 | # define expect(expr,value) (expr) |
237 | # define inline_speed static |
|
|
238 | # define inline_size static |
|
|
239 | # define noinline |
230 | # define noinline |
|
|
231 | # if __STDC_VERSION__ < 199901L |
|
|
232 | # define inline |
|
|
233 | # endif |
240 | #endif |
234 | #endif |
241 | |
235 | |
242 | #define expect_false(expr) expect ((expr) != 0, 0) |
236 | #define expect_false(expr) expect ((expr) != 0, 0) |
243 | #define expect_true(expr) expect ((expr) != 0, 1) |
237 | #define expect_true(expr) expect ((expr) != 0, 1) |
|
|
238 | #define inline_size static inline |
|
|
239 | |
|
|
240 | #if EV_MINIMAL |
|
|
241 | # define inline_speed static noinline |
|
|
242 | #else |
|
|
243 | # define inline_speed static inline |
|
|
244 | #endif |
244 | |
245 | |
245 | #define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) |
246 | #define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) |
246 | #define ABSPRI(w) ((w)->priority - EV_MINPRI) |
247 | #define ABSPRI(w) (((W)w)->priority - EV_MINPRI) |
247 | |
248 | |
248 | #define EMPTY0 /* required for microsofts broken pseudo-c compiler */ |
249 | #define EMPTY /* required for microsofts broken pseudo-c compiler */ |
249 | #define EMPTY2(a,b) /* used to suppress some warnings */ |
250 | #define EMPTY2(a,b) /* used to suppress some warnings */ |
250 | |
251 | |
251 | typedef ev_watcher *W; |
252 | typedef ev_watcher *W; |
252 | typedef ev_watcher_list *WL; |
253 | typedef ev_watcher_list *WL; |
253 | typedef ev_watcher_time *WT; |
254 | typedef ev_watcher_time *WT; |
… | |
… | |
396 | { |
397 | { |
397 | return ev_rt_now; |
398 | return ev_rt_now; |
398 | } |
399 | } |
399 | #endif |
400 | #endif |
400 | |
401 | |
401 | #define array_roundsize(type,n) (((n) | 4) & ~3) |
402 | int inline_size |
|
|
403 | array_nextsize (int elem, int cur, int cnt) |
|
|
404 | { |
|
|
405 | int ncur = cur + 1; |
|
|
406 | |
|
|
407 | do |
|
|
408 | ncur <<= 1; |
|
|
409 | while (cnt > ncur); |
|
|
410 | |
|
|
411 | /* if size > 4096, round to 4096 - 4 * longs to accomodate malloc overhead */ |
|
|
412 | if (elem * ncur > 4096) |
|
|
413 | { |
|
|
414 | ncur *= elem; |
|
|
415 | ncur = (ncur + elem + 4095 + sizeof (void *) * 4) & ~4095; |
|
|
416 | ncur = ncur - sizeof (void *) * 4; |
|
|
417 | ncur /= elem; |
|
|
418 | } |
|
|
419 | |
|
|
420 | return ncur; |
|
|
421 | } |
|
|
422 | |
|
|
423 | static noinline void * |
|
|
424 | array_realloc (int elem, void *base, int *cur, int cnt) |
|
|
425 | { |
|
|
426 | *cur = array_nextsize (elem, *cur, cnt); |
|
|
427 | return ev_realloc (base, elem * *cur); |
|
|
428 | } |
402 | |
429 | |
403 | #define array_needsize(type,base,cur,cnt,init) \ |
430 | #define array_needsize(type,base,cur,cnt,init) \ |
404 | if (expect_false ((cnt) > cur)) \ |
431 | if (expect_false ((cnt) > (cur))) \ |
405 | { \ |
432 | { \ |
406 | int newcnt = cur; \ |
433 | int ocur_ = (cur); \ |
407 | do \ |
434 | (base) = (type *)array_realloc \ |
408 | { \ |
435 | (sizeof (type), (base), &(cur), (cnt)); \ |
409 | newcnt = array_roundsize (type, newcnt << 1); \ |
436 | 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 | } |
437 | } |
417 | |
438 | |
|
|
439 | #if 0 |
418 | #define array_slim(type,stem) \ |
440 | #define array_slim(type,stem) \ |
419 | if (stem ## max < array_roundsize (stem ## cnt >> 2)) \ |
441 | if (stem ## max < array_roundsize (stem ## cnt >> 2)) \ |
420 | { \ |
442 | { \ |
421 | stem ## max = array_roundsize (stem ## cnt >> 1); \ |
443 | stem ## max = array_roundsize (stem ## cnt >> 1); \ |
422 | base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\ |
444 | base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\ |
423 | fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ |
445 | fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ |
424 | } |
446 | } |
|
|
447 | #endif |
425 | |
448 | |
426 | #define array_free(stem, idx) \ |
449 | #define array_free(stem, idx) \ |
427 | ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; |
450 | ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; |
428 | |
451 | |
429 | /*****************************************************************************/ |
452 | /*****************************************************************************/ |
430 | |
453 | |
431 | void noinline |
454 | void noinline |
432 | ev_feed_event (EV_P_ void *w, int revents) |
455 | ev_feed_event (EV_P_ void *w, int revents) |
433 | { |
456 | { |
434 | W w_ = (W)w; |
457 | W w_ = (W)w; |
|
|
458 | int pri = ABSPRI (w_); |
435 | |
459 | |
436 | if (expect_false (w_->pending)) |
460 | if (expect_false (w_->pending)) |
|
|
461 | pendings [pri][w_->pending - 1].events |= revents; |
|
|
462 | else |
437 | { |
463 | { |
|
|
464 | w_->pending = ++pendingcnt [pri]; |
|
|
465 | array_needsize (ANPENDING, pendings [pri], pendingmax [pri], w_->pending, EMPTY2); |
|
|
466 | pendings [pri][w_->pending - 1].w = w_; |
438 | pendings [ABSPRI (w_)][w_->pending - 1].events |= revents; |
467 | pendings [pri][w_->pending - 1].events = revents; |
439 | return; |
|
|
440 | } |
468 | } |
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 | } |
469 | } |
447 | |
470 | |
448 | void inline_size |
471 | void inline_size |
449 | queue_events (EV_P_ W *events, int eventcnt, int type) |
472 | queue_events (EV_P_ W *events, int eventcnt, int type) |
450 | { |
473 | { |
… | |
… | |
485 | } |
508 | } |
486 | |
509 | |
487 | void |
510 | void |
488 | ev_feed_fd_event (EV_P_ int fd, int revents) |
511 | ev_feed_fd_event (EV_P_ int fd, int revents) |
489 | { |
512 | { |
|
|
513 | if (fd >= 0 && fd < anfdmax) |
490 | fd_event (EV_A_ fd, revents); |
514 | fd_event (EV_A_ fd, revents); |
491 | } |
515 | } |
492 | |
516 | |
493 | void inline_size |
517 | void inline_size |
494 | fd_reify (EV_P) |
518 | fd_reify (EV_P) |
495 | { |
519 | { |
… | |
… | |
589 | static void noinline |
613 | static void noinline |
590 | fd_rearm_all (EV_P) |
614 | fd_rearm_all (EV_P) |
591 | { |
615 | { |
592 | int fd; |
616 | int fd; |
593 | |
617 | |
594 | /* this should be highly optimised to not do anything but set a flag */ |
|
|
595 | for (fd = 0; fd < anfdmax; ++fd) |
618 | for (fd = 0; fd < anfdmax; ++fd) |
596 | if (anfds [fd].events) |
619 | if (anfds [fd].events) |
597 | { |
620 | { |
598 | anfds [fd].events = 0; |
621 | anfds [fd].events = 0; |
599 | fd_change (EV_A_ fd); |
622 | fd_change (EV_A_ fd); |
… | |
… | |
726 | for (signum = signalmax; signum--; ) |
749 | for (signum = signalmax; signum--; ) |
727 | if (signals [signum].gotsig) |
750 | if (signals [signum].gotsig) |
728 | ev_feed_signal_event (EV_A_ signum + 1); |
751 | ev_feed_signal_event (EV_A_ signum + 1); |
729 | } |
752 | } |
730 | |
753 | |
731 | void inline_size |
754 | void inline_speed |
732 | fd_intern (int fd) |
755 | fd_intern (int fd) |
733 | { |
756 | { |
734 | #ifdef _WIN32 |
757 | #ifdef _WIN32 |
735 | int arg = 1; |
758 | int arg = 1; |
736 | ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg); |
759 | ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg); |
… | |
… | |
765 | ev_child *w; |
788 | ev_child *w; |
766 | |
789 | |
767 | for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next) |
790 | for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next) |
768 | if (w->pid == pid || !w->pid) |
791 | if (w->pid == pid || !w->pid) |
769 | { |
792 | { |
770 | ev_priority (w) = ev_priority (sw); /* need to do it *now* */ |
793 | ev_set_priority (w, ev_priority (sw)); /* need to do it *now* */ |
771 | w->rpid = pid; |
794 | w->rpid = pid; |
772 | w->rstatus = status; |
795 | w->rstatus = status; |
773 | ev_feed_event (EV_A_ (W)w, EV_CHILD); |
796 | ev_feed_event (EV_A_ (W)w, EV_CHILD); |
774 | } |
797 | } |
775 | } |
798 | } |
776 | |
799 | |
777 | #ifndef WCONTINUED |
800 | #ifndef WCONTINUED |
… | |
… | |
887 | ev_backend (EV_P) |
910 | ev_backend (EV_P) |
888 | { |
911 | { |
889 | return backend; |
912 | return backend; |
890 | } |
913 | } |
891 | |
914 | |
|
|
915 | unsigned int |
|
|
916 | ev_loop_count (EV_P) |
|
|
917 | { |
|
|
918 | return loop_count; |
|
|
919 | } |
|
|
920 | |
892 | static void noinline |
921 | static void noinline |
893 | loop_init (EV_P_ unsigned int flags) |
922 | loop_init (EV_P_ unsigned int flags) |
894 | { |
923 | { |
895 | if (!backend) |
924 | if (!backend) |
896 | { |
925 | { |
… | |
… | |
905 | ev_rt_now = ev_time (); |
934 | ev_rt_now = ev_time (); |
906 | mn_now = get_clock (); |
935 | mn_now = get_clock (); |
907 | now_floor = mn_now; |
936 | now_floor = mn_now; |
908 | rtmn_diff = ev_rt_now - mn_now; |
937 | rtmn_diff = ev_rt_now - mn_now; |
909 | |
938 | |
|
|
939 | /* pid check not overridable via env */ |
|
|
940 | #ifndef _WIN32 |
|
|
941 | if (flags & EVFLAG_FORKCHECK) |
|
|
942 | curpid = getpid (); |
|
|
943 | #endif |
|
|
944 | |
910 | if (!(flags & EVFLAG_NOENV) |
945 | if (!(flags & EVFLAG_NOENV) |
911 | && !enable_secure () |
946 | && !enable_secure () |
912 | && getenv ("LIBEV_FLAGS")) |
947 | && getenv ("LIBEV_FLAGS")) |
913 | flags = atoi (getenv ("LIBEV_FLAGS")); |
948 | flags = atoi (getenv ("LIBEV_FLAGS")); |
914 | |
949 | |
… | |
… | |
970 | #if EV_USE_SELECT |
1005 | #if EV_USE_SELECT |
971 | if (backend == EVBACKEND_SELECT) select_destroy (EV_A); |
1006 | if (backend == EVBACKEND_SELECT) select_destroy (EV_A); |
972 | #endif |
1007 | #endif |
973 | |
1008 | |
974 | for (i = NUMPRI; i--; ) |
1009 | for (i = NUMPRI; i--; ) |
|
|
1010 | { |
975 | array_free (pending, [i]); |
1011 | array_free (pending, [i]); |
|
|
1012 | #if EV_IDLE_ENABLE |
|
|
1013 | array_free (idle, [i]); |
|
|
1014 | #endif |
|
|
1015 | } |
976 | |
1016 | |
977 | /* have to use the microsoft-never-gets-it-right macro */ |
1017 | /* have to use the microsoft-never-gets-it-right macro */ |
978 | array_free (fdchange, EMPTY0); |
1018 | array_free (fdchange, EMPTY); |
979 | array_free (timer, EMPTY0); |
1019 | array_free (timer, EMPTY); |
980 | #if EV_PERIODIC_ENABLE |
1020 | #if EV_PERIODIC_ENABLE |
981 | array_free (periodic, EMPTY0); |
1021 | array_free (periodic, EMPTY); |
982 | #endif |
1022 | #endif |
983 | array_free (idle, EMPTY0); |
|
|
984 | array_free (prepare, EMPTY0); |
1023 | array_free (prepare, EMPTY); |
985 | array_free (check, EMPTY0); |
1024 | array_free (check, EMPTY); |
986 | |
1025 | |
987 | backend = 0; |
1026 | backend = 0; |
988 | } |
1027 | } |
989 | |
1028 | |
990 | void inline_size infy_fork (EV_P); |
1029 | void inline_size infy_fork (EV_P); |
… | |
… | |
1126 | postfork = 1; |
1165 | postfork = 1; |
1127 | } |
1166 | } |
1128 | |
1167 | |
1129 | /*****************************************************************************/ |
1168 | /*****************************************************************************/ |
1130 | |
1169 | |
1131 | int inline_size |
1170 | void |
1132 | any_pending (EV_P) |
1171 | ev_invoke (EV_P_ void *w, int revents) |
1133 | { |
1172 | { |
1134 | int pri; |
1173 | EV_CB_INVOKE ((W)w, revents); |
1135 | |
|
|
1136 | for (pri = NUMPRI; pri--; ) |
|
|
1137 | if (pendingcnt [pri]) |
|
|
1138 | return 1; |
|
|
1139 | |
|
|
1140 | return 0; |
|
|
1141 | } |
1174 | } |
1142 | |
1175 | |
1143 | void inline_speed |
1176 | void inline_speed |
1144 | call_pending (EV_P) |
1177 | call_pending (EV_P) |
1145 | { |
1178 | { |
… | |
… | |
1237 | for (i = periodiccnt >> 1; i--; ) |
1270 | for (i = periodiccnt >> 1; i--; ) |
1238 | downheap ((WT *)periodics, periodiccnt, i); |
1271 | downheap ((WT *)periodics, periodiccnt, i); |
1239 | } |
1272 | } |
1240 | #endif |
1273 | #endif |
1241 | |
1274 | |
|
|
1275 | #if EV_IDLE_ENABLE |
|
|
1276 | void inline_size |
|
|
1277 | idle_reify (EV_P) |
|
|
1278 | { |
|
|
1279 | if (expect_false (idleall)) |
|
|
1280 | { |
|
|
1281 | int pri; |
|
|
1282 | |
|
|
1283 | for (pri = NUMPRI; pri--; ) |
|
|
1284 | { |
|
|
1285 | if (pendingcnt [pri]) |
|
|
1286 | break; |
|
|
1287 | |
|
|
1288 | if (idlecnt [pri]) |
|
|
1289 | { |
|
|
1290 | queue_events (EV_A_ (W *)idles [pri], idlecnt [pri], EV_IDLE); |
|
|
1291 | break; |
|
|
1292 | } |
|
|
1293 | } |
|
|
1294 | } |
|
|
1295 | } |
|
|
1296 | #endif |
|
|
1297 | |
1242 | int inline_size |
1298 | int inline_size |
1243 | time_update_monotonic (EV_P) |
1299 | time_update_monotonic (EV_P) |
1244 | { |
1300 | { |
1245 | mn_now = get_clock (); |
1301 | mn_now = get_clock (); |
1246 | |
1302 | |
… | |
… | |
1270 | ev_tstamp odiff = rtmn_diff; |
1326 | ev_tstamp odiff = rtmn_diff; |
1271 | |
1327 | |
1272 | /* loop a few times, before making important decisions. |
1328 | /* loop a few times, before making important decisions. |
1273 | * on the choice of "4": one iteration isn't enough, |
1329 | * on the choice of "4": one iteration isn't enough, |
1274 | * in case we get preempted during the calls to |
1330 | * in case we get preempted during the calls to |
1275 | * ev_time and get_clock. a second call is almost guarenteed |
1331 | * ev_time and get_clock. a second call is almost guaranteed |
1276 | * to succeed in that case, though. and looping a few more times |
1332 | * to succeed in that case, though. and looping a few more times |
1277 | * doesn't hurt either as we only do this on time-jumps or |
1333 | * doesn't hurt either as we only do this on time-jumps or |
1278 | * in the unlikely event of getting preempted here. |
1334 | * in the unlikely event of having been preempted here. |
1279 | */ |
1335 | */ |
1280 | for (i = 4; --i; ) |
1336 | for (i = 4; --i; ) |
1281 | { |
1337 | { |
1282 | rtmn_diff = ev_rt_now - mn_now; |
1338 | rtmn_diff = ev_rt_now - mn_now; |
1283 | |
1339 | |
… | |
… | |
1305 | { |
1361 | { |
1306 | #if EV_PERIODIC_ENABLE |
1362 | #if EV_PERIODIC_ENABLE |
1307 | periodics_reschedule (EV_A); |
1363 | periodics_reschedule (EV_A); |
1308 | #endif |
1364 | #endif |
1309 | |
1365 | |
1310 | /* adjust timers. this is easy, as the offset is the same for all */ |
1366 | /* adjust timers. this is easy, as the offset is the same for all of them */ |
1311 | for (i = 0; i < timercnt; ++i) |
1367 | for (i = 0; i < timercnt; ++i) |
1312 | ((WT)timers [i])->at += ev_rt_now - mn_now; |
1368 | ((WT)timers [i])->at += ev_rt_now - mn_now; |
1313 | } |
1369 | } |
1314 | |
1370 | |
1315 | mn_now = ev_rt_now; |
1371 | mn_now = ev_rt_now; |
… | |
… | |
1335 | { |
1391 | { |
1336 | loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) |
1392 | loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) |
1337 | ? EVUNLOOP_ONE |
1393 | ? EVUNLOOP_ONE |
1338 | : EVUNLOOP_CANCEL; |
1394 | : EVUNLOOP_CANCEL; |
1339 | |
1395 | |
1340 | while (activecnt) |
1396 | call_pending (EV_A); /* in case we recurse, ensure ordering stays nice and clean */ |
|
|
1397 | |
|
|
1398 | do |
1341 | { |
1399 | { |
1342 | /* we might have forked, so reify kernel state if necessary */ |
1400 | #ifndef _WIN32 |
|
|
1401 | if (expect_false (curpid)) /* penalise the forking check even more */ |
|
|
1402 | if (expect_false (getpid () != curpid)) |
|
|
1403 | { |
|
|
1404 | curpid = getpid (); |
|
|
1405 | postfork = 1; |
|
|
1406 | } |
|
|
1407 | #endif |
|
|
1408 | |
1343 | #if EV_FORK_ENABLE |
1409 | #if EV_FORK_ENABLE |
|
|
1410 | /* we might have forked, so queue fork handlers */ |
1344 | if (expect_false (postfork)) |
1411 | if (expect_false (postfork)) |
1345 | if (forkcnt) |
1412 | if (forkcnt) |
1346 | { |
1413 | { |
1347 | queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK); |
1414 | queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK); |
1348 | call_pending (EV_A); |
1415 | call_pending (EV_A); |
1349 | } |
1416 | } |
1350 | #endif |
1417 | #endif |
1351 | |
1418 | |
1352 | /* queue check watchers (and execute them) */ |
1419 | /* queue prepare watchers (and execute them) */ |
1353 | if (expect_false (preparecnt)) |
1420 | if (expect_false (preparecnt)) |
1354 | { |
1421 | { |
1355 | queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); |
1422 | queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); |
1356 | call_pending (EV_A); |
1423 | call_pending (EV_A); |
1357 | } |
1424 | } |
1358 | |
1425 | |
|
|
1426 | if (expect_false (!activecnt)) |
|
|
1427 | break; |
|
|
1428 | |
1359 | /* we might have forked, so reify kernel state if necessary */ |
1429 | /* we might have forked, so reify kernel state if necessary */ |
1360 | if (expect_false (postfork)) |
1430 | if (expect_false (postfork)) |
1361 | loop_fork (EV_A); |
1431 | loop_fork (EV_A); |
1362 | |
1432 | |
1363 | /* update fd-related kernel structures */ |
1433 | /* update fd-related kernel structures */ |
1364 | fd_reify (EV_A); |
1434 | fd_reify (EV_A); |
1365 | |
1435 | |
1366 | /* calculate blocking time */ |
1436 | /* calculate blocking time */ |
1367 | { |
1437 | { |
1368 | double block; |
1438 | ev_tstamp block; |
1369 | |
1439 | |
1370 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
1440 | if (expect_false (flags & EVLOOP_NONBLOCK || idleall || !activecnt)) |
1371 | block = 0.; /* do not block at all */ |
1441 | block = 0.; /* do not block at all */ |
1372 | else |
1442 | else |
1373 | { |
1443 | { |
1374 | /* update time to cancel out callback processing overhead */ |
1444 | /* update time to cancel out callback processing overhead */ |
1375 | #if EV_USE_MONOTONIC |
1445 | #if EV_USE_MONOTONIC |
… | |
… | |
1399 | #endif |
1469 | #endif |
1400 | |
1470 | |
1401 | if (expect_false (block < 0.)) block = 0.; |
1471 | if (expect_false (block < 0.)) block = 0.; |
1402 | } |
1472 | } |
1403 | |
1473 | |
|
|
1474 | ++loop_count; |
1404 | backend_poll (EV_A_ block); |
1475 | backend_poll (EV_A_ block); |
1405 | } |
1476 | } |
1406 | |
1477 | |
1407 | /* update ev_rt_now, do magic */ |
1478 | /* update ev_rt_now, do magic */ |
1408 | time_update (EV_A); |
1479 | time_update (EV_A); |
… | |
… | |
1411 | timers_reify (EV_A); /* relative timers called last */ |
1482 | timers_reify (EV_A); /* relative timers called last */ |
1412 | #if EV_PERIODIC_ENABLE |
1483 | #if EV_PERIODIC_ENABLE |
1413 | periodics_reify (EV_A); /* absolute timers called first */ |
1484 | periodics_reify (EV_A); /* absolute timers called first */ |
1414 | #endif |
1485 | #endif |
1415 | |
1486 | |
|
|
1487 | #if EV_IDLE_ENABLE |
1416 | /* queue idle watchers unless other events are pending */ |
1488 | /* queue idle watchers unless other events are pending */ |
1417 | if (idlecnt && !any_pending (EV_A)) |
1489 | idle_reify (EV_A); |
1418 | queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); |
1490 | #endif |
1419 | |
1491 | |
1420 | /* queue check watchers, to be executed first */ |
1492 | /* queue check watchers, to be executed first */ |
1421 | if (expect_false (checkcnt)) |
1493 | if (expect_false (checkcnt)) |
1422 | queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); |
1494 | queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); |
1423 | |
1495 | |
1424 | call_pending (EV_A); |
1496 | call_pending (EV_A); |
1425 | |
1497 | |
1426 | if (expect_false (loop_done)) |
|
|
1427 | break; |
|
|
1428 | } |
1498 | } |
|
|
1499 | while (expect_true (activecnt && !loop_done)); |
1429 | |
1500 | |
1430 | if (loop_done == EVUNLOOP_ONE) |
1501 | if (loop_done == EVUNLOOP_ONE) |
1431 | loop_done = EVUNLOOP_CANCEL; |
1502 | loop_done = EVUNLOOP_CANCEL; |
1432 | } |
1503 | } |
1433 | |
1504 | |
… | |
… | |
1460 | head = &(*head)->next; |
1531 | head = &(*head)->next; |
1461 | } |
1532 | } |
1462 | } |
1533 | } |
1463 | |
1534 | |
1464 | void inline_speed |
1535 | void inline_speed |
1465 | ev_clear_pending (EV_P_ W w) |
1536 | clear_pending (EV_P_ W w) |
1466 | { |
1537 | { |
1467 | if (w->pending) |
1538 | if (w->pending) |
1468 | { |
1539 | { |
1469 | pendings [ABSPRI (w)][w->pending - 1].w = 0; |
1540 | pendings [ABSPRI (w)][w->pending - 1].w = 0; |
1470 | w->pending = 0; |
1541 | w->pending = 0; |
1471 | } |
1542 | } |
1472 | } |
1543 | } |
1473 | |
1544 | |
|
|
1545 | int |
|
|
1546 | ev_clear_pending (EV_P_ void *w) |
|
|
1547 | { |
|
|
1548 | W w_ = (W)w; |
|
|
1549 | int pending = w_->pending; |
|
|
1550 | |
|
|
1551 | if (!pending) |
|
|
1552 | return 0; |
|
|
1553 | |
|
|
1554 | w_->pending = 0; |
|
|
1555 | ANPENDING *p = pendings [ABSPRI (w_)] + pending - 1; |
|
|
1556 | p->w = 0; |
|
|
1557 | |
|
|
1558 | return p->events; |
|
|
1559 | } |
|
|
1560 | |
|
|
1561 | void inline_size |
|
|
1562 | pri_adjust (EV_P_ W w) |
|
|
1563 | { |
|
|
1564 | int pri = w->priority; |
|
|
1565 | pri = pri < EV_MINPRI ? EV_MINPRI : pri; |
|
|
1566 | pri = pri > EV_MAXPRI ? EV_MAXPRI : pri; |
|
|
1567 | w->priority = pri; |
|
|
1568 | } |
|
|
1569 | |
1474 | void inline_speed |
1570 | void inline_speed |
1475 | ev_start (EV_P_ W w, int active) |
1571 | ev_start (EV_P_ W w, int active) |
1476 | { |
1572 | { |
1477 | if (w->priority < EV_MINPRI) w->priority = EV_MINPRI; |
1573 | pri_adjust (EV_A_ w); |
1478 | if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI; |
|
|
1479 | |
|
|
1480 | w->active = active; |
1574 | w->active = active; |
1481 | ev_ref (EV_A); |
1575 | ev_ref (EV_A); |
1482 | } |
1576 | } |
1483 | |
1577 | |
1484 | void inline_size |
1578 | void inline_size |
… | |
… | |
1488 | w->active = 0; |
1582 | w->active = 0; |
1489 | } |
1583 | } |
1490 | |
1584 | |
1491 | /*****************************************************************************/ |
1585 | /*****************************************************************************/ |
1492 | |
1586 | |
1493 | void |
1587 | void noinline |
1494 | ev_io_start (EV_P_ ev_io *w) |
1588 | ev_io_start (EV_P_ ev_io *w) |
1495 | { |
1589 | { |
1496 | int fd = w->fd; |
1590 | int fd = w->fd; |
1497 | |
1591 | |
1498 | if (expect_false (ev_is_active (w))) |
1592 | if (expect_false (ev_is_active (w))) |
… | |
… | |
1505 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
1599 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
1506 | |
1600 | |
1507 | fd_change (EV_A_ fd); |
1601 | fd_change (EV_A_ fd); |
1508 | } |
1602 | } |
1509 | |
1603 | |
1510 | void |
1604 | void noinline |
1511 | ev_io_stop (EV_P_ ev_io *w) |
1605 | ev_io_stop (EV_P_ ev_io *w) |
1512 | { |
1606 | { |
1513 | ev_clear_pending (EV_A_ (W)w); |
1607 | clear_pending (EV_A_ (W)w); |
1514 | if (expect_false (!ev_is_active (w))) |
1608 | if (expect_false (!ev_is_active (w))) |
1515 | return; |
1609 | return; |
1516 | |
1610 | |
1517 | assert (("ev_io_start called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax)); |
1611 | assert (("ev_io_start called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax)); |
1518 | |
1612 | |
… | |
… | |
1520 | ev_stop (EV_A_ (W)w); |
1614 | ev_stop (EV_A_ (W)w); |
1521 | |
1615 | |
1522 | fd_change (EV_A_ w->fd); |
1616 | fd_change (EV_A_ w->fd); |
1523 | } |
1617 | } |
1524 | |
1618 | |
1525 | void |
1619 | void noinline |
1526 | ev_timer_start (EV_P_ ev_timer *w) |
1620 | ev_timer_start (EV_P_ ev_timer *w) |
1527 | { |
1621 | { |
1528 | if (expect_false (ev_is_active (w))) |
1622 | if (expect_false (ev_is_active (w))) |
1529 | return; |
1623 | return; |
1530 | |
1624 | |
… | |
… | |
1538 | upheap ((WT *)timers, timercnt - 1); |
1632 | upheap ((WT *)timers, timercnt - 1); |
1539 | |
1633 | |
1540 | /*assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));*/ |
1634 | /*assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));*/ |
1541 | } |
1635 | } |
1542 | |
1636 | |
1543 | void |
1637 | void noinline |
1544 | ev_timer_stop (EV_P_ ev_timer *w) |
1638 | ev_timer_stop (EV_P_ ev_timer *w) |
1545 | { |
1639 | { |
1546 | ev_clear_pending (EV_A_ (W)w); |
1640 | clear_pending (EV_A_ (W)w); |
1547 | if (expect_false (!ev_is_active (w))) |
1641 | if (expect_false (!ev_is_active (w))) |
1548 | return; |
1642 | return; |
1549 | |
1643 | |
1550 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1644 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1551 | |
1645 | |
… | |
… | |
1562 | ((WT)w)->at -= mn_now; |
1656 | ((WT)w)->at -= mn_now; |
1563 | |
1657 | |
1564 | ev_stop (EV_A_ (W)w); |
1658 | ev_stop (EV_A_ (W)w); |
1565 | } |
1659 | } |
1566 | |
1660 | |
1567 | void |
1661 | void noinline |
1568 | ev_timer_again (EV_P_ ev_timer *w) |
1662 | ev_timer_again (EV_P_ ev_timer *w) |
1569 | { |
1663 | { |
1570 | if (ev_is_active (w)) |
1664 | if (ev_is_active (w)) |
1571 | { |
1665 | { |
1572 | if (w->repeat) |
1666 | if (w->repeat) |
… | |
… | |
1583 | ev_timer_start (EV_A_ w); |
1677 | ev_timer_start (EV_A_ w); |
1584 | } |
1678 | } |
1585 | } |
1679 | } |
1586 | |
1680 | |
1587 | #if EV_PERIODIC_ENABLE |
1681 | #if EV_PERIODIC_ENABLE |
1588 | void |
1682 | void noinline |
1589 | ev_periodic_start (EV_P_ ev_periodic *w) |
1683 | ev_periodic_start (EV_P_ ev_periodic *w) |
1590 | { |
1684 | { |
1591 | if (expect_false (ev_is_active (w))) |
1685 | if (expect_false (ev_is_active (w))) |
1592 | return; |
1686 | return; |
1593 | |
1687 | |
… | |
… | |
1606 | upheap ((WT *)periodics, periodiccnt - 1); |
1700 | upheap ((WT *)periodics, periodiccnt - 1); |
1607 | |
1701 | |
1608 | /*assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));*/ |
1702 | /*assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));*/ |
1609 | } |
1703 | } |
1610 | |
1704 | |
1611 | void |
1705 | void noinline |
1612 | ev_periodic_stop (EV_P_ ev_periodic *w) |
1706 | ev_periodic_stop (EV_P_ ev_periodic *w) |
1613 | { |
1707 | { |
1614 | ev_clear_pending (EV_A_ (W)w); |
1708 | clear_pending (EV_A_ (W)w); |
1615 | if (expect_false (!ev_is_active (w))) |
1709 | if (expect_false (!ev_is_active (w))) |
1616 | return; |
1710 | return; |
1617 | |
1711 | |
1618 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1712 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1619 | |
1713 | |
… | |
… | |
1628 | } |
1722 | } |
1629 | |
1723 | |
1630 | ev_stop (EV_A_ (W)w); |
1724 | ev_stop (EV_A_ (W)w); |
1631 | } |
1725 | } |
1632 | |
1726 | |
1633 | void |
1727 | void noinline |
1634 | ev_periodic_again (EV_P_ ev_periodic *w) |
1728 | ev_periodic_again (EV_P_ ev_periodic *w) |
1635 | { |
1729 | { |
1636 | /* TODO: use adjustheap and recalculation */ |
1730 | /* TODO: use adjustheap and recalculation */ |
1637 | ev_periodic_stop (EV_A_ w); |
1731 | ev_periodic_stop (EV_A_ w); |
1638 | ev_periodic_start (EV_A_ w); |
1732 | ev_periodic_start (EV_A_ w); |
… | |
… | |
1641 | |
1735 | |
1642 | #ifndef SA_RESTART |
1736 | #ifndef SA_RESTART |
1643 | # define SA_RESTART 0 |
1737 | # define SA_RESTART 0 |
1644 | #endif |
1738 | #endif |
1645 | |
1739 | |
1646 | void |
1740 | void noinline |
1647 | ev_signal_start (EV_P_ ev_signal *w) |
1741 | ev_signal_start (EV_P_ ev_signal *w) |
1648 | { |
1742 | { |
1649 | #if EV_MULTIPLICITY |
1743 | #if EV_MULTIPLICITY |
1650 | assert (("signal watchers are only supported in the default loop", loop == ev_default_loop_ptr)); |
1744 | assert (("signal watchers are only supported in the default loop", loop == ev_default_loop_ptr)); |
1651 | #endif |
1745 | #endif |
… | |
… | |
1670 | sigaction (w->signum, &sa, 0); |
1764 | sigaction (w->signum, &sa, 0); |
1671 | #endif |
1765 | #endif |
1672 | } |
1766 | } |
1673 | } |
1767 | } |
1674 | |
1768 | |
1675 | void |
1769 | void noinline |
1676 | ev_signal_stop (EV_P_ ev_signal *w) |
1770 | ev_signal_stop (EV_P_ ev_signal *w) |
1677 | { |
1771 | { |
1678 | ev_clear_pending (EV_A_ (W)w); |
1772 | clear_pending (EV_A_ (W)w); |
1679 | if (expect_false (!ev_is_active (w))) |
1773 | if (expect_false (!ev_is_active (w))) |
1680 | return; |
1774 | return; |
1681 | |
1775 | |
1682 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
1776 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
1683 | ev_stop (EV_A_ (W)w); |
1777 | ev_stop (EV_A_ (W)w); |
… | |
… | |
1700 | } |
1794 | } |
1701 | |
1795 | |
1702 | void |
1796 | void |
1703 | ev_child_stop (EV_P_ ev_child *w) |
1797 | ev_child_stop (EV_P_ ev_child *w) |
1704 | { |
1798 | { |
1705 | ev_clear_pending (EV_A_ (W)w); |
1799 | clear_pending (EV_A_ (W)w); |
1706 | if (expect_false (!ev_is_active (w))) |
1800 | if (expect_false (!ev_is_active (w))) |
1707 | return; |
1801 | return; |
1708 | |
1802 | |
1709 | wlist_del ((WL *)&childs [w->pid & (EV_PID_HASHSIZE - 1)], (WL)w); |
1803 | wlist_del ((WL *)&childs [w->pid & (EV_PID_HASHSIZE - 1)], (WL)w); |
1710 | ev_stop (EV_A_ (W)w); |
1804 | ev_stop (EV_A_ (W)w); |
… | |
… | |
1718 | # endif |
1812 | # endif |
1719 | |
1813 | |
1720 | #define DEF_STAT_INTERVAL 5.0074891 |
1814 | #define DEF_STAT_INTERVAL 5.0074891 |
1721 | #define MIN_STAT_INTERVAL 0.1074891 |
1815 | #define MIN_STAT_INTERVAL 0.1074891 |
1722 | |
1816 | |
1723 | void noinline stat_timer_cb (EV_P_ ev_timer *w_, int revents); |
1817 | static void noinline stat_timer_cb (EV_P_ ev_timer *w_, int revents); |
1724 | |
1818 | |
1725 | #if EV_USE_INOTIFY |
1819 | #if EV_USE_INOTIFY |
1726 | # define EV_INOTIFY_BUFSIZE 8192 |
1820 | # define EV_INOTIFY_BUFSIZE 8192 |
1727 | |
1821 | |
1728 | static void noinline |
1822 | static void noinline |
… | |
… | |
1879 | w->attr.st_nlink = 0; |
1973 | w->attr.st_nlink = 0; |
1880 | else if (!w->attr.st_nlink) |
1974 | else if (!w->attr.st_nlink) |
1881 | w->attr.st_nlink = 1; |
1975 | w->attr.st_nlink = 1; |
1882 | } |
1976 | } |
1883 | |
1977 | |
1884 | void noinline |
1978 | static void noinline |
1885 | stat_timer_cb (EV_P_ ev_timer *w_, int revents) |
1979 | stat_timer_cb (EV_P_ ev_timer *w_, int revents) |
1886 | { |
1980 | { |
1887 | ev_stat *w = (ev_stat *)(((char *)w_) - offsetof (ev_stat, timer)); |
1981 | ev_stat *w = (ev_stat *)(((char *)w_) - offsetof (ev_stat, timer)); |
1888 | |
1982 | |
1889 | /* we copy this here each the time so that */ |
1983 | /* we copy this here each the time so that */ |
… | |
… | |
1946 | } |
2040 | } |
1947 | |
2041 | |
1948 | void |
2042 | void |
1949 | ev_stat_stop (EV_P_ ev_stat *w) |
2043 | ev_stat_stop (EV_P_ ev_stat *w) |
1950 | { |
2044 | { |
1951 | ev_clear_pending (EV_A_ (W)w); |
2045 | clear_pending (EV_A_ (W)w); |
1952 | if (expect_false (!ev_is_active (w))) |
2046 | if (expect_false (!ev_is_active (w))) |
1953 | return; |
2047 | return; |
1954 | |
2048 | |
1955 | #if EV_USE_INOTIFY |
2049 | #if EV_USE_INOTIFY |
1956 | infy_del (EV_A_ w); |
2050 | infy_del (EV_A_ w); |
… | |
… | |
1959 | |
2053 | |
1960 | ev_stop (EV_A_ (W)w); |
2054 | ev_stop (EV_A_ (W)w); |
1961 | } |
2055 | } |
1962 | #endif |
2056 | #endif |
1963 | |
2057 | |
|
|
2058 | #if EV_IDLE_ENABLE |
1964 | void |
2059 | void |
1965 | ev_idle_start (EV_P_ ev_idle *w) |
2060 | ev_idle_start (EV_P_ ev_idle *w) |
1966 | { |
2061 | { |
1967 | if (expect_false (ev_is_active (w))) |
2062 | if (expect_false (ev_is_active (w))) |
1968 | return; |
2063 | return; |
1969 | |
2064 | |
|
|
2065 | pri_adjust (EV_A_ (W)w); |
|
|
2066 | |
|
|
2067 | { |
|
|
2068 | int active = ++idlecnt [ABSPRI (w)]; |
|
|
2069 | |
|
|
2070 | ++idleall; |
1970 | ev_start (EV_A_ (W)w, ++idlecnt); |
2071 | ev_start (EV_A_ (W)w, active); |
|
|
2072 | |
1971 | array_needsize (ev_idle *, idles, idlemax, idlecnt, EMPTY2); |
2073 | array_needsize (ev_idle *, idles [ABSPRI (w)], idlemax [ABSPRI (w)], active, EMPTY2); |
1972 | idles [idlecnt - 1] = w; |
2074 | idles [ABSPRI (w)][active - 1] = w; |
|
|
2075 | } |
1973 | } |
2076 | } |
1974 | |
2077 | |
1975 | void |
2078 | void |
1976 | ev_idle_stop (EV_P_ ev_idle *w) |
2079 | ev_idle_stop (EV_P_ ev_idle *w) |
1977 | { |
2080 | { |
1978 | ev_clear_pending (EV_A_ (W)w); |
2081 | clear_pending (EV_A_ (W)w); |
1979 | if (expect_false (!ev_is_active (w))) |
2082 | if (expect_false (!ev_is_active (w))) |
1980 | return; |
2083 | return; |
1981 | |
2084 | |
1982 | { |
2085 | { |
1983 | int active = ((W)w)->active; |
2086 | int active = ((W)w)->active; |
1984 | idles [active - 1] = idles [--idlecnt]; |
2087 | |
|
|
2088 | idles [ABSPRI (w)][active - 1] = idles [ABSPRI (w)][--idlecnt [ABSPRI (w)]]; |
1985 | ((W)idles [active - 1])->active = active; |
2089 | ((W)idles [ABSPRI (w)][active - 1])->active = active; |
|
|
2090 | |
|
|
2091 | ev_stop (EV_A_ (W)w); |
|
|
2092 | --idleall; |
1986 | } |
2093 | } |
1987 | |
|
|
1988 | ev_stop (EV_A_ (W)w); |
|
|
1989 | } |
2094 | } |
|
|
2095 | #endif |
1990 | |
2096 | |
1991 | void |
2097 | void |
1992 | ev_prepare_start (EV_P_ ev_prepare *w) |
2098 | ev_prepare_start (EV_P_ ev_prepare *w) |
1993 | { |
2099 | { |
1994 | if (expect_false (ev_is_active (w))) |
2100 | if (expect_false (ev_is_active (w))) |
… | |
… | |
2000 | } |
2106 | } |
2001 | |
2107 | |
2002 | void |
2108 | void |
2003 | ev_prepare_stop (EV_P_ ev_prepare *w) |
2109 | ev_prepare_stop (EV_P_ ev_prepare *w) |
2004 | { |
2110 | { |
2005 | ev_clear_pending (EV_A_ (W)w); |
2111 | clear_pending (EV_A_ (W)w); |
2006 | if (expect_false (!ev_is_active (w))) |
2112 | if (expect_false (!ev_is_active (w))) |
2007 | return; |
2113 | return; |
2008 | |
2114 | |
2009 | { |
2115 | { |
2010 | int active = ((W)w)->active; |
2116 | int active = ((W)w)->active; |
… | |
… | |
2027 | } |
2133 | } |
2028 | |
2134 | |
2029 | void |
2135 | void |
2030 | ev_check_stop (EV_P_ ev_check *w) |
2136 | ev_check_stop (EV_P_ ev_check *w) |
2031 | { |
2137 | { |
2032 | ev_clear_pending (EV_A_ (W)w); |
2138 | clear_pending (EV_A_ (W)w); |
2033 | if (expect_false (!ev_is_active (w))) |
2139 | if (expect_false (!ev_is_active (w))) |
2034 | return; |
2140 | return; |
2035 | |
2141 | |
2036 | { |
2142 | { |
2037 | int active = ((W)w)->active; |
2143 | int active = ((W)w)->active; |
… | |
… | |
2079 | } |
2185 | } |
2080 | |
2186 | |
2081 | void |
2187 | void |
2082 | ev_embed_stop (EV_P_ ev_embed *w) |
2188 | ev_embed_stop (EV_P_ ev_embed *w) |
2083 | { |
2189 | { |
2084 | ev_clear_pending (EV_A_ (W)w); |
2190 | clear_pending (EV_A_ (W)w); |
2085 | if (expect_false (!ev_is_active (w))) |
2191 | if (expect_false (!ev_is_active (w))) |
2086 | return; |
2192 | return; |
2087 | |
2193 | |
2088 | ev_io_stop (EV_A_ &w->io); |
2194 | ev_io_stop (EV_A_ &w->io); |
2089 | |
2195 | |
… | |
… | |
2104 | } |
2210 | } |
2105 | |
2211 | |
2106 | void |
2212 | void |
2107 | ev_fork_stop (EV_P_ ev_fork *w) |
2213 | ev_fork_stop (EV_P_ ev_fork *w) |
2108 | { |
2214 | { |
2109 | ev_clear_pending (EV_A_ (W)w); |
2215 | clear_pending (EV_A_ (W)w); |
2110 | if (expect_false (!ev_is_active (w))) |
2216 | if (expect_false (!ev_is_active (w))) |
2111 | return; |
2217 | return; |
2112 | |
2218 | |
2113 | { |
2219 | { |
2114 | int active = ((W)w)->active; |
2220 | int active = ((W)w)->active; |