… | |
… | |
221 | /* |
221 | /* |
222 | * This is used to avoid floating point rounding problems. |
222 | * This is used to avoid floating point rounding problems. |
223 | * It is added to ev_rt_now when scheduling periodics |
223 | * It is added to ev_rt_now when scheduling periodics |
224 | * to ensure progress, time-wise, even when rounding |
224 | * to ensure progress, time-wise, even when rounding |
225 | * errors are against us. |
225 | * errors are against us. |
226 | * This value is good at least till the year 4000 |
226 | * This value is good at least till the year 4000. |
227 | * and intervals up to 20 years. |
|
|
228 | * Better solutions welcome. |
227 | * Better solutions welcome. |
229 | */ |
228 | */ |
230 | #define TIME_EPSILON 0.0001220703125 /* 1/8192 */ |
229 | #define TIME_EPSILON 0.0001220703125 /* 1/8192 */ |
231 | |
230 | |
232 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
231 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
… | |
… | |
1248 | assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now)); |
1247 | assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now)); |
1249 | downheap ((WT *)periodics, periodiccnt, 0); |
1248 | downheap ((WT *)periodics, periodiccnt, 0); |
1250 | } |
1249 | } |
1251 | else if (w->interval) |
1250 | else if (w->interval) |
1252 | { |
1251 | { |
1253 | ((WT)w)->at = w->offset + floor ((ev_rt_now + TIME_EPSILON - w->offset) / w->interval + 1.) * w->interval; |
1252 | ((WT)w)->at = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; |
|
|
1253 | if (((WT)w)->at - ev_rt_now <= TIME_EPSILON) ((WT)w)->at += w->interval; |
1254 | 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)); |
1255 | downheap ((WT *)periodics, periodiccnt, 0); |
1255 | downheap ((WT *)periodics, periodiccnt, 0); |
1256 | } |
1256 | } |
1257 | else |
1257 | else |
1258 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
1258 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
… | |
… | |
1304 | } |
1304 | } |
1305 | } |
1305 | } |
1306 | } |
1306 | } |
1307 | #endif |
1307 | #endif |
1308 | |
1308 | |
1309 | int inline_size |
1309 | void inline_speed |
1310 | time_update_monotonic (EV_P) |
1310 | time_update (EV_P_ ev_tstamp max_block) |
1311 | { |
1311 | { |
|
|
1312 | int i; |
|
|
1313 | |
|
|
1314 | #if EV_USE_MONOTONIC |
|
|
1315 | if (expect_true (have_monotonic)) |
|
|
1316 | { |
|
|
1317 | ev_tstamp odiff = rtmn_diff; |
|
|
1318 | |
1312 | mn_now = get_clock (); |
1319 | mn_now = get_clock (); |
1313 | |
1320 | |
|
|
1321 | /* only fetch the realtime clock every 0.5*MIN_TIMEJUMP seconds */ |
|
|
1322 | /* interpolate in the meantime */ |
1314 | if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) |
1323 | if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) |
1315 | { |
1324 | { |
1316 | ev_rt_now = rtmn_diff + mn_now; |
1325 | ev_rt_now = rtmn_diff + mn_now; |
1317 | return 0; |
1326 | return; |
1318 | } |
1327 | } |
1319 | else |
1328 | |
1320 | { |
|
|
1321 | now_floor = mn_now; |
1329 | now_floor = mn_now; |
1322 | ev_rt_now = ev_time (); |
1330 | ev_rt_now = ev_time (); |
1323 | return 1; |
|
|
1324 | } |
|
|
1325 | } |
|
|
1326 | |
1331 | |
1327 | void inline_size |
1332 | /* loop a few times, before making important decisions. |
1328 | time_update (EV_P) |
1333 | * on the choice of "4": one iteration isn't enough, |
1329 | { |
1334 | * in case we get preempted during the calls to |
1330 | int i; |
1335 | * ev_time and get_clock. a second call is almost guaranteed |
1331 | |
1336 | * to succeed in that case, though. and looping a few more times |
1332 | #if EV_USE_MONOTONIC |
1337 | * doesn't hurt either as we only do this on time-jumps or |
1333 | if (expect_true (have_monotonic)) |
1338 | * in the unlikely event of having been preempted here. |
1334 | { |
1339 | */ |
1335 | if (time_update_monotonic (EV_A)) |
1340 | for (i = 4; --i; ) |
1336 | { |
1341 | { |
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; |
1342 | rtmn_diff = ev_rt_now - mn_now; |
1350 | |
1343 | |
1351 | if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) |
1344 | if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) |
1352 | return; /* all is well */ |
1345 | return; /* all is well */ |
1353 | |
1346 | |
1354 | ev_rt_now = ev_time (); |
1347 | ev_rt_now = ev_time (); |
1355 | mn_now = get_clock (); |
1348 | mn_now = get_clock (); |
1356 | now_floor = mn_now; |
1349 | now_floor = mn_now; |
1357 | } |
1350 | } |
1358 | |
1351 | |
1359 | # if EV_PERIODIC_ENABLE |
1352 | # if EV_PERIODIC_ENABLE |
1360 | periodics_reschedule (EV_A); |
1353 | periodics_reschedule (EV_A); |
1361 | # endif |
1354 | # endif |
1362 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
1355 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
1363 | /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ |
1356 | /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ |
1364 | } |
|
|
1365 | } |
1357 | } |
1366 | else |
1358 | else |
1367 | #endif |
1359 | #endif |
1368 | { |
1360 | { |
1369 | ev_rt_now = ev_time (); |
1361 | ev_rt_now = ev_time (); |
1370 | |
1362 | |
1371 | if (expect_false (mn_now > ev_rt_now || mn_now < ev_rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
1363 | if (expect_false (mn_now > ev_rt_now || ev_rt_now > mn_now + max_block + MIN_TIMEJUMP)) |
1372 | { |
1364 | { |
1373 | #if EV_PERIODIC_ENABLE |
1365 | #if EV_PERIODIC_ENABLE |
1374 | periodics_reschedule (EV_A); |
1366 | periodics_reschedule (EV_A); |
1375 | #endif |
1367 | #endif |
1376 | |
|
|
1377 | /* adjust timers. this is easy, as the offset is the same for all of them */ |
1368 | /* adjust timers. this is easy, as the offset is the same for all of them */ |
1378 | for (i = 0; i < timercnt; ++i) |
1369 | for (i = 0; i < timercnt; ++i) |
1379 | ((WT)timers [i])->at += ev_rt_now - mn_now; |
1370 | ((WT)timers [i])->at += ev_rt_now - mn_now; |
1380 | } |
1371 | } |
1381 | |
1372 | |
… | |
… | |
1451 | if (expect_false (flags & EVLOOP_NONBLOCK || idleall || !activecnt)) |
1442 | if (expect_false (flags & EVLOOP_NONBLOCK || idleall || !activecnt)) |
1452 | block = 0.; /* do not block at all */ |
1443 | block = 0.; /* do not block at all */ |
1453 | else |
1444 | else |
1454 | { |
1445 | { |
1455 | /* update time to cancel out callback processing overhead */ |
1446 | /* update time to cancel out callback processing overhead */ |
1456 | #if EV_USE_MONOTONIC |
|
|
1457 | if (expect_true (have_monotonic)) |
|
|
1458 | time_update_monotonic (EV_A); |
1447 | time_update (EV_A_ 1e100); |
1459 | else |
|
|
1460 | #endif |
|
|
1461 | { |
|
|
1462 | ev_rt_now = ev_time (); |
|
|
1463 | mn_now = ev_rt_now; |
|
|
1464 | } |
|
|
1465 | |
1448 | |
1466 | block = MAX_BLOCKTIME; |
1449 | block = MAX_BLOCKTIME; |
1467 | |
1450 | |
1468 | if (timercnt) |
1451 | if (timercnt) |
1469 | { |
1452 | { |
… | |
… | |
1482 | if (expect_false (block < 0.)) block = 0.; |
1465 | if (expect_false (block < 0.)) block = 0.; |
1483 | } |
1466 | } |
1484 | |
1467 | |
1485 | ++loop_count; |
1468 | ++loop_count; |
1486 | backend_poll (EV_A_ block); |
1469 | backend_poll (EV_A_ block); |
|
|
1470 | |
|
|
1471 | /* update ev_rt_now, do magic */ |
|
|
1472 | time_update (EV_A_ block); |
1487 | } |
1473 | } |
1488 | |
|
|
1489 | /* update ev_rt_now, do magic */ |
|
|
1490 | time_update (EV_A); |
|
|
1491 | |
1474 | |
1492 | /* queue pending timers and reschedule them */ |
1475 | /* queue pending timers and reschedule them */ |
1493 | timers_reify (EV_A); /* relative timers called last */ |
1476 | timers_reify (EV_A); /* relative timers called last */ |
1494 | #if EV_PERIODIC_ENABLE |
1477 | #if EV_PERIODIC_ENABLE |
1495 | periodics_reify (EV_A); /* absolute timers called first */ |
1478 | periodics_reify (EV_A); /* absolute timers called first */ |