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.181 by root, Wed Dec 12 00:17:08 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
654} 657}
655 658
656void inline_speed 659void inline_speed
657downheap (WT *heap, int N, int k) 660downheap (WT *heap, int N, int k)
658{ 661{
659 WT w = heap [k]; 662 WT w = heap [k];
660 663
661 while (k < (N >> 1)) 664 for (;;)
662 { 665 {
663 int j = k << 1; 666 int c = (k << 1) + 1;
664 667
665 if (j + 1 < N && heap [j]->at > heap [j + 1]->at) 668 if (c >= N)
666 ++j;
667
668 if (w->at <= heap [j]->at)
669 break; 669 break;
670 670
671 c += c + 1 < N && heap [c]->at > heap [c + 1]->at
672 ? 1 : 0;
673
674 if (w->at <= heap [c]->at)
675 break;
676
671 heap [k] = heap [j]; 677 heap [k] = heap [c];
672 ((W)heap [k])->active = k + 1; 678 ((W)heap [k])->active = k + 1;
679
673 k = j; 680 k = c;
674 } 681 }
675 682
676 heap [k] = w; 683 heap [k] = w;
677 ((W)heap [k])->active = k + 1; 684 ((W)heap [k])->active = k + 1;
678} 685}
1207void inline_size 1214void inline_size
1208timers_reify (EV_P) 1215timers_reify (EV_P)
1209{ 1216{
1210 while (timercnt && ((WT)timers [0])->at <= mn_now) 1217 while (timercnt && ((WT)timers [0])->at <= mn_now)
1211 { 1218 {
1212 ev_timer *w = timers [0]; 1219 ev_timer *w = (ev_timer *)timers [0];
1213 1220
1214 /*assert (("inactive timer on timer heap detected", ev_is_active (w)));*/ 1221 /*assert (("inactive timer on timer heap detected", ev_is_active (w)));*/
1215 1222
1216 /* first reschedule or stop timer */ 1223 /* first reschedule or stop timer */
1217 if (w->repeat) 1224 if (w->repeat)
1220 1227
1221 ((WT)w)->at += w->repeat; 1228 ((WT)w)->at += w->repeat;
1222 if (((WT)w)->at < mn_now) 1229 if (((WT)w)->at < mn_now)
1223 ((WT)w)->at = mn_now; 1230 ((WT)w)->at = mn_now;
1224 1231
1225 downheap ((WT *)timers, timercnt, 0); 1232 downheap (timers, timercnt, 0);
1226 } 1233 }
1227 else 1234 else
1228 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ 1235 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
1229 1236
1230 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); 1237 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT);
1235void inline_size 1242void inline_size
1236periodics_reify (EV_P) 1243periodics_reify (EV_P)
1237{ 1244{
1238 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) 1245 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now)
1239 { 1246 {
1240 ev_periodic *w = periodics [0]; 1247 ev_periodic *w = (ev_periodic *)periodics [0];
1241 1248
1242 /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/ 1249 /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/
1243 1250
1244 /* first reschedule or stop timer */ 1251 /* first reschedule or stop timer */
1245 if (w->reschedule_cb) 1252 if (w->reschedule_cb)
1246 { 1253 {
1247 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + TIME_EPSILON); 1254 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + TIME_EPSILON);
1248 assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now)); 1255 assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now));
1249 downheap ((WT *)periodics, periodiccnt, 0); 1256 downheap (periodics, periodiccnt, 0);
1250 } 1257 }
1251 else if (w->interval) 1258 else if (w->interval)
1252 { 1259 {
1253 ((WT)w)->at = w->offset + floor ((ev_rt_now + TIME_EPSILON - w->offset) / w->interval + 1.) * w->interval; 1260 ((WT)w)->at = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval;
1261 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)); 1262 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); 1263 downheap (periodics, periodiccnt, 0);
1256 } 1264 }
1257 else 1265 else
1258 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ 1266 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
1259 1267
1260 ev_feed_event (EV_A_ (W)w, EV_PERIODIC); 1268 ev_feed_event (EV_A_ (W)w, EV_PERIODIC);
1267 int i; 1275 int i;
1268 1276
1269 /* adjust periodics after time jump */ 1277 /* adjust periodics after time jump */
1270 for (i = 0; i < periodiccnt; ++i) 1278 for (i = 0; i < periodiccnt; ++i)
1271 { 1279 {
1272 ev_periodic *w = periodics [i]; 1280 ev_periodic *w = (ev_periodic *)periodics [i];
1273 1281
1274 if (w->reschedule_cb) 1282 if (w->reschedule_cb)
1275 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); 1283 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
1276 else if (w->interval) 1284 else if (w->interval)
1277 ((WT)w)->at = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; 1285 ((WT)w)->at = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval;
1278 } 1286 }
1279 1287
1280 /* now rebuild the heap */ 1288 /* now rebuild the heap */
1281 for (i = periodiccnt >> 1; i--; ) 1289 for (i = periodiccnt >> 1; i--; )
1282 downheap ((WT *)periodics, periodiccnt, i); 1290 downheap (periodics, periodiccnt, i);
1283} 1291}
1284#endif 1292#endif
1285 1293
1286#if EV_IDLE_ENABLE 1294#if EV_IDLE_ENABLE
1287void inline_size 1295void inline_size
1304 } 1312 }
1305 } 1313 }
1306} 1314}
1307#endif 1315#endif
1308 1316
1309int inline_size 1317void inline_speed
1310time_update_monotonic (EV_P) 1318time_update (EV_P_ ev_tstamp max_block)
1311{ 1319{
1320 int i;
1321
1322#if EV_USE_MONOTONIC
1323 if (expect_true (have_monotonic))
1324 {
1325 ev_tstamp odiff = rtmn_diff;
1326
1312 mn_now = get_clock (); 1327 mn_now = get_clock ();
1313 1328
1329 /* only fetch the realtime clock every 0.5*MIN_TIMEJUMP seconds */
1330 /* interpolate in the meantime */
1314 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) 1331 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5))
1315 { 1332 {
1316 ev_rt_now = rtmn_diff + mn_now; 1333 ev_rt_now = rtmn_diff + mn_now;
1317 return 0; 1334 return;
1318 } 1335 }
1319 else 1336
1320 {
1321 now_floor = mn_now; 1337 now_floor = mn_now;
1322 ev_rt_now = ev_time (); 1338 ev_rt_now = ev_time ();
1323 return 1;
1324 }
1325}
1326 1339
1327void inline_size 1340 /* loop a few times, before making important decisions.
1328time_update (EV_P) 1341 * on the choice of "4": one iteration isn't enough,
1329{ 1342 * in case we get preempted during the calls to
1330 int i; 1343 * ev_time and get_clock. a second call is almost guaranteed
1331 1344 * to succeed in that case, though. and looping a few more times
1332#if EV_USE_MONOTONIC 1345 * doesn't hurt either as we only do this on time-jumps or
1333 if (expect_true (have_monotonic)) 1346 * in the unlikely event of having been preempted here.
1334 { 1347 */
1335 if (time_update_monotonic (EV_A)) 1348 for (i = 4; --i; )
1336 { 1349 {
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; 1350 rtmn_diff = ev_rt_now - mn_now;
1350 1351
1351 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) 1352 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP)
1352 return; /* all is well */ 1353 return; /* all is well */
1353 1354
1354 ev_rt_now = ev_time (); 1355 ev_rt_now = ev_time ();
1355 mn_now = get_clock (); 1356 mn_now = get_clock ();
1356 now_floor = mn_now; 1357 now_floor = mn_now;
1357 } 1358 }
1358 1359
1359# if EV_PERIODIC_ENABLE 1360# if EV_PERIODIC_ENABLE
1360 periodics_reschedule (EV_A); 1361 periodics_reschedule (EV_A);
1361# endif 1362# endif
1362 /* no timer adjustment, as the monotonic clock doesn't jump */ 1363 /* no timer adjustment, as the monotonic clock doesn't jump */
1363 /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ 1364 /* timers_reschedule (EV_A_ rtmn_diff - odiff) */
1364 }
1365 } 1365 }
1366 else 1366 else
1367#endif 1367#endif
1368 { 1368 {
1369 ev_rt_now = ev_time (); 1369 ev_rt_now = ev_time ();
1370 1370
1371 if (expect_false (mn_now > ev_rt_now || mn_now < ev_rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) 1371 if (expect_false (mn_now > ev_rt_now || ev_rt_now > mn_now + max_block + MIN_TIMEJUMP))
1372 { 1372 {
1373#if EV_PERIODIC_ENABLE 1373#if EV_PERIODIC_ENABLE
1374 periodics_reschedule (EV_A); 1374 periodics_reschedule (EV_A);
1375#endif 1375#endif
1376
1377 /* adjust timers. this is easy, as the offset is the same for all of them */ 1376 /* adjust timers. this is easy, as the offset is the same for all of them */
1378 for (i = 0; i < timercnt; ++i) 1377 for (i = 0; i < timercnt; ++i)
1379 ((WT)timers [i])->at += ev_rt_now - mn_now; 1378 ((WT)timers [i])->at += ev_rt_now - mn_now;
1380 } 1379 }
1381 1380
1451 if (expect_false (flags & EVLOOP_NONBLOCK || idleall || !activecnt)) 1450 if (expect_false (flags & EVLOOP_NONBLOCK || idleall || !activecnt))
1452 block = 0.; /* do not block at all */ 1451 block = 0.; /* do not block at all */
1453 else 1452 else
1454 { 1453 {
1455 /* update time to cancel out callback processing overhead */ 1454 /* 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); 1455 time_update (EV_A_ 1e100);
1459 else
1460#endif
1461 {
1462 ev_rt_now = ev_time ();
1463 mn_now = ev_rt_now;
1464 }
1465 1456
1466 block = MAX_BLOCKTIME; 1457 block = MAX_BLOCKTIME;
1467 1458
1468 if (timercnt) 1459 if (timercnt)
1469 { 1460 {
1482 if (expect_false (block < 0.)) block = 0.; 1473 if (expect_false (block < 0.)) block = 0.;
1483 } 1474 }
1484 1475
1485 ++loop_count; 1476 ++loop_count;
1486 backend_poll (EV_A_ block); 1477 backend_poll (EV_A_ block);
1478
1479 /* update ev_rt_now, do magic */
1480 time_update (EV_A_ block);
1487 } 1481 }
1488
1489 /* update ev_rt_now, do magic */
1490 time_update (EV_A);
1491 1482
1492 /* queue pending timers and reschedule them */ 1483 /* queue pending timers and reschedule them */
1493 timers_reify (EV_A); /* relative timers called last */ 1484 timers_reify (EV_A); /* relative timers called last */
1494#if EV_PERIODIC_ENABLE 1485#if EV_PERIODIC_ENABLE
1495 periodics_reify (EV_A); /* absolute timers called first */ 1486 periodics_reify (EV_A); /* absolute timers called first */
1637 ((WT)w)->at += mn_now; 1628 ((WT)w)->at += mn_now;
1638 1629
1639 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); 1630 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
1640 1631
1641 ev_start (EV_A_ (W)w, ++timercnt); 1632 ev_start (EV_A_ (W)w, ++timercnt);
1642 array_needsize (ev_timer *, timers, timermax, timercnt, EMPTY2); 1633 array_needsize (WT, timers, timermax, timercnt, EMPTY2);
1643 timers [timercnt - 1] = w; 1634 timers [timercnt - 1] = (WT)w;
1644 upheap ((WT *)timers, timercnt - 1); 1635 upheap (timers, timercnt - 1);
1645 1636
1646 /*assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));*/ 1637 /*assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));*/
1647} 1638}
1648 1639
1649void noinline 1640void noinline
1651{ 1642{
1652 clear_pending (EV_A_ (W)w); 1643 clear_pending (EV_A_ (W)w);
1653 if (expect_false (!ev_is_active (w))) 1644 if (expect_false (!ev_is_active (w)))
1654 return; 1645 return;
1655 1646
1656 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1647 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == (WT)w));
1657 1648
1658 { 1649 {
1659 int active = ((W)w)->active; 1650 int active = ((W)w)->active;
1660 1651
1661 if (expect_true (--active < --timercnt)) 1652 if (expect_true (--active < --timercnt))
1662 { 1653 {
1663 timers [active] = timers [timercnt]; 1654 timers [active] = timers [timercnt];
1664 adjustheap ((WT *)timers, timercnt, active); 1655 adjustheap (timers, timercnt, active);
1665 } 1656 }
1666 } 1657 }
1667 1658
1668 ((WT)w)->at -= mn_now; 1659 ((WT)w)->at -= mn_now;
1669 1660
1676 if (ev_is_active (w)) 1667 if (ev_is_active (w))
1677 { 1668 {
1678 if (w->repeat) 1669 if (w->repeat)
1679 { 1670 {
1680 ((WT)w)->at = mn_now + w->repeat; 1671 ((WT)w)->at = mn_now + w->repeat;
1681 adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1); 1672 adjustheap (timers, timercnt, ((W)w)->active - 1);
1682 } 1673 }
1683 else 1674 else
1684 ev_timer_stop (EV_A_ w); 1675 ev_timer_stop (EV_A_ w);
1685 } 1676 }
1686 else if (w->repeat) 1677 else if (w->repeat)
1707 } 1698 }
1708 else 1699 else
1709 ((WT)w)->at = w->offset; 1700 ((WT)w)->at = w->offset;
1710 1701
1711 ev_start (EV_A_ (W)w, ++periodiccnt); 1702 ev_start (EV_A_ (W)w, ++periodiccnt);
1712 array_needsize (ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2); 1703 array_needsize (WT, periodics, periodicmax, periodiccnt, EMPTY2);
1713 periodics [periodiccnt - 1] = w; 1704 periodics [periodiccnt - 1] = (WT)w;
1714 upheap ((WT *)periodics, periodiccnt - 1); 1705 upheap (periodics, periodiccnt - 1);
1715 1706
1716 /*assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));*/ 1707 /*assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));*/
1717} 1708}
1718 1709
1719void noinline 1710void noinline
1721{ 1712{
1722 clear_pending (EV_A_ (W)w); 1713 clear_pending (EV_A_ (W)w);
1723 if (expect_false (!ev_is_active (w))) 1714 if (expect_false (!ev_is_active (w)))
1724 return; 1715 return;
1725 1716
1726 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1717 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == (WT)w));
1727 1718
1728 { 1719 {
1729 int active = ((W)w)->active; 1720 int active = ((W)w)->active;
1730 1721
1731 if (expect_true (--active < --periodiccnt)) 1722 if (expect_true (--active < --periodiccnt))
1732 { 1723 {
1733 periodics [active] = periodics [periodiccnt]; 1724 periodics [active] = periodics [periodiccnt];
1734 adjustheap ((WT *)periodics, periodiccnt, active); 1725 adjustheap (periodics, periodiccnt, active);
1735 } 1726 }
1736 } 1727 }
1737 1728
1738 ev_stop (EV_A_ (W)w); 1729 ev_stop (EV_A_ (W)w);
1739} 1730}
1760 if (expect_false (ev_is_active (w))) 1751 if (expect_false (ev_is_active (w)))
1761 return; 1752 return;
1762 1753
1763 assert (("ev_signal_start called with illegal signal number", w->signum > 0)); 1754 assert (("ev_signal_start called with illegal signal number", w->signum > 0));
1764 1755
1756 {
1757#ifndef _WIN32
1758 sigset_t full, prev;
1759 sigfillset (&full);
1760 sigprocmask (SIG_SETMASK, &full, &prev);
1761#endif
1762
1763 array_needsize (ANSIG, signals, signalmax, w->signum, signals_init);
1764
1765#ifndef _WIN32
1766 sigprocmask (SIG_SETMASK, &prev, 0);
1767#endif
1768 }
1769
1765 ev_start (EV_A_ (W)w, 1); 1770 ev_start (EV_A_ (W)w, 1);
1766 array_needsize (ANSIG, signals, signalmax, w->signum, signals_init);
1767 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 1771 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
1768 1772
1769 if (!((WL)w)->next) 1773 if (!((WL)w)->next)
1770 { 1774 {
1771#if _WIN32 1775#if _WIN32

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines