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.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
1486#endif 1594#endif
1487 1595
1488void inline_size 1596void inline_size
1489timers_reify (EV_P) 1597timers_reify (EV_P)
1490{ 1598{
1491 while (timercnt && ev_at (timers [1]) <= mn_now) 1599 while (timercnt && ANHE_at (timers [HEAP0]) <= mn_now)
1492 { 1600 {
1493 ev_timer *w = (ev_timer *)timers [1]; 1601 ev_timer *w = (ev_timer *)ANHE_w (timers [HEAP0]);
1494 1602
1495 /*assert (("inactive timer on timer heap detected", ev_is_active (w)));*/ 1603 /*assert (("inactive timer on timer heap detected", ev_is_active (w)));*/
1496 1604
1497 /* first reschedule or stop timer */ 1605 /* first reschedule or stop timer */
1498 if (w->repeat) 1606 if (w->repeat)
1501 1609
1502 ev_at (w) += w->repeat; 1610 ev_at (w) += w->repeat;
1503 if (ev_at (w) < mn_now) 1611 if (ev_at (w) < mn_now)
1504 ev_at (w) = mn_now; 1612 ev_at (w) = mn_now;
1505 1613
1506 downheap (timers, timercnt, 1); 1614 downheap (timers, timercnt, HEAP0);
1507 } 1615 }
1508 else 1616 else
1509 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ 1617 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
1510 1618
1511 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); 1619 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT);
1514 1622
1515#if EV_PERIODIC_ENABLE 1623#if EV_PERIODIC_ENABLE
1516void inline_size 1624void inline_size
1517periodics_reify (EV_P) 1625periodics_reify (EV_P)
1518{ 1626{
1519 while (periodiccnt && ev_at (periodics [1]) <= ev_rt_now) 1627 while (periodiccnt && ANHE_at (periodics [HEAP0]) <= ev_rt_now)
1520 { 1628 {
1521 ev_periodic *w = (ev_periodic *)periodics [1]; 1629 ev_periodic *w = (ev_periodic *)ANHE_w (periodics [HEAP0]);
1522 1630
1523 /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/ 1631 /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/
1524 1632
1525 /* first reschedule or stop timer */ 1633 /* first reschedule or stop timer */
1526 if (w->reschedule_cb) 1634 if (w->reschedule_cb)
1532 else if (w->interval) 1640 else if (w->interval)
1533 { 1641 {
1534 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;
1535 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;
1536 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));
1537 downheap (periodics, periodiccnt, 1); 1645 downheap (periodics, periodiccnt, HEAP0);
1538 } 1646 }
1539 else 1647 else
1540 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ 1648 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
1541 1649
1542 ev_feed_event (EV_A_ (W)w, EV_PERIODIC); 1650 ev_feed_event (EV_A_ (W)w, EV_PERIODIC);
1547periodics_reschedule (EV_P) 1655periodics_reschedule (EV_P)
1548{ 1656{
1549 int i; 1657 int i;
1550 1658
1551 /* adjust periodics after time jump */ 1659 /* adjust periodics after time jump */
1552 for (i = 1; i <= periodiccnt; ++i) 1660 for (i = HEAP0; i < periodiccnt + HEAP0; ++i)
1553 { 1661 {
1554 ev_periodic *w = (ev_periodic *)periodics [i]; 1662 ev_periodic *w = (ev_periodic *)ANHE_w (periodics [i]);
1555 1663
1556 if (w->reschedule_cb) 1664 if (w->reschedule_cb)
1557 ev_at (w) = w->reschedule_cb (w, ev_rt_now); 1665 ev_at (w) = w->reschedule_cb (w, ev_rt_now);
1558 else if (w->interval) 1666 else if (w->interval)
1559 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;
1560 } 1668 }
1561 1669
1562 /* now rebuild the heap */ 1670 /* now rebuild the heap, this for the 2-heap, inefficient for the 4-heap, but correct */
1563 for (i = periodiccnt >> 1; i--; ) 1671 for (i = periodiccnt >> 1; --i; )
1564 downheap (periodics, periodiccnt, i); 1672 downheap (periodics, periodiccnt, i + HEAP0);
1565} 1673}
1566#endif 1674#endif
1567 1675
1568void inline_speed 1676void inline_speed
1569time_update (EV_P_ ev_tstamp max_block) 1677time_update (EV_P_ ev_tstamp max_block)
1623 { 1731 {
1624#if EV_PERIODIC_ENABLE 1732#if EV_PERIODIC_ENABLE
1625 periodics_reschedule (EV_A); 1733 periodics_reschedule (EV_A);
1626#endif 1734#endif
1627 /* 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 */
1628 for (i = 1; i <= timercnt; ++i) 1736 for (i = 0; i < timercnt; ++i)
1629 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 }
1630 } 1742 }
1631 1743
1632 mn_now = ev_rt_now; 1744 mn_now = ev_rt_now;
1633 } 1745 }
1634} 1746}
1704 1816
1705 waittime = MAX_BLOCKTIME; 1817 waittime = MAX_BLOCKTIME;
1706 1818
1707 if (timercnt) 1819 if (timercnt)
1708 { 1820 {
1709 ev_tstamp to = ev_at (timers [1]) - mn_now + backend_fudge; 1821 ev_tstamp to = ANHE_at (timers [HEAP0]) - mn_now + backend_fudge;
1710 if (waittime > to) waittime = to; 1822 if (waittime > to) waittime = to;
1711 } 1823 }
1712 1824
1713#if EV_PERIODIC_ENABLE 1825#if EV_PERIODIC_ENABLE
1714 if (periodiccnt) 1826 if (periodiccnt)
1715 { 1827 {
1716 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;
1717 if (waittime > to) waittime = to; 1829 if (waittime > to) waittime = to;
1718 } 1830 }
1719#endif 1831#endif
1720 1832
1721 if (expect_false (waittime < timeout_blocktime)) 1833 if (expect_false (waittime < timeout_blocktime))
1891 2003
1892 ev_at (w) += mn_now; 2004 ev_at (w) += mn_now;
1893 2005
1894 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.));
1895 2007
1896 ev_start (EV_A_ (W)w, ++timercnt); 2008 ev_start (EV_A_ (W)w, ++timercnt + HEAP0 - 1);
1897 array_needsize (WT, timers, timermax, timercnt + 1, EMPTY2); 2009 array_needsize (ANHE, timers, timermax, ev_active (w) + 1, EMPTY2);
1898 timers [timercnt] = (WT)w; 2010 ANHE_w (timers [ev_active (w)]) = (WT)w;
2011 ANHE_at_set (timers [ev_active (w)]);
1899 upheap (timers, timercnt); 2012 upheap (timers, ev_active (w));
1900 2013
1901 /*assert (("internal timer heap corruption", timers [ev_active (w)] == w));*/ 2014 /*assert (("internal timer heap corruption", timers [ev_active (w)] == w));*/
1902} 2015}
1903 2016
1904void noinline 2017void noinline
1909 return; 2022 return;
1910 2023
1911 { 2024 {
1912 int active = ev_active (w); 2025 int active = ev_active (w);
1913 2026
1914 assert (("internal timer heap corruption", timers [active] == (WT)w)); 2027 assert (("internal timer heap corruption", ANHE_w (timers [active]) == (WT)w));
1915 2028
1916 if (expect_true (active < timercnt)) 2029 if (expect_true (active < timercnt + HEAP0 - 1))
1917 { 2030 {
1918 timers [active] = timers [timercnt]; 2031 timers [active] = timers [timercnt + HEAP0 - 1];
1919 adjustheap (timers, timercnt, active); 2032 adjustheap (timers, timercnt, active);
1920 } 2033 }
1921 2034
1922 --timercnt; 2035 --timercnt;
1923 } 2036 }
1933 if (ev_is_active (w)) 2046 if (ev_is_active (w))
1934 { 2047 {
1935 if (w->repeat) 2048 if (w->repeat)
1936 { 2049 {
1937 ev_at (w) = mn_now + w->repeat; 2050 ev_at (w) = mn_now + w->repeat;
2051 ANHE_at_set (timers [ev_active (w)]);
1938 adjustheap (timers, timercnt, ev_active (w)); 2052 adjustheap (timers, timercnt, ev_active (w));
1939 } 2053 }
1940 else 2054 else
1941 ev_timer_stop (EV_A_ w); 2055 ev_timer_stop (EV_A_ w);
1942 } 2056 }
1963 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;
1964 } 2078 }
1965 else 2079 else
1966 ev_at (w) = w->offset; 2080 ev_at (w) = w->offset;
1967 2081
1968 ev_start (EV_A_ (W)w, ++periodiccnt); 2082 ev_start (EV_A_ (W)w, ++periodiccnt + HEAP0 - 1);
1969 array_needsize (WT, periodics, periodicmax, periodiccnt + 1, EMPTY2); 2083 array_needsize (ANHE, periodics, periodicmax, ev_active (w) + 1, EMPTY2);
1970 periodics [periodiccnt] = (WT)w; 2084 ANHE_w (periodics [ev_active (w)]) = (WT)w;
1971 upheap (periodics, periodiccnt); 2085 upheap (periodics, ev_active (w));
1972 2086
1973 /*assert (("internal periodic heap corruption", periodics [ev_active (w)] == w));*/ 2087 /*assert (("internal periodic heap corruption", ANHE_w (periodics [ev_active (w)]) == (WT)w));*/
1974} 2088}
1975 2089
1976void noinline 2090void noinline
1977ev_periodic_stop (EV_P_ ev_periodic *w) 2091ev_periodic_stop (EV_P_ ev_periodic *w)
1978{ 2092{
1981 return; 2095 return;
1982 2096
1983 { 2097 {
1984 int active = ev_active (w); 2098 int active = ev_active (w);
1985 2099
1986 assert (("internal periodic heap corruption", periodics [active] == (WT)w)); 2100 assert (("internal periodic heap corruption", ANHE_w (periodics [active]) == (WT)w));
1987 2101
1988 if (expect_true (active < periodiccnt)) 2102 if (expect_true (active < periodiccnt + HEAP0 - 1))
1989 { 2103 {
1990 periodics [active] = periodics [periodiccnt]; 2104 periodics [active] = periodics [periodiccnt + HEAP0 - 1];
1991 adjustheap (periodics, periodiccnt, active); 2105 adjustheap (periodics, periodiccnt, active);
1992 } 2106 }
1993 2107
1994 --periodiccnt; 2108 --periodiccnt;
1995 } 2109 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines