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

Comparing libev/ev.c (file contents):
Revision 1.233 by root, Tue May 6 23:34: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
1370void 1479void
1371ev_loop_fork (EV_P) 1480ev_loop_fork (EV_P)
1372{ 1481{
1373 postfork = 1; /* must be in line with ev_default_fork */ 1482 postfork = 1; /* must be in line with ev_default_fork */
1374} 1483}
1375
1376#endif 1484#endif
1377 1485
1378#if EV_MULTIPLICITY 1486#if EV_MULTIPLICITY
1379struct ev_loop * 1487struct ev_loop *
1380ev_default_loop_init (unsigned int flags) 1488ev_default_loop_init (unsigned int flags)
1461 EV_CB_INVOKE (p->w, p->events); 1569 EV_CB_INVOKE (p->w, p->events);
1462 } 1570 }
1463 } 1571 }
1464} 1572}
1465 1573
1574#if EV_IDLE_ENABLE
1575void inline_size
1576idle_reify (EV_P)
1577{
1578 if (expect_false (idleall))
1579 {
1580 int pri;
1581
1582 for (pri = NUMPRI; pri--; )
1583 {
1584 if (pendingcnt [pri])
1585 break;
1586
1587 if (idlecnt [pri])
1588 {
1589 queue_events (EV_A_ (W *)idles [pri], idlecnt [pri], EV_IDLE);
1590 break;
1591 }
1592 }
1593 }
1594}
1595#endif
1596
1466void inline_size 1597void inline_size
1467timers_reify (EV_P) 1598timers_reify (EV_P)
1468{ 1599{
1469 while (timercnt && ev_at (timers [1]) <= mn_now) 1600 while (timercnt && ANHE_at (timers [HEAP0]) <= mn_now)
1470 { 1601 {
1471 ev_timer *w = (ev_timer *)timers [1]; 1602 ev_timer *w = (ev_timer *)ANHE_w (timers [HEAP0]);
1472 1603
1473 /*assert (("inactive timer on timer heap detected", ev_is_active (w)));*/ 1604 /*assert (("inactive timer on timer heap detected", ev_is_active (w)));*/
1474 1605
1475 /* first reschedule or stop timer */ 1606 /* first reschedule or stop timer */
1476 if (w->repeat) 1607 if (w->repeat)
1479 1610
1480 ev_at (w) += w->repeat; 1611 ev_at (w) += w->repeat;
1481 if (ev_at (w) < mn_now) 1612 if (ev_at (w) < mn_now)
1482 ev_at (w) = mn_now; 1613 ev_at (w) = mn_now;
1483 1614
1615 ANHE_at_set (timers [HEAP0]);
1484 downheap (timers, timercnt, 1); 1616 downheap (timers, timercnt, HEAP0);
1485 } 1617 }
1486 else 1618 else
1487 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ 1619 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
1488 1620
1489 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); 1621 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT);
1492 1624
1493#if EV_PERIODIC_ENABLE 1625#if EV_PERIODIC_ENABLE
1494void inline_size 1626void inline_size
1495periodics_reify (EV_P) 1627periodics_reify (EV_P)
1496{ 1628{
1497 while (periodiccnt && ev_at (periodics [1]) <= ev_rt_now) 1629 while (periodiccnt && ANHE_at (periodics [HEAP0]) <= ev_rt_now)
1498 { 1630 {
1499 ev_periodic *w = (ev_periodic *)periodics [1]; 1631 ev_periodic *w = (ev_periodic *)ANHE_w (periodics [HEAP0]);
1500 1632
1501 /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/ 1633 /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/
1502 1634
1503 /* first reschedule or stop timer */ 1635 /* first reschedule or stop timer */
1504 if (w->reschedule_cb) 1636 if (w->reschedule_cb)
1505 { 1637 {
1506 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);
1507 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]);
1508 downheap (periodics, periodiccnt, 1); 1641 downheap (periodics, periodiccnt, HEAP0);
1509 } 1642 }
1510 else if (w->interval) 1643 else if (w->interval)
1511 { 1644 {
1512 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;
1513 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;
1514 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]);
1515 downheap (periodics, periodiccnt, 1); 1649 downheap (periodics, periodiccnt, HEAP0);
1516 } 1650 }
1517 else 1651 else
1518 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ 1652 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
1519 1653
1520 ev_feed_event (EV_A_ (W)w, EV_PERIODIC); 1654 ev_feed_event (EV_A_ (W)w, EV_PERIODIC);
1525periodics_reschedule (EV_P) 1659periodics_reschedule (EV_P)
1526{ 1660{
1527 int i; 1661 int i;
1528 1662
1529 /* adjust periodics after time jump */ 1663 /* adjust periodics after time jump */
1530 for (i = 1; i <= periodiccnt; ++i) 1664 for (i = HEAP0; i < periodiccnt + HEAP0; ++i)
1531 { 1665 {
1532 ev_periodic *w = (ev_periodic *)periodics [i]; 1666 ev_periodic *w = (ev_periodic *)ANHE_w (periodics [i]);
1533 1667
1534 if (w->reschedule_cb) 1668 if (w->reschedule_cb)
1535 ev_at (w) = w->reschedule_cb (w, ev_rt_now); 1669 ev_at (w) = w->reschedule_cb (w, ev_rt_now);
1536 else if (w->interval) 1670 else if (w->interval)
1537 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;
1538 }
1539 1672
1540 /* 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 */
1541 for (i = periodiccnt >> 1; i--; ) 1677 for (i = periodiccnt >> 1; --i; )
1542 downheap (periodics, periodiccnt, i); 1678 downheap (periodics, periodiccnt, i + HEAP0);
1543}
1544#endif
1545
1546#if EV_IDLE_ENABLE
1547void inline_size
1548idle_reify (EV_P)
1549{
1550 if (expect_false (idleall))
1551 {
1552 int pri;
1553
1554 for (pri = NUMPRI; pri--; )
1555 {
1556 if (pendingcnt [pri])
1557 break;
1558
1559 if (idlecnt [pri])
1560 {
1561 queue_events (EV_A_ (W *)idles [pri], idlecnt [pri], EV_IDLE);
1562 break;
1563 }
1564 }
1565 }
1566} 1679}
1567#endif 1680#endif
1568 1681
1569void inline_speed 1682void inline_speed
1570time_update (EV_P_ ev_tstamp max_block) 1683time_update (EV_P_ ev_tstamp max_block)
1599 */ 1712 */
1600 for (i = 4; --i; ) 1713 for (i = 4; --i; )
1601 { 1714 {
1602 rtmn_diff = ev_rt_now - mn_now; 1715 rtmn_diff = ev_rt_now - mn_now;
1603 1716
1604 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) 1717 if (expect_true (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP))
1605 return; /* all is well */ 1718 return; /* all is well */
1606 1719
1607 ev_rt_now = ev_time (); 1720 ev_rt_now = ev_time ();
1608 mn_now = get_clock (); 1721 mn_now = get_clock ();
1609 now_floor = mn_now; 1722 now_floor = mn_now;
1624 { 1737 {
1625#if EV_PERIODIC_ENABLE 1738#if EV_PERIODIC_ENABLE
1626 periodics_reschedule (EV_A); 1739 periodics_reschedule (EV_A);
1627#endif 1740#endif
1628 /* 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 */
1629 for (i = 1; i <= timercnt; ++i) 1742 for (i = 0; i < timercnt; ++i)
1630 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 }
1631 } 1748 }
1632 1749
1633 mn_now = ev_rt_now; 1750 mn_now = ev_rt_now;
1634 } 1751 }
1635} 1752}
1705 1822
1706 waittime = MAX_BLOCKTIME; 1823 waittime = MAX_BLOCKTIME;
1707 1824
1708 if (timercnt) 1825 if (timercnt)
1709 { 1826 {
1710 ev_tstamp to = ev_at (timers [1]) - mn_now + backend_fudge; 1827 ev_tstamp to = ANHE_at (timers [HEAP0]) - mn_now + backend_fudge;
1711 if (waittime > to) waittime = to; 1828 if (waittime > to) waittime = to;
1712 } 1829 }
1713 1830
1714#if EV_PERIODIC_ENABLE 1831#if EV_PERIODIC_ENABLE
1715 if (periodiccnt) 1832 if (periodiccnt)
1716 { 1833 {
1717 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;
1718 if (waittime > to) waittime = to; 1835 if (waittime > to) waittime = to;
1719 } 1836 }
1720#endif 1837#endif
1721 1838
1722 if (expect_false (waittime < timeout_blocktime)) 1839 if (expect_false (waittime < timeout_blocktime))
1874{ 1991{
1875 clear_pending (EV_A_ (W)w); 1992 clear_pending (EV_A_ (W)w);
1876 if (expect_false (!ev_is_active (w))) 1993 if (expect_false (!ev_is_active (w)))
1877 return; 1994 return;
1878 1995
1879 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));
1880 1997
1881 wlist_del (&anfds[w->fd].head, (WL)w); 1998 wlist_del (&anfds[w->fd].head, (WL)w);
1882 ev_stop (EV_A_ (W)w); 1999 ev_stop (EV_A_ (W)w);
1883 2000
1884 fd_change (EV_A_ w->fd, 1); 2001 fd_change (EV_A_ w->fd, 1);
1892 2009
1893 ev_at (w) += mn_now; 2010 ev_at (w) += mn_now;
1894 2011
1895 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.));
1896 2013
1897 ev_start (EV_A_ (W)w, ++timercnt); 2014 ev_start (EV_A_ (W)w, ++timercnt + HEAP0 - 1);
1898 array_needsize (WT, timers, timermax, timercnt + 1, EMPTY2); 2015 array_needsize (ANHE, timers, timermax, ev_active (w) + 1, EMPTY2);
1899 timers [timercnt] = (WT)w; 2016 ANHE_w (timers [ev_active (w)]) = (WT)w;
2017 ANHE_at_set (timers [ev_active (w)]);
1900 upheap (timers, timercnt); 2018 upheap (timers, ev_active (w));
1901 2019
1902 /*assert (("internal timer heap corruption", timers [ev_active (w)] == w));*/ 2020 /*assert (("internal timer heap corruption", timers [ev_active (w)] == (WT)w));*/
1903} 2021}
1904 2022
1905void noinline 2023void noinline
1906ev_timer_stop (EV_P_ ev_timer *w) 2024ev_timer_stop (EV_P_ ev_timer *w)
1907{ 2025{
1910 return; 2028 return;
1911 2029
1912 { 2030 {
1913 int active = ev_active (w); 2031 int active = ev_active (w);
1914 2032
1915 assert (("internal timer heap corruption", timers [active] == (WT)w)); 2033 assert (("internal timer heap corruption", ANHE_w (timers [active]) == (WT)w));
1916 2034
1917 if (expect_true (active < timercnt)) 2035 if (expect_true (active < timercnt + HEAP0 - 1))
1918 { 2036 {
1919 timers [active] = timers [timercnt]; 2037 timers [active] = timers [timercnt + HEAP0 - 1];
1920 adjustheap (timers, timercnt, active); 2038 adjustheap (timers, timercnt, active);
1921 } 2039 }
1922 2040
1923 --timercnt; 2041 --timercnt;
1924 } 2042 }
1934 if (ev_is_active (w)) 2052 if (ev_is_active (w))
1935 { 2053 {
1936 if (w->repeat) 2054 if (w->repeat)
1937 { 2055 {
1938 ev_at (w) = mn_now + w->repeat; 2056 ev_at (w) = mn_now + w->repeat;
2057 ANHE_at_set (timers [ev_active (w)]);
1939 adjustheap (timers, timercnt, ev_active (w)); 2058 adjustheap (timers, timercnt, ev_active (w));
1940 } 2059 }
1941 else 2060 else
1942 ev_timer_stop (EV_A_ w); 2061 ev_timer_stop (EV_A_ w);
1943 } 2062 }
1964 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;
1965 } 2084 }
1966 else 2085 else
1967 ev_at (w) = w->offset; 2086 ev_at (w) = w->offset;
1968 2087
1969 ev_start (EV_A_ (W)w, ++periodiccnt); 2088 ev_start (EV_A_ (W)w, ++periodiccnt + HEAP0 - 1);
1970 array_needsize (WT, periodics, periodicmax, periodiccnt + 1, EMPTY2); 2089 array_needsize (ANHE, periodics, periodicmax, ev_active (w) + 1, EMPTY2);
1971 periodics [periodiccnt] = (WT)w; 2090 ANHE_w (periodics [ev_active (w)]) = (WT)w;
1972 upheap (periodics, periodiccnt); 2091 upheap (periodics, ev_active (w));
1973 2092
1974 /*assert (("internal periodic heap corruption", periodics [ev_active (w)] == w));*/ 2093 /*assert (("internal periodic heap corruption", ANHE_w (periodics [ev_active (w)]) == (WT)w));*/
1975} 2094}
1976 2095
1977void noinline 2096void noinline
1978ev_periodic_stop (EV_P_ ev_periodic *w) 2097ev_periodic_stop (EV_P_ ev_periodic *w)
1979{ 2098{
1982 return; 2101 return;
1983 2102
1984 { 2103 {
1985 int active = ev_active (w); 2104 int active = ev_active (w);
1986 2105
1987 assert (("internal periodic heap corruption", periodics [active] == (WT)w)); 2106 assert (("internal periodic heap corruption", ANHE_w (periodics [active]) == (WT)w));
1988 2107
1989 if (expect_true (active < periodiccnt)) 2108 if (expect_true (active < periodiccnt + HEAP0 - 1))
1990 { 2109 {
1991 periodics [active] = periodics [periodiccnt]; 2110 periodics [active] = periodics [periodiccnt + HEAP0 - 1];
1992 adjustheap (periodics, periodiccnt, active); 2111 adjustheap (periodics, periodiccnt, active);
1993 } 2112 }
1994 2113
1995 --periodiccnt; 2114 --periodiccnt;
1996 } 2115 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines