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.237 by root, Wed May 7 15:16:56 2008 UTC

759 } 759 }
760} 760}
761 761
762/*****************************************************************************/ 762/*****************************************************************************/
763 763
764/*
765 * 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
767 * which is more cache-efficient.
768 * the difference is about 5% with 50000+ watchers.
769 */
770#define USE_4HEAP !EV_MINIMAL
771#if USE_4HEAP
772
773#define DHEAP 4
774#define HEAP0 (DHEAP - 1) /* index of first element in heap */
775
764/* towards the root */ 776/* towards the root */
765void inline_speed 777void inline_speed
766upheap (WT *heap, int k) 778upheap (WT *heap, int k)
767{ 779{
768 WT w = heap [k]; 780 WT w = heap [k];
769 781
770 for (;;) 782 for (;;)
771 { 783 {
784 int p = ((k - HEAP0 - 1) / DHEAP) + HEAP0;
785
786 if (p >= HEAP0 || heap [p]->at <= w->at)
787 break;
788
789 heap [k] = heap [p];
790 ev_active (heap [k]) = k;
791 k = p;
792 }
793
794 heap [k] = w;
795 ev_active (heap [k]) = k;
796}
797
798/* away from the root */
799void inline_speed
800downheap (WT *heap, int N, int k)
801{
802 WT w = heap [k];
803 WT *E = heap + N + HEAP0;
804
805 for (;;)
806 {
807 ev_tstamp minat;
808 WT *minpos;
809 WT *pos = heap + DHEAP * (k - HEAP0) + HEAP0;
810
811 // find minimum child
812 if (expect_true (pos + DHEAP - 1 < E))
813 {
814 /* fast path */
815 (minpos = pos + 0), (minat = (*minpos)->at);
816 if (pos [1]->at < minat) (minpos = pos + 1), (minat = (*minpos)->at);
817 if (pos [2]->at < minat) (minpos = pos + 2), (minat = (*minpos)->at);
818 if (pos [3]->at < minat) (minpos = pos + 3), (minat = (*minpos)->at);
819 }
820 else
821 {
822 /* slow path */
823 if (pos >= E)
824 break;
825 (minpos = pos + 0), (minat = (*minpos)->at);
826 if (pos + 1 < E && pos [1]->at < minat) (minpos = pos + 1), (minat = (*minpos)->at);
827 if (pos + 2 < E && pos [2]->at < minat) (minpos = pos + 2), (minat = (*minpos)->at);
828 if (pos + 3 < E && pos [3]->at < minat) (minpos = pos + 3), (minat = (*minpos)->at);
829 }
830
831 if (w->at <= minat)
832 break;
833
834 ev_active (*minpos) = k;
835 heap [k] = *minpos;
836
837 k = minpos - heap;
838 }
839
840 heap [k] = w;
841 ev_active (heap [k]) = k;
842}
843
844#else // 4HEAP
845
846#define HEAP0 1
847
848/* towards the root */
849void inline_speed
850upheap (WT *heap, int k)
851{
852 WT w = heap [k];
853
854 for (;;)
855 {
772 int p = k >> 1; 856 int p = k >> 1;
773 857
774 /* maybe we could use a dummy element at heap [0]? */ 858 /* maybe we could use a dummy element at heap [0]? */
775 if (!p || heap [p]->at <= w->at) 859 if (!p || heap [p]->at <= w->at)
776 break; 860 break;
795 int c = k << 1; 879 int c = k << 1;
796 880
797 if (c > N) 881 if (c > N)
798 break; 882 break;
799 883
800 c += c < N && heap [c]->at > heap [c + 1]->at 884 c += c + 1 < N && heap [c]->at > heap [c + 1]->at
801 ? 1 : 0; 885 ? 1 : 0;
802 886
803 if (w->at <= heap [c]->at) 887 if (w->at <= heap [c]->at)
804 break; 888 break;
805 889
806 heap [k] = heap [c]; 890 heap [k] = heap [c];
807 ev_active (heap [k]) = k; 891 ((W)heap [k])->active = k;
808 892
809 k = c; 893 k = c;
810 } 894 }
811 895
812 heap [k] = w; 896 heap [k] = w;
813 ev_active (heap [k]) = k; 897 ev_active (heap [k]) = k;
814} 898}
899#endif
815 900
816void inline_size 901void inline_size
817adjustheap (WT *heap, int N, int k) 902adjustheap (WT *heap, int N, int k)
818{ 903{
819 upheap (heap, k); 904 upheap (heap, k);
1370void 1455void
1371ev_loop_fork (EV_P) 1456ev_loop_fork (EV_P)
1372{ 1457{
1373 postfork = 1; /* must be in line with ev_default_fork */ 1458 postfork = 1; /* must be in line with ev_default_fork */
1374} 1459}
1375
1376#endif 1460#endif
1377 1461
1378#if EV_MULTIPLICITY 1462#if EV_MULTIPLICITY
1379struct ev_loop * 1463struct ev_loop *
1380ev_default_loop_init (unsigned int flags) 1464ev_default_loop_init (unsigned int flags)
1461 EV_CB_INVOKE (p->w, p->events); 1545 EV_CB_INVOKE (p->w, p->events);
1462 } 1546 }
1463 } 1547 }
1464} 1548}
1465 1549
1550#if EV_IDLE_ENABLE
1551void inline_size
1552idle_reify (EV_P)
1553{
1554 if (expect_false (idleall))
1555 {
1556 int pri;
1557
1558 for (pri = NUMPRI; pri--; )
1559 {
1560 if (pendingcnt [pri])
1561 break;
1562
1563 if (idlecnt [pri])
1564 {
1565 queue_events (EV_A_ (W *)idles [pri], idlecnt [pri], EV_IDLE);
1566 break;
1567 }
1568 }
1569 }
1570}
1571#endif
1572
1466void inline_size 1573void inline_size
1467timers_reify (EV_P) 1574timers_reify (EV_P)
1468{ 1575{
1469 while (timercnt && ev_at (timers [1]) <= mn_now) 1576 while (timercnt && ev_at (timers [HEAP0]) <= mn_now)
1470 { 1577 {
1471 ev_timer *w = (ev_timer *)timers [1]; 1578 ev_timer *w = (ev_timer *)timers [HEAP0];
1472 1579
1473 /*assert (("inactive timer on timer heap detected", ev_is_active (w)));*/ 1580 /*assert (("inactive timer on timer heap detected", ev_is_active (w)));*/
1474 1581
1475 /* first reschedule or stop timer */ 1582 /* first reschedule or stop timer */
1476 if (w->repeat) 1583 if (w->repeat)
1479 1586
1480 ev_at (w) += w->repeat; 1587 ev_at (w) += w->repeat;
1481 if (ev_at (w) < mn_now) 1588 if (ev_at (w) < mn_now)
1482 ev_at (w) = mn_now; 1589 ev_at (w) = mn_now;
1483 1590
1484 downheap (timers, timercnt, 1); 1591 downheap (timers, timercnt, HEAP0);
1485 } 1592 }
1486 else 1593 else
1487 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ 1594 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
1488 1595
1489 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); 1596 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT);
1492 1599
1493#if EV_PERIODIC_ENABLE 1600#if EV_PERIODIC_ENABLE
1494void inline_size 1601void inline_size
1495periodics_reify (EV_P) 1602periodics_reify (EV_P)
1496{ 1603{
1497 while (periodiccnt && ev_at (periodics [1]) <= ev_rt_now) 1604 while (periodiccnt && ev_at (periodics [HEAP0]) <= ev_rt_now)
1498 { 1605 {
1499 ev_periodic *w = (ev_periodic *)periodics [1]; 1606 ev_periodic *w = (ev_periodic *)periodics [HEAP0];
1500 1607
1501 /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/ 1608 /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/
1502 1609
1503 /* first reschedule or stop timer */ 1610 /* first reschedule or stop timer */
1504 if (w->reschedule_cb) 1611 if (w->reschedule_cb)
1510 else if (w->interval) 1617 else if (w->interval)
1511 { 1618 {
1512 ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; 1619 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; 1620 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)); 1621 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ev_at (w) > ev_rt_now));
1515 downheap (periodics, periodiccnt, 1); 1622 downheap (periodics, periodiccnt, HEAP0);
1516 } 1623 }
1517 else 1624 else
1518 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ 1625 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
1519 1626
1520 ev_feed_event (EV_A_ (W)w, EV_PERIODIC); 1627 ev_feed_event (EV_A_ (W)w, EV_PERIODIC);
1536 else if (w->interval) 1643 else if (w->interval)
1537 ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; 1644 ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval;
1538 } 1645 }
1539 1646
1540 /* now rebuild the heap */ 1647 /* now rebuild the heap */
1541 for (i = periodiccnt >> 1; i--; ) 1648 for (i = periodiccnt >> 1; --i; )
1542 downheap (periodics, periodiccnt, i); 1649 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} 1650}
1567#endif 1651#endif
1568 1652
1569void inline_speed 1653void inline_speed
1570time_update (EV_P_ ev_tstamp max_block) 1654time_update (EV_P_ ev_tstamp max_block)
1599 */ 1683 */
1600 for (i = 4; --i; ) 1684 for (i = 4; --i; )
1601 { 1685 {
1602 rtmn_diff = ev_rt_now - mn_now; 1686 rtmn_diff = ev_rt_now - mn_now;
1603 1687
1604 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) 1688 if (expect_true (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP))
1605 return; /* all is well */ 1689 return; /* all is well */
1606 1690
1607 ev_rt_now = ev_time (); 1691 ev_rt_now = ev_time ();
1608 mn_now = get_clock (); 1692 mn_now = get_clock ();
1609 now_floor = mn_now; 1693 now_floor = mn_now;
1705 1789
1706 waittime = MAX_BLOCKTIME; 1790 waittime = MAX_BLOCKTIME;
1707 1791
1708 if (timercnt) 1792 if (timercnt)
1709 { 1793 {
1710 ev_tstamp to = ev_at (timers [1]) - mn_now + backend_fudge; 1794 ev_tstamp to = ev_at (timers [HEAP0]) - mn_now + backend_fudge;
1711 if (waittime > to) waittime = to; 1795 if (waittime > to) waittime = to;
1712 } 1796 }
1713 1797
1714#if EV_PERIODIC_ENABLE 1798#if EV_PERIODIC_ENABLE
1715 if (periodiccnt) 1799 if (periodiccnt)
1716 { 1800 {
1717 ev_tstamp to = ev_at (periodics [1]) - ev_rt_now + backend_fudge; 1801 ev_tstamp to = ev_at (periodics [HEAP0]) - ev_rt_now + backend_fudge;
1718 if (waittime > to) waittime = to; 1802 if (waittime > to) waittime = to;
1719 } 1803 }
1720#endif 1804#endif
1721 1805
1722 if (expect_false (waittime < timeout_blocktime)) 1806 if (expect_false (waittime < timeout_blocktime))
1892 1976
1893 ev_at (w) += mn_now; 1977 ev_at (w) += mn_now;
1894 1978
1895 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); 1979 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
1896 1980
1897 ev_start (EV_A_ (W)w, ++timercnt); 1981 ev_start (EV_A_ (W)w, ++timercnt + HEAP0 - 1);
1898 array_needsize (WT, timers, timermax, timercnt + 1, EMPTY2); 1982 array_needsize (WT, timers, timermax, timercnt + HEAP0, EMPTY2);
1899 timers [timercnt] = (WT)w; 1983 timers [ev_active (w)] = (WT)w;
1900 upheap (timers, timercnt); 1984 upheap (timers, ev_active (w));
1901 1985
1902 /*assert (("internal timer heap corruption", timers [ev_active (w)] == w));*/ 1986 /*assert (("internal timer heap corruption", timers [ev_active (w)] == w));*/
1903} 1987}
1904 1988
1905void noinline 1989void noinline
1912 { 1996 {
1913 int active = ev_active (w); 1997 int active = ev_active (w);
1914 1998
1915 assert (("internal timer heap corruption", timers [active] == (WT)w)); 1999 assert (("internal timer heap corruption", timers [active] == (WT)w));
1916 2000
1917 if (expect_true (active < timercnt)) 2001 if (expect_true (active < timercnt + HEAP0 - 1))
1918 { 2002 {
1919 timers [active] = timers [timercnt]; 2003 timers [active] = timers [timercnt + HEAP0 - 1];
1920 adjustheap (timers, timercnt, active); 2004 adjustheap (timers, timercnt, active);
1921 } 2005 }
1922 2006
1923 --timercnt; 2007 --timercnt;
1924 } 2008 }
1964 ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; 2048 ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval;
1965 } 2049 }
1966 else 2050 else
1967 ev_at (w) = w->offset; 2051 ev_at (w) = w->offset;
1968 2052
1969 ev_start (EV_A_ (W)w, ++periodiccnt); 2053 ev_start (EV_A_ (W)w, ++periodiccnt + HEAP0 - 1);
1970 array_needsize (WT, periodics, periodicmax, periodiccnt + 1, EMPTY2); 2054 array_needsize (WT, periodics, periodicmax, periodiccnt + HEAP0, EMPTY2);
1971 periodics [periodiccnt] = (WT)w; 2055 periodics [ev_active (w)] = (WT)w;
1972 upheap (periodics, periodiccnt); 2056 upheap (periodics, ev_active (w));
1973 2057
1974 /*assert (("internal periodic heap corruption", periodics [ev_active (w)] == w));*/ 2058 /*assert (("internal periodic heap corruption", periodics [ev_active (w)] == w));*/
1975} 2059}
1976 2060
1977void noinline 2061void noinline
1984 { 2068 {
1985 int active = ev_active (w); 2069 int active = ev_active (w);
1986 2070
1987 assert (("internal periodic heap corruption", periodics [active] == (WT)w)); 2071 assert (("internal periodic heap corruption", periodics [active] == (WT)w));
1988 2072
1989 if (expect_true (active < periodiccnt)) 2073 if (expect_true (active < periodiccnt + HEAP0 - 1))
1990 { 2074 {
1991 periodics [active] = periodics [periodiccnt]; 2075 periodics [active] = periodics [periodiccnt + HEAP0 - 1];
1992 adjustheap (periodics, periodiccnt, active); 2076 adjustheap (periodics, periodiccnt, active);
1993 } 2077 }
1994 2078
1995 --periodiccnt; 2079 --periodiccnt;
1996 } 2080 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines