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.235 by root, Wed May 7 14:45:17 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 HEAP0 3 /* index of first element in heap */
774
759/* towards the root */ 775/* towards the root */
760void inline_speed 776void inline_speed
761upheap (WT *heap, int k) 777upheap (WT *heap, int k)
762{ 778{
763 WT w = heap [k]; 779 WT w = heap [k];
764 780
765 while (k) 781 for (;;)
766 { 782 {
767 int p = (k - 1) >> 1; 783 int p = ((k - HEAP0 - 1) / 4) + HEAP0;
768 784
769 if (heap [p]->at <= w->at) 785 if (p >= HEAP0 || heap [p]->at <= w->at)
770 break; 786 break;
771 787
772 heap [k] = heap [p]; 788 heap [k] = heap [p];
773 ((W)heap [k])->active = k + 1; 789 ev_active (heap [k]) = k;
774 k = p; 790 k = p;
775 } 791 }
776 792
777 heap [k] = w; 793 heap [k] = w;
778 ((W)heap [k])->active = k + 1; 794 ev_active (heap [k]) = k;
779} 795}
780 796
781/* away from the root */ 797/* away from the root */
782void inline_speed 798void inline_speed
783downheap (WT *heap, int N, int k) 799downheap (WT *heap, int N, int k)
784{ 800{
785 WT w = heap [k]; 801 WT w = heap [k];
802 WT *E = heap + N + HEAP0;
786 803
787 for (;;) 804 for (;;)
788 { 805 {
806 ev_tstamp minat;
807 WT *minpos;
808 WT *pos = heap + 4 * (k - HEAP0) + HEAP0;
809
810 // find minimum child
811 if (expect_true (pos +3 < E))
812 {
813 (minpos = pos + 0), (minat = (*minpos)->at);
814 if (pos [1]->at < minat) (minpos = pos + 1), (minat = (*minpos)->at);
815 if (pos [2]->at < minat) (minpos = pos + 2), (minat = (*minpos)->at);
816 if (pos [3]->at < minat) (minpos = pos + 3), (minat = (*minpos)->at);
817 }
818 else
819 {
820 if (pos >= E)
821 break;
822
823 (minpos = pos + 0), (minat = (*minpos)->at);
824 if (pos + 1 < E && pos [1]->at < minat) (minpos = pos + 1), (minat = (*minpos)->at);
825 if (pos + 2 < E && pos [2]->at < minat) (minpos = pos + 2), (minat = (*minpos)->at);
826 if (pos + 3 < E && pos [3]->at < minat) (minpos = pos + 3), (minat = (*minpos)->at);
827 }
828
829 if (w->at <= minat)
830 break;
831
832 ev_active (*minpos) = k;
833 heap [k] = *minpos;
834
835 k = minpos - heap;
836 }
837
838 heap [k] = w;
839 ev_active (heap [k]) = k;
840}
841
842#else // 4HEAP
843
844#define HEAP0 1
845
846/* towards the root */
847void inline_speed
848upheap (WT *heap, int k)
849{
850 WT w = heap [k];
851
852 for (;;)
853 {
854 int p = k >> 1;
855
856 /* maybe we could use a dummy element at heap [0]? */
857 if (!p || heap [p]->at <= w->at)
858 break;
859
860 heap [k] = heap [p];
861 ev_active (heap [k]) = k;
862 k = p;
863 }
864
865 heap [k] = w;
866 ev_active (heap [k]) = k;
867}
868
869/* away from the root */
870void inline_speed
871downheap (WT *heap, int N, int k)
872{
873 WT w = heap [k];
874
875 for (;;)
876 {
789 int c = (k << 1) + 1; 877 int c = k << 1;
790 878
791 if (c >= N) 879 if (c > N)
792 break; 880 break;
793 881
794 c += c + 1 < N && heap [c]->at > heap [c + 1]->at 882 c += c + 1 < N && heap [c]->at > heap [c + 1]->at
795 ? 1 : 0; 883 ? 1 : 0;
796 884
797 if (w->at <= heap [c]->at) 885 if (w->at <= heap [c]->at)
798 break; 886 break;
799 887
800 heap [k] = heap [c]; 888 heap [k] = heap [c];
801 ((W)heap [k])->active = k + 1; 889 ((W)heap [k])->active = k;
802 890
803 k = c; 891 k = c;
804 } 892 }
805 893
806 heap [k] = w; 894 heap [k] = w;
807 ((W)heap [k])->active = k + 1; 895 ev_active (heap [k]) = k;
808} 896}
897#endif
809 898
810void inline_size 899void inline_size
811adjustheap (WT *heap, int N, int k) 900adjustheap (WT *heap, int N, int k)
812{ 901{
813 upheap (heap, k); 902 upheap (heap, k);
908pipecb (EV_P_ ev_io *iow, int revents) 997pipecb (EV_P_ ev_io *iow, int revents)
909{ 998{
910#if EV_USE_EVENTFD 999#if EV_USE_EVENTFD
911 if (evfd >= 0) 1000 if (evfd >= 0)
912 { 1001 {
913 uint64_t counter = 1; 1002 uint64_t counter;
914 read (evfd, &counter, sizeof (uint64_t)); 1003 read (evfd, &counter, sizeof (uint64_t));
915 } 1004 }
916 else 1005 else
917#endif 1006#endif
918 { 1007 {
1364void 1453void
1365ev_loop_fork (EV_P) 1454ev_loop_fork (EV_P)
1366{ 1455{
1367 postfork = 1; /* must be in line with ev_default_fork */ 1456 postfork = 1; /* must be in line with ev_default_fork */
1368} 1457}
1369
1370#endif 1458#endif
1371 1459
1372#if EV_MULTIPLICITY 1460#if EV_MULTIPLICITY
1373struct ev_loop * 1461struct ev_loop *
1374ev_default_loop_init (unsigned int flags) 1462ev_default_loop_init (unsigned int flags)
1455 EV_CB_INVOKE (p->w, p->events); 1543 EV_CB_INVOKE (p->w, p->events);
1456 } 1544 }
1457 } 1545 }
1458} 1546}
1459 1547
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 1548#if EV_IDLE_ENABLE
1541void inline_size 1549void inline_size
1542idle_reify (EV_P) 1550idle_reify (EV_P)
1543{ 1551{
1544 if (expect_false (idleall)) 1552 if (expect_false (idleall))
1555 queue_events (EV_A_ (W *)idles [pri], idlecnt [pri], EV_IDLE); 1563 queue_events (EV_A_ (W *)idles [pri], idlecnt [pri], EV_IDLE);
1556 break; 1564 break;
1557 } 1565 }
1558 } 1566 }
1559 } 1567 }
1568}
1569#endif
1570
1571void inline_size
1572timers_reify (EV_P)
1573{
1574 while (timercnt && ev_at (timers [HEAP0]) <= mn_now)
1575 {
1576 ev_timer *w = (ev_timer *)timers [HEAP0];
1577
1578 /*assert (("inactive timer on timer heap detected", ev_is_active (w)));*/
1579
1580 /* first reschedule or stop timer */
1581 if (w->repeat)
1582 {
1583 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
1584
1585 ev_at (w) += w->repeat;
1586 if (ev_at (w) < mn_now)
1587 ev_at (w) = mn_now;
1588
1589 downheap (timers, timercnt, HEAP0);
1590 }
1591 else
1592 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
1593
1594 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT);
1595 }
1596}
1597
1598#if EV_PERIODIC_ENABLE
1599void inline_size
1600periodics_reify (EV_P)
1601{
1602 while (periodiccnt && ev_at (periodics [HEAP0]) <= ev_rt_now)
1603 {
1604 ev_periodic *w = (ev_periodic *)periodics [HEAP0];
1605
1606 /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/
1607
1608 /* first reschedule or stop timer */
1609 if (w->reschedule_cb)
1610 {
1611 ev_at (w) = w->reschedule_cb (w, ev_rt_now + TIME_EPSILON);
1612 assert (("ev_periodic reschedule callback returned time in the past", ev_at (w) > ev_rt_now));
1613 downheap (periodics, periodiccnt, 1);
1614 }
1615 else if (w->interval)
1616 {
1617 ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval;
1618 if (ev_at (w) - ev_rt_now <= TIME_EPSILON) ev_at (w) += w->interval;
1619 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ev_at (w) > ev_rt_now));
1620 downheap (periodics, periodiccnt, HEAP0);
1621 }
1622 else
1623 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
1624
1625 ev_feed_event (EV_A_ (W)w, EV_PERIODIC);
1626 }
1627}
1628
1629static void noinline
1630periodics_reschedule (EV_P)
1631{
1632 int i;
1633
1634 /* adjust periodics after time jump */
1635 for (i = 1; i <= periodiccnt; ++i)
1636 {
1637 ev_periodic *w = (ev_periodic *)periodics [i];
1638
1639 if (w->reschedule_cb)
1640 ev_at (w) = w->reschedule_cb (w, ev_rt_now);
1641 else if (w->interval)
1642 ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval;
1643 }
1644
1645 /* now rebuild the heap */
1646 for (i = periodiccnt >> 1; --i; )
1647 downheap (periodics, periodiccnt, i + HEAP0);
1560} 1648}
1561#endif 1649#endif
1562 1650
1563void inline_speed 1651void inline_speed
1564time_update (EV_P_ ev_tstamp max_block) 1652time_update (EV_P_ ev_tstamp max_block)
1593 */ 1681 */
1594 for (i = 4; --i; ) 1682 for (i = 4; --i; )
1595 { 1683 {
1596 rtmn_diff = ev_rt_now - mn_now; 1684 rtmn_diff = ev_rt_now - mn_now;
1597 1685
1598 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) 1686 if (expect_true (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP))
1599 return; /* all is well */ 1687 return; /* all is well */
1600 1688
1601 ev_rt_now = ev_time (); 1689 ev_rt_now = ev_time ();
1602 mn_now = get_clock (); 1690 mn_now = get_clock ();
1603 now_floor = mn_now; 1691 now_floor = mn_now;
1618 { 1706 {
1619#if EV_PERIODIC_ENABLE 1707#if EV_PERIODIC_ENABLE
1620 periodics_reschedule (EV_A); 1708 periodics_reschedule (EV_A);
1621#endif 1709#endif
1622 /* adjust timers. this is easy, as the offset is the same for all of them */ 1710 /* adjust timers. this is easy, as the offset is the same for all of them */
1623 for (i = 0; i < timercnt; ++i) 1711 for (i = 1; i <= timercnt; ++i)
1624 ((WT)timers [i])->at += ev_rt_now - mn_now; 1712 ev_at (timers [i]) += ev_rt_now - mn_now;
1625 } 1713 }
1626 1714
1627 mn_now = ev_rt_now; 1715 mn_now = ev_rt_now;
1628 } 1716 }
1629} 1717}
1699 1787
1700 waittime = MAX_BLOCKTIME; 1788 waittime = MAX_BLOCKTIME;
1701 1789
1702 if (timercnt) 1790 if (timercnt)
1703 { 1791 {
1704 ev_tstamp to = ((WT)timers [0])->at - mn_now + backend_fudge; 1792 ev_tstamp to = ev_at (timers [HEAP0]) - mn_now + backend_fudge;
1705 if (waittime > to) waittime = to; 1793 if (waittime > to) waittime = to;
1706 } 1794 }
1707 1795
1708#if EV_PERIODIC_ENABLE 1796#if EV_PERIODIC_ENABLE
1709 if (periodiccnt) 1797 if (periodiccnt)
1710 { 1798 {
1711 ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + backend_fudge; 1799 ev_tstamp to = ev_at (periodics [HEAP0]) - ev_rt_now + backend_fudge;
1712 if (waittime > to) waittime = to; 1800 if (waittime > to) waittime = to;
1713 } 1801 }
1714#endif 1802#endif
1715 1803
1716 if (expect_false (waittime < timeout_blocktime)) 1804 if (expect_false (waittime < timeout_blocktime))
1882ev_timer_start (EV_P_ ev_timer *w) 1970ev_timer_start (EV_P_ ev_timer *w)
1883{ 1971{
1884 if (expect_false (ev_is_active (w))) 1972 if (expect_false (ev_is_active (w)))
1885 return; 1973 return;
1886 1974
1887 ((WT)w)->at += mn_now; 1975 ev_at (w) += mn_now;
1888 1976
1889 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); 1977 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
1890 1978
1891 ev_start (EV_A_ (W)w, ++timercnt); 1979 ev_start (EV_A_ (W)w, ++timercnt + HEAP0 - 1);
1892 array_needsize (WT, timers, timermax, timercnt, EMPTY2); 1980 array_needsize (WT, timers, timermax, timercnt + HEAP0, EMPTY2);
1893 timers [timercnt - 1] = (WT)w; 1981 timers [ev_active (w)] = (WT)w;
1894 upheap (timers, timercnt - 1); 1982 upheap (timers, ev_active (w));
1895 1983
1896 /*assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));*/ 1984 /*assert (("internal timer heap corruption", timers [ev_active (w)] == w));*/
1897} 1985}
1898 1986
1899void noinline 1987void noinline
1900ev_timer_stop (EV_P_ ev_timer *w) 1988ev_timer_stop (EV_P_ ev_timer *w)
1901{ 1989{
1902 clear_pending (EV_A_ (W)w); 1990 clear_pending (EV_A_ (W)w);
1903 if (expect_false (!ev_is_active (w))) 1991 if (expect_false (!ev_is_active (w)))
1904 return; 1992 return;
1905 1993
1906 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == (WT)w));
1907
1908 { 1994 {
1909 int active = ((W)w)->active; 1995 int active = ev_active (w);
1910 1996
1997 assert (("internal timer heap corruption", timers [active] == (WT)w));
1998
1911 if (expect_true (--active < --timercnt)) 1999 if (expect_true (active < timercnt + HEAP0 - 1))
1912 { 2000 {
1913 timers [active] = timers [timercnt]; 2001 timers [active] = timers [timercnt + HEAP0 - 1];
1914 adjustheap (timers, timercnt, active); 2002 adjustheap (timers, timercnt, active);
1915 } 2003 }
2004
2005 --timercnt;
1916 } 2006 }
1917 2007
1918 ((WT)w)->at -= mn_now; 2008 ev_at (w) -= mn_now;
1919 2009
1920 ev_stop (EV_A_ (W)w); 2010 ev_stop (EV_A_ (W)w);
1921} 2011}
1922 2012
1923void noinline 2013void noinline
1925{ 2015{
1926 if (ev_is_active (w)) 2016 if (ev_is_active (w))
1927 { 2017 {
1928 if (w->repeat) 2018 if (w->repeat)
1929 { 2019 {
1930 ((WT)w)->at = mn_now + w->repeat; 2020 ev_at (w) = mn_now + w->repeat;
1931 adjustheap (timers, timercnt, ((W)w)->active - 1); 2021 adjustheap (timers, timercnt, ev_active (w));
1932 } 2022 }
1933 else 2023 else
1934 ev_timer_stop (EV_A_ w); 2024 ev_timer_stop (EV_A_ w);
1935 } 2025 }
1936 else if (w->repeat) 2026 else if (w->repeat)
1937 { 2027 {
1938 w->at = w->repeat; 2028 ev_at (w) = w->repeat;
1939 ev_timer_start (EV_A_ w); 2029 ev_timer_start (EV_A_ w);
1940 } 2030 }
1941} 2031}
1942 2032
1943#if EV_PERIODIC_ENABLE 2033#if EV_PERIODIC_ENABLE
1946{ 2036{
1947 if (expect_false (ev_is_active (w))) 2037 if (expect_false (ev_is_active (w)))
1948 return; 2038 return;
1949 2039
1950 if (w->reschedule_cb) 2040 if (w->reschedule_cb)
1951 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); 2041 ev_at (w) = w->reschedule_cb (w, ev_rt_now);
1952 else if (w->interval) 2042 else if (w->interval)
1953 { 2043 {
1954 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); 2044 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 */ 2045 /* 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; 2046 ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval;
1957 } 2047 }
1958 else 2048 else
1959 ((WT)w)->at = w->offset; 2049 ev_at (w) = w->offset;
1960 2050
1961 ev_start (EV_A_ (W)w, ++periodiccnt); 2051 ev_start (EV_A_ (W)w, ++periodiccnt + HEAP0 - 1);
1962 array_needsize (WT, periodics, periodicmax, periodiccnt, EMPTY2); 2052 array_needsize (WT, periodics, periodicmax, periodiccnt + HEAP0, EMPTY2);
1963 periodics [periodiccnt - 1] = (WT)w; 2053 periodics [ev_active (w)] = (WT)w;
1964 upheap (periodics, periodiccnt - 1); 2054 upheap (periodics, ev_active (w));
1965 2055
1966 /*assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));*/ 2056 /*assert (("internal periodic heap corruption", periodics [ev_active (w)] == w));*/
1967} 2057}
1968 2058
1969void noinline 2059void noinline
1970ev_periodic_stop (EV_P_ ev_periodic *w) 2060ev_periodic_stop (EV_P_ ev_periodic *w)
1971{ 2061{
1972 clear_pending (EV_A_ (W)w); 2062 clear_pending (EV_A_ (W)w);
1973 if (expect_false (!ev_is_active (w))) 2063 if (expect_false (!ev_is_active (w)))
1974 return; 2064 return;
1975 2065
1976 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == (WT)w));
1977
1978 { 2066 {
1979 int active = ((W)w)->active; 2067 int active = ev_active (w);
1980 2068
2069 assert (("internal periodic heap corruption", periodics [active] == (WT)w));
2070
1981 if (expect_true (--active < --periodiccnt)) 2071 if (expect_true (active < periodiccnt + HEAP0 - 1))
1982 { 2072 {
1983 periodics [active] = periodics [periodiccnt]; 2073 periodics [active] = periodics [periodiccnt + HEAP0 - 1];
1984 adjustheap (periodics, periodiccnt, active); 2074 adjustheap (periodics, periodiccnt, active);
1985 } 2075 }
2076
2077 --periodiccnt;
1986 } 2078 }
1987 2079
1988 ev_stop (EV_A_ (W)w); 2080 ev_stop (EV_A_ (W)w);
1989} 2081}
1990 2082
2106 if (w->wd < 0) 2198 if (w->wd < 0)
2107 { 2199 {
2108 ev_timer_start (EV_A_ &w->timer); /* this is not race-free, so we still need to recheck periodically */ 2200 ev_timer_start (EV_A_ &w->timer); /* this is not race-free, so we still need to recheck periodically */
2109 2201
2110 /* monitor some parent directory for speedup hints */ 2202 /* monitor some parent directory for speedup hints */
2203 /* note that exceeding the hardcoded limit is not a correctness issue, */
2204 /* but an efficiency issue only */
2111 if ((errno == ENOENT || errno == EACCES) && strlen (w->path) < 4096) 2205 if ((errno == ENOENT || errno == EACCES) && strlen (w->path) < 4096)
2112 { 2206 {
2113 char path [4096]; 2207 char path [4096];
2114 strcpy (path, w->path); 2208 strcpy (path, w->path);
2115 2209
2360 clear_pending (EV_A_ (W)w); 2454 clear_pending (EV_A_ (W)w);
2361 if (expect_false (!ev_is_active (w))) 2455 if (expect_false (!ev_is_active (w)))
2362 return; 2456 return;
2363 2457
2364 { 2458 {
2365 int active = ((W)w)->active; 2459 int active = ev_active (w);
2366 2460
2367 idles [ABSPRI (w)][active - 1] = idles [ABSPRI (w)][--idlecnt [ABSPRI (w)]]; 2461 idles [ABSPRI (w)][active - 1] = idles [ABSPRI (w)][--idlecnt [ABSPRI (w)]];
2368 ((W)idles [ABSPRI (w)][active - 1])->active = active; 2462 ev_active (idles [ABSPRI (w)][active - 1]) = active;
2369 2463
2370 ev_stop (EV_A_ (W)w); 2464 ev_stop (EV_A_ (W)w);
2371 --idleall; 2465 --idleall;
2372 } 2466 }
2373} 2467}
2390 clear_pending (EV_A_ (W)w); 2484 clear_pending (EV_A_ (W)w);
2391 if (expect_false (!ev_is_active (w))) 2485 if (expect_false (!ev_is_active (w)))
2392 return; 2486 return;
2393 2487
2394 { 2488 {
2395 int active = ((W)w)->active; 2489 int active = ev_active (w);
2490
2396 prepares [active - 1] = prepares [--preparecnt]; 2491 prepares [active - 1] = prepares [--preparecnt];
2397 ((W)prepares [active - 1])->active = active; 2492 ev_active (prepares [active - 1]) = active;
2398 } 2493 }
2399 2494
2400 ev_stop (EV_A_ (W)w); 2495 ev_stop (EV_A_ (W)w);
2401} 2496}
2402 2497
2417 clear_pending (EV_A_ (W)w); 2512 clear_pending (EV_A_ (W)w);
2418 if (expect_false (!ev_is_active (w))) 2513 if (expect_false (!ev_is_active (w)))
2419 return; 2514 return;
2420 2515
2421 { 2516 {
2422 int active = ((W)w)->active; 2517 int active = ev_active (w);
2518
2423 checks [active - 1] = checks [--checkcnt]; 2519 checks [active - 1] = checks [--checkcnt];
2424 ((W)checks [active - 1])->active = active; 2520 ev_active (checks [active - 1]) = active;
2425 } 2521 }
2426 2522
2427 ev_stop (EV_A_ (W)w); 2523 ev_stop (EV_A_ (W)w);
2428} 2524}
2429 2525
2525 clear_pending (EV_A_ (W)w); 2621 clear_pending (EV_A_ (W)w);
2526 if (expect_false (!ev_is_active (w))) 2622 if (expect_false (!ev_is_active (w)))
2527 return; 2623 return;
2528 2624
2529 { 2625 {
2530 int active = ((W)w)->active; 2626 int active = ev_active (w);
2627
2531 forks [active - 1] = forks [--forkcnt]; 2628 forks [active - 1] = forks [--forkcnt];
2532 ((W)forks [active - 1])->active = active; 2629 ev_active (forks [active - 1]) = active;
2533 } 2630 }
2534 2631
2535 ev_stop (EV_A_ (W)w); 2632 ev_stop (EV_A_ (W)w);
2536} 2633}
2537#endif 2634#endif
2556 clear_pending (EV_A_ (W)w); 2653 clear_pending (EV_A_ (W)w);
2557 if (expect_false (!ev_is_active (w))) 2654 if (expect_false (!ev_is_active (w)))
2558 return; 2655 return;
2559 2656
2560 { 2657 {
2561 int active = ((W)w)->active; 2658 int active = ev_active (w);
2659
2562 asyncs [active - 1] = asyncs [--asynccnt]; 2660 asyncs [active - 1] = asyncs [--asynccnt];
2563 ((W)asyncs [active - 1])->active = active; 2661 ev_active (asyncs [active - 1]) = active;
2564 } 2662 }
2565 2663
2566 ev_stop (EV_A_ (W)w); 2664 ev_stop (EV_A_ (W)w);
2567} 2665}
2568 2666

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines