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

Comparing libev/ev.c (file contents):
Revision 1.233 by root, Tue May 6 23:34:16 2008 UTC vs.
Revision 1.241 by root, Fri May 9 13:57:00 2008 UTC

422 W w; 422 W w;
423 int events; 423 int events;
424} ANPENDING; 424} ANPENDING;
425 425
426#if EV_USE_INOTIFY 426#if EV_USE_INOTIFY
427/* hash table entry per inotify-id */
427typedef struct 428typedef struct
428{ 429{
429 WL head; 430 WL head;
430} ANFS; 431} ANFS;
432#endif
433
434/* Heap Entry */
435#if EV_HEAP_CACHE_AT
436 typedef struct {
437 WT w;
438 ev_tstamp at;
439 } ANHE;
440
441 #define ANHE_w(he) (he) /* access watcher, read-write */
442 #define ANHE_at(he) (he)->at /* acces cahced at, read-only */
443 #define ANHE_at_set(he) (he)->at = (he)->w->at /* update at from watcher */
444#else
445 typedef WT ANHE;
446
447 #define ANHE_w(he) (he)
448 #define ANHE_at(he) (he)->at
449 #define ANHE_at_set(he)
431#endif 450#endif
432 451
433#if EV_MULTIPLICITY 452#if EV_MULTIPLICITY
434 453
435 struct ev_loop 454 struct ev_loop
759 } 778 }
760} 779}
761 780
762/*****************************************************************************/ 781/*****************************************************************************/
763 782
783/*
784 * the heap functions want a real array index. array index 0 uis guaranteed to not
785 * be in-use at any time. the first heap entry is at array [HEAP0]. DHEAP gives
786 * the branching factor of the d-tree.
787 */
788
789/*
790 * at the moment we allow libev the luxury of two heaps,
791 * a small-code-size 2-heap one and a ~1.5kb larger 4-heap
792 * which is more cache-efficient.
793 * the difference is about 5% with 50000+ watchers.
794 */
795#define EV_USE_4HEAP !EV_MINIMAL
796#if EV_USE_4HEAP
797
798#define DHEAP 4
799#define HEAP0 (DHEAP - 1) /* index of first element in heap */
800
764/* towards the root */ 801/* towards the root */
765void inline_speed 802void inline_speed
766upheap (WT *heap, int k) 803upheap (ANHE *heap, int k)
767{ 804{
768 WT w = heap [k]; 805 ANHE he = heap [k];
769 806
770 for (;;) 807 for (;;)
771 { 808 {
772 int p = k >> 1; 809 int p = ((k - HEAP0 - 1) / DHEAP) + HEAP0;
773 810
774 /* maybe we could use a dummy element at heap [0]? */ 811 if (p == k || ANHE_at (heap [p]) <= ANHE_at (he))
775 if (!p || heap [p]->at <= w->at)
776 break; 812 break;
777 813
778 heap [k] = heap [p]; 814 heap [k] = heap [p];
779 ev_active (heap [k]) = k; 815 ev_active (ANHE_w (heap [k])) = k;
780 k = p; 816 k = p;
781 } 817 }
782 818
819 ev_active (ANHE_w (he)) = k;
783 heap [k] = w; 820 heap [k] = he;
784 ev_active (heap [k]) = k;
785} 821}
786 822
787/* away from the root */ 823/* away from the root */
788void inline_speed 824void inline_speed
789downheap (WT *heap, int N, int k) 825downheap (ANHE *heap, int N, int k)
790{ 826{
791 WT w = heap [k]; 827 ANHE he = heap [k];
828 ANHE *E = heap + N + HEAP0;
829
830 for (;;)
831 {
832 ev_tstamp minat;
833 ANHE *minpos;
834 ANHE *pos = heap + DHEAP * (k - HEAP0) + HEAP0;
835
836 // find minimum child
837 if (expect_true (pos + DHEAP - 1 < E))
838 {
839 /* fast path */ (minpos = pos + 0), (minat = ANHE_at (*minpos));
840 if (ANHE_at (pos [1]) < minat) (minpos = pos + 1), (minat = ANHE_at (*minpos));
841 if (ANHE_at (pos [2]) < minat) (minpos = pos + 2), (minat = ANHE_at (*minpos));
842 if (ANHE_at (pos [3]) < minat) (minpos = pos + 3), (minat = ANHE_at (*minpos));
843 }
844 else if (pos < E)
845 {
846 /* slow path */ (minpos = pos + 0), (minat = ANHE_at (*minpos));
847 if (pos + 1 < E && ANHE_at (pos [1]) < minat) (minpos = pos + 1), (minat = ANHE_at (*minpos));
848 if (pos + 2 < E && ANHE_at (pos [2]) < minat) (minpos = pos + 2), (minat = ANHE_at (*minpos));
849 if (pos + 3 < E && ANHE_at (pos [3]) < minat) (minpos = pos + 3), (minat = ANHE_at (*minpos));
850 }
851 else
852 break;
853
854 if (ANHE_at (he) <= minat)
855 break;
856
857 ev_active (ANHE_w (*minpos)) = k;
858 heap [k] = *minpos;
859
860 k = minpos - heap;
861 }
862
863 ev_active (ANHE_w (he)) = k;
864 heap [k] = he;
865}
866
867#else // 4HEAP
868
869#define HEAP0 1
870
871/* towards the root */
872void inline_speed
873upheap (ANHE *heap, int k)
874{
875 ANHE he = heap [k];
876
877 for (;;)
878 {
879 int p = k >> 1;
880
881 /* maybe we could use a dummy element at heap [0]? */
882 if (!p || ANHE_at (heap [p]) <= ANHE_at (he))
883 break;
884
885 heap [k] = heap [p];
886 ev_active (ANHE_w (heap [k])) = k;
887 k = p;
888 }
889
890 heap [k] = w;
891 ev_active (ANHE_w (heap [k])) = k;
892}
893
894/* away from the root */
895void inline_speed
896downheap (ANHE *heap, int N, int k)
897{
898 ANHE he = heap [k];
792 899
793 for (;;) 900 for (;;)
794 { 901 {
795 int c = k << 1; 902 int c = k << 1;
796 903
797 if (c > N) 904 if (c > N)
798 break; 905 break;
799 906
800 c += c < N && heap [c]->at > heap [c + 1]->at 907 c += c + 1 < N && ANHE_at (heap [c]) > ANHE_at (heap [c + 1])
801 ? 1 : 0; 908 ? 1 : 0;
802 909
803 if (w->at <= heap [c]->at) 910 if (w->at <= ANHE_at (heap [c]))
804 break; 911 break;
805 912
806 heap [k] = heap [c]; 913 heap [k] = heap [c];
807 ev_active (heap [k]) = k; 914 ev_active (ANHE_w (heap [k])) = k;
808 915
809 k = c; 916 k = c;
810 } 917 }
811 918
812 heap [k] = w; 919 heap [k] = he;
813 ev_active (heap [k]) = k; 920 ev_active (ANHE_w (he)) = k;
814} 921}
922#endif
815 923
816void inline_size 924void inline_size
817adjustheap (WT *heap, int N, int k) 925adjustheap (ANHE *heap, int N, int k)
818{ 926{
819 upheap (heap, k); 927 upheap (heap, k);
820 downheap (heap, N, k); 928 downheap (heap, N, k);
821} 929}
822 930
1370void 1478void
1371ev_loop_fork (EV_P) 1479ev_loop_fork (EV_P)
1372{ 1480{
1373 postfork = 1; /* must be in line with ev_default_fork */ 1481 postfork = 1; /* must be in line with ev_default_fork */
1374} 1482}
1375
1376#endif 1483#endif
1377 1484
1378#if EV_MULTIPLICITY 1485#if EV_MULTIPLICITY
1379struct ev_loop * 1486struct ev_loop *
1380ev_default_loop_init (unsigned int flags) 1487ev_default_loop_init (unsigned int flags)
1461 EV_CB_INVOKE (p->w, p->events); 1568 EV_CB_INVOKE (p->w, p->events);
1462 } 1569 }
1463 } 1570 }
1464} 1571}
1465 1572
1573#if EV_IDLE_ENABLE
1574void inline_size
1575idle_reify (EV_P)
1576{
1577 if (expect_false (idleall))
1578 {
1579 int pri;
1580
1581 for (pri = NUMPRI; pri--; )
1582 {
1583 if (pendingcnt [pri])
1584 break;
1585
1586 if (idlecnt [pri])
1587 {
1588 queue_events (EV_A_ (W *)idles [pri], idlecnt [pri], EV_IDLE);
1589 break;
1590 }
1591 }
1592 }
1593}
1594#endif
1595
1466void inline_size 1596void inline_size
1467timers_reify (EV_P) 1597timers_reify (EV_P)
1468{ 1598{
1469 while (timercnt && ev_at (timers [1]) <= mn_now) 1599 while (timercnt && ANHE_at (timers [HEAP0]) <= mn_now)
1470 { 1600 {
1471 ev_timer *w = (ev_timer *)timers [1]; 1601 ev_timer *w = (ev_timer *)ANHE_w (timers [HEAP0]);
1472 1602
1473 /*assert (("inactive timer on timer heap detected", ev_is_active (w)));*/ 1603 /*assert (("inactive timer on timer heap detected", ev_is_active (w)));*/
1474 1604
1475 /* first reschedule or stop timer */ 1605 /* first reschedule or stop timer */
1476 if (w->repeat) 1606 if (w->repeat)
1479 1609
1480 ev_at (w) += w->repeat; 1610 ev_at (w) += w->repeat;
1481 if (ev_at (w) < mn_now) 1611 if (ev_at (w) < mn_now)
1482 ev_at (w) = mn_now; 1612 ev_at (w) = mn_now;
1483 1613
1484 downheap (timers, timercnt, 1); 1614 downheap (timers, timercnt, HEAP0);
1485 } 1615 }
1486 else 1616 else
1487 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ 1617 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
1488 1618
1489 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); 1619 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT);
1492 1622
1493#if EV_PERIODIC_ENABLE 1623#if EV_PERIODIC_ENABLE
1494void inline_size 1624void inline_size
1495periodics_reify (EV_P) 1625periodics_reify (EV_P)
1496{ 1626{
1497 while (periodiccnt && ev_at (periodics [1]) <= ev_rt_now) 1627 while (periodiccnt && ANHE_at (periodics [HEAP0]) <= ev_rt_now)
1498 { 1628 {
1499 ev_periodic *w = (ev_periodic *)periodics [1]; 1629 ev_periodic *w = (ev_periodic *)ANHE_w (periodics [HEAP0]);
1500 1630
1501 /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/ 1631 /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/
1502 1632
1503 /* first reschedule or stop timer */ 1633 /* first reschedule or stop timer */
1504 if (w->reschedule_cb) 1634 if (w->reschedule_cb)
1510 else if (w->interval) 1640 else if (w->interval)
1511 { 1641 {
1512 ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; 1642 ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval;
1513 if (ev_at (w) - ev_rt_now <= TIME_EPSILON) ev_at (w) += w->interval; 1643 if (ev_at (w) - ev_rt_now <= TIME_EPSILON) ev_at (w) += w->interval;
1514 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ev_at (w) > ev_rt_now)); 1644 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ev_at (w) > ev_rt_now));
1515 downheap (periodics, periodiccnt, 1); 1645 downheap (periodics, periodiccnt, HEAP0);
1516 } 1646 }
1517 else 1647 else
1518 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ 1648 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
1519 1649
1520 ev_feed_event (EV_A_ (W)w, EV_PERIODIC); 1650 ev_feed_event (EV_A_ (W)w, EV_PERIODIC);
1525periodics_reschedule (EV_P) 1655periodics_reschedule (EV_P)
1526{ 1656{
1527 int i; 1657 int i;
1528 1658
1529 /* adjust periodics after time jump */ 1659 /* adjust periodics after time jump */
1530 for (i = 1; i <= periodiccnt; ++i) 1660 for (i = HEAP0; i < periodiccnt + HEAP0; ++i)
1531 { 1661 {
1532 ev_periodic *w = (ev_periodic *)periodics [i]; 1662 ev_periodic *w = (ev_periodic *)ANHE_w (periodics [i]);
1533 1663
1534 if (w->reschedule_cb) 1664 if (w->reschedule_cb)
1535 ev_at (w) = w->reschedule_cb (w, ev_rt_now); 1665 ev_at (w) = w->reschedule_cb (w, ev_rt_now);
1536 else if (w->interval) 1666 else if (w->interval)
1537 ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; 1667 ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval;
1538 } 1668 }
1539 1669
1540 /* now rebuild the heap */ 1670 /* now rebuild the heap, this for the 2-heap, inefficient for the 4-heap, but correct */
1541 for (i = periodiccnt >> 1; i--; ) 1671 for (i = periodiccnt >> 1; --i; )
1542 downheap (periodics, periodiccnt, i); 1672 downheap (periodics, periodiccnt, i + HEAP0);
1543}
1544#endif
1545
1546#if EV_IDLE_ENABLE
1547void inline_size
1548idle_reify (EV_P)
1549{
1550 if (expect_false (idleall))
1551 {
1552 int pri;
1553
1554 for (pri = NUMPRI; pri--; )
1555 {
1556 if (pendingcnt [pri])
1557 break;
1558
1559 if (idlecnt [pri])
1560 {
1561 queue_events (EV_A_ (W *)idles [pri], idlecnt [pri], EV_IDLE);
1562 break;
1563 }
1564 }
1565 }
1566} 1673}
1567#endif 1674#endif
1568 1675
1569void inline_speed 1676void inline_speed
1570time_update (EV_P_ ev_tstamp max_block) 1677time_update (EV_P_ ev_tstamp max_block)
1599 */ 1706 */
1600 for (i = 4; --i; ) 1707 for (i = 4; --i; )
1601 { 1708 {
1602 rtmn_diff = ev_rt_now - mn_now; 1709 rtmn_diff = ev_rt_now - mn_now;
1603 1710
1604 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) 1711 if (expect_true (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP))
1605 return; /* all is well */ 1712 return; /* all is well */
1606 1713
1607 ev_rt_now = ev_time (); 1714 ev_rt_now = ev_time ();
1608 mn_now = get_clock (); 1715 mn_now = get_clock ();
1609 now_floor = mn_now; 1716 now_floor = mn_now;
1624 { 1731 {
1625#if EV_PERIODIC_ENABLE 1732#if EV_PERIODIC_ENABLE
1626 periodics_reschedule (EV_A); 1733 periodics_reschedule (EV_A);
1627#endif 1734#endif
1628 /* adjust timers. this is easy, as the offset is the same for all of them */ 1735 /* adjust timers. this is easy, as the offset is the same for all of them */
1629 for (i = 1; i <= timercnt; ++i) 1736 for (i = 0; i < timercnt; ++i)
1630 ev_at (timers [i]) += ev_rt_now - mn_now; 1737 {
1738 ANHE *he = timers + i + HEAP0;
1739 ANHE_w (*he)->at += ev_rt_now - mn_now;
1740 ANHE_at_set (*he);
1741 }
1631 } 1742 }
1632 1743
1633 mn_now = ev_rt_now; 1744 mn_now = ev_rt_now;
1634 } 1745 }
1635} 1746}
1705 1816
1706 waittime = MAX_BLOCKTIME; 1817 waittime = MAX_BLOCKTIME;
1707 1818
1708 if (timercnt) 1819 if (timercnt)
1709 { 1820 {
1710 ev_tstamp to = ev_at (timers [1]) - mn_now + backend_fudge; 1821 ev_tstamp to = ANHE_at (timers [HEAP0]) - mn_now + backend_fudge;
1711 if (waittime > to) waittime = to; 1822 if (waittime > to) waittime = to;
1712 } 1823 }
1713 1824
1714#if EV_PERIODIC_ENABLE 1825#if EV_PERIODIC_ENABLE
1715 if (periodiccnt) 1826 if (periodiccnt)
1716 { 1827 {
1717 ev_tstamp to = ev_at (periodics [1]) - ev_rt_now + backend_fudge; 1828 ev_tstamp to = ANHE_at (periodics [HEAP0]) - ev_rt_now + backend_fudge;
1718 if (waittime > to) waittime = to; 1829 if (waittime > to) waittime = to;
1719 } 1830 }
1720#endif 1831#endif
1721 1832
1722 if (expect_false (waittime < timeout_blocktime)) 1833 if (expect_false (waittime < timeout_blocktime))
1892 2003
1893 ev_at (w) += mn_now; 2004 ev_at (w) += mn_now;
1894 2005
1895 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); 2006 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
1896 2007
1897 ev_start (EV_A_ (W)w, ++timercnt); 2008 ev_start (EV_A_ (W)w, ++timercnt + HEAP0 - 1);
1898 array_needsize (WT, timers, timermax, timercnt + 1, EMPTY2); 2009 array_needsize (ANHE, timers, timermax, ev_active (w) + 1, EMPTY2);
1899 timers [timercnt] = (WT)w; 2010 ANHE_w (timers [ev_active (w)]) = (WT)w;
2011 ANHE_at_set (timers [ev_active (w)]);
1900 upheap (timers, timercnt); 2012 upheap (timers, ev_active (w));
1901 2013
1902 /*assert (("internal timer heap corruption", timers [ev_active (w)] == w));*/ 2014 /*assert (("internal timer heap corruption", timers [ev_active (w)] == w));*/
1903} 2015}
1904 2016
1905void noinline 2017void noinline
1910 return; 2022 return;
1911 2023
1912 { 2024 {
1913 int active = ev_active (w); 2025 int active = ev_active (w);
1914 2026
1915 assert (("internal timer heap corruption", timers [active] == (WT)w)); 2027 assert (("internal timer heap corruption", ANHE_w (timers [active]) == (WT)w));
1916 2028
1917 if (expect_true (active < timercnt)) 2029 if (expect_true (active < timercnt + HEAP0 - 1))
1918 { 2030 {
1919 timers [active] = timers [timercnt]; 2031 timers [active] = timers [timercnt + HEAP0 - 1];
1920 adjustheap (timers, timercnt, active); 2032 adjustheap (timers, timercnt, active);
1921 } 2033 }
1922 2034
1923 --timercnt; 2035 --timercnt;
1924 } 2036 }
1934 if (ev_is_active (w)) 2046 if (ev_is_active (w))
1935 { 2047 {
1936 if (w->repeat) 2048 if (w->repeat)
1937 { 2049 {
1938 ev_at (w) = mn_now + w->repeat; 2050 ev_at (w) = mn_now + w->repeat;
2051 ANHE_at_set (timers [ev_active (w)]);
1939 adjustheap (timers, timercnt, ev_active (w)); 2052 adjustheap (timers, timercnt, ev_active (w));
1940 } 2053 }
1941 else 2054 else
1942 ev_timer_stop (EV_A_ w); 2055 ev_timer_stop (EV_A_ w);
1943 } 2056 }
1964 ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; 2077 ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval;
1965 } 2078 }
1966 else 2079 else
1967 ev_at (w) = w->offset; 2080 ev_at (w) = w->offset;
1968 2081
1969 ev_start (EV_A_ (W)w, ++periodiccnt); 2082 ev_start (EV_A_ (W)w, ++periodiccnt + HEAP0 - 1);
1970 array_needsize (WT, periodics, periodicmax, periodiccnt + 1, EMPTY2); 2083 array_needsize (ANHE, periodics, periodicmax, ev_active (w) + 1, EMPTY2);
1971 periodics [periodiccnt] = (WT)w; 2084 ANHE_w (periodics [ev_active (w)]) = (WT)w;
1972 upheap (periodics, periodiccnt); 2085 upheap (periodics, ev_active (w));
1973 2086
1974 /*assert (("internal periodic heap corruption", periodics [ev_active (w)] == w));*/ 2087 /*assert (("internal periodic heap corruption", ANHE_w (periodics [ev_active (w)]) == (WT)w));*/
1975} 2088}
1976 2089
1977void noinline 2090void noinline
1978ev_periodic_stop (EV_P_ ev_periodic *w) 2091ev_periodic_stop (EV_P_ ev_periodic *w)
1979{ 2092{
1982 return; 2095 return;
1983 2096
1984 { 2097 {
1985 int active = ev_active (w); 2098 int active = ev_active (w);
1986 2099
1987 assert (("internal periodic heap corruption", periodics [active] == (WT)w)); 2100 assert (("internal periodic heap corruption", ANHE_w (periodics [active]) == (WT)w));
1988 2101
1989 if (expect_true (active < periodiccnt)) 2102 if (expect_true (active < periodiccnt + HEAP0 - 1))
1990 { 2103 {
1991 periodics [active] = periodics [periodiccnt]; 2104 periodics [active] = periodics [periodiccnt + HEAP0 - 1];
1992 adjustheap (periodics, periodiccnt, active); 2105 adjustheap (periodics, periodiccnt, active);
1993 } 2106 }
1994 2107
1995 --periodiccnt; 2108 --periodiccnt;
1996 } 2109 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines