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

Comparing libev/ev.c (file contents):
Revision 1.228 by root, Fri May 2 08:07:37 2008 UTC vs.
Revision 1.242 by root, Fri May 9 14:07:19 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
330#define ev_at(w) ((WT)(w))->at 331#define ev_at(w) ((WT)(w))->at
331 332
332#if EV_USE_MONOTONIC 333#if EV_USE_MONOTONIC
333/* 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 */
334/* giving it a reasonably high chance of working on typical architetcures */ 335/* giving it a reasonably high chance of working on typical architetcures */
421 W w; 422 W w;
422 int events; 423 int events;
423} ANPENDING; 424} ANPENDING;
424 425
425#if EV_USE_INOTIFY 426#if EV_USE_INOTIFY
427/* hash table entry per inotify-id */
426typedef struct 428typedef struct
427{ 429{
428 WL head; 430 WL head;
429} ANFS; 431} ANFS;
432#endif
433
434/* Heap Entry */
435#define EV_HEAP_CACHE_AT 0
436#if EV_HEAP_CACHE_AT
437 typedef struct {
438 WT w;
439 ev_tstamp at;
440 } ANHE;
441
442 #define ANHE_w(he) (he).w /* access watcher, read-write */
443 #define ANHE_at(he) (he).at /* access cached at, read-only */
444 #define ANHE_at_set(he) (he).at = (he).w->at /* update at from watcher */
445#else
446 typedef WT ANHE;
447
448 #define ANHE_w(he) (he)
449 #define ANHE_at(he) (he)->at
450 #define ANHE_at_set(he)
430#endif 451#endif
431 452
432#if EV_MULTIPLICITY 453#if EV_MULTIPLICITY
433 454
434 struct ev_loop 455 struct ev_loop
519 } 540 }
520} 541}
521 542
522/*****************************************************************************/ 543/*****************************************************************************/
523 544
545#define MALLOC_ROUND 4096 /* prefer to allocate in chunks of this size, must be 2**n and >> 4 longs */
546
524int inline_size 547int inline_size
525array_nextsize (int elem, int cur, int cnt) 548array_nextsize (int elem, int cur, int cnt)
526{ 549{
527 int ncur = cur + 1; 550 int ncur = cur + 1;
528 551
529 do 552 do
530 ncur <<= 1; 553 ncur <<= 1;
531 while (cnt > ncur); 554 while (cnt > ncur);
532 555
533 /* if size > 4096, round to 4096 - 4 * longs to accomodate malloc overhead */ 556 /* if size is large, round to MALLOC_ROUND - 4 * longs to accomodate malloc overhead */
534 if (elem * ncur > 4096) 557 if (elem * ncur > MALLOC_ROUND - sizeof (void *) * 4)
535 { 558 {
536 ncur *= elem; 559 ncur *= elem;
537 ncur = (ncur + elem + 4095 + sizeof (void *) * 4) & ~4095; 560 ncur = (ncur + elem + (MALLOC_ROUND - 1) + sizeof (void *) * 4) & ~(MALLOC_ROUND - 1);
538 ncur = ncur - sizeof (void *) * 4; 561 ncur = ncur - sizeof (void *) * 4;
539 ncur /= elem; 562 ncur /= elem;
540 } 563 }
541 564
542 return ncur; 565 return ncur;
756 } 779 }
757} 780}
758 781
759/*****************************************************************************/ 782/*****************************************************************************/
760 783
784/*
785 * the heap functions want a real array index. array index 0 uis guaranteed to not
786 * be in-use at any time. the first heap entry is at array [HEAP0]. DHEAP gives
787 * the branching factor of the d-tree.
788 */
789
790/*
791 * at the moment we allow libev the luxury of two heaps,
792 * a small-code-size 2-heap one and a ~1.5kb larger 4-heap
793 * which is more cache-efficient.
794 * the difference is about 5% with 50000+ watchers.
795 */
796#define EV_USE_4HEAP !EV_MINIMAL
797#if EV_USE_4HEAP
798
799#define DHEAP 4
800#define HEAP0 (DHEAP - 1) /* index of first element in heap */
801
761/* towards the root */ 802/* towards the root */
762void inline_speed 803void inline_speed
763upheap (WT *heap, int k) 804upheap (ANHE *heap, int k)
764{ 805{
765 WT w = heap [k]; 806 ANHE he = heap [k];
766 807
767 for (;;) 808 for (;;)
768 { 809 {
769 int p = k >> 1; 810 int p = ((k - HEAP0 - 1) / DHEAP) + HEAP0;
770 811
771 /* maybe we could use a dummy element at heap [0]? */ 812 if (p == k || ANHE_at (heap [p]) <= ANHE_at (he))
772 if (!p || heap [p]->at <= w->at)
773 break; 813 break;
774 814
775 heap [k] = heap [p]; 815 heap [k] = heap [p];
776 ((W)heap [k])->active = k; 816 ev_active (ANHE_w (heap [k])) = k;
777 k = p; 817 k = p;
778 } 818 }
779 819
820 ev_active (ANHE_w (he)) = k;
780 heap [k] = w; 821 heap [k] = he;
781 ((W)heap [k])->active = k;
782} 822}
783 823
784/* away from the root */ 824/* away from the root */
785void inline_speed 825void inline_speed
786downheap (WT *heap, int N, int k) 826downheap (ANHE *heap, int N, int k)
787{ 827{
788 WT w = heap [k]; 828 ANHE he = heap [k];
829 ANHE *E = heap + N + HEAP0;
830
831 for (;;)
832 {
833 ev_tstamp minat;
834 ANHE *minpos;
835 ANHE *pos = heap + DHEAP * (k - HEAP0) + HEAP0;
836
837 // find minimum child
838 if (expect_true (pos + DHEAP - 1 < E))
839 {
840 /* fast path */ (minpos = pos + 0), (minat = ANHE_at (*minpos));
841 if (ANHE_at (pos [1]) < minat) (minpos = pos + 1), (minat = ANHE_at (*minpos));
842 if (ANHE_at (pos [2]) < minat) (minpos = pos + 2), (minat = ANHE_at (*minpos));
843 if (ANHE_at (pos [3]) < minat) (minpos = pos + 3), (minat = ANHE_at (*minpos));
844 }
845 else if (pos < E)
846 {
847 /* slow path */ (minpos = pos + 0), (minat = ANHE_at (*minpos));
848 if (pos + 1 < E && ANHE_at (pos [1]) < minat) (minpos = pos + 1), (minat = ANHE_at (*minpos));
849 if (pos + 2 < E && ANHE_at (pos [2]) < minat) (minpos = pos + 2), (minat = ANHE_at (*minpos));
850 if (pos + 3 < E && ANHE_at (pos [3]) < minat) (minpos = pos + 3), (minat = ANHE_at (*minpos));
851 }
852 else
853 break;
854
855 if (ANHE_at (he) <= minat)
856 break;
857
858 ev_active (ANHE_w (*minpos)) = k;
859 heap [k] = *minpos;
860
861 k = minpos - heap;
862 }
863
864 ev_active (ANHE_w (he)) = k;
865 heap [k] = he;
866}
867
868#else // 4HEAP
869
870#define HEAP0 1
871
872/* towards the root */
873void inline_speed
874upheap (ANHE *heap, int k)
875{
876 ANHE he = heap [k];
877
878 for (;;)
879 {
880 int p = k >> 1;
881
882 /* maybe we could use a dummy element at heap [0]? */
883 if (!p || ANHE_at (heap [p]) <= ANHE_at (he))
884 break;
885
886 heap [k] = heap [p];
887 ev_active (ANHE_w (heap [k])) = k;
888 k = p;
889 }
890
891 heap [k] = w;
892 ev_active (ANHE_w (heap [k])) = k;
893}
894
895/* away from the root */
896void inline_speed
897downheap (ANHE *heap, int N, int k)
898{
899 ANHE he = heap [k];
789 900
790 for (;;) 901 for (;;)
791 { 902 {
792 int c = k << 1; 903 int c = k << 1;
793 904
794 if (c > N) 905 if (c > N)
795 break; 906 break;
796 907
797 c += c < N && heap [c]->at > heap [c + 1]->at 908 c += c + 1 < N && ANHE_at (heap [c]) > ANHE_at (heap [c + 1])
798 ? 1 : 0; 909 ? 1 : 0;
799 910
800 if (w->at <= heap [c]->at) 911 if (w->at <= ANHE_at (heap [c]))
801 break; 912 break;
802 913
803 heap [k] = heap [c]; 914 heap [k] = heap [c];
804 ((W)heap [k])->active = k; 915 ev_active (ANHE_w (heap [k])) = k;
805 916
806 k = c; 917 k = c;
807 } 918 }
808 919
809 heap [k] = w; 920 heap [k] = he;
810 ((W)heap [k])->active = k; 921 ev_active (ANHE_w (he)) = k;
811} 922}
923#endif
812 924
813void inline_size 925void inline_size
814adjustheap (WT *heap, int N, int k) 926adjustheap (ANHE *heap, int N, int k)
815{ 927{
816 upheap (heap, k); 928 upheap (heap, k);
817 downheap (heap, N, k); 929 downheap (heap, N, k);
818} 930}
819 931
911pipecb (EV_P_ ev_io *iow, int revents) 1023pipecb (EV_P_ ev_io *iow, int revents)
912{ 1024{
913#if EV_USE_EVENTFD 1025#if EV_USE_EVENTFD
914 if (evfd >= 0) 1026 if (evfd >= 0)
915 { 1027 {
916 uint64_t counter = 1; 1028 uint64_t counter;
917 read (evfd, &counter, sizeof (uint64_t)); 1029 read (evfd, &counter, sizeof (uint64_t));
918 } 1030 }
919 else 1031 else
920#endif 1032#endif
921 { 1033 {
1367void 1479void
1368ev_loop_fork (EV_P) 1480ev_loop_fork (EV_P)
1369{ 1481{
1370 postfork = 1; /* must be in line with ev_default_fork */ 1482 postfork = 1; /* must be in line with ev_default_fork */
1371} 1483}
1372
1373#endif 1484#endif
1374 1485
1375#if EV_MULTIPLICITY 1486#if EV_MULTIPLICITY
1376struct ev_loop * 1487struct ev_loop *
1377ev_default_loop_init (unsigned int flags) 1488ev_default_loop_init (unsigned int flags)
1458 EV_CB_INVOKE (p->w, p->events); 1569 EV_CB_INVOKE (p->w, p->events);
1459 } 1570 }
1460 } 1571 }
1461} 1572}
1462 1573
1574#if EV_IDLE_ENABLE
1575void inline_size
1576idle_reify (EV_P)
1577{
1578 if (expect_false (idleall))
1579 {
1580 int pri;
1581
1582 for (pri = NUMPRI; pri--; )
1583 {
1584 if (pendingcnt [pri])
1585 break;
1586
1587 if (idlecnt [pri])
1588 {
1589 queue_events (EV_A_ (W *)idles [pri], idlecnt [pri], EV_IDLE);
1590 break;
1591 }
1592 }
1593 }
1594}
1595#endif
1596
1463void inline_size 1597void inline_size
1464timers_reify (EV_P) 1598timers_reify (EV_P)
1465{ 1599{
1466 while (timercnt && ev_at (timers [1]) <= mn_now) 1600 while (timercnt && ANHE_at (timers [HEAP0]) <= mn_now)
1467 { 1601 {
1468 ev_timer *w = (ev_timer *)timers [1]; 1602 ev_timer *w = (ev_timer *)ANHE_w (timers [HEAP0]);
1469 1603
1470 /*assert (("inactive timer on timer heap detected", ev_is_active (w)));*/ 1604 /*assert (("inactive timer on timer heap detected", ev_is_active (w)));*/
1471 1605
1472 /* first reschedule or stop timer */ 1606 /* first reschedule or stop timer */
1473 if (w->repeat) 1607 if (w->repeat)
1476 1610
1477 ev_at (w) += w->repeat; 1611 ev_at (w) += w->repeat;
1478 if (ev_at (w) < mn_now) 1612 if (ev_at (w) < mn_now)
1479 ev_at (w) = mn_now; 1613 ev_at (w) = mn_now;
1480 1614
1615 ANHE_at_set (timers [HEAP0]);
1481 downheap (timers, timercnt, 1); 1616 downheap (timers, timercnt, HEAP0);
1482 } 1617 }
1483 else 1618 else
1484 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ 1619 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
1485 1620
1486 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); 1621 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT);
1489 1624
1490#if EV_PERIODIC_ENABLE 1625#if EV_PERIODIC_ENABLE
1491void inline_size 1626void inline_size
1492periodics_reify (EV_P) 1627periodics_reify (EV_P)
1493{ 1628{
1494 while (periodiccnt && ev_at (periodics [1]) <= ev_rt_now) 1629 while (periodiccnt && ANHE_at (periodics [HEAP0]) <= ev_rt_now)
1495 { 1630 {
1496 ev_periodic *w = (ev_periodic *)periodics [1]; 1631 ev_periodic *w = (ev_periodic *)ANHE_w (periodics [HEAP0]);
1497 1632
1498 /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/ 1633 /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/
1499 1634
1500 /* first reschedule or stop timer */ 1635 /* first reschedule or stop timer */
1501 if (w->reschedule_cb) 1636 if (w->reschedule_cb)
1502 { 1637 {
1503 ev_at (w) = w->reschedule_cb (w, ev_rt_now + TIME_EPSILON); 1638 ev_at (w) = w->reschedule_cb (w, ev_rt_now + TIME_EPSILON);
1504 assert (("ev_periodic reschedule callback returned time in the past", ev_at (w) > ev_rt_now)); 1639 assert (("ev_periodic reschedule callback returned time in the past", ev_at (w) > ev_rt_now));
1640 ANHE_at_set (periodics [HEAP0]);
1505 downheap (periodics, periodiccnt, 1); 1641 downheap (periodics, periodiccnt, HEAP0);
1506 } 1642 }
1507 else if (w->interval) 1643 else if (w->interval)
1508 { 1644 {
1509 ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; 1645 ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval;
1510 if (ev_at (w) - ev_rt_now <= TIME_EPSILON) ev_at (w) += w->interval; 1646 if (ev_at (w) - ev_rt_now <= TIME_EPSILON) ev_at (w) += w->interval;
1511 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ev_at (w) > ev_rt_now)); 1647 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ev_at (w) > ev_rt_now));
1648 ANHE_at_set (periodics [HEAP0]);
1512 downheap (periodics, periodiccnt, 1); 1649 downheap (periodics, periodiccnt, HEAP0);
1513 } 1650 }
1514 else 1651 else
1515 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ 1652 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
1516 1653
1517 ev_feed_event (EV_A_ (W)w, EV_PERIODIC); 1654 ev_feed_event (EV_A_ (W)w, EV_PERIODIC);
1522periodics_reschedule (EV_P) 1659periodics_reschedule (EV_P)
1523{ 1660{
1524 int i; 1661 int i;
1525 1662
1526 /* adjust periodics after time jump */ 1663 /* adjust periodics after time jump */
1527 for (i = 0; i < periodiccnt; ++i) 1664 for (i = HEAP0; i < periodiccnt + HEAP0; ++i)
1528 { 1665 {
1529 ev_periodic *w = (ev_periodic *)periodics [i]; 1666 ev_periodic *w = (ev_periodic *)ANHE_w (periodics [i]);
1530 1667
1531 if (w->reschedule_cb) 1668 if (w->reschedule_cb)
1532 ev_at (w) = w->reschedule_cb (w, ev_rt_now); 1669 ev_at (w) = w->reschedule_cb (w, ev_rt_now);
1533 else if (w->interval) 1670 else if (w->interval)
1534 ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; 1671 ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval;
1535 }
1536 1672
1537 /* now rebuild the heap */ 1673 ANHE_at_set (periodics [i]);
1674 }
1675
1676 /* now rebuild the heap, this for the 2-heap, inefficient for the 4-heap, but correct */
1538 for (i = periodiccnt >> 1; i--; ) 1677 for (i = periodiccnt >> 1; --i; )
1539 downheap (periodics, periodiccnt, i); 1678 downheap (periodics, periodiccnt, i + HEAP0);
1540}
1541#endif
1542
1543#if EV_IDLE_ENABLE
1544void inline_size
1545idle_reify (EV_P)
1546{
1547 if (expect_false (idleall))
1548 {
1549 int pri;
1550
1551 for (pri = NUMPRI; pri--; )
1552 {
1553 if (pendingcnt [pri])
1554 break;
1555
1556 if (idlecnt [pri])
1557 {
1558 queue_events (EV_A_ (W *)idles [pri], idlecnt [pri], EV_IDLE);
1559 break;
1560 }
1561 }
1562 }
1563} 1679}
1564#endif 1680#endif
1565 1681
1566void inline_speed 1682void inline_speed
1567time_update (EV_P_ ev_tstamp max_block) 1683time_update (EV_P_ ev_tstamp max_block)
1596 */ 1712 */
1597 for (i = 4; --i; ) 1713 for (i = 4; --i; )
1598 { 1714 {
1599 rtmn_diff = ev_rt_now - mn_now; 1715 rtmn_diff = ev_rt_now - mn_now;
1600 1716
1601 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) 1717 if (expect_true (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP))
1602 return; /* all is well */ 1718 return; /* all is well */
1603 1719
1604 ev_rt_now = ev_time (); 1720 ev_rt_now = ev_time ();
1605 mn_now = get_clock (); 1721 mn_now = get_clock ();
1606 now_floor = mn_now; 1722 now_floor = mn_now;
1621 { 1737 {
1622#if EV_PERIODIC_ENABLE 1738#if EV_PERIODIC_ENABLE
1623 periodics_reschedule (EV_A); 1739 periodics_reschedule (EV_A);
1624#endif 1740#endif
1625 /* adjust timers. this is easy, as the offset is the same for all of them */ 1741 /* adjust timers. this is easy, as the offset is the same for all of them */
1626 for (i = 1; i <= timercnt; ++i) 1742 for (i = 0; i < timercnt; ++i)
1627 ev_at (timers [i]) += ev_rt_now - mn_now; 1743 {
1744 ANHE *he = timers + i + HEAP0;
1745 ANHE_w (*he)->at += ev_rt_now - mn_now;
1746 ANHE_at_set (*he);
1747 }
1628 } 1748 }
1629 1749
1630 mn_now = ev_rt_now; 1750 mn_now = ev_rt_now;
1631 } 1751 }
1632} 1752}
1702 1822
1703 waittime = MAX_BLOCKTIME; 1823 waittime = MAX_BLOCKTIME;
1704 1824
1705 if (timercnt) 1825 if (timercnt)
1706 { 1826 {
1707 ev_tstamp to = ev_at (timers [1]) - mn_now + backend_fudge; 1827 ev_tstamp to = ANHE_at (timers [HEAP0]) - mn_now + backend_fudge;
1708 if (waittime > to) waittime = to; 1828 if (waittime > to) waittime = to;
1709 } 1829 }
1710 1830
1711#if EV_PERIODIC_ENABLE 1831#if EV_PERIODIC_ENABLE
1712 if (periodiccnt) 1832 if (periodiccnt)
1713 { 1833 {
1714 ev_tstamp to = ev_at (periodics [1]) - ev_rt_now + backend_fudge; 1834 ev_tstamp to = ANHE_at (periodics [HEAP0]) - ev_rt_now + backend_fudge;
1715 if (waittime > to) waittime = to; 1835 if (waittime > to) waittime = to;
1716 } 1836 }
1717#endif 1837#endif
1718 1838
1719 if (expect_false (waittime < timeout_blocktime)) 1839 if (expect_false (waittime < timeout_blocktime))
1871{ 1991{
1872 clear_pending (EV_A_ (W)w); 1992 clear_pending (EV_A_ (W)w);
1873 if (expect_false (!ev_is_active (w))) 1993 if (expect_false (!ev_is_active (w)))
1874 return; 1994 return;
1875 1995
1876 assert (("ev_io_start called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax)); 1996 assert (("ev_io_stop called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax));
1877 1997
1878 wlist_del (&anfds[w->fd].head, (WL)w); 1998 wlist_del (&anfds[w->fd].head, (WL)w);
1879 ev_stop (EV_A_ (W)w); 1999 ev_stop (EV_A_ (W)w);
1880 2000
1881 fd_change (EV_A_ w->fd, 1); 2001 fd_change (EV_A_ w->fd, 1);
1889 2009
1890 ev_at (w) += mn_now; 2010 ev_at (w) += mn_now;
1891 2011
1892 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); 2012 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
1893 2013
1894 ev_start (EV_A_ (W)w, ++timercnt); 2014 ev_start (EV_A_ (W)w, ++timercnt + HEAP0 - 1);
1895 array_needsize (WT, timers, timermax, timercnt + 1, EMPTY2); 2015 array_needsize (ANHE, timers, timermax, ev_active (w) + 1, EMPTY2);
1896 timers [timercnt] = (WT)w; 2016 ANHE_w (timers [ev_active (w)]) = (WT)w;
2017 ANHE_at_set (timers [ev_active (w)]);
1897 upheap (timers, timercnt); 2018 upheap (timers, ev_active (w));
1898 2019
1899 /*assert (("internal timer heap corruption", timers [((W)w)->active] == w));*/ 2020 /*assert (("internal timer heap corruption", timers [ev_active (w)] == (WT)w));*/
1900} 2021}
1901 2022
1902void noinline 2023void noinline
1903ev_timer_stop (EV_P_ ev_timer *w) 2024ev_timer_stop (EV_P_ ev_timer *w)
1904{ 2025{
1905 clear_pending (EV_A_ (W)w); 2026 clear_pending (EV_A_ (W)w);
1906 if (expect_false (!ev_is_active (w))) 2027 if (expect_false (!ev_is_active (w)))
1907 return; 2028 return;
1908 2029
1909 assert (("internal timer heap corruption", timers [((W)w)->active] == (WT)w));
1910
1911 { 2030 {
1912 int active = ((W)w)->active; 2031 int active = ev_active (w);
1913 2032
2033 assert (("internal timer heap corruption", ANHE_w (timers [active]) == (WT)w));
2034
1914 if (expect_true (active < timercnt)) 2035 if (expect_true (active < timercnt + HEAP0 - 1))
1915 { 2036 {
1916 timers [active] = timers [timercnt]; 2037 timers [active] = timers [timercnt + HEAP0 - 1];
1917 adjustheap (timers, timercnt, active); 2038 adjustheap (timers, timercnt, active);
1918 } 2039 }
1919 2040
1920 --timercnt; 2041 --timercnt;
1921 } 2042 }
1931 if (ev_is_active (w)) 2052 if (ev_is_active (w))
1932 { 2053 {
1933 if (w->repeat) 2054 if (w->repeat)
1934 { 2055 {
1935 ev_at (w) = mn_now + w->repeat; 2056 ev_at (w) = mn_now + w->repeat;
2057 ANHE_at_set (timers [ev_active (w)]);
1936 adjustheap (timers, timercnt, ((W)w)->active); 2058 adjustheap (timers, timercnt, ev_active (w));
1937 } 2059 }
1938 else 2060 else
1939 ev_timer_stop (EV_A_ w); 2061 ev_timer_stop (EV_A_ w);
1940 } 2062 }
1941 else if (w->repeat) 2063 else if (w->repeat)
1942 { 2064 {
1943 w->at = w->repeat; 2065 ev_at (w) = w->repeat;
1944 ev_timer_start (EV_A_ w); 2066 ev_timer_start (EV_A_ w);
1945 } 2067 }
1946} 2068}
1947 2069
1948#if EV_PERIODIC_ENABLE 2070#if EV_PERIODIC_ENABLE
1961 ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; 2083 ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval;
1962 } 2084 }
1963 else 2085 else
1964 ev_at (w) = w->offset; 2086 ev_at (w) = w->offset;
1965 2087
1966 ev_start (EV_A_ (W)w, ++periodiccnt); 2088 ev_start (EV_A_ (W)w, ++periodiccnt + HEAP0 - 1);
1967 array_needsize (WT, periodics, periodicmax, periodiccnt + 1, EMPTY2); 2089 array_needsize (ANHE, periodics, periodicmax, ev_active (w) + 1, EMPTY2);
1968 periodics [periodiccnt] = (WT)w; 2090 ANHE_w (periodics [ev_active (w)]) = (WT)w;
1969 upheap (periodics, periodiccnt); 2091 upheap (periodics, ev_active (w));
1970 2092
1971 /*assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));*/ 2093 /*assert (("internal periodic heap corruption", ANHE_w (periodics [ev_active (w)]) == (WT)w));*/
1972} 2094}
1973 2095
1974void noinline 2096void noinline
1975ev_periodic_stop (EV_P_ ev_periodic *w) 2097ev_periodic_stop (EV_P_ ev_periodic *w)
1976{ 2098{
1977 clear_pending (EV_A_ (W)w); 2099 clear_pending (EV_A_ (W)w);
1978 if (expect_false (!ev_is_active (w))) 2100 if (expect_false (!ev_is_active (w)))
1979 return; 2101 return;
1980 2102
1981 assert (("internal periodic heap corruption", periodics [((W)w)->active] == (WT)w));
1982
1983 { 2103 {
1984 int active = ((W)w)->active; 2104 int active = ev_active (w);
1985 2105
2106 assert (("internal periodic heap corruption", ANHE_w (periodics [active]) == (WT)w));
2107
1986 if (expect_true (active < periodiccnt)) 2108 if (expect_true (active < periodiccnt + HEAP0 - 1))
1987 { 2109 {
1988 periodics [active] = periodics [periodiccnt]; 2110 periodics [active] = periodics [periodiccnt + HEAP0 - 1];
1989 adjustheap (periodics, periodiccnt, active); 2111 adjustheap (periodics, periodiccnt, active);
1990 } 2112 }
1991 2113
1992 --periodiccnt; 2114 --periodiccnt;
1993 } 2115 }
2113 if (w->wd < 0) 2235 if (w->wd < 0)
2114 { 2236 {
2115 ev_timer_start (EV_A_ &w->timer); /* this is not race-free, so we still need to recheck periodically */ 2237 ev_timer_start (EV_A_ &w->timer); /* this is not race-free, so we still need to recheck periodically */
2116 2238
2117 /* monitor some parent directory for speedup hints */ 2239 /* monitor some parent directory for speedup hints */
2240 /* note that exceeding the hardcoded limit is not a correctness issue, */
2241 /* but an efficiency issue only */
2118 if ((errno == ENOENT || errno == EACCES) && strlen (w->path) < 4096) 2242 if ((errno == ENOENT || errno == EACCES) && strlen (w->path) < 4096)
2119 { 2243 {
2120 char path [4096]; 2244 char path [4096];
2121 strcpy (path, w->path); 2245 strcpy (path, w->path);
2122 2246
2367 clear_pending (EV_A_ (W)w); 2491 clear_pending (EV_A_ (W)w);
2368 if (expect_false (!ev_is_active (w))) 2492 if (expect_false (!ev_is_active (w)))
2369 return; 2493 return;
2370 2494
2371 { 2495 {
2372 int active = ((W)w)->active; 2496 int active = ev_active (w);
2373 2497
2374 idles [ABSPRI (w)][active - 1] = idles [ABSPRI (w)][--idlecnt [ABSPRI (w)]]; 2498 idles [ABSPRI (w)][active - 1] = idles [ABSPRI (w)][--idlecnt [ABSPRI (w)]];
2375 ((W)idles [ABSPRI (w)][active - 1])->active = active; 2499 ev_active (idles [ABSPRI (w)][active - 1]) = active;
2376 2500
2377 ev_stop (EV_A_ (W)w); 2501 ev_stop (EV_A_ (W)w);
2378 --idleall; 2502 --idleall;
2379 } 2503 }
2380} 2504}
2397 clear_pending (EV_A_ (W)w); 2521 clear_pending (EV_A_ (W)w);
2398 if (expect_false (!ev_is_active (w))) 2522 if (expect_false (!ev_is_active (w)))
2399 return; 2523 return;
2400 2524
2401 { 2525 {
2402 int active = ((W)w)->active; 2526 int active = ev_active (w);
2527
2403 prepares [active - 1] = prepares [--preparecnt]; 2528 prepares [active - 1] = prepares [--preparecnt];
2404 ((W)prepares [active - 1])->active = active; 2529 ev_active (prepares [active - 1]) = active;
2405 } 2530 }
2406 2531
2407 ev_stop (EV_A_ (W)w); 2532 ev_stop (EV_A_ (W)w);
2408} 2533}
2409 2534
2424 clear_pending (EV_A_ (W)w); 2549 clear_pending (EV_A_ (W)w);
2425 if (expect_false (!ev_is_active (w))) 2550 if (expect_false (!ev_is_active (w)))
2426 return; 2551 return;
2427 2552
2428 { 2553 {
2429 int active = ((W)w)->active; 2554 int active = ev_active (w);
2555
2430 checks [active - 1] = checks [--checkcnt]; 2556 checks [active - 1] = checks [--checkcnt];
2431 ((W)checks [active - 1])->active = active; 2557 ev_active (checks [active - 1]) = active;
2432 } 2558 }
2433 2559
2434 ev_stop (EV_A_ (W)w); 2560 ev_stop (EV_A_ (W)w);
2435} 2561}
2436 2562
2532 clear_pending (EV_A_ (W)w); 2658 clear_pending (EV_A_ (W)w);
2533 if (expect_false (!ev_is_active (w))) 2659 if (expect_false (!ev_is_active (w)))
2534 return; 2660 return;
2535 2661
2536 { 2662 {
2537 int active = ((W)w)->active; 2663 int active = ev_active (w);
2664
2538 forks [active - 1] = forks [--forkcnt]; 2665 forks [active - 1] = forks [--forkcnt];
2539 ((W)forks [active - 1])->active = active; 2666 ev_active (forks [active - 1]) = active;
2540 } 2667 }
2541 2668
2542 ev_stop (EV_A_ (W)w); 2669 ev_stop (EV_A_ (W)w);
2543} 2670}
2544#endif 2671#endif
2563 clear_pending (EV_A_ (W)w); 2690 clear_pending (EV_A_ (W)w);
2564 if (expect_false (!ev_is_active (w))) 2691 if (expect_false (!ev_is_active (w)))
2565 return; 2692 return;
2566 2693
2567 { 2694 {
2568 int active = ((W)w)->active; 2695 int active = ev_active (w);
2696
2569 asyncs [active - 1] = asyncs [--asynccnt]; 2697 asyncs [active - 1] = asyncs [--asynccnt];
2570 ((W)asyncs [active - 1])->active = active; 2698 ev_active (asyncs [active - 1]) = active;
2571 } 2699 }
2572 2700
2573 ev_stop (EV_A_ (W)w); 2701 ev_stop (EV_A_ (W)w);
2574} 2702}
2575 2703

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines