ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libev/ev.c
(Generate patch)

Comparing libev/ev.c (file contents):
Revision 1.176 by root, Tue Dec 11 04:31:55 2007 UTC vs.
Revision 1.179 by root, Tue Dec 11 21:04:40 2007 UTC

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) */
477 pendings [pri][w_->pending - 1].w = w_; 476 pendings [pri][w_->pending - 1].w = w_;
478 pendings [pri][w_->pending - 1].events = revents; 477 pendings [pri][w_->pending - 1].events = revents;
479 } 478 }
480} 479}
481 480
482void inline_size 481void inline_speed
483queue_events (EV_P_ W *events, int eventcnt, int type) 482queue_events (EV_P_ W *events, int eventcnt, int type)
484{ 483{
485 int i; 484 int i;
486 485
487 for (i = 0; i < eventcnt; ++i) 486 for (i = 0; i < eventcnt; ++i)
639void inline_speed 638void inline_speed
640upheap (WT *heap, int k) 639upheap (WT *heap, int k)
641{ 640{
642 WT w = heap [k]; 641 WT w = heap [k];
643 642
644 while (k && heap [k >> 1]->at > w->at) 643 while (k)
645 { 644 {
645 int p = (k - 1) >> 1;
646
647 if (heap [p]->at <= w->at)
648 break;
649
646 heap [k] = heap [k >> 1]; 650 heap [k] = heap [p];
647 ((W)heap [k])->active = k + 1; 651 ((W)heap [k])->active = k + 1;
648 k >>= 1; 652 k = p;
649 } 653 }
650 654
651 heap [k] = w; 655 heap [k] = w;
652 ((W)heap [k])->active = k + 1; 656 ((W)heap [k])->active = k + 1;
653 657
656void inline_speed 660void inline_speed
657downheap (WT *heap, int N, int k) 661downheap (WT *heap, int N, int k)
658{ 662{
659 WT w = heap [k]; 663 WT w = heap [k];
660 664
661 while (k < (N >> 1)) 665 for (;;)
662 { 666 {
663 int j = k << 1; 667 int c = (k << 1) + 1;
664 668
665 if (j + 1 < N && heap [j]->at > heap [j + 1]->at) 669 if (c >= N)
666 ++j;
667
668 if (w->at <= heap [j]->at)
669 break; 670 break;
670 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
671 heap [k] = heap [j]; 678 heap [k] = heap [c];
672 ((W)heap [k])->active = k + 1; 679 ((W)heap [k])->active = k + 1;
680
673 k = j; 681 k = c;
674 } 682 }
675 683
676 heap [k] = w; 684 heap [k] = w;
677 ((W)heap [k])->active = k + 1; 685 ((W)heap [k])->active = k + 1;
678} 686}
1248 assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now)); 1256 assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now));
1249 downheap ((WT *)periodics, periodiccnt, 0); 1257 downheap ((WT *)periodics, periodiccnt, 0);
1250 } 1258 }
1251 else if (w->interval) 1259 else if (w->interval)
1252 { 1260 {
1253 ((WT)w)->at = w->offset + floor ((ev_rt_now + TIME_EPSILON - w->offset) / w->interval + 1.) * w->interval; 1261 ((WT)w)->at = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval;
1262 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)); 1263 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); 1264 downheap ((WT *)periodics, periodiccnt, 0);
1256 } 1265 }
1257 else 1266 else
1258 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ 1267 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
1304 } 1313 }
1305 } 1314 }
1306} 1315}
1307#endif 1316#endif
1308 1317
1309int inline_size 1318void inline_speed
1310time_update_monotonic (EV_P) 1319time_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
1327void inline_size 1341 /* loop a few times, before making important decisions.
1328time_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
1377 /* 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 */
1378 for (i = 0; i < timercnt; ++i) 1378 for (i = 0; i < timercnt; ++i)
1379 ((WT)timers [i])->at += ev_rt_now - mn_now; 1379 ((WT)timers [i])->at += ev_rt_now - mn_now;
1380 } 1380 }
1381 1381
1451 if (expect_false (flags & EVLOOP_NONBLOCK || idleall || !activecnt)) 1451 if (expect_false (flags & EVLOOP_NONBLOCK || idleall || !activecnt))
1452 block = 0.; /* do not block at all */ 1452 block = 0.; /* do not block at all */
1453 else 1453 else
1454 { 1454 {
1455 /* update time to cancel out callback processing overhead */ 1455 /* 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); 1456 time_update (EV_A_ 1e100);
1459 else
1460#endif
1461 {
1462 ev_rt_now = ev_time ();
1463 mn_now = ev_rt_now;
1464 }
1465 1457
1466 block = MAX_BLOCKTIME; 1458 block = MAX_BLOCKTIME;
1467 1459
1468 if (timercnt) 1460 if (timercnt)
1469 { 1461 {
1482 if (expect_false (block < 0.)) block = 0.; 1474 if (expect_false (block < 0.)) block = 0.;
1483 } 1475 }
1484 1476
1485 ++loop_count; 1477 ++loop_count;
1486 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);
1487 } 1482 }
1488
1489 /* update ev_rt_now, do magic */
1490 time_update (EV_A);
1491 1483
1492 /* queue pending timers and reschedule them */ 1484 /* queue pending timers and reschedule them */
1493 timers_reify (EV_A); /* relative timers called last */ 1485 timers_reify (EV_A); /* relative timers called last */
1494#if EV_PERIODIC_ENABLE 1486#if EV_PERIODIC_ENABLE
1495 periodics_reify (EV_A); /* absolute timers called first */ 1487 periodics_reify (EV_A); /* absolute timers called first */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines