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

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines