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

Comparing libev/ev.c (file contents):
Revision 1.235 by root, Wed May 7 14:45:17 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
760} 779}
761 780
762/*****************************************************************************/ 781/*****************************************************************************/
763 782
764/* 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/*
765 * at the moment we allow libev the luxury of two heaps, 790 * 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 791 * a small-code-size 2-heap one and a ~1.5kb larger 4-heap
767 * which is more cache-efficient. 792 * which is more cache-efficient.
768 * the difference is about 5% with 50000+ watchers. 793 * the difference is about 5% with 50000+ watchers.
769 */ 794 */
770#define USE_4HEAP !EV_MINIMAL 795#define EV_USE_4HEAP !EV_MINIMAL
771#if USE_4HEAP 796#if EV_USE_4HEAP
772 797
798#define DHEAP 4
773#define HEAP0 3 /* index of first element in heap */ 799#define HEAP0 (DHEAP - 1) /* index of first element in heap */
774 800
775/* towards the root */ 801/* towards the root */
776void inline_speed 802void inline_speed
777upheap (WT *heap, int k) 803upheap (ANHE *heap, int k)
778{ 804{
779 WT w = heap [k]; 805 ANHE he = heap [k];
780 806
781 for (;;) 807 for (;;)
782 { 808 {
783 int p = ((k - HEAP0 - 1) / 4) + HEAP0; 809 int p = ((k - HEAP0 - 1) / DHEAP) + HEAP0;
784 810
785 if (p >= HEAP0 || heap [p]->at <= w->at) 811 if (p == k || ANHE_at (heap [p]) <= ANHE_at (he))
786 break; 812 break;
787 813
788 heap [k] = heap [p]; 814 heap [k] = heap [p];
789 ev_active (heap [k]) = k; 815 ev_active (ANHE_w (heap [k])) = k;
790 k = p; 816 k = p;
791 } 817 }
792 818
819 ev_active (ANHE_w (he)) = k;
793 heap [k] = w; 820 heap [k] = he;
794 ev_active (heap [k]) = k;
795} 821}
796 822
797/* away from the root */ 823/* away from the root */
798void inline_speed 824void inline_speed
799downheap (WT *heap, int N, int k) 825downheap (ANHE *heap, int N, int k)
800{ 826{
801 WT w = heap [k]; 827 ANHE he = heap [k];
802 WT *E = heap + N + HEAP0; 828 ANHE *E = heap + N + HEAP0;
803 829
804 for (;;) 830 for (;;)
805 { 831 {
806 ev_tstamp minat; 832 ev_tstamp minat;
807 WT *minpos; 833 ANHE *minpos;
808 WT *pos = heap + 4 * (k - HEAP0) + HEAP0; 834 ANHE *pos = heap + DHEAP * (k - HEAP0) + HEAP0;
809 835
810 // find minimum child 836 // find minimum child
811 if (expect_true (pos +3 < E)) 837 if (expect_true (pos + DHEAP - 1 < E))
812 { 838 {
813 (minpos = pos + 0), (minat = (*minpos)->at); 839 /* fast path */ (minpos = pos + 0), (minat = ANHE_at (*minpos));
814 if (pos [1]->at < minat) (minpos = pos + 1), (minat = (*minpos)->at); 840 if (ANHE_at (pos [1]) < minat) (minpos = pos + 1), (minat = ANHE_at (*minpos));
815 if (pos [2]->at < minat) (minpos = pos + 2), (minat = (*minpos)->at); 841 if (ANHE_at (pos [2]) < minat) (minpos = pos + 2), (minat = ANHE_at (*minpos));
816 if (pos [3]->at < minat) (minpos = pos + 3), (minat = (*minpos)->at); 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));
817 } 850 }
818 else 851 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; 852 break;
831 853
854 if (ANHE_at (he) <= minat)
855 break;
856
832 ev_active (*minpos) = k; 857 ev_active (ANHE_w (*minpos)) = k;
833 heap [k] = *minpos; 858 heap [k] = *minpos;
834 859
835 k = minpos - heap; 860 k = minpos - heap;
836 } 861 }
837 862
863 ev_active (ANHE_w (he)) = k;
838 heap [k] = w; 864 heap [k] = he;
839 ev_active (heap [k]) = k;
840} 865}
841 866
842#else // 4HEAP 867#else // 4HEAP
843 868
844#define HEAP0 1 869#define HEAP0 1
845 870
846/* towards the root */ 871/* towards the root */
847void inline_speed 872void inline_speed
848upheap (WT *heap, int k) 873upheap (ANHE *heap, int k)
849{ 874{
850 WT w = heap [k]; 875 ANHE he = heap [k];
851 876
852 for (;;) 877 for (;;)
853 { 878 {
854 int p = k >> 1; 879 int p = k >> 1;
855 880
856 /* maybe we could use a dummy element at heap [0]? */ 881 /* maybe we could use a dummy element at heap [0]? */
857 if (!p || heap [p]->at <= w->at) 882 if (!p || ANHE_at (heap [p]) <= ANHE_at (he))
858 break; 883 break;
859 884
860 heap [k] = heap [p]; 885 heap [k] = heap [p];
861 ev_active (heap [k]) = k; 886 ev_active (ANHE_w (heap [k])) = k;
862 k = p; 887 k = p;
863 } 888 }
864 889
865 heap [k] = w; 890 heap [k] = w;
866 ev_active (heap [k]) = k; 891 ev_active (ANHE_w (heap [k])) = k;
867} 892}
868 893
869/* away from the root */ 894/* away from the root */
870void inline_speed 895void inline_speed
871downheap (WT *heap, int N, int k) 896downheap (ANHE *heap, int N, int k)
872{ 897{
873 WT w = heap [k]; 898 ANHE he = heap [k];
874 899
875 for (;;) 900 for (;;)
876 { 901 {
877 int c = k << 1; 902 int c = k << 1;
878 903
879 if (c > N) 904 if (c > N)
880 break; 905 break;
881 906
882 c += c + 1 < N && heap [c]->at > heap [c + 1]->at 907 c += c + 1 < N && ANHE_at (heap [c]) > ANHE_at (heap [c + 1])
883 ? 1 : 0; 908 ? 1 : 0;
884 909
885 if (w->at <= heap [c]->at) 910 if (w->at <= ANHE_at (heap [c]))
886 break; 911 break;
887 912
888 heap [k] = heap [c]; 913 heap [k] = heap [c];
889 ((W)heap [k])->active = k; 914 ev_active (ANHE_w (heap [k])) = k;
890 915
891 k = c; 916 k = c;
892 } 917 }
893 918
894 heap [k] = w; 919 heap [k] = he;
895 ev_active (heap [k]) = k; 920 ev_active (ANHE_w (he)) = k;
896} 921}
897#endif 922#endif
898 923
899void inline_size 924void inline_size
900adjustheap (WT *heap, int N, int k) 925adjustheap (ANHE *heap, int N, int k)
901{ 926{
902 upheap (heap, k); 927 upheap (heap, k);
903 downheap (heap, N, k); 928 downheap (heap, N, k);
904} 929}
905 930
1569#endif 1594#endif
1570 1595
1571void inline_size 1596void inline_size
1572timers_reify (EV_P) 1597timers_reify (EV_P)
1573{ 1598{
1574 while (timercnt && ev_at (timers [HEAP0]) <= mn_now) 1599 while (timercnt && ANHE_at (timers [HEAP0]) <= mn_now)
1575 { 1600 {
1576 ev_timer *w = (ev_timer *)timers [HEAP0]; 1601 ev_timer *w = (ev_timer *)ANHE_w (timers [HEAP0]);
1577 1602
1578 /*assert (("inactive timer on timer heap detected", ev_is_active (w)));*/ 1603 /*assert (("inactive timer on timer heap detected", ev_is_active (w)));*/
1579 1604
1580 /* first reschedule or stop timer */ 1605 /* first reschedule or stop timer */
1581 if (w->repeat) 1606 if (w->repeat)
1597 1622
1598#if EV_PERIODIC_ENABLE 1623#if EV_PERIODIC_ENABLE
1599void inline_size 1624void inline_size
1600periodics_reify (EV_P) 1625periodics_reify (EV_P)
1601{ 1626{
1602 while (periodiccnt && ev_at (periodics [HEAP0]) <= ev_rt_now) 1627 while (periodiccnt && ANHE_at (periodics [HEAP0]) <= ev_rt_now)
1603 { 1628 {
1604 ev_periodic *w = (ev_periodic *)periodics [HEAP0]; 1629 ev_periodic *w = (ev_periodic *)ANHE_w (periodics [HEAP0]);
1605 1630
1606 /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/ 1631 /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/
1607 1632
1608 /* first reschedule or stop timer */ 1633 /* first reschedule or stop timer */
1609 if (w->reschedule_cb) 1634 if (w->reschedule_cb)
1630periodics_reschedule (EV_P) 1655periodics_reschedule (EV_P)
1631{ 1656{
1632 int i; 1657 int i;
1633 1658
1634 /* adjust periodics after time jump */ 1659 /* adjust periodics after time jump */
1635 for (i = 1; i <= periodiccnt; ++i) 1660 for (i = HEAP0; i < periodiccnt + HEAP0; ++i)
1636 { 1661 {
1637 ev_periodic *w = (ev_periodic *)periodics [i]; 1662 ev_periodic *w = (ev_periodic *)ANHE_w (periodics [i]);
1638 1663
1639 if (w->reschedule_cb) 1664 if (w->reschedule_cb)
1640 ev_at (w) = w->reschedule_cb (w, ev_rt_now); 1665 ev_at (w) = w->reschedule_cb (w, ev_rt_now);
1641 else if (w->interval) 1666 else if (w->interval)
1642 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;
1643 } 1668 }
1644 1669
1645 /* now rebuild the heap */ 1670 /* now rebuild the heap, this for the 2-heap, inefficient for the 4-heap, but correct */
1646 for (i = periodiccnt >> 1; --i; ) 1671 for (i = periodiccnt >> 1; --i; )
1647 downheap (periodics, periodiccnt, i + HEAP0); 1672 downheap (periodics, periodiccnt, i + HEAP0);
1648} 1673}
1649#endif 1674#endif
1650 1675
1706 { 1731 {
1707#if EV_PERIODIC_ENABLE 1732#if EV_PERIODIC_ENABLE
1708 periodics_reschedule (EV_A); 1733 periodics_reschedule (EV_A);
1709#endif 1734#endif
1710 /* 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 */
1711 for (i = 1; i <= timercnt; ++i) 1736 for (i = 0; i < timercnt; ++i)
1712 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 }
1713 } 1742 }
1714 1743
1715 mn_now = ev_rt_now; 1744 mn_now = ev_rt_now;
1716 } 1745 }
1717} 1746}
1787 1816
1788 waittime = MAX_BLOCKTIME; 1817 waittime = MAX_BLOCKTIME;
1789 1818
1790 if (timercnt) 1819 if (timercnt)
1791 { 1820 {
1792 ev_tstamp to = ev_at (timers [HEAP0]) - mn_now + backend_fudge; 1821 ev_tstamp to = ANHE_at (timers [HEAP0]) - mn_now + backend_fudge;
1793 if (waittime > to) waittime = to; 1822 if (waittime > to) waittime = to;
1794 } 1823 }
1795 1824
1796#if EV_PERIODIC_ENABLE 1825#if EV_PERIODIC_ENABLE
1797 if (periodiccnt) 1826 if (periodiccnt)
1798 { 1827 {
1799 ev_tstamp to = ev_at (periodics [HEAP0]) - ev_rt_now + backend_fudge; 1828 ev_tstamp to = ANHE_at (periodics [HEAP0]) - ev_rt_now + backend_fudge;
1800 if (waittime > to) waittime = to; 1829 if (waittime > to) waittime = to;
1801 } 1830 }
1802#endif 1831#endif
1803 1832
1804 if (expect_false (waittime < timeout_blocktime)) 1833 if (expect_false (waittime < timeout_blocktime))
1975 ev_at (w) += mn_now; 2004 ev_at (w) += mn_now;
1976 2005
1977 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.));
1978 2007
1979 ev_start (EV_A_ (W)w, ++timercnt + HEAP0 - 1); 2008 ev_start (EV_A_ (W)w, ++timercnt + HEAP0 - 1);
1980 array_needsize (WT, timers, timermax, timercnt + HEAP0, EMPTY2); 2009 array_needsize (ANHE, timers, timermax, ev_active (w) + 1, EMPTY2);
1981 timers [ev_active (w)] = (WT)w; 2010 ANHE_w (timers [ev_active (w)]) = (WT)w;
2011 ANHE_at_set (timers [ev_active (w)]);
1982 upheap (timers, ev_active (w)); 2012 upheap (timers, ev_active (w));
1983 2013
1984 /*assert (("internal timer heap corruption", timers [ev_active (w)] == w));*/ 2014 /*assert (("internal timer heap corruption", timers [ev_active (w)] == w));*/
1985} 2015}
1986 2016
1992 return; 2022 return;
1993 2023
1994 { 2024 {
1995 int active = ev_active (w); 2025 int active = ev_active (w);
1996 2026
1997 assert (("internal timer heap corruption", timers [active] == (WT)w)); 2027 assert (("internal timer heap corruption", ANHE_w (timers [active]) == (WT)w));
1998 2028
1999 if (expect_true (active < timercnt + HEAP0 - 1)) 2029 if (expect_true (active < timercnt + HEAP0 - 1))
2000 { 2030 {
2001 timers [active] = timers [timercnt + HEAP0 - 1]; 2031 timers [active] = timers [timercnt + HEAP0 - 1];
2002 adjustheap (timers, timercnt, active); 2032 adjustheap (timers, timercnt, active);
2016 if (ev_is_active (w)) 2046 if (ev_is_active (w))
2017 { 2047 {
2018 if (w->repeat) 2048 if (w->repeat)
2019 { 2049 {
2020 ev_at (w) = mn_now + w->repeat; 2050 ev_at (w) = mn_now + w->repeat;
2051 ANHE_at_set (timers [ev_active (w)]);
2021 adjustheap (timers, timercnt, ev_active (w)); 2052 adjustheap (timers, timercnt, ev_active (w));
2022 } 2053 }
2023 else 2054 else
2024 ev_timer_stop (EV_A_ w); 2055 ev_timer_stop (EV_A_ w);
2025 } 2056 }
2047 } 2078 }
2048 else 2079 else
2049 ev_at (w) = w->offset; 2080 ev_at (w) = w->offset;
2050 2081
2051 ev_start (EV_A_ (W)w, ++periodiccnt + HEAP0 - 1); 2082 ev_start (EV_A_ (W)w, ++periodiccnt + HEAP0 - 1);
2052 array_needsize (WT, periodics, periodicmax, periodiccnt + HEAP0, EMPTY2); 2083 array_needsize (ANHE, periodics, periodicmax, ev_active (w) + 1, EMPTY2);
2053 periodics [ev_active (w)] = (WT)w; 2084 ANHE_w (periodics [ev_active (w)]) = (WT)w;
2054 upheap (periodics, ev_active (w)); 2085 upheap (periodics, ev_active (w));
2055 2086
2056 /*assert (("internal periodic heap corruption", periodics [ev_active (w)] == w));*/ 2087 /*assert (("internal periodic heap corruption", ANHE_w (periodics [ev_active (w)]) == (WT)w));*/
2057} 2088}
2058 2089
2059void noinline 2090void noinline
2060ev_periodic_stop (EV_P_ ev_periodic *w) 2091ev_periodic_stop (EV_P_ ev_periodic *w)
2061{ 2092{
2064 return; 2095 return;
2065 2096
2066 { 2097 {
2067 int active = ev_active (w); 2098 int active = ev_active (w);
2068 2099
2069 assert (("internal periodic heap corruption", periodics [active] == (WT)w)); 2100 assert (("internal periodic heap corruption", ANHE_w (periodics [active]) == (WT)w));
2070 2101
2071 if (expect_true (active < periodiccnt + HEAP0 - 1)) 2102 if (expect_true (active < periodiccnt + HEAP0 - 1))
2072 { 2103 {
2073 periodics [active] = periodics [periodiccnt + HEAP0 - 1]; 2104 periodics [active] = periodics [periodiccnt + HEAP0 - 1];
2074 adjustheap (periodics, periodiccnt, active); 2105 adjustheap (periodics, periodiccnt, active);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines