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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines