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

Comparing libev/ev.c (file contents):
Revision 1.226 by root, Fri Apr 18 17:16:44 2008 UTC vs.
Revision 1.238 by root, Thu May 8 20:49:12 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#define USE_4HEAP 1/* they do not work corretcly */
772#if USE_4HEAP
773
774#define DHEAP 4
775#define HEAP0 (DHEAP - 1) /* index of first element in heap */
776
777/* towards the root */
759void inline_speed 778void inline_speed
760upheap (WT *heap, int k) 779upheap (WT *heap, int k)
761{ 780{
762 WT w = heap [k]; 781 WT w = heap [k];
763 782
764 while (k) 783 for (;;)
765 { 784 {
766 int p = (k - 1) >> 1; 785 int p = ((k - HEAP0 - 1) / DHEAP) + HEAP0;
767 786
768 if (heap [p]->at <= w->at) 787 if (p == k || heap [p]->at <= w->at)
769 break; 788 break;
770 789
771 heap [k] = heap [p]; 790 heap [k] = heap [p];
772 ((W)heap [k])->active = k + 1; 791 ev_active (heap [k]) = k;
773 k = p; 792 k = p;
774 } 793 }
775 794
776 heap [k] = w; 795 heap [k] = w;
777 ((W)heap [k])->active = k + 1; 796 ev_active (heap [k]) = k;
778} 797}
779 798
799/* away from the root */
780void inline_speed 800void inline_speed
781downheap (WT *heap, int N, int k) 801downheap (WT *heap, int N, int k)
782{ 802{
783 WT w = heap [k]; 803 WT w = heap [k];
804 WT *E = heap + N + HEAP0;
784 805
785 for (;;) 806 for (;;)
786 { 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 */
816 (minpos = pos + 0), (minat = (*minpos)->at);
817 if (pos [1]->at < minat) (minpos = pos + 1), (minat = (*minpos)->at);
818 if (pos [2]->at < minat) (minpos = pos + 2), (minat = (*minpos)->at);
819 if (pos [3]->at < minat) (minpos = pos + 3), (minat = (*minpos)->at);
820 }
821 else
822 {
823 /* slow path */
824 if (pos >= E)
825 break;
826 (minpos = pos + 0), (minat = (*minpos)->at);
827 if (pos + 1 < E && pos [1]->at < minat) (minpos = pos + 1), (minat = (*minpos)->at);
828 if (pos + 2 < E && pos [2]->at < minat) (minpos = pos + 2), (minat = (*minpos)->at);
829 if (pos + 3 < E && pos [3]->at < minat) (minpos = pos + 3), (minat = (*minpos)->at);
830 }
831
832 if (w->at <= minat)
833 break;
834
835 ev_active (*minpos) = k;
836 heap [k] = *minpos;
837
838 k = minpos - heap;
839 }
840
841 heap [k] = w;
842 ev_active (heap [k]) = k;
843}
844
845#else // 4HEAP
846
847#define HEAP0 1
848
849/* towards the root */
850void inline_speed
851upheap (WT *heap, int k)
852{
853 WT w = heap [k];
854
855 for (;;)
856 {
857 int p = k >> 1;
858
859 /* maybe we could use a dummy element at heap [0]? */
860 if (!p || heap [p]->at <= w->at)
861 break;
862
863 heap [k] = heap [p];
864 ev_active (heap [k]) = k;
865 k = p;
866 }
867
868 heap [k] = w;
869 ev_active (heap [k]) = k;
870}
871
872/* away from the root */
873void inline_speed
874downheap (WT *heap, int N, int k)
875{
876 WT w = heap [k];
877
878 for (;;)
879 {
787 int c = (k << 1) + 1; 880 int c = k << 1;
788 881
789 if (c >= N) 882 if (c > N)
790 break; 883 break;
791 884
792 c += c + 1 < N && heap [c]->at > heap [c + 1]->at 885 c += c + 1 < N && heap [c]->at > heap [c + 1]->at
793 ? 1 : 0; 886 ? 1 : 0;
794 887
795 if (w->at <= heap [c]->at) 888 if (w->at <= heap [c]->at)
796 break; 889 break;
797 890
798 heap [k] = heap [c]; 891 heap [k] = heap [c];
799 ((W)heap [k])->active = k + 1; 892 ((W)heap [k])->active = k;
800 893
801 k = c; 894 k = c;
802 } 895 }
803 896
804 heap [k] = w; 897 heap [k] = w;
805 ((W)heap [k])->active = k + 1; 898 ev_active (heap [k]) = k;
806} 899}
900#endif
807 901
808void inline_size 902void inline_size
809adjustheap (WT *heap, int N, int k) 903adjustheap (WT *heap, int N, int k)
810{ 904{
811 upheap (heap, k); 905 upheap (heap, k);
906pipecb (EV_P_ ev_io *iow, int revents) 1000pipecb (EV_P_ ev_io *iow, int revents)
907{ 1001{
908#if EV_USE_EVENTFD 1002#if EV_USE_EVENTFD
909 if (evfd >= 0) 1003 if (evfd >= 0)
910 { 1004 {
911 uint64_t counter = 1; 1005 uint64_t counter;
912 read (evfd, &counter, sizeof (uint64_t)); 1006 read (evfd, &counter, sizeof (uint64_t));
913 } 1007 }
914 else 1008 else
915#endif 1009#endif
916 { 1010 {
1362void 1456void
1363ev_loop_fork (EV_P) 1457ev_loop_fork (EV_P)
1364{ 1458{
1365 postfork = 1; /* must be in line with ev_default_fork */ 1459 postfork = 1; /* must be in line with ev_default_fork */
1366} 1460}
1367
1368#endif 1461#endif
1369 1462
1370#if EV_MULTIPLICITY 1463#if EV_MULTIPLICITY
1371struct ev_loop * 1464struct ev_loop *
1372ev_default_loop_init (unsigned int flags) 1465ev_default_loop_init (unsigned int flags)
1453 EV_CB_INVOKE (p->w, p->events); 1546 EV_CB_INVOKE (p->w, p->events);
1454 } 1547 }
1455 } 1548 }
1456} 1549}
1457 1550
1458void inline_size
1459timers_reify (EV_P)
1460{
1461 while (timercnt && ((WT)timers [0])->at <= mn_now)
1462 {
1463 ev_timer *w = (ev_timer *)timers [0];
1464
1465 /*assert (("inactive timer on timer heap detected", ev_is_active (w)));*/
1466
1467 /* first reschedule or stop timer */
1468 if (w->repeat)
1469 {
1470 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
1471
1472 ((WT)w)->at += w->repeat;
1473 if (((WT)w)->at < mn_now)
1474 ((WT)w)->at = mn_now;
1475
1476 downheap (timers, timercnt, 0);
1477 }
1478 else
1479 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
1480
1481 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT);
1482 }
1483}
1484
1485#if EV_PERIODIC_ENABLE
1486void inline_size
1487periodics_reify (EV_P)
1488{
1489 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now)
1490 {
1491 ev_periodic *w = (ev_periodic *)periodics [0];
1492
1493 /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/
1494
1495 /* first reschedule or stop timer */
1496 if (w->reschedule_cb)
1497 {
1498 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + TIME_EPSILON);
1499 assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now));
1500 downheap (periodics, periodiccnt, 0);
1501 }
1502 else if (w->interval)
1503 {
1504 ((WT)w)->at = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval;
1505 if (((WT)w)->at - ev_rt_now <= TIME_EPSILON) ((WT)w)->at += w->interval;
1506 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > ev_rt_now));
1507 downheap (periodics, periodiccnt, 0);
1508 }
1509 else
1510 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
1511
1512 ev_feed_event (EV_A_ (W)w, EV_PERIODIC);
1513 }
1514}
1515
1516static void noinline
1517periodics_reschedule (EV_P)
1518{
1519 int i;
1520
1521 /* adjust periodics after time jump */
1522 for (i = 0; i < periodiccnt; ++i)
1523 {
1524 ev_periodic *w = (ev_periodic *)periodics [i];
1525
1526 if (w->reschedule_cb)
1527 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
1528 else if (w->interval)
1529 ((WT)w)->at = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval;
1530 }
1531
1532 /* now rebuild the heap */
1533 for (i = periodiccnt >> 1; i--; )
1534 downheap (periodics, periodiccnt, i);
1535}
1536#endif
1537
1538#if EV_IDLE_ENABLE 1551#if EV_IDLE_ENABLE
1539void inline_size 1552void inline_size
1540idle_reify (EV_P) 1553idle_reify (EV_P)
1541{ 1554{
1542 if (expect_false (idleall)) 1555 if (expect_false (idleall))
1553 queue_events (EV_A_ (W *)idles [pri], idlecnt [pri], EV_IDLE); 1566 queue_events (EV_A_ (W *)idles [pri], idlecnt [pri], EV_IDLE);
1554 break; 1567 break;
1555 } 1568 }
1556 } 1569 }
1557 } 1570 }
1571}
1572#endif
1573
1574void inline_size
1575timers_reify (EV_P)
1576{
1577 while (timercnt && ev_at (timers [HEAP0]) <= mn_now)
1578 {
1579 ev_timer *w = (ev_timer *)timers [HEAP0];
1580
1581 /*assert (("inactive timer on timer heap detected", ev_is_active (w)));*/
1582
1583 /* first reschedule or stop timer */
1584 if (w->repeat)
1585 {
1586 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
1587
1588 ev_at (w) += w->repeat;
1589 if (ev_at (w) < mn_now)
1590 ev_at (w) = mn_now;
1591
1592 downheap (timers, timercnt, HEAP0);
1593 }
1594 else
1595 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
1596
1597 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT);
1598 }
1599}
1600
1601#if EV_PERIODIC_ENABLE
1602void inline_size
1603periodics_reify (EV_P)
1604{
1605 while (periodiccnt && ev_at (periodics [HEAP0]) <= ev_rt_now)
1606 {
1607 ev_periodic *w = (ev_periodic *)periodics [HEAP0];
1608
1609 /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/
1610
1611 /* first reschedule or stop timer */
1612 if (w->reschedule_cb)
1613 {
1614 ev_at (w) = w->reschedule_cb (w, ev_rt_now + TIME_EPSILON);
1615 assert (("ev_periodic reschedule callback returned time in the past", ev_at (w) > ev_rt_now));
1616 downheap (periodics, periodiccnt, 1);
1617 }
1618 else if (w->interval)
1619 {
1620 ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval;
1621 if (ev_at (w) - ev_rt_now <= TIME_EPSILON) ev_at (w) += w->interval;
1622 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ev_at (w) > ev_rt_now));
1623 downheap (periodics, periodiccnt, HEAP0);
1624 }
1625 else
1626 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
1627
1628 ev_feed_event (EV_A_ (W)w, EV_PERIODIC);
1629 }
1630}
1631
1632static void noinline
1633periodics_reschedule (EV_P)
1634{
1635 int i;
1636
1637 /* adjust periodics after time jump */
1638 for (i = 1; i <= periodiccnt; ++i)
1639 {
1640 ev_periodic *w = (ev_periodic *)periodics [i];
1641
1642 if (w->reschedule_cb)
1643 ev_at (w) = w->reschedule_cb (w, ev_rt_now);
1644 else if (w->interval)
1645 ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval;
1646 }
1647
1648 /* now rebuild the heap */
1649 for (i = periodiccnt >> 1; --i; )
1650 downheap (periodics, periodiccnt, i + HEAP0);
1558} 1651}
1559#endif 1652#endif
1560 1653
1561void inline_speed 1654void inline_speed
1562time_update (EV_P_ ev_tstamp max_block) 1655time_update (EV_P_ ev_tstamp max_block)
1591 */ 1684 */
1592 for (i = 4; --i; ) 1685 for (i = 4; --i; )
1593 { 1686 {
1594 rtmn_diff = ev_rt_now - mn_now; 1687 rtmn_diff = ev_rt_now - mn_now;
1595 1688
1596 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) 1689 if (expect_true (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP))
1597 return; /* all is well */ 1690 return; /* all is well */
1598 1691
1599 ev_rt_now = ev_time (); 1692 ev_rt_now = ev_time ();
1600 mn_now = get_clock (); 1693 mn_now = get_clock ();
1601 now_floor = mn_now; 1694 now_floor = mn_now;
1616 { 1709 {
1617#if EV_PERIODIC_ENABLE 1710#if EV_PERIODIC_ENABLE
1618 periodics_reschedule (EV_A); 1711 periodics_reschedule (EV_A);
1619#endif 1712#endif
1620 /* adjust timers. this is easy, as the offset is the same for all of them */ 1713 /* adjust timers. this is easy, as the offset is the same for all of them */
1621 for (i = 0; i < timercnt; ++i) 1714 for (i = 1; i <= timercnt; ++i)
1622 ((WT)timers [i])->at += ev_rt_now - mn_now; 1715 ev_at (timers [i]) += ev_rt_now - mn_now;
1623 } 1716 }
1624 1717
1625 mn_now = ev_rt_now; 1718 mn_now = ev_rt_now;
1626 } 1719 }
1627} 1720}
1697 1790
1698 waittime = MAX_BLOCKTIME; 1791 waittime = MAX_BLOCKTIME;
1699 1792
1700 if (timercnt) 1793 if (timercnt)
1701 { 1794 {
1702 ev_tstamp to = ((WT)timers [0])->at - mn_now + backend_fudge; 1795 ev_tstamp to = ev_at (timers [HEAP0]) - mn_now + backend_fudge;
1703 if (waittime > to) waittime = to; 1796 if (waittime > to) waittime = to;
1704 } 1797 }
1705 1798
1706#if EV_PERIODIC_ENABLE 1799#if EV_PERIODIC_ENABLE
1707 if (periodiccnt) 1800 if (periodiccnt)
1708 { 1801 {
1709 ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + backend_fudge; 1802 ev_tstamp to = ev_at (periodics [HEAP0]) - ev_rt_now + backend_fudge;
1710 if (waittime > to) waittime = to; 1803 if (waittime > to) waittime = to;
1711 } 1804 }
1712#endif 1805#endif
1713 1806
1714 if (expect_false (waittime < timeout_blocktime)) 1807 if (expect_false (waittime < timeout_blocktime))
1880ev_timer_start (EV_P_ ev_timer *w) 1973ev_timer_start (EV_P_ ev_timer *w)
1881{ 1974{
1882 if (expect_false (ev_is_active (w))) 1975 if (expect_false (ev_is_active (w)))
1883 return; 1976 return;
1884 1977
1885 ((WT)w)->at += mn_now; 1978 ev_at (w) += mn_now;
1886 1979
1887 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); 1980 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
1888 1981
1889 ev_start (EV_A_ (W)w, ++timercnt); 1982 ev_start (EV_A_ (W)w, ++timercnt + HEAP0 - 1);
1890 array_needsize (WT, timers, timermax, timercnt, EMPTY2); 1983 array_needsize (WT, timers, timermax, timercnt + HEAP0, EMPTY2);
1891 timers [timercnt - 1] = (WT)w; 1984 timers [ev_active (w)] = (WT)w;
1892 upheap (timers, timercnt - 1); 1985 upheap (timers, ev_active (w));
1893 1986
1894 /*assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));*/ 1987 /*assert (("internal timer heap corruption", timers [ev_active (w)] == w));*/
1895} 1988}
1896 1989
1897void noinline 1990void noinline
1898ev_timer_stop (EV_P_ ev_timer *w) 1991ev_timer_stop (EV_P_ ev_timer *w)
1899{ 1992{
1900 clear_pending (EV_A_ (W)w); 1993 clear_pending (EV_A_ (W)w);
1901 if (expect_false (!ev_is_active (w))) 1994 if (expect_false (!ev_is_active (w)))
1902 return; 1995 return;
1903 1996
1904 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == (WT)w));
1905
1906 { 1997 {
1907 int active = ((W)w)->active; 1998 int active = ev_active (w);
1908 1999
2000 assert (("internal timer heap corruption", timers [active] == (WT)w));
2001
1909 if (expect_true (--active < --timercnt)) 2002 if (expect_true (active < timercnt + HEAP0 - 1))
1910 { 2003 {
1911 timers [active] = timers [timercnt]; 2004 timers [active] = timers [timercnt + HEAP0 - 1];
1912 adjustheap (timers, timercnt, active); 2005 adjustheap (timers, timercnt, active);
1913 } 2006 }
2007
2008 --timercnt;
1914 } 2009 }
1915 2010
1916 ((WT)w)->at -= mn_now; 2011 ev_at (w) -= mn_now;
1917 2012
1918 ev_stop (EV_A_ (W)w); 2013 ev_stop (EV_A_ (W)w);
1919} 2014}
1920 2015
1921void noinline 2016void noinline
1923{ 2018{
1924 if (ev_is_active (w)) 2019 if (ev_is_active (w))
1925 { 2020 {
1926 if (w->repeat) 2021 if (w->repeat)
1927 { 2022 {
1928 ((WT)w)->at = mn_now + w->repeat; 2023 ev_at (w) = mn_now + w->repeat;
1929 adjustheap (timers, timercnt, ((W)w)->active - 1); 2024 adjustheap (timers, timercnt, ev_active (w));
1930 } 2025 }
1931 else 2026 else
1932 ev_timer_stop (EV_A_ w); 2027 ev_timer_stop (EV_A_ w);
1933 } 2028 }
1934 else if (w->repeat) 2029 else if (w->repeat)
1935 { 2030 {
1936 w->at = w->repeat; 2031 ev_at (w) = w->repeat;
1937 ev_timer_start (EV_A_ w); 2032 ev_timer_start (EV_A_ w);
1938 } 2033 }
1939} 2034}
1940 2035
1941#if EV_PERIODIC_ENABLE 2036#if EV_PERIODIC_ENABLE
1944{ 2039{
1945 if (expect_false (ev_is_active (w))) 2040 if (expect_false (ev_is_active (w)))
1946 return; 2041 return;
1947 2042
1948 if (w->reschedule_cb) 2043 if (w->reschedule_cb)
1949 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); 2044 ev_at (w) = w->reschedule_cb (w, ev_rt_now);
1950 else if (w->interval) 2045 else if (w->interval)
1951 { 2046 {
1952 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); 2047 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.));
1953 /* this formula differs from the one in periodic_reify because we do not always round up */ 2048 /* this formula differs from the one in periodic_reify because we do not always round up */
1954 ((WT)w)->at = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; 2049 ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval;
1955 } 2050 }
1956 else 2051 else
1957 ((WT)w)->at = w->offset; 2052 ev_at (w) = w->offset;
1958 2053
1959 ev_start (EV_A_ (W)w, ++periodiccnt); 2054 ev_start (EV_A_ (W)w, ++periodiccnt + HEAP0 - 1);
1960 array_needsize (WT, periodics, periodicmax, periodiccnt, EMPTY2); 2055 array_needsize (WT, periodics, periodicmax, periodiccnt + HEAP0, EMPTY2);
1961 periodics [periodiccnt - 1] = (WT)w; 2056 periodics [ev_active (w)] = (WT)w;
1962 upheap (periodics, periodiccnt - 1); 2057 upheap (periodics, ev_active (w));
1963 2058
1964 /*assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));*/ 2059 /*assert (("internal periodic heap corruption", periodics [ev_active (w)] == w));*/
1965} 2060}
1966 2061
1967void noinline 2062void noinline
1968ev_periodic_stop (EV_P_ ev_periodic *w) 2063ev_periodic_stop (EV_P_ ev_periodic *w)
1969{ 2064{
1970 clear_pending (EV_A_ (W)w); 2065 clear_pending (EV_A_ (W)w);
1971 if (expect_false (!ev_is_active (w))) 2066 if (expect_false (!ev_is_active (w)))
1972 return; 2067 return;
1973 2068
1974 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == (WT)w));
1975
1976 { 2069 {
1977 int active = ((W)w)->active; 2070 int active = ev_active (w);
1978 2071
2072 assert (("internal periodic heap corruption", periodics [active] == (WT)w));
2073
1979 if (expect_true (--active < --periodiccnt)) 2074 if (expect_true (active < periodiccnt + HEAP0 - 1))
1980 { 2075 {
1981 periodics [active] = periodics [periodiccnt]; 2076 periodics [active] = periodics [periodiccnt + HEAP0 - 1];
1982 adjustheap (periodics, periodiccnt, active); 2077 adjustheap (periodics, periodiccnt, active);
1983 } 2078 }
2079
2080 --periodiccnt;
1984 } 2081 }
1985 2082
1986 ev_stop (EV_A_ (W)w); 2083 ev_stop (EV_A_ (W)w);
1987} 2084}
1988 2085
2104 if (w->wd < 0) 2201 if (w->wd < 0)
2105 { 2202 {
2106 ev_timer_start (EV_A_ &w->timer); /* this is not race-free, so we still need to recheck periodically */ 2203 ev_timer_start (EV_A_ &w->timer); /* this is not race-free, so we still need to recheck periodically */
2107 2204
2108 /* monitor some parent directory for speedup hints */ 2205 /* monitor some parent directory for speedup hints */
2206 /* note that exceeding the hardcoded limit is not a correctness issue, */
2207 /* but an efficiency issue only */
2109 if ((errno == ENOENT || errno == EACCES) && strlen (w->path) < 4096) 2208 if ((errno == ENOENT || errno == EACCES) && strlen (w->path) < 4096)
2110 { 2209 {
2111 char path [4096]; 2210 char path [4096];
2112 strcpy (path, w->path); 2211 strcpy (path, w->path);
2113 2212
2358 clear_pending (EV_A_ (W)w); 2457 clear_pending (EV_A_ (W)w);
2359 if (expect_false (!ev_is_active (w))) 2458 if (expect_false (!ev_is_active (w)))
2360 return; 2459 return;
2361 2460
2362 { 2461 {
2363 int active = ((W)w)->active; 2462 int active = ev_active (w);
2364 2463
2365 idles [ABSPRI (w)][active - 1] = idles [ABSPRI (w)][--idlecnt [ABSPRI (w)]]; 2464 idles [ABSPRI (w)][active - 1] = idles [ABSPRI (w)][--idlecnt [ABSPRI (w)]];
2366 ((W)idles [ABSPRI (w)][active - 1])->active = active; 2465 ev_active (idles [ABSPRI (w)][active - 1]) = active;
2367 2466
2368 ev_stop (EV_A_ (W)w); 2467 ev_stop (EV_A_ (W)w);
2369 --idleall; 2468 --idleall;
2370 } 2469 }
2371} 2470}
2388 clear_pending (EV_A_ (W)w); 2487 clear_pending (EV_A_ (W)w);
2389 if (expect_false (!ev_is_active (w))) 2488 if (expect_false (!ev_is_active (w)))
2390 return; 2489 return;
2391 2490
2392 { 2491 {
2393 int active = ((W)w)->active; 2492 int active = ev_active (w);
2493
2394 prepares [active - 1] = prepares [--preparecnt]; 2494 prepares [active - 1] = prepares [--preparecnt];
2395 ((W)prepares [active - 1])->active = active; 2495 ev_active (prepares [active - 1]) = active;
2396 } 2496 }
2397 2497
2398 ev_stop (EV_A_ (W)w); 2498 ev_stop (EV_A_ (W)w);
2399} 2499}
2400 2500
2415 clear_pending (EV_A_ (W)w); 2515 clear_pending (EV_A_ (W)w);
2416 if (expect_false (!ev_is_active (w))) 2516 if (expect_false (!ev_is_active (w)))
2417 return; 2517 return;
2418 2518
2419 { 2519 {
2420 int active = ((W)w)->active; 2520 int active = ev_active (w);
2521
2421 checks [active - 1] = checks [--checkcnt]; 2522 checks [active - 1] = checks [--checkcnt];
2422 ((W)checks [active - 1])->active = active; 2523 ev_active (checks [active - 1]) = active;
2423 } 2524 }
2424 2525
2425 ev_stop (EV_A_ (W)w); 2526 ev_stop (EV_A_ (W)w);
2426} 2527}
2427 2528
2523 clear_pending (EV_A_ (W)w); 2624 clear_pending (EV_A_ (W)w);
2524 if (expect_false (!ev_is_active (w))) 2625 if (expect_false (!ev_is_active (w)))
2525 return; 2626 return;
2526 2627
2527 { 2628 {
2528 int active = ((W)w)->active; 2629 int active = ev_active (w);
2630
2529 forks [active - 1] = forks [--forkcnt]; 2631 forks [active - 1] = forks [--forkcnt];
2530 ((W)forks [active - 1])->active = active; 2632 ev_active (forks [active - 1]) = active;
2531 } 2633 }
2532 2634
2533 ev_stop (EV_A_ (W)w); 2635 ev_stop (EV_A_ (W)w);
2534} 2636}
2535#endif 2637#endif
2554 clear_pending (EV_A_ (W)w); 2656 clear_pending (EV_A_ (W)w);
2555 if (expect_false (!ev_is_active (w))) 2657 if (expect_false (!ev_is_active (w)))
2556 return; 2658 return;
2557 2659
2558 { 2660 {
2559 int active = ((W)w)->active; 2661 int active = ev_active (w);
2662
2560 asyncs [active - 1] = asyncs [--asynccnt]; 2663 asyncs [active - 1] = asyncs [--asynccnt];
2561 ((W)asyncs [active - 1])->active = active; 2664 ev_active (asyncs [active - 1]) = active;
2562 } 2665 }
2563 2666
2564 ev_stop (EV_A_ (W)w); 2667 ev_stop (EV_A_ (W)w);
2565} 2668}
2566 2669

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines