… | |
… | |
476 | pendings [pri][w_->pending - 1].w = w_; |
476 | pendings [pri][w_->pending - 1].w = w_; |
477 | pendings [pri][w_->pending - 1].events = revents; |
477 | pendings [pri][w_->pending - 1].events = revents; |
478 | } |
478 | } |
479 | } |
479 | } |
480 | |
480 | |
481 | void inline_size |
481 | void inline_speed |
482 | queue_events (EV_P_ W *events, int eventcnt, int type) |
482 | queue_events (EV_P_ W *events, int eventcnt, int type) |
483 | { |
483 | { |
484 | int i; |
484 | int i; |
485 | |
485 | |
486 | for (i = 0; i < eventcnt; ++i) |
486 | for (i = 0; i < eventcnt; ++i) |
… | |
… | |
638 | void inline_speed |
638 | void inline_speed |
639 | upheap (WT *heap, int k) |
639 | upheap (WT *heap, int k) |
640 | { |
640 | { |
641 | WT w = heap [k]; |
641 | WT w = heap [k]; |
642 | |
642 | |
643 | while (k && heap [k >> 1]->at > w->at) |
643 | while (k) |
644 | { |
644 | { |
|
|
645 | int p = (k - 1) >> 1; |
|
|
646 | |
|
|
647 | if (heap [p]->at <= w->at) |
|
|
648 | break; |
|
|
649 | |
645 | heap [k] = heap [k >> 1]; |
650 | heap [k] = heap [p]; |
646 | ((W)heap [k])->active = k + 1; |
651 | ((W)heap [k])->active = k + 1; |
647 | k >>= 1; |
652 | k = p; |
648 | } |
653 | } |
649 | |
654 | |
650 | heap [k] = w; |
655 | heap [k] = w; |
651 | ((W)heap [k])->active = k + 1; |
656 | ((W)heap [k])->active = k + 1; |
652 | |
657 | |
… | |
… | |
655 | void inline_speed |
660 | void inline_speed |
656 | downheap (WT *heap, int N, int k) |
661 | downheap (WT *heap, int N, int k) |
657 | { |
662 | { |
658 | WT w = heap [k]; |
663 | WT w = heap [k]; |
659 | |
664 | |
660 | while (k < (N >> 1)) |
665 | for (;;) |
661 | { |
666 | { |
662 | int j = k << 1; |
667 | int c = (k << 1) + 1; |
663 | |
668 | |
664 | if (j + 1 < N && heap [j]->at > heap [j + 1]->at) |
669 | if (c >= N) |
665 | ++j; |
|
|
666 | |
|
|
667 | if (w->at <= heap [j]->at) |
|
|
668 | break; |
670 | break; |
669 | |
671 | |
|
|
672 | c += c + 1 < N && heap [c]->at > heap [c + 1]->at |
|
|
673 | ? 1 : 0; |
|
|
674 | |
|
|
675 | if (w->at <= heap [c]->at) |
|
|
676 | break; |
|
|
677 | |
670 | heap [k] = heap [j]; |
678 | heap [k] = heap [c]; |
671 | ((W)heap [k])->active = k + 1; |
679 | ((W)heap [k])->active = k + 1; |
|
|
680 | |
672 | k = j; |
681 | k = c; |
673 | } |
682 | } |
674 | |
683 | |
675 | heap [k] = w; |
684 | heap [k] = w; |
676 | ((W)heap [k])->active = k + 1; |
685 | ((W)heap [k])->active = k + 1; |
677 | } |
686 | } |
… | |
… | |
1304 | } |
1313 | } |
1305 | } |
1314 | } |
1306 | } |
1315 | } |
1307 | #endif |
1316 | #endif |
1308 | |
1317 | |
1309 | int inline_size |
1318 | void inline_speed |
1310 | time_update_monotonic (EV_P) |
1319 | time_update (EV_P_ ev_tstamp max_block) |
1311 | { |
1320 | { |
|
|
1321 | int i; |
|
|
1322 | |
|
|
1323 | #if EV_USE_MONOTONIC |
|
|
1324 | if (expect_true (have_monotonic)) |
|
|
1325 | { |
|
|
1326 | ev_tstamp odiff = rtmn_diff; |
|
|
1327 | |
1312 | mn_now = get_clock (); |
1328 | mn_now = get_clock (); |
1313 | |
1329 | |
|
|
1330 | /* only fetch the realtime clock every 0.5*MIN_TIMEJUMP seconds */ |
|
|
1331 | /* interpolate in the meantime */ |
1314 | if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) |
1332 | if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) |
1315 | { |
1333 | { |
1316 | ev_rt_now = rtmn_diff + mn_now; |
1334 | ev_rt_now = rtmn_diff + mn_now; |
1317 | return 0; |
1335 | return; |
1318 | } |
1336 | } |
1319 | else |
1337 | |
1320 | { |
|
|
1321 | now_floor = mn_now; |
1338 | now_floor = mn_now; |
1322 | ev_rt_now = ev_time (); |
1339 | ev_rt_now = ev_time (); |
1323 | return 1; |
|
|
1324 | } |
|
|
1325 | } |
|
|
1326 | |
1340 | |
1327 | void inline_size |
1341 | /* loop a few times, before making important decisions. |
1328 | time_update (EV_P) |
1342 | * on the choice of "4": one iteration isn't enough, |
1329 | { |
1343 | * in case we get preempted during the calls to |
1330 | int i; |
1344 | * ev_time and get_clock. a second call is almost guaranteed |
1331 | |
1345 | * to succeed in that case, though. and looping a few more times |
1332 | #if EV_USE_MONOTONIC |
1346 | * doesn't hurt either as we only do this on time-jumps or |
1333 | if (expect_true (have_monotonic)) |
1347 | * in the unlikely event of having been preempted here. |
1334 | { |
1348 | */ |
1335 | if (time_update_monotonic (EV_A)) |
1349 | for (i = 4; --i; ) |
1336 | { |
1350 | { |
1337 | ev_tstamp odiff = rtmn_diff; |
|
|
1338 | |
|
|
1339 | /* loop a few times, before making important decisions. |
|
|
1340 | * on the choice of "4": one iteration isn't enough, |
|
|
1341 | * in case we get preempted during the calls to |
|
|
1342 | * ev_time and get_clock. a second call is almost guaranteed |
|
|
1343 | * to succeed in that case, though. and looping a few more times |
|
|
1344 | * doesn't hurt either as we only do this on time-jumps or |
|
|
1345 | * in the unlikely event of having been preempted here. |
|
|
1346 | */ |
|
|
1347 | for (i = 4; --i; ) |
|
|
1348 | { |
|
|
1349 | rtmn_diff = ev_rt_now - mn_now; |
1351 | rtmn_diff = ev_rt_now - mn_now; |
1350 | |
1352 | |
1351 | if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) |
1353 | if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) |
1352 | return; /* all is well */ |
1354 | return; /* all is well */ |
1353 | |
1355 | |
1354 | ev_rt_now = ev_time (); |
1356 | ev_rt_now = ev_time (); |
1355 | mn_now = get_clock (); |
1357 | mn_now = get_clock (); |
1356 | now_floor = mn_now; |
1358 | now_floor = mn_now; |
1357 | } |
1359 | } |
1358 | |
1360 | |
1359 | # if EV_PERIODIC_ENABLE |
1361 | # if EV_PERIODIC_ENABLE |
1360 | periodics_reschedule (EV_A); |
1362 | periodics_reschedule (EV_A); |
1361 | # endif |
1363 | # endif |
1362 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
1364 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
1363 | /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ |
1365 | /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ |
1364 | } |
|
|
1365 | } |
1366 | } |
1366 | else |
1367 | else |
1367 | #endif |
1368 | #endif |
1368 | { |
1369 | { |
1369 | ev_rt_now = ev_time (); |
1370 | ev_rt_now = ev_time (); |
1370 | |
1371 | |
1371 | if (expect_false (mn_now > ev_rt_now || mn_now < ev_rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
1372 | if (expect_false (mn_now > ev_rt_now || ev_rt_now > mn_now + max_block + MIN_TIMEJUMP)) |
1372 | { |
1373 | { |
1373 | #if EV_PERIODIC_ENABLE |
1374 | #if EV_PERIODIC_ENABLE |
1374 | periodics_reschedule (EV_A); |
1375 | periodics_reschedule (EV_A); |
1375 | #endif |
1376 | #endif |
1376 | /* adjust timers. this is easy, as the offset is the same for all of them */ |
1377 | /* adjust timers. this is easy, as the offset is the same for all of them */ |
… | |
… | |
1450 | if (expect_false (flags & EVLOOP_NONBLOCK || idleall || !activecnt)) |
1451 | if (expect_false (flags & EVLOOP_NONBLOCK || idleall || !activecnt)) |
1451 | block = 0.; /* do not block at all */ |
1452 | block = 0.; /* do not block at all */ |
1452 | else |
1453 | else |
1453 | { |
1454 | { |
1454 | /* update time to cancel out callback processing overhead */ |
1455 | /* update time to cancel out callback processing overhead */ |
1455 | #if EV_USE_MONOTONIC |
|
|
1456 | if (expect_true (have_monotonic)) |
|
|
1457 | time_update_monotonic (EV_A); |
1456 | time_update (EV_A_ 1e100); |
1458 | else |
|
|
1459 | #endif |
|
|
1460 | { |
|
|
1461 | ev_rt_now = ev_time (); |
|
|
1462 | mn_now = ev_rt_now; |
|
|
1463 | } |
|
|
1464 | |
1457 | |
1465 | block = MAX_BLOCKTIME; |
1458 | block = MAX_BLOCKTIME; |
1466 | |
1459 | |
1467 | if (timercnt) |
1460 | if (timercnt) |
1468 | { |
1461 | { |
… | |
… | |
1481 | if (expect_false (block < 0.)) block = 0.; |
1474 | if (expect_false (block < 0.)) block = 0.; |
1482 | } |
1475 | } |
1483 | |
1476 | |
1484 | ++loop_count; |
1477 | ++loop_count; |
1485 | backend_poll (EV_A_ block); |
1478 | backend_poll (EV_A_ block); |
|
|
1479 | |
|
|
1480 | /* update ev_rt_now, do magic */ |
|
|
1481 | time_update (EV_A_ block); |
1486 | } |
1482 | } |
1487 | |
|
|
1488 | /* update ev_rt_now, do magic */ |
|
|
1489 | time_update (EV_A); |
|
|
1490 | |
1483 | |
1491 | /* queue pending timers and reschedule them */ |
1484 | /* queue pending timers and reschedule them */ |
1492 | timers_reify (EV_A); /* relative timers called last */ |
1485 | timers_reify (EV_A); /* relative timers called last */ |
1493 | #if EV_PERIODIC_ENABLE |
1486 | #if EV_PERIODIC_ENABLE |
1494 | periodics_reify (EV_A); /* absolute timers called first */ |
1487 | periodics_reify (EV_A); /* absolute timers called first */ |