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

Comparing libev/ev.c (file contents):
Revision 1.234 by root, Tue May 6 23:42:16 2008 UTC vs.
Revision 1.238 by root, Thu May 8 20:49:12 2008 UTC

759 } 759 }
760} 760}
761 761
762/*****************************************************************************/ 762/*****************************************************************************/
763 763
764/*
765 * at the moment we allow libev the luxury of two heaps,
766 * a small-code-size 2-heap one and a ~1.5kb larger 4-heap
767 * which is more cache-efficient.
768 * the difference is about 5% with 50000+ watchers.
769 */
770#define USE_4HEAP !EV_MINIMAL
771#define USE_4HEAP 1/* they do not work corretcly */
772#if USE_4HEAP
773
774#define DHEAP 4
775#define HEAP0 (DHEAP - 1) /* index of first element in heap */
776
764/* towards the root */ 777/* towards the root */
765void inline_speed 778void inline_speed
766upheap (WT *heap, int k) 779upheap (WT *heap, int k)
767{ 780{
768 WT w = heap [k]; 781 WT w = heap [k];
769 782
770 for (;;) 783 for (;;)
771 { 784 {
785 int p = ((k - HEAP0 - 1) / DHEAP) + HEAP0;
786
787 if (p == k || heap [p]->at <= w->at)
788 break;
789
790 heap [k] = heap [p];
791 ev_active (heap [k]) = k;
792 k = p;
793 }
794
795 heap [k] = w;
796 ev_active (heap [k]) = k;
797}
798
799/* away from the root */
800void inline_speed
801downheap (WT *heap, int N, int k)
802{
803 WT w = heap [k];
804 WT *E = heap + N + HEAP0;
805
806 for (;;)
807 {
808 ev_tstamp minat;
809 WT *minpos;
810 WT *pos = heap + DHEAP * (k - HEAP0) + HEAP0;
811
812 // find minimum child
813 if (expect_true (pos + DHEAP - 1 < E))
814 {
815 /* fast path */
816 (minpos = pos + 0), (minat = (*minpos)->at);
817 if (pos [1]->at < minat) (minpos = pos + 1), (minat = (*minpos)->at);
818 if (pos [2]->at < minat) (minpos = pos + 2), (minat = (*minpos)->at);
819 if (pos [3]->at < minat) (minpos = pos + 3), (minat = (*minpos)->at);
820 }
821 else
822 {
823 /* slow path */
824 if (pos >= E)
825 break;
826 (minpos = pos + 0), (minat = (*minpos)->at);
827 if (pos + 1 < E && pos [1]->at < minat) (minpos = pos + 1), (minat = (*minpos)->at);
828 if (pos + 2 < E && pos [2]->at < minat) (minpos = pos + 2), (minat = (*minpos)->at);
829 if (pos + 3 < E && pos [3]->at < minat) (minpos = pos + 3), (minat = (*minpos)->at);
830 }
831
832 if (w->at <= minat)
833 break;
834
835 ev_active (*minpos) = k;
836 heap [k] = *minpos;
837
838 k = minpos - heap;
839 }
840
841 heap [k] = w;
842 ev_active (heap [k]) = k;
843}
844
845#else // 4HEAP
846
847#define HEAP0 1
848
849/* towards the root */
850void inline_speed
851upheap (WT *heap, int k)
852{
853 WT w = heap [k];
854
855 for (;;)
856 {
772 int p = k >> 1; 857 int p = k >> 1;
773 858
774 /* maybe we could use a dummy element at heap [0]? */ 859 /* maybe we could use a dummy element at heap [0]? */
775 if (!p || heap [p]->at <= w->at) 860 if (!p || heap [p]->at <= w->at)
776 break; 861 break;
795 int c = k << 1; 880 int c = k << 1;
796 881
797 if (c > N) 882 if (c > N)
798 break; 883 break;
799 884
800 c += c < N && heap [c]->at > heap [c + 1]->at 885 c += c + 1 < N && heap [c]->at > heap [c + 1]->at
801 ? 1 : 0; 886 ? 1 : 0;
802 887
803 if (w->at <= heap [c]->at) 888 if (w->at <= heap [c]->at)
804 break; 889 break;
805 890
806 heap [k] = heap [c]; 891 heap [k] = heap [c];
807 ev_active (heap [k]) = k; 892 ((W)heap [k])->active = k;
808 893
809 k = c; 894 k = c;
810 } 895 }
811 896
812 heap [k] = w; 897 heap [k] = w;
813 ev_active (heap [k]) = k; 898 ev_active (heap [k]) = k;
814} 899}
900#endif
815 901
816void inline_size 902void inline_size
817adjustheap (WT *heap, int N, int k) 903adjustheap (WT *heap, int N, int k)
818{ 904{
819 upheap (heap, k); 905 upheap (heap, k);
1486#endif 1572#endif
1487 1573
1488void inline_size 1574void inline_size
1489timers_reify (EV_P) 1575timers_reify (EV_P)
1490{ 1576{
1491 while (timercnt && ev_at (timers [1]) <= mn_now) 1577 while (timercnt && ev_at (timers [HEAP0]) <= mn_now)
1492 { 1578 {
1493 ev_timer *w = (ev_timer *)timers [1]; 1579 ev_timer *w = (ev_timer *)timers [HEAP0];
1494 1580
1495 /*assert (("inactive timer on timer heap detected", ev_is_active (w)));*/ 1581 /*assert (("inactive timer on timer heap detected", ev_is_active (w)));*/
1496 1582
1497 /* first reschedule or stop timer */ 1583 /* first reschedule or stop timer */
1498 if (w->repeat) 1584 if (w->repeat)
1501 1587
1502 ev_at (w) += w->repeat; 1588 ev_at (w) += w->repeat;
1503 if (ev_at (w) < mn_now) 1589 if (ev_at (w) < mn_now)
1504 ev_at (w) = mn_now; 1590 ev_at (w) = mn_now;
1505 1591
1506 downheap (timers, timercnt, 1); 1592 downheap (timers, timercnt, HEAP0);
1507 } 1593 }
1508 else 1594 else
1509 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ 1595 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
1510 1596
1511 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); 1597 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT);
1514 1600
1515#if EV_PERIODIC_ENABLE 1601#if EV_PERIODIC_ENABLE
1516void inline_size 1602void inline_size
1517periodics_reify (EV_P) 1603periodics_reify (EV_P)
1518{ 1604{
1519 while (periodiccnt && ev_at (periodics [1]) <= ev_rt_now) 1605 while (periodiccnt && ev_at (periodics [HEAP0]) <= ev_rt_now)
1520 { 1606 {
1521 ev_periodic *w = (ev_periodic *)periodics [1]; 1607 ev_periodic *w = (ev_periodic *)periodics [HEAP0];
1522 1608
1523 /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/ 1609 /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/
1524 1610
1525 /* first reschedule or stop timer */ 1611 /* first reschedule or stop timer */
1526 if (w->reschedule_cb) 1612 if (w->reschedule_cb)
1532 else if (w->interval) 1618 else if (w->interval)
1533 { 1619 {
1534 ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; 1620 ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval;
1535 if (ev_at (w) - ev_rt_now <= TIME_EPSILON) ev_at (w) += w->interval; 1621 if (ev_at (w) - ev_rt_now <= TIME_EPSILON) ev_at (w) += w->interval;
1536 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ev_at (w) > ev_rt_now)); 1622 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ev_at (w) > ev_rt_now));
1537 downheap (periodics, periodiccnt, 1); 1623 downheap (periodics, periodiccnt, HEAP0);
1538 } 1624 }
1539 else 1625 else
1540 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ 1626 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
1541 1627
1542 ev_feed_event (EV_A_ (W)w, EV_PERIODIC); 1628 ev_feed_event (EV_A_ (W)w, EV_PERIODIC);
1558 else if (w->interval) 1644 else if (w->interval)
1559 ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; 1645 ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval;
1560 } 1646 }
1561 1647
1562 /* now rebuild the heap */ 1648 /* now rebuild the heap */
1563 for (i = periodiccnt >> 1; i--; ) 1649 for (i = periodiccnt >> 1; --i; )
1564 downheap (periodics, periodiccnt, i); 1650 downheap (periodics, periodiccnt, i + HEAP0);
1565} 1651}
1566#endif 1652#endif
1567 1653
1568void inline_speed 1654void inline_speed
1569time_update (EV_P_ ev_tstamp max_block) 1655time_update (EV_P_ ev_tstamp max_block)
1704 1790
1705 waittime = MAX_BLOCKTIME; 1791 waittime = MAX_BLOCKTIME;
1706 1792
1707 if (timercnt) 1793 if (timercnt)
1708 { 1794 {
1709 ev_tstamp to = ev_at (timers [1]) - mn_now + backend_fudge; 1795 ev_tstamp to = ev_at (timers [HEAP0]) - mn_now + backend_fudge;
1710 if (waittime > to) waittime = to; 1796 if (waittime > to) waittime = to;
1711 } 1797 }
1712 1798
1713#if EV_PERIODIC_ENABLE 1799#if EV_PERIODIC_ENABLE
1714 if (periodiccnt) 1800 if (periodiccnt)
1715 { 1801 {
1716 ev_tstamp to = ev_at (periodics [1]) - ev_rt_now + backend_fudge; 1802 ev_tstamp to = ev_at (periodics [HEAP0]) - ev_rt_now + backend_fudge;
1717 if (waittime > to) waittime = to; 1803 if (waittime > to) waittime = to;
1718 } 1804 }
1719#endif 1805#endif
1720 1806
1721 if (expect_false (waittime < timeout_blocktime)) 1807 if (expect_false (waittime < timeout_blocktime))
1891 1977
1892 ev_at (w) += mn_now; 1978 ev_at (w) += mn_now;
1893 1979
1894 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); 1980 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
1895 1981
1896 ev_start (EV_A_ (W)w, ++timercnt); 1982 ev_start (EV_A_ (W)w, ++timercnt + HEAP0 - 1);
1897 array_needsize (WT, timers, timermax, timercnt + 1, EMPTY2); 1983 array_needsize (WT, timers, timermax, timercnt + HEAP0, EMPTY2);
1898 timers [timercnt] = (WT)w; 1984 timers [ev_active (w)] = (WT)w;
1899 upheap (timers, timercnt); 1985 upheap (timers, ev_active (w));
1900 1986
1901 /*assert (("internal timer heap corruption", timers [ev_active (w)] == w));*/ 1987 /*assert (("internal timer heap corruption", timers [ev_active (w)] == w));*/
1902} 1988}
1903 1989
1904void noinline 1990void noinline
1911 { 1997 {
1912 int active = ev_active (w); 1998 int active = ev_active (w);
1913 1999
1914 assert (("internal timer heap corruption", timers [active] == (WT)w)); 2000 assert (("internal timer heap corruption", timers [active] == (WT)w));
1915 2001
1916 if (expect_true (active < timercnt)) 2002 if (expect_true (active < timercnt + HEAP0 - 1))
1917 { 2003 {
1918 timers [active] = timers [timercnt]; 2004 timers [active] = timers [timercnt + HEAP0 - 1];
1919 adjustheap (timers, timercnt, active); 2005 adjustheap (timers, timercnt, active);
1920 } 2006 }
1921 2007
1922 --timercnt; 2008 --timercnt;
1923 } 2009 }
1963 ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; 2049 ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval;
1964 } 2050 }
1965 else 2051 else
1966 ev_at (w) = w->offset; 2052 ev_at (w) = w->offset;
1967 2053
1968 ev_start (EV_A_ (W)w, ++periodiccnt); 2054 ev_start (EV_A_ (W)w, ++periodiccnt + HEAP0 - 1);
1969 array_needsize (WT, periodics, periodicmax, periodiccnt + 1, EMPTY2); 2055 array_needsize (WT, periodics, periodicmax, periodiccnt + HEAP0, EMPTY2);
1970 periodics [periodiccnt] = (WT)w; 2056 periodics [ev_active (w)] = (WT)w;
1971 upheap (periodics, periodiccnt); 2057 upheap (periodics, ev_active (w));
1972 2058
1973 /*assert (("internal periodic heap corruption", periodics [ev_active (w)] == w));*/ 2059 /*assert (("internal periodic heap corruption", periodics [ev_active (w)] == w));*/
1974} 2060}
1975 2061
1976void noinline 2062void noinline
1983 { 2069 {
1984 int active = ev_active (w); 2070 int active = ev_active (w);
1985 2071
1986 assert (("internal periodic heap corruption", periodics [active] == (WT)w)); 2072 assert (("internal periodic heap corruption", periodics [active] == (WT)w));
1987 2073
1988 if (expect_true (active < periodiccnt)) 2074 if (expect_true (active < periodiccnt + HEAP0 - 1))
1989 { 2075 {
1990 periodics [active] = periodics [periodiccnt]; 2076 periodics [active] = periodics [periodiccnt + HEAP0 - 1];
1991 adjustheap (periodics, periodiccnt, active); 2077 adjustheap (periodics, periodiccnt, active);
1992 } 2078 }
1993 2079
1994 --periodiccnt; 2080 --periodiccnt;
1995 } 2081 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines