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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines