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

Comparing libev/ev.c (file contents):
Revision 1.227 by root, Fri May 2 07:20:01 2008 UTC vs.
Revision 1.239 by root, Thu May 8 20:52:13 2008 UTC

325 325
326typedef ev_watcher *W; 326typedef ev_watcher *W;
327typedef ev_watcher_list *WL; 327typedef ev_watcher_list *WL;
328typedef ev_watcher_time *WT; 328typedef ev_watcher_time *WT;
329 329
330#define ev_active(w) ((W)(w))->active
331#define ev_at(w) ((WT)(w))->at
332
330#if EV_USE_MONOTONIC 333#if EV_USE_MONOTONIC
331/* sig_atomic_t is used to avoid per-thread variables or locking but still */ 334/* sig_atomic_t is used to avoid per-thread variables or locking but still */
332/* giving it a reasonably high chance of working on typical architetcures */ 335/* giving it a reasonably high chance of working on typical architetcures */
333static EV_ATOMIC_T have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ 336static EV_ATOMIC_T have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
334#endif 337#endif
517 } 520 }
518} 521}
519 522
520/*****************************************************************************/ 523/*****************************************************************************/
521 524
525#define MALLOC_ROUND 4096 /* prefer to allocate in chunks of this size, must be 2**n and >> 4 longs */
526
522int inline_size 527int inline_size
523array_nextsize (int elem, int cur, int cnt) 528array_nextsize (int elem, int cur, int cnt)
524{ 529{
525 int ncur = cur + 1; 530 int ncur = cur + 1;
526 531
527 do 532 do
528 ncur <<= 1; 533 ncur <<= 1;
529 while (cnt > ncur); 534 while (cnt > ncur);
530 535
531 /* if size > 4096, round to 4096 - 4 * longs to accomodate malloc overhead */ 536 /* if size is large, round to MALLOC_ROUND - 4 * longs to accomodate malloc overhead */
532 if (elem * ncur > 4096) 537 if (elem * ncur > MALLOC_ROUND - sizeof (void *) * 4)
533 { 538 {
534 ncur *= elem; 539 ncur *= elem;
535 ncur = (ncur + elem + 4095 + sizeof (void *) * 4) & ~4095; 540 ncur = (ncur + elem + (MALLOC_ROUND - 1) + sizeof (void *) * 4) & ~(MALLOC_ROUND - 1);
536 ncur = ncur - sizeof (void *) * 4; 541 ncur = ncur - sizeof (void *) * 4;
537 ncur /= elem; 542 ncur /= elem;
538 } 543 }
539 544
540 return ncur; 545 return ncur;
754 } 759 }
755} 760}
756 761
757/*****************************************************************************/ 762/*****************************************************************************/
758 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
759/* towards the root */ 776/* towards the root */
760void inline_speed 777void inline_speed
761upheap (WT *heap, int k) 778upheap (WT *heap, int k)
762{ 779{
763 WT w = heap [k]; 780 WT w = heap [k];
764 781
765 while (k) 782 for (;;)
766 { 783 {
767 int p = (k - 1) >> 1; 784 int p = ((k - HEAP0 - 1) / DHEAP) + HEAP0;
768 785
769 if (heap [p]->at <= w->at) 786 if (p == k || heap [p]->at <= w->at)
770 break; 787 break;
771 788
772 heap [k] = heap [p]; 789 heap [k] = heap [p];
773 ((W)heap [k])->active = k + 1; 790 ev_active (heap [k]) = k;
774 k = p; 791 k = p;
775 } 792 }
776 793
777 heap [k] = w; 794 heap [k] = w;
778 ((W)heap [k])->active = k + 1; 795 ev_active (heap [k]) = k;
779} 796}
780 797
781/* away from the root */ 798/* away from the root */
782void inline_speed 799void inline_speed
783downheap (WT *heap, int N, int k) 800downheap (WT *heap, int N, int k)
784{ 801{
785 WT w = heap [k]; 802 WT w = heap [k];
803 WT *E = heap + N + HEAP0;
786 804
787 for (;;) 805 for (;;)
788 { 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 {
856 int p = k >> 1;
857
858 /* maybe we could use a dummy element at heap [0]? */
859 if (!p || heap [p]->at <= w->at)
860 break;
861
862 heap [k] = heap [p];
863 ev_active (heap [k]) = k;
864 k = p;
865 }
866
867 heap [k] = w;
868 ev_active (heap [k]) = k;
869}
870
871/* away from the root */
872void inline_speed
873downheap (WT *heap, int N, int k)
874{
875 WT w = heap [k];
876
877 for (;;)
878 {
789 int c = (k << 1) + 1; 879 int c = k << 1;
790 880
791 if (c >= N) 881 if (c > N)
792 break; 882 break;
793 883
794 c += c + 1 < N && heap [c]->at > heap [c + 1]->at 884 c += c + 1 < N && heap [c]->at > heap [c + 1]->at
795 ? 1 : 0; 885 ? 1 : 0;
796 886
797 if (w->at <= heap [c]->at) 887 if (w->at <= heap [c]->at)
798 break; 888 break;
799 889
800 heap [k] = heap [c]; 890 heap [k] = heap [c];
801 ((W)heap [k])->active = k + 1; 891 ((W)heap [k])->active = k;
802 892
803 k = c; 893 k = c;
804 } 894 }
805 895
806 heap [k] = w; 896 heap [k] = w;
807 ((W)heap [k])->active = k + 1; 897 ev_active (heap [k]) = k;
808} 898}
899#endif
809 900
810void inline_size 901void inline_size
811adjustheap (WT *heap, int N, int k) 902adjustheap (WT *heap, int N, int k)
812{ 903{
813 upheap (heap, k); 904 upheap (heap, k);
908pipecb (EV_P_ ev_io *iow, int revents) 999pipecb (EV_P_ ev_io *iow, int revents)
909{ 1000{
910#if EV_USE_EVENTFD 1001#if EV_USE_EVENTFD
911 if (evfd >= 0) 1002 if (evfd >= 0)
912 { 1003 {
913 uint64_t counter = 1; 1004 uint64_t counter;
914 read (evfd, &counter, sizeof (uint64_t)); 1005 read (evfd, &counter, sizeof (uint64_t));
915 } 1006 }
916 else 1007 else
917#endif 1008#endif
918 { 1009 {
1364void 1455void
1365ev_loop_fork (EV_P) 1456ev_loop_fork (EV_P)
1366{ 1457{
1367 postfork = 1; /* must be in line with ev_default_fork */ 1458 postfork = 1; /* must be in line with ev_default_fork */
1368} 1459}
1369
1370#endif 1460#endif
1371 1461
1372#if EV_MULTIPLICITY 1462#if EV_MULTIPLICITY
1373struct ev_loop * 1463struct ev_loop *
1374ev_default_loop_init (unsigned int flags) 1464ev_default_loop_init (unsigned int flags)
1455 EV_CB_INVOKE (p->w, p->events); 1545 EV_CB_INVOKE (p->w, p->events);
1456 } 1546 }
1457 } 1547 }
1458} 1548}
1459 1549
1460void inline_size
1461timers_reify (EV_P)
1462{
1463 while (timercnt && ((WT)timers [0])->at <= mn_now)
1464 {
1465 ev_timer *w = (ev_timer *)timers [0];
1466
1467 /*assert (("inactive timer on timer heap detected", ev_is_active (w)));*/
1468
1469 /* first reschedule or stop timer */
1470 if (w->repeat)
1471 {
1472 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
1473
1474 ((WT)w)->at += w->repeat;
1475 if (((WT)w)->at < mn_now)
1476 ((WT)w)->at = mn_now;
1477
1478 downheap (timers, timercnt, 0);
1479 }
1480 else
1481 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
1482
1483 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT);
1484 }
1485}
1486
1487#if EV_PERIODIC_ENABLE
1488void inline_size
1489periodics_reify (EV_P)
1490{
1491 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now)
1492 {
1493 ev_periodic *w = (ev_periodic *)periodics [0];
1494
1495 /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/
1496
1497 /* first reschedule or stop timer */
1498 if (w->reschedule_cb)
1499 {
1500 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + TIME_EPSILON);
1501 assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now));
1502 downheap (periodics, periodiccnt, 0);
1503 }
1504 else if (w->interval)
1505 {
1506 ((WT)w)->at = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval;
1507 if (((WT)w)->at - ev_rt_now <= TIME_EPSILON) ((WT)w)->at += w->interval;
1508 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > ev_rt_now));
1509 downheap (periodics, periodiccnt, 0);
1510 }
1511 else
1512 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
1513
1514 ev_feed_event (EV_A_ (W)w, EV_PERIODIC);
1515 }
1516}
1517
1518static void noinline
1519periodics_reschedule (EV_P)
1520{
1521 int i;
1522
1523 /* adjust periodics after time jump */
1524 for (i = 0; i < periodiccnt; ++i)
1525 {
1526 ev_periodic *w = (ev_periodic *)periodics [i];
1527
1528 if (w->reschedule_cb)
1529 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
1530 else if (w->interval)
1531 ((WT)w)->at = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval;
1532 }
1533
1534 /* now rebuild the heap */
1535 for (i = periodiccnt >> 1; i--; )
1536 downheap (periodics, periodiccnt, i);
1537}
1538#endif
1539
1540#if EV_IDLE_ENABLE 1550#if EV_IDLE_ENABLE
1541void inline_size 1551void inline_size
1542idle_reify (EV_P) 1552idle_reify (EV_P)
1543{ 1553{
1544 if (expect_false (idleall)) 1554 if (expect_false (idleall))
1555 queue_events (EV_A_ (W *)idles [pri], idlecnt [pri], EV_IDLE); 1565 queue_events (EV_A_ (W *)idles [pri], idlecnt [pri], EV_IDLE);
1556 break; 1566 break;
1557 } 1567 }
1558 } 1568 }
1559 } 1569 }
1570}
1571#endif
1572
1573void inline_size
1574timers_reify (EV_P)
1575{
1576 while (timercnt && ev_at (timers [HEAP0]) <= mn_now)
1577 {
1578 ev_timer *w = (ev_timer *)timers [HEAP0];
1579
1580 /*assert (("inactive timer on timer heap detected", ev_is_active (w)));*/
1581
1582 /* first reschedule or stop timer */
1583 if (w->repeat)
1584 {
1585 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
1586
1587 ev_at (w) += w->repeat;
1588 if (ev_at (w) < mn_now)
1589 ev_at (w) = mn_now;
1590
1591 downheap (timers, timercnt, HEAP0);
1592 }
1593 else
1594 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
1595
1596 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT);
1597 }
1598}
1599
1600#if EV_PERIODIC_ENABLE
1601void inline_size
1602periodics_reify (EV_P)
1603{
1604 while (periodiccnt && ev_at (periodics [HEAP0]) <= ev_rt_now)
1605 {
1606 ev_periodic *w = (ev_periodic *)periodics [HEAP0];
1607
1608 /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/
1609
1610 /* first reschedule or stop timer */
1611 if (w->reschedule_cb)
1612 {
1613 ev_at (w) = w->reschedule_cb (w, ev_rt_now + TIME_EPSILON);
1614 assert (("ev_periodic reschedule callback returned time in the past", ev_at (w) > ev_rt_now));
1615 downheap (periodics, periodiccnt, 1);
1616 }
1617 else if (w->interval)
1618 {
1619 ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval;
1620 if (ev_at (w) - ev_rt_now <= TIME_EPSILON) ev_at (w) += w->interval;
1621 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ev_at (w) > ev_rt_now));
1622 downheap (periodics, periodiccnt, HEAP0);
1623 }
1624 else
1625 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
1626
1627 ev_feed_event (EV_A_ (W)w, EV_PERIODIC);
1628 }
1629}
1630
1631static void noinline
1632periodics_reschedule (EV_P)
1633{
1634 int i;
1635
1636 /* adjust periodics after time jump */
1637 for (i = 1; i <= periodiccnt; ++i)
1638 {
1639 ev_periodic *w = (ev_periodic *)periodics [i];
1640
1641 if (w->reschedule_cb)
1642 ev_at (w) = w->reschedule_cb (w, ev_rt_now);
1643 else if (w->interval)
1644 ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval;
1645 }
1646
1647 /* now rebuild the heap */
1648 for (i = periodiccnt >> 1; --i; )
1649 downheap (periodics, periodiccnt, i + HEAP0);
1560} 1650}
1561#endif 1651#endif
1562 1652
1563void inline_speed 1653void inline_speed
1564time_update (EV_P_ ev_tstamp max_block) 1654time_update (EV_P_ ev_tstamp max_block)
1593 */ 1683 */
1594 for (i = 4; --i; ) 1684 for (i = 4; --i; )
1595 { 1685 {
1596 rtmn_diff = ev_rt_now - mn_now; 1686 rtmn_diff = ev_rt_now - mn_now;
1597 1687
1598 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) 1688 if (expect_true (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP))
1599 return; /* all is well */ 1689 return; /* all is well */
1600 1690
1601 ev_rt_now = ev_time (); 1691 ev_rt_now = ev_time ();
1602 mn_now = get_clock (); 1692 mn_now = get_clock ();
1603 now_floor = mn_now; 1693 now_floor = mn_now;
1618 { 1708 {
1619#if EV_PERIODIC_ENABLE 1709#if EV_PERIODIC_ENABLE
1620 periodics_reschedule (EV_A); 1710 periodics_reschedule (EV_A);
1621#endif 1711#endif
1622 /* adjust timers. this is easy, as the offset is the same for all of them */ 1712 /* adjust timers. this is easy, as the offset is the same for all of them */
1623 for (i = 0; i < timercnt; ++i) 1713 for (i = 1; i <= timercnt; ++i)
1624 ((WT)timers [i])->at += ev_rt_now - mn_now; 1714 ev_at (timers [i]) += ev_rt_now - mn_now;
1625 } 1715 }
1626 1716
1627 mn_now = ev_rt_now; 1717 mn_now = ev_rt_now;
1628 } 1718 }
1629} 1719}
1699 1789
1700 waittime = MAX_BLOCKTIME; 1790 waittime = MAX_BLOCKTIME;
1701 1791
1702 if (timercnt) 1792 if (timercnt)
1703 { 1793 {
1704 ev_tstamp to = ((WT)timers [0])->at - mn_now + backend_fudge; 1794 ev_tstamp to = ev_at (timers [HEAP0]) - mn_now + backend_fudge;
1705 if (waittime > to) waittime = to; 1795 if (waittime > to) waittime = to;
1706 } 1796 }
1707 1797
1708#if EV_PERIODIC_ENABLE 1798#if EV_PERIODIC_ENABLE
1709 if (periodiccnt) 1799 if (periodiccnt)
1710 { 1800 {
1711 ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + backend_fudge; 1801 ev_tstamp to = ev_at (periodics [HEAP0]) - ev_rt_now + backend_fudge;
1712 if (waittime > to) waittime = to; 1802 if (waittime > to) waittime = to;
1713 } 1803 }
1714#endif 1804#endif
1715 1805
1716 if (expect_false (waittime < timeout_blocktime)) 1806 if (expect_false (waittime < timeout_blocktime))
1882ev_timer_start (EV_P_ ev_timer *w) 1972ev_timer_start (EV_P_ ev_timer *w)
1883{ 1973{
1884 if (expect_false (ev_is_active (w))) 1974 if (expect_false (ev_is_active (w)))
1885 return; 1975 return;
1886 1976
1887 ((WT)w)->at += mn_now; 1977 ev_at (w) += mn_now;
1888 1978
1889 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.));
1890 1980
1891 ev_start (EV_A_ (W)w, ++timercnt); 1981 ev_start (EV_A_ (W)w, ++timercnt + HEAP0 - 1);
1892 array_needsize (WT, timers, timermax, timercnt, EMPTY2); 1982 array_needsize (WT, timers, timermax, timercnt + HEAP0, EMPTY2);
1893 timers [timercnt - 1] = (WT)w; 1983 timers [ev_active (w)] = (WT)w;
1894 upheap (timers, timercnt - 1); 1984 upheap (timers, ev_active (w));
1895 1985
1896 /*assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));*/ 1986 /*assert (("internal timer heap corruption", timers [ev_active (w)] == w));*/
1897} 1987}
1898 1988
1899void noinline 1989void noinline
1900ev_timer_stop (EV_P_ ev_timer *w) 1990ev_timer_stop (EV_P_ ev_timer *w)
1901{ 1991{
1902 clear_pending (EV_A_ (W)w); 1992 clear_pending (EV_A_ (W)w);
1903 if (expect_false (!ev_is_active (w))) 1993 if (expect_false (!ev_is_active (w)))
1904 return; 1994 return;
1905 1995
1906 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == (WT)w));
1907
1908 { 1996 {
1909 int active = ((W)w)->active; 1997 int active = ev_active (w);
1910 1998
1999 assert (("internal timer heap corruption", timers [active] == (WT)w));
2000
1911 if (expect_true (--active < --timercnt)) 2001 if (expect_true (active < timercnt + HEAP0 - 1))
1912 { 2002 {
1913 timers [active] = timers [timercnt]; 2003 timers [active] = timers [timercnt + HEAP0 - 1];
1914 adjustheap (timers, timercnt, active); 2004 adjustheap (timers, timercnt, active);
1915 } 2005 }
2006
2007 --timercnt;
1916 } 2008 }
1917 2009
1918 ((WT)w)->at -= mn_now; 2010 ev_at (w) -= mn_now;
1919 2011
1920 ev_stop (EV_A_ (W)w); 2012 ev_stop (EV_A_ (W)w);
1921} 2013}
1922 2014
1923void noinline 2015void noinline
1925{ 2017{
1926 if (ev_is_active (w)) 2018 if (ev_is_active (w))
1927 { 2019 {
1928 if (w->repeat) 2020 if (w->repeat)
1929 { 2021 {
1930 ((WT)w)->at = mn_now + w->repeat; 2022 ev_at (w) = mn_now + w->repeat;
1931 adjustheap (timers, timercnt, ((W)w)->active - 1); 2023 adjustheap (timers, timercnt, ev_active (w));
1932 } 2024 }
1933 else 2025 else
1934 ev_timer_stop (EV_A_ w); 2026 ev_timer_stop (EV_A_ w);
1935 } 2027 }
1936 else if (w->repeat) 2028 else if (w->repeat)
1937 { 2029 {
1938 w->at = w->repeat; 2030 ev_at (w) = w->repeat;
1939 ev_timer_start (EV_A_ w); 2031 ev_timer_start (EV_A_ w);
1940 } 2032 }
1941} 2033}
1942 2034
1943#if EV_PERIODIC_ENABLE 2035#if EV_PERIODIC_ENABLE
1946{ 2038{
1947 if (expect_false (ev_is_active (w))) 2039 if (expect_false (ev_is_active (w)))
1948 return; 2040 return;
1949 2041
1950 if (w->reschedule_cb) 2042 if (w->reschedule_cb)
1951 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); 2043 ev_at (w) = w->reschedule_cb (w, ev_rt_now);
1952 else if (w->interval) 2044 else if (w->interval)
1953 { 2045 {
1954 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); 2046 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.));
1955 /* this formula differs from the one in periodic_reify because we do not always round up */ 2047 /* this formula differs from the one in periodic_reify because we do not always round up */
1956 ((WT)w)->at = 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;
1957 } 2049 }
1958 else 2050 else
1959 ((WT)w)->at = w->offset; 2051 ev_at (w) = w->offset;
1960 2052
1961 ev_start (EV_A_ (W)w, ++periodiccnt); 2053 ev_start (EV_A_ (W)w, ++periodiccnt + HEAP0 - 1);
1962 array_needsize (WT, periodics, periodicmax, periodiccnt, EMPTY2); 2054 array_needsize (WT, periodics, periodicmax, periodiccnt + HEAP0, EMPTY2);
1963 periodics [periodiccnt - 1] = (WT)w; 2055 periodics [ev_active (w)] = (WT)w;
1964 upheap (periodics, periodiccnt - 1); 2056 upheap (periodics, ev_active (w));
1965 2057
1966 /*assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));*/ 2058 /*assert (("internal periodic heap corruption", periodics [ev_active (w)] == w));*/
1967} 2059}
1968 2060
1969void noinline 2061void noinline
1970ev_periodic_stop (EV_P_ ev_periodic *w) 2062ev_periodic_stop (EV_P_ ev_periodic *w)
1971{ 2063{
1972 clear_pending (EV_A_ (W)w); 2064 clear_pending (EV_A_ (W)w);
1973 if (expect_false (!ev_is_active (w))) 2065 if (expect_false (!ev_is_active (w)))
1974 return; 2066 return;
1975 2067
1976 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == (WT)w));
1977
1978 { 2068 {
1979 int active = ((W)w)->active; 2069 int active = ev_active (w);
1980 2070
2071 assert (("internal periodic heap corruption", periodics [active] == (WT)w));
2072
1981 if (expect_true (--active < --periodiccnt)) 2073 if (expect_true (active < periodiccnt + HEAP0 - 1))
1982 { 2074 {
1983 periodics [active] = periodics [periodiccnt]; 2075 periodics [active] = periodics [periodiccnt + HEAP0 - 1];
1984 adjustheap (periodics, periodiccnt, active); 2076 adjustheap (periodics, periodiccnt, active);
1985 } 2077 }
2078
2079 --periodiccnt;
1986 } 2080 }
1987 2081
1988 ev_stop (EV_A_ (W)w); 2082 ev_stop (EV_A_ (W)w);
1989} 2083}
1990 2084
2106 if (w->wd < 0) 2200 if (w->wd < 0)
2107 { 2201 {
2108 ev_timer_start (EV_A_ &w->timer); /* this is not race-free, so we still need to recheck periodically */ 2202 ev_timer_start (EV_A_ &w->timer); /* this is not race-free, so we still need to recheck periodically */
2109 2203
2110 /* monitor some parent directory for speedup hints */ 2204 /* monitor some parent directory for speedup hints */
2205 /* note that exceeding the hardcoded limit is not a correctness issue, */
2206 /* but an efficiency issue only */
2111 if ((errno == ENOENT || errno == EACCES) && strlen (w->path) < 4096) 2207 if ((errno == ENOENT || errno == EACCES) && strlen (w->path) < 4096)
2112 { 2208 {
2113 char path [4096]; 2209 char path [4096];
2114 strcpy (path, w->path); 2210 strcpy (path, w->path);
2115 2211
2360 clear_pending (EV_A_ (W)w); 2456 clear_pending (EV_A_ (W)w);
2361 if (expect_false (!ev_is_active (w))) 2457 if (expect_false (!ev_is_active (w)))
2362 return; 2458 return;
2363 2459
2364 { 2460 {
2365 int active = ((W)w)->active; 2461 int active = ev_active (w);
2366 2462
2367 idles [ABSPRI (w)][active - 1] = idles [ABSPRI (w)][--idlecnt [ABSPRI (w)]]; 2463 idles [ABSPRI (w)][active - 1] = idles [ABSPRI (w)][--idlecnt [ABSPRI (w)]];
2368 ((W)idles [ABSPRI (w)][active - 1])->active = active; 2464 ev_active (idles [ABSPRI (w)][active - 1]) = active;
2369 2465
2370 ev_stop (EV_A_ (W)w); 2466 ev_stop (EV_A_ (W)w);
2371 --idleall; 2467 --idleall;
2372 } 2468 }
2373} 2469}
2390 clear_pending (EV_A_ (W)w); 2486 clear_pending (EV_A_ (W)w);
2391 if (expect_false (!ev_is_active (w))) 2487 if (expect_false (!ev_is_active (w)))
2392 return; 2488 return;
2393 2489
2394 { 2490 {
2395 int active = ((W)w)->active; 2491 int active = ev_active (w);
2492
2396 prepares [active - 1] = prepares [--preparecnt]; 2493 prepares [active - 1] = prepares [--preparecnt];
2397 ((W)prepares [active - 1])->active = active; 2494 ev_active (prepares [active - 1]) = active;
2398 } 2495 }
2399 2496
2400 ev_stop (EV_A_ (W)w); 2497 ev_stop (EV_A_ (W)w);
2401} 2498}
2402 2499
2417 clear_pending (EV_A_ (W)w); 2514 clear_pending (EV_A_ (W)w);
2418 if (expect_false (!ev_is_active (w))) 2515 if (expect_false (!ev_is_active (w)))
2419 return; 2516 return;
2420 2517
2421 { 2518 {
2422 int active = ((W)w)->active; 2519 int active = ev_active (w);
2520
2423 checks [active - 1] = checks [--checkcnt]; 2521 checks [active - 1] = checks [--checkcnt];
2424 ((W)checks [active - 1])->active = active; 2522 ev_active (checks [active - 1]) = active;
2425 } 2523 }
2426 2524
2427 ev_stop (EV_A_ (W)w); 2525 ev_stop (EV_A_ (W)w);
2428} 2526}
2429 2527
2525 clear_pending (EV_A_ (W)w); 2623 clear_pending (EV_A_ (W)w);
2526 if (expect_false (!ev_is_active (w))) 2624 if (expect_false (!ev_is_active (w)))
2527 return; 2625 return;
2528 2626
2529 { 2627 {
2530 int active = ((W)w)->active; 2628 int active = ev_active (w);
2629
2531 forks [active - 1] = forks [--forkcnt]; 2630 forks [active - 1] = forks [--forkcnt];
2532 ((W)forks [active - 1])->active = active; 2631 ev_active (forks [active - 1]) = active;
2533 } 2632 }
2534 2633
2535 ev_stop (EV_A_ (W)w); 2634 ev_stop (EV_A_ (W)w);
2536} 2635}
2537#endif 2636#endif
2556 clear_pending (EV_A_ (W)w); 2655 clear_pending (EV_A_ (W)w);
2557 if (expect_false (!ev_is_active (w))) 2656 if (expect_false (!ev_is_active (w)))
2558 return; 2657 return;
2559 2658
2560 { 2659 {
2561 int active = ((W)w)->active; 2660 int active = ev_active (w);
2661
2562 asyncs [active - 1] = asyncs [--asynccnt]; 2662 asyncs [active - 1] = asyncs [--asynccnt];
2563 ((W)asyncs [active - 1])->active = active; 2663 ev_active (asyncs [active - 1]) = active;
2564 } 2664 }
2565 2665
2566 ev_stop (EV_A_ (W)w); 2666 ev_stop (EV_A_ (W)w);
2567} 2667}
2568 2668

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines