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

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines