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.242 by root, Fri May 9 14:07:19 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#define EV_HEAP_CACHE_AT 0
436#if EV_HEAP_CACHE_AT
437 typedef struct {
438 WT w;
439 ev_tstamp at;
440 } ANHE;
441
442 #define ANHE_w(he) (he).w /* access watcher, read-write */
443 #define ANHE_at(he) (he).at /* access cached at, read-only */
444 #define ANHE_at_set(he) (he).at = (he).w->at /* update at from watcher */
445#else
446 typedef WT ANHE;
447
448 #define ANHE_w(he) (he)
449 #define ANHE_at(he) (he)->at
450 #define ANHE_at_set(he)
431#endif 451#endif
432 452
433#if EV_MULTIPLICITY 453#if EV_MULTIPLICITY
434 454
435 struct ev_loop 455 struct ev_loop
759 } 779 }
760} 780}
761 781
762/*****************************************************************************/ 782/*****************************************************************************/
763 783
784/*
785 * the heap functions want a real array index. array index 0 uis guaranteed to not
786 * be in-use at any time. the first heap entry is at array [HEAP0]. DHEAP gives
787 * the branching factor of the d-tree.
788 */
789
790/*
791 * at the moment we allow libev the luxury of two heaps,
792 * a small-code-size 2-heap one and a ~1.5kb larger 4-heap
793 * which is more cache-efficient.
794 * the difference is about 5% with 50000+ watchers.
795 */
796#define EV_USE_4HEAP !EV_MINIMAL
797#if EV_USE_4HEAP
798
799#define DHEAP 4
800#define HEAP0 (DHEAP - 1) /* index of first element in heap */
801
764/* towards the root */ 802/* towards the root */
765void inline_speed 803void inline_speed
766upheap (WT *heap, int k) 804upheap (ANHE *heap, int k)
767{ 805{
768 WT w = heap [k]; 806 ANHE he = heap [k];
769 807
770 for (;;) 808 for (;;)
771 { 809 {
772 int p = k >> 1; 810 int p = ((k - HEAP0 - 1) / DHEAP) + HEAP0;
773 811
774 /* maybe we could use a dummy element at heap [0]? */ 812 if (p == k || ANHE_at (heap [p]) <= ANHE_at (he))
775 if (!p || heap [p]->at <= w->at)
776 break; 813 break;
777 814
778 heap [k] = heap [p]; 815 heap [k] = heap [p];
779 ev_active (heap [k]) = k; 816 ev_active (ANHE_w (heap [k])) = k;
780 k = p; 817 k = p;
781 } 818 }
782 819
820 ev_active (ANHE_w (he)) = k;
783 heap [k] = w; 821 heap [k] = he;
784 ev_active (heap [k]) = k;
785} 822}
786 823
787/* away from the root */ 824/* away from the root */
788void inline_speed 825void inline_speed
789downheap (WT *heap, int N, int k) 826downheap (ANHE *heap, int N, int k)
790{ 827{
791 WT w = heap [k]; 828 ANHE he = heap [k];
829 ANHE *E = heap + N + HEAP0;
830
831 for (;;)
832 {
833 ev_tstamp minat;
834 ANHE *minpos;
835 ANHE *pos = heap + DHEAP * (k - HEAP0) + HEAP0;
836
837 // find minimum child
838 if (expect_true (pos + DHEAP - 1 < E))
839 {
840 /* fast path */ (minpos = pos + 0), (minat = ANHE_at (*minpos));
841 if (ANHE_at (pos [1]) < minat) (minpos = pos + 1), (minat = ANHE_at (*minpos));
842 if (ANHE_at (pos [2]) < minat) (minpos = pos + 2), (minat = ANHE_at (*minpos));
843 if (ANHE_at (pos [3]) < minat) (minpos = pos + 3), (minat = ANHE_at (*minpos));
844 }
845 else if (pos < E)
846 {
847 /* slow path */ (minpos = pos + 0), (minat = ANHE_at (*minpos));
848 if (pos + 1 < E && ANHE_at (pos [1]) < minat) (minpos = pos + 1), (minat = ANHE_at (*minpos));
849 if (pos + 2 < E && ANHE_at (pos [2]) < minat) (minpos = pos + 2), (minat = ANHE_at (*minpos));
850 if (pos + 3 < E && ANHE_at (pos [3]) < minat) (minpos = pos + 3), (minat = ANHE_at (*minpos));
851 }
852 else
853 break;
854
855 if (ANHE_at (he) <= minat)
856 break;
857
858 ev_active (ANHE_w (*minpos)) = k;
859 heap [k] = *minpos;
860
861 k = minpos - heap;
862 }
863
864 ev_active (ANHE_w (he)) = k;
865 heap [k] = he;
866}
867
868#else // 4HEAP
869
870#define HEAP0 1
871
872/* towards the root */
873void inline_speed
874upheap (ANHE *heap, int k)
875{
876 ANHE he = heap [k];
877
878 for (;;)
879 {
880 int p = k >> 1;
881
882 /* maybe we could use a dummy element at heap [0]? */
883 if (!p || ANHE_at (heap [p]) <= ANHE_at (he))
884 break;
885
886 heap [k] = heap [p];
887 ev_active (ANHE_w (heap [k])) = k;
888 k = p;
889 }
890
891 heap [k] = w;
892 ev_active (ANHE_w (heap [k])) = k;
893}
894
895/* away from the root */
896void inline_speed
897downheap (ANHE *heap, int N, int k)
898{
899 ANHE he = heap [k];
792 900
793 for (;;) 901 for (;;)
794 { 902 {
795 int c = k << 1; 903 int c = k << 1;
796 904
797 if (c > N) 905 if (c > N)
798 break; 906 break;
799 907
800 c += c < N && heap [c]->at > heap [c + 1]->at 908 c += c + 1 < N && ANHE_at (heap [c]) > ANHE_at (heap [c + 1])
801 ? 1 : 0; 909 ? 1 : 0;
802 910
803 if (w->at <= heap [c]->at) 911 if (w->at <= ANHE_at (heap [c]))
804 break; 912 break;
805 913
806 heap [k] = heap [c]; 914 heap [k] = heap [c];
807 ev_active (heap [k]) = k; 915 ev_active (ANHE_w (heap [k])) = k;
808 916
809 k = c; 917 k = c;
810 } 918 }
811 919
812 heap [k] = w; 920 heap [k] = he;
813 ev_active (heap [k]) = k; 921 ev_active (ANHE_w (he)) = k;
814} 922}
923#endif
815 924
816void inline_size 925void inline_size
817adjustheap (WT *heap, int N, int k) 926adjustheap (ANHE *heap, int N, int k)
818{ 927{
819 upheap (heap, k); 928 upheap (heap, k);
820 downheap (heap, N, k); 929 downheap (heap, N, k);
821} 930}
822 931
1486#endif 1595#endif
1487 1596
1488void inline_size 1597void inline_size
1489timers_reify (EV_P) 1598timers_reify (EV_P)
1490{ 1599{
1491 while (timercnt && ev_at (timers [1]) <= mn_now) 1600 while (timercnt && ANHE_at (timers [HEAP0]) <= mn_now)
1492 { 1601 {
1493 ev_timer *w = (ev_timer *)timers [1]; 1602 ev_timer *w = (ev_timer *)ANHE_w (timers [HEAP0]);
1494 1603
1495 /*assert (("inactive timer on timer heap detected", ev_is_active (w)));*/ 1604 /*assert (("inactive timer on timer heap detected", ev_is_active (w)));*/
1496 1605
1497 /* first reschedule or stop timer */ 1606 /* first reschedule or stop timer */
1498 if (w->repeat) 1607 if (w->repeat)
1501 1610
1502 ev_at (w) += w->repeat; 1611 ev_at (w) += w->repeat;
1503 if (ev_at (w) < mn_now) 1612 if (ev_at (w) < mn_now)
1504 ev_at (w) = mn_now; 1613 ev_at (w) = mn_now;
1505 1614
1615 ANHE_at_set (timers [HEAP0]);
1506 downheap (timers, timercnt, 1); 1616 downheap (timers, timercnt, HEAP0);
1507 } 1617 }
1508 else 1618 else
1509 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ 1619 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
1510 1620
1511 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); 1621 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT);
1514 1624
1515#if EV_PERIODIC_ENABLE 1625#if EV_PERIODIC_ENABLE
1516void inline_size 1626void inline_size
1517periodics_reify (EV_P) 1627periodics_reify (EV_P)
1518{ 1628{
1519 while (periodiccnt && ev_at (periodics [1]) <= ev_rt_now) 1629 while (periodiccnt && ANHE_at (periodics [HEAP0]) <= ev_rt_now)
1520 { 1630 {
1521 ev_periodic *w = (ev_periodic *)periodics [1]; 1631 ev_periodic *w = (ev_periodic *)ANHE_w (periodics [HEAP0]);
1522 1632
1523 /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/ 1633 /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/
1524 1634
1525 /* first reschedule or stop timer */ 1635 /* first reschedule or stop timer */
1526 if (w->reschedule_cb) 1636 if (w->reschedule_cb)
1527 { 1637 {
1528 ev_at (w) = w->reschedule_cb (w, ev_rt_now + TIME_EPSILON); 1638 ev_at (w) = w->reschedule_cb (w, ev_rt_now + TIME_EPSILON);
1529 assert (("ev_periodic reschedule callback returned time in the past", ev_at (w) > ev_rt_now)); 1639 assert (("ev_periodic reschedule callback returned time in the past", ev_at (w) > ev_rt_now));
1640 ANHE_at_set (periodics [HEAP0]);
1530 downheap (periodics, periodiccnt, 1); 1641 downheap (periodics, periodiccnt, HEAP0);
1531 } 1642 }
1532 else if (w->interval) 1643 else if (w->interval)
1533 { 1644 {
1534 ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; 1645 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; 1646 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)); 1647 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ev_at (w) > ev_rt_now));
1648 ANHE_at_set (periodics [HEAP0]);
1537 downheap (periodics, periodiccnt, 1); 1649 downheap (periodics, periodiccnt, HEAP0);
1538 } 1650 }
1539 else 1651 else
1540 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ 1652 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
1541 1653
1542 ev_feed_event (EV_A_ (W)w, EV_PERIODIC); 1654 ev_feed_event (EV_A_ (W)w, EV_PERIODIC);
1547periodics_reschedule (EV_P) 1659periodics_reschedule (EV_P)
1548{ 1660{
1549 int i; 1661 int i;
1550 1662
1551 /* adjust periodics after time jump */ 1663 /* adjust periodics after time jump */
1552 for (i = 1; i <= periodiccnt; ++i) 1664 for (i = HEAP0; i < periodiccnt + HEAP0; ++i)
1553 { 1665 {
1554 ev_periodic *w = (ev_periodic *)periodics [i]; 1666 ev_periodic *w = (ev_periodic *)ANHE_w (periodics [i]);
1555 1667
1556 if (w->reschedule_cb) 1668 if (w->reschedule_cb)
1557 ev_at (w) = w->reschedule_cb (w, ev_rt_now); 1669 ev_at (w) = w->reschedule_cb (w, ev_rt_now);
1558 else if (w->interval) 1670 else if (w->interval)
1559 ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; 1671 ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval;
1560 }
1561 1672
1562 /* now rebuild the heap */ 1673 ANHE_at_set (periodics [i]);
1674 }
1675
1676 /* now rebuild the heap, this for the 2-heap, inefficient for the 4-heap, but correct */
1563 for (i = periodiccnt >> 1; i--; ) 1677 for (i = periodiccnt >> 1; --i; )
1564 downheap (periodics, periodiccnt, i); 1678 downheap (periodics, periodiccnt, i + HEAP0);
1565} 1679}
1566#endif 1680#endif
1567 1681
1568void inline_speed 1682void inline_speed
1569time_update (EV_P_ ev_tstamp max_block) 1683time_update (EV_P_ ev_tstamp max_block)
1623 { 1737 {
1624#if EV_PERIODIC_ENABLE 1738#if EV_PERIODIC_ENABLE
1625 periodics_reschedule (EV_A); 1739 periodics_reschedule (EV_A);
1626#endif 1740#endif
1627 /* adjust timers. this is easy, as the offset is the same for all of them */ 1741 /* adjust timers. this is easy, as the offset is the same for all of them */
1628 for (i = 1; i <= timercnt; ++i) 1742 for (i = 0; i < timercnt; ++i)
1629 ev_at (timers [i]) += ev_rt_now - mn_now; 1743 {
1744 ANHE *he = timers + i + HEAP0;
1745 ANHE_w (*he)->at += ev_rt_now - mn_now;
1746 ANHE_at_set (*he);
1747 }
1630 } 1748 }
1631 1749
1632 mn_now = ev_rt_now; 1750 mn_now = ev_rt_now;
1633 } 1751 }
1634} 1752}
1704 1822
1705 waittime = MAX_BLOCKTIME; 1823 waittime = MAX_BLOCKTIME;
1706 1824
1707 if (timercnt) 1825 if (timercnt)
1708 { 1826 {
1709 ev_tstamp to = ev_at (timers [1]) - mn_now + backend_fudge; 1827 ev_tstamp to = ANHE_at (timers [HEAP0]) - mn_now + backend_fudge;
1710 if (waittime > to) waittime = to; 1828 if (waittime > to) waittime = to;
1711 } 1829 }
1712 1830
1713#if EV_PERIODIC_ENABLE 1831#if EV_PERIODIC_ENABLE
1714 if (periodiccnt) 1832 if (periodiccnt)
1715 { 1833 {
1716 ev_tstamp to = ev_at (periodics [1]) - ev_rt_now + backend_fudge; 1834 ev_tstamp to = ANHE_at (periodics [HEAP0]) - ev_rt_now + backend_fudge;
1717 if (waittime > to) waittime = to; 1835 if (waittime > to) waittime = to;
1718 } 1836 }
1719#endif 1837#endif
1720 1838
1721 if (expect_false (waittime < timeout_blocktime)) 1839 if (expect_false (waittime < timeout_blocktime))
1873{ 1991{
1874 clear_pending (EV_A_ (W)w); 1992 clear_pending (EV_A_ (W)w);
1875 if (expect_false (!ev_is_active (w))) 1993 if (expect_false (!ev_is_active (w)))
1876 return; 1994 return;
1877 1995
1878 assert (("ev_io_start called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax)); 1996 assert (("ev_io_stop called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax));
1879 1997
1880 wlist_del (&anfds[w->fd].head, (WL)w); 1998 wlist_del (&anfds[w->fd].head, (WL)w);
1881 ev_stop (EV_A_ (W)w); 1999 ev_stop (EV_A_ (W)w);
1882 2000
1883 fd_change (EV_A_ w->fd, 1); 2001 fd_change (EV_A_ w->fd, 1);
1891 2009
1892 ev_at (w) += mn_now; 2010 ev_at (w) += mn_now;
1893 2011
1894 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); 2012 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
1895 2013
1896 ev_start (EV_A_ (W)w, ++timercnt); 2014 ev_start (EV_A_ (W)w, ++timercnt + HEAP0 - 1);
1897 array_needsize (WT, timers, timermax, timercnt + 1, EMPTY2); 2015 array_needsize (ANHE, timers, timermax, ev_active (w) + 1, EMPTY2);
1898 timers [timercnt] = (WT)w; 2016 ANHE_w (timers [ev_active (w)]) = (WT)w;
2017 ANHE_at_set (timers [ev_active (w)]);
1899 upheap (timers, timercnt); 2018 upheap (timers, ev_active (w));
1900 2019
1901 /*assert (("internal timer heap corruption", timers [ev_active (w)] == w));*/ 2020 /*assert (("internal timer heap corruption", timers [ev_active (w)] == (WT)w));*/
1902} 2021}
1903 2022
1904void noinline 2023void noinline
1905ev_timer_stop (EV_P_ ev_timer *w) 2024ev_timer_stop (EV_P_ ev_timer *w)
1906{ 2025{
1909 return; 2028 return;
1910 2029
1911 { 2030 {
1912 int active = ev_active (w); 2031 int active = ev_active (w);
1913 2032
1914 assert (("internal timer heap corruption", timers [active] == (WT)w)); 2033 assert (("internal timer heap corruption", ANHE_w (timers [active]) == (WT)w));
1915 2034
1916 if (expect_true (active < timercnt)) 2035 if (expect_true (active < timercnt + HEAP0 - 1))
1917 { 2036 {
1918 timers [active] = timers [timercnt]; 2037 timers [active] = timers [timercnt + HEAP0 - 1];
1919 adjustheap (timers, timercnt, active); 2038 adjustheap (timers, timercnt, active);
1920 } 2039 }
1921 2040
1922 --timercnt; 2041 --timercnt;
1923 } 2042 }
1933 if (ev_is_active (w)) 2052 if (ev_is_active (w))
1934 { 2053 {
1935 if (w->repeat) 2054 if (w->repeat)
1936 { 2055 {
1937 ev_at (w) = mn_now + w->repeat; 2056 ev_at (w) = mn_now + w->repeat;
2057 ANHE_at_set (timers [ev_active (w)]);
1938 adjustheap (timers, timercnt, ev_active (w)); 2058 adjustheap (timers, timercnt, ev_active (w));
1939 } 2059 }
1940 else 2060 else
1941 ev_timer_stop (EV_A_ w); 2061 ev_timer_stop (EV_A_ w);
1942 } 2062 }
1963 ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; 2083 ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval;
1964 } 2084 }
1965 else 2085 else
1966 ev_at (w) = w->offset; 2086 ev_at (w) = w->offset;
1967 2087
1968 ev_start (EV_A_ (W)w, ++periodiccnt); 2088 ev_start (EV_A_ (W)w, ++periodiccnt + HEAP0 - 1);
1969 array_needsize (WT, periodics, periodicmax, periodiccnt + 1, EMPTY2); 2089 array_needsize (ANHE, periodics, periodicmax, ev_active (w) + 1, EMPTY2);
1970 periodics [periodiccnt] = (WT)w; 2090 ANHE_w (periodics [ev_active (w)]) = (WT)w;
1971 upheap (periodics, periodiccnt); 2091 upheap (periodics, ev_active (w));
1972 2092
1973 /*assert (("internal periodic heap corruption", periodics [ev_active (w)] == w));*/ 2093 /*assert (("internal periodic heap corruption", ANHE_w (periodics [ev_active (w)]) == (WT)w));*/
1974} 2094}
1975 2095
1976void noinline 2096void noinline
1977ev_periodic_stop (EV_P_ ev_periodic *w) 2097ev_periodic_stop (EV_P_ ev_periodic *w)
1978{ 2098{
1981 return; 2101 return;
1982 2102
1983 { 2103 {
1984 int active = ev_active (w); 2104 int active = ev_active (w);
1985 2105
1986 assert (("internal periodic heap corruption", periodics [active] == (WT)w)); 2106 assert (("internal periodic heap corruption", ANHE_w (periodics [active]) == (WT)w));
1987 2107
1988 if (expect_true (active < periodiccnt)) 2108 if (expect_true (active < periodiccnt + HEAP0 - 1))
1989 { 2109 {
1990 periodics [active] = periodics [periodiccnt]; 2110 periodics [active] = periodics [periodiccnt + HEAP0 - 1];
1991 adjustheap (periodics, periodiccnt, active); 2111 adjustheap (periodics, periodiccnt, active);
1992 } 2112 }
1993 2113
1994 --periodiccnt; 2114 --periodiccnt;
1995 } 2115 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines