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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines