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.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
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
419 W w; 422 W w;
420 int events; 423 int events;
421} ANPENDING; 424} ANPENDING;
422 425
423#if EV_USE_INOTIFY 426#if EV_USE_INOTIFY
427/* hash table entry per inotify-id */
424typedef struct 428typedef struct
425{ 429{
426 WL head; 430 WL head;
427} 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)
428#endif 451#endif
429 452
430#if EV_MULTIPLICITY 453#if EV_MULTIPLICITY
431 454
432 struct ev_loop 455 struct ev_loop
517 } 540 }
518} 541}
519 542
520/*****************************************************************************/ 543/*****************************************************************************/
521 544
545#define MALLOC_ROUND 4096 /* prefer to allocate in chunks of this size, must be 2**n and >> 4 longs */
546
522int inline_size 547int inline_size
523array_nextsize (int elem, int cur, int cnt) 548array_nextsize (int elem, int cur, int cnt)
524{ 549{
525 int ncur = cur + 1; 550 int ncur = cur + 1;
526 551
527 do 552 do
528 ncur <<= 1; 553 ncur <<= 1;
529 while (cnt > ncur); 554 while (cnt > ncur);
530 555
531 /* 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 */
532 if (elem * ncur > 4096) 557 if (elem * ncur > MALLOC_ROUND - sizeof (void *) * 4)
533 { 558 {
534 ncur *= elem; 559 ncur *= elem;
535 ncur = (ncur + elem + 4095 + sizeof (void *) * 4) & ~4095; 560 ncur = (ncur + elem + (MALLOC_ROUND - 1) + sizeof (void *) * 4) & ~(MALLOC_ROUND - 1);
536 ncur = ncur - sizeof (void *) * 4; 561 ncur = ncur - sizeof (void *) * 4;
537 ncur /= elem; 562 ncur /= elem;
538 } 563 }
539 564
540 return ncur; 565 return ncur;
754 } 779 }
755} 780}
756 781
757/*****************************************************************************/ 782/*****************************************************************************/
758 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
759/* towards the root */ 802/* towards the root */
760void inline_speed 803void inline_speed
761upheap (WT *heap, int k) 804upheap (ANHE *heap, int k)
762{ 805{
763 WT w = heap [k]; 806 ANHE he = heap [k];
764 807
765 while (k) 808 for (;;)
766 { 809 {
767 int p = (k - 1) >> 1; 810 int p = ((k - HEAP0 - 1) / DHEAP) + HEAP0;
768 811
769 if (heap [p]->at <= w->at) 812 if (p == k || ANHE_at (heap [p]) <= ANHE_at (he))
770 break; 813 break;
771 814
772 heap [k] = heap [p]; 815 heap [k] = heap [p];
773 ((W)heap [k])->active = k + 1; 816 ev_active (ANHE_w (heap [k])) = k;
774 k = p; 817 k = p;
775 } 818 }
776 819
820 ev_active (ANHE_w (he)) = k;
777 heap [k] = w; 821 heap [k] = he;
778 ((W)heap [k])->active = k + 1;
779} 822}
780 823
781/* away from the root */ 824/* away from the root */
782void inline_speed 825void inline_speed
783downheap (WT *heap, int N, int k) 826downheap (ANHE *heap, int N, int k)
784{ 827{
785 WT w = heap [k]; 828 ANHE he = heap [k];
829 ANHE *E = heap + N + HEAP0;
786 830
787 for (;;) 831 for (;;)
788 { 832 {
789 int c = (k << 1) + 1; 833 ev_tstamp minat;
834 ANHE *minpos;
835 ANHE *pos = heap + DHEAP * (k - HEAP0) + HEAP0;
790 836
791 if (c >= N) 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
792 break; 853 break;
793 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];
900
901 for (;;)
902 {
903 int c = k << 1;
904
905 if (c > N)
906 break;
907
794 c += c + 1 < N && heap [c]->at > heap [c + 1]->at 908 c += c + 1 < N && ANHE_at (heap [c]) > ANHE_at (heap [c + 1])
795 ? 1 : 0; 909 ? 1 : 0;
796 910
797 if (w->at <= heap [c]->at) 911 if (w->at <= ANHE_at (heap [c]))
798 break; 912 break;
799 913
800 heap [k] = heap [c]; 914 heap [k] = heap [c];
801 ((W)heap [k])->active = k + 1; 915 ev_active (ANHE_w (heap [k])) = k;
802 916
803 k = c; 917 k = c;
804 } 918 }
805 919
806 heap [k] = w; 920 heap [k] = he;
807 ((W)heap [k])->active = k + 1; 921 ev_active (ANHE_w (he)) = k;
808} 922}
923#endif
809 924
810void inline_size 925void inline_size
811adjustheap (WT *heap, int N, int k) 926adjustheap (ANHE *heap, int N, int k)
812{ 927{
813 upheap (heap, k); 928 upheap (heap, k);
814 downheap (heap, N, k); 929 downheap (heap, N, k);
815} 930}
816 931
908pipecb (EV_P_ ev_io *iow, int revents) 1023pipecb (EV_P_ ev_io *iow, int revents)
909{ 1024{
910#if EV_USE_EVENTFD 1025#if EV_USE_EVENTFD
911 if (evfd >= 0) 1026 if (evfd >= 0)
912 { 1027 {
913 uint64_t counter = 1; 1028 uint64_t counter;
914 read (evfd, &counter, sizeof (uint64_t)); 1029 read (evfd, &counter, sizeof (uint64_t));
915 } 1030 }
916 else 1031 else
917#endif 1032#endif
918 { 1033 {
1364void 1479void
1365ev_loop_fork (EV_P) 1480ev_loop_fork (EV_P)
1366{ 1481{
1367 postfork = 1; /* must be in line with ev_default_fork */ 1482 postfork = 1; /* must be in line with ev_default_fork */
1368} 1483}
1369
1370#endif 1484#endif
1371 1485
1372#if EV_MULTIPLICITY 1486#if EV_MULTIPLICITY
1373struct ev_loop * 1487struct ev_loop *
1374ev_default_loop_init (unsigned int flags) 1488ev_default_loop_init (unsigned int flags)
1455 EV_CB_INVOKE (p->w, p->events); 1569 EV_CB_INVOKE (p->w, p->events);
1456 } 1570 }
1457 } 1571 }
1458} 1572}
1459 1573
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 1574#if EV_IDLE_ENABLE
1541void inline_size 1575void inline_size
1542idle_reify (EV_P) 1576idle_reify (EV_P)
1543{ 1577{
1544 if (expect_false (idleall)) 1578 if (expect_false (idleall))
1555 queue_events (EV_A_ (W *)idles [pri], idlecnt [pri], EV_IDLE); 1589 queue_events (EV_A_ (W *)idles [pri], idlecnt [pri], EV_IDLE);
1556 break; 1590 break;
1557 } 1591 }
1558 } 1592 }
1559 } 1593 }
1594}
1595#endif
1596
1597void inline_size
1598timers_reify (EV_P)
1599{
1600 while (timercnt && ANHE_at (timers [HEAP0]) <= mn_now)
1601 {
1602 ev_timer *w = (ev_timer *)ANHE_w (timers [HEAP0]);
1603
1604 /*assert (("inactive timer on timer heap detected", ev_is_active (w)));*/
1605
1606 /* first reschedule or stop timer */
1607 if (w->repeat)
1608 {
1609 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
1610
1611 ev_at (w) += w->repeat;
1612 if (ev_at (w) < mn_now)
1613 ev_at (w) = mn_now;
1614
1615 ANHE_at_set (timers [HEAP0]);
1616 downheap (timers, timercnt, HEAP0);
1617 }
1618 else
1619 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
1620
1621 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT);
1622 }
1623}
1624
1625#if EV_PERIODIC_ENABLE
1626void inline_size
1627periodics_reify (EV_P)
1628{
1629 while (periodiccnt && ANHE_at (periodics [HEAP0]) <= ev_rt_now)
1630 {
1631 ev_periodic *w = (ev_periodic *)ANHE_w (periodics [HEAP0]);
1632
1633 /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/
1634
1635 /* first reschedule or stop timer */
1636 if (w->reschedule_cb)
1637 {
1638 ev_at (w) = w->reschedule_cb (w, ev_rt_now + TIME_EPSILON);
1639 assert (("ev_periodic reschedule callback returned time in the past", ev_at (w) > ev_rt_now));
1640 ANHE_at_set (periodics [HEAP0]);
1641 downheap (periodics, periodiccnt, HEAP0);
1642 }
1643 else if (w->interval)
1644 {
1645 ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval;
1646 if (ev_at (w) - ev_rt_now <= TIME_EPSILON) ev_at (w) += w->interval;
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]);
1649 downheap (periodics, periodiccnt, HEAP0);
1650 }
1651 else
1652 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
1653
1654 ev_feed_event (EV_A_ (W)w, EV_PERIODIC);
1655 }
1656}
1657
1658static void noinline
1659periodics_reschedule (EV_P)
1660{
1661 int i;
1662
1663 /* adjust periodics after time jump */
1664 for (i = HEAP0; i < periodiccnt + HEAP0; ++i)
1665 {
1666 ev_periodic *w = (ev_periodic *)ANHE_w (periodics [i]);
1667
1668 if (w->reschedule_cb)
1669 ev_at (w) = w->reschedule_cb (w, ev_rt_now);
1670 else if (w->interval)
1671 ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval;
1672
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 */
1677 for (i = periodiccnt >> 1; --i; )
1678 downheap (periodics, periodiccnt, i + HEAP0);
1560} 1679}
1561#endif 1680#endif
1562 1681
1563void inline_speed 1682void inline_speed
1564time_update (EV_P_ ev_tstamp max_block) 1683time_update (EV_P_ ev_tstamp max_block)
1593 */ 1712 */
1594 for (i = 4; --i; ) 1713 for (i = 4; --i; )
1595 { 1714 {
1596 rtmn_diff = ev_rt_now - mn_now; 1715 rtmn_diff = ev_rt_now - mn_now;
1597 1716
1598 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) 1717 if (expect_true (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP))
1599 return; /* all is well */ 1718 return; /* all is well */
1600 1719
1601 ev_rt_now = ev_time (); 1720 ev_rt_now = ev_time ();
1602 mn_now = get_clock (); 1721 mn_now = get_clock ();
1603 now_floor = mn_now; 1722 now_floor = mn_now;
1619#if EV_PERIODIC_ENABLE 1738#if EV_PERIODIC_ENABLE
1620 periodics_reschedule (EV_A); 1739 periodics_reschedule (EV_A);
1621#endif 1740#endif
1622 /* 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 */
1623 for (i = 0; i < timercnt; ++i) 1742 for (i = 0; i < timercnt; ++i)
1743 {
1744 ANHE *he = timers + i + HEAP0;
1624 ((WT)timers [i])->at += ev_rt_now - mn_now; 1745 ANHE_w (*he)->at += ev_rt_now - mn_now;
1746 ANHE_at_set (*he);
1747 }
1625 } 1748 }
1626 1749
1627 mn_now = ev_rt_now; 1750 mn_now = ev_rt_now;
1628 } 1751 }
1629} 1752}
1699 1822
1700 waittime = MAX_BLOCKTIME; 1823 waittime = MAX_BLOCKTIME;
1701 1824
1702 if (timercnt) 1825 if (timercnt)
1703 { 1826 {
1704 ev_tstamp to = ((WT)timers [0])->at - mn_now + backend_fudge; 1827 ev_tstamp to = ANHE_at (timers [HEAP0]) - mn_now + backend_fudge;
1705 if (waittime > to) waittime = to; 1828 if (waittime > to) waittime = to;
1706 } 1829 }
1707 1830
1708#if EV_PERIODIC_ENABLE 1831#if EV_PERIODIC_ENABLE
1709 if (periodiccnt) 1832 if (periodiccnt)
1710 { 1833 {
1711 ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + backend_fudge; 1834 ev_tstamp to = ANHE_at (periodics [HEAP0]) - ev_rt_now + backend_fudge;
1712 if (waittime > to) waittime = to; 1835 if (waittime > to) waittime = to;
1713 } 1836 }
1714#endif 1837#endif
1715 1838
1716 if (expect_false (waittime < timeout_blocktime)) 1839 if (expect_false (waittime < timeout_blocktime))
1868{ 1991{
1869 clear_pending (EV_A_ (W)w); 1992 clear_pending (EV_A_ (W)w);
1870 if (expect_false (!ev_is_active (w))) 1993 if (expect_false (!ev_is_active (w)))
1871 return; 1994 return;
1872 1995
1873 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));
1874 1997
1875 wlist_del (&anfds[w->fd].head, (WL)w); 1998 wlist_del (&anfds[w->fd].head, (WL)w);
1876 ev_stop (EV_A_ (W)w); 1999 ev_stop (EV_A_ (W)w);
1877 2000
1878 fd_change (EV_A_ w->fd, 1); 2001 fd_change (EV_A_ w->fd, 1);
1882ev_timer_start (EV_P_ ev_timer *w) 2005ev_timer_start (EV_P_ ev_timer *w)
1883{ 2006{
1884 if (expect_false (ev_is_active (w))) 2007 if (expect_false (ev_is_active (w)))
1885 return; 2008 return;
1886 2009
1887 ((WT)w)->at += mn_now; 2010 ev_at (w) += mn_now;
1888 2011
1889 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.));
1890 2013
1891 ev_start (EV_A_ (W)w, ++timercnt); 2014 ev_start (EV_A_ (W)w, ++timercnt + HEAP0 - 1);
1892 array_needsize (WT, timers, timermax, timercnt, EMPTY2); 2015 array_needsize (ANHE, timers, timermax, ev_active (w) + 1, EMPTY2);
1893 timers [timercnt - 1] = (WT)w; 2016 ANHE_w (timers [ev_active (w)]) = (WT)w;
1894 upheap (timers, timercnt - 1); 2017 ANHE_at_set (timers [ev_active (w)]);
2018 upheap (timers, ev_active (w));
1895 2019
1896 /*assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));*/ 2020 /*assert (("internal timer heap corruption", timers [ev_active (w)] == (WT)w));*/
1897} 2021}
1898 2022
1899void noinline 2023void noinline
1900ev_timer_stop (EV_P_ ev_timer *w) 2024ev_timer_stop (EV_P_ ev_timer *w)
1901{ 2025{
1902 clear_pending (EV_A_ (W)w); 2026 clear_pending (EV_A_ (W)w);
1903 if (expect_false (!ev_is_active (w))) 2027 if (expect_false (!ev_is_active (w)))
1904 return; 2028 return;
1905 2029
1906 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == (WT)w));
1907
1908 { 2030 {
1909 int active = ((W)w)->active; 2031 int active = ev_active (w);
1910 2032
2033 assert (("internal timer heap corruption", ANHE_w (timers [active]) == (WT)w));
2034
1911 if (expect_true (--active < --timercnt)) 2035 if (expect_true (active < timercnt + HEAP0 - 1))
1912 { 2036 {
1913 timers [active] = timers [timercnt]; 2037 timers [active] = timers [timercnt + HEAP0 - 1];
1914 adjustheap (timers, timercnt, active); 2038 adjustheap (timers, timercnt, active);
1915 } 2039 }
2040
2041 --timercnt;
1916 } 2042 }
1917 2043
1918 ((WT)w)->at -= mn_now; 2044 ev_at (w) -= mn_now;
1919 2045
1920 ev_stop (EV_A_ (W)w); 2046 ev_stop (EV_A_ (W)w);
1921} 2047}
1922 2048
1923void noinline 2049void noinline
1925{ 2051{
1926 if (ev_is_active (w)) 2052 if (ev_is_active (w))
1927 { 2053 {
1928 if (w->repeat) 2054 if (w->repeat)
1929 { 2055 {
1930 ((WT)w)->at = mn_now + w->repeat; 2056 ev_at (w) = mn_now + w->repeat;
2057 ANHE_at_set (timers [ev_active (w)]);
1931 adjustheap (timers, timercnt, ((W)w)->active - 1); 2058 adjustheap (timers, timercnt, ev_active (w));
1932 } 2059 }
1933 else 2060 else
1934 ev_timer_stop (EV_A_ w); 2061 ev_timer_stop (EV_A_ w);
1935 } 2062 }
1936 else if (w->repeat) 2063 else if (w->repeat)
1937 { 2064 {
1938 w->at = w->repeat; 2065 ev_at (w) = w->repeat;
1939 ev_timer_start (EV_A_ w); 2066 ev_timer_start (EV_A_ w);
1940 } 2067 }
1941} 2068}
1942 2069
1943#if EV_PERIODIC_ENABLE 2070#if EV_PERIODIC_ENABLE
1946{ 2073{
1947 if (expect_false (ev_is_active (w))) 2074 if (expect_false (ev_is_active (w)))
1948 return; 2075 return;
1949 2076
1950 if (w->reschedule_cb) 2077 if (w->reschedule_cb)
1951 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); 2078 ev_at (w) = w->reschedule_cb (w, ev_rt_now);
1952 else if (w->interval) 2079 else if (w->interval)
1953 { 2080 {
1954 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); 2081 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 */ 2082 /* 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; 2083 ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval;
1957 } 2084 }
1958 else 2085 else
1959 ((WT)w)->at = w->offset; 2086 ev_at (w) = w->offset;
1960 2087
1961 ev_start (EV_A_ (W)w, ++periodiccnt); 2088 ev_start (EV_A_ (W)w, ++periodiccnt + HEAP0 - 1);
1962 array_needsize (WT, periodics, periodicmax, periodiccnt, EMPTY2); 2089 array_needsize (ANHE, periodics, periodicmax, ev_active (w) + 1, EMPTY2);
1963 periodics [periodiccnt - 1] = (WT)w; 2090 ANHE_w (periodics [ev_active (w)]) = (WT)w;
1964 upheap (periodics, periodiccnt - 1); 2091 upheap (periodics, ev_active (w));
1965 2092
1966 /*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));*/
1967} 2094}
1968 2095
1969void noinline 2096void noinline
1970ev_periodic_stop (EV_P_ ev_periodic *w) 2097ev_periodic_stop (EV_P_ ev_periodic *w)
1971{ 2098{
1972 clear_pending (EV_A_ (W)w); 2099 clear_pending (EV_A_ (W)w);
1973 if (expect_false (!ev_is_active (w))) 2100 if (expect_false (!ev_is_active (w)))
1974 return; 2101 return;
1975 2102
1976 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == (WT)w));
1977
1978 { 2103 {
1979 int active = ((W)w)->active; 2104 int active = ev_active (w);
1980 2105
2106 assert (("internal periodic heap corruption", ANHE_w (periodics [active]) == (WT)w));
2107
1981 if (expect_true (--active < --periodiccnt)) 2108 if (expect_true (active < periodiccnt + HEAP0 - 1))
1982 { 2109 {
1983 periodics [active] = periodics [periodiccnt]; 2110 periodics [active] = periodics [periodiccnt + HEAP0 - 1];
1984 adjustheap (periodics, periodiccnt, active); 2111 adjustheap (periodics, periodiccnt, active);
1985 } 2112 }
2113
2114 --periodiccnt;
1986 } 2115 }
1987 2116
1988 ev_stop (EV_A_ (W)w); 2117 ev_stop (EV_A_ (W)w);
1989} 2118}
1990 2119
2106 if (w->wd < 0) 2235 if (w->wd < 0)
2107 { 2236 {
2108 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 */
2109 2238
2110 /* 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 */
2111 if ((errno == ENOENT || errno == EACCES) && strlen (w->path) < 4096) 2242 if ((errno == ENOENT || errno == EACCES) && strlen (w->path) < 4096)
2112 { 2243 {
2113 char path [4096]; 2244 char path [4096];
2114 strcpy (path, w->path); 2245 strcpy (path, w->path);
2115 2246
2360 clear_pending (EV_A_ (W)w); 2491 clear_pending (EV_A_ (W)w);
2361 if (expect_false (!ev_is_active (w))) 2492 if (expect_false (!ev_is_active (w)))
2362 return; 2493 return;
2363 2494
2364 { 2495 {
2365 int active = ((W)w)->active; 2496 int active = ev_active (w);
2366 2497
2367 idles [ABSPRI (w)][active - 1] = idles [ABSPRI (w)][--idlecnt [ABSPRI (w)]]; 2498 idles [ABSPRI (w)][active - 1] = idles [ABSPRI (w)][--idlecnt [ABSPRI (w)]];
2368 ((W)idles [ABSPRI (w)][active - 1])->active = active; 2499 ev_active (idles [ABSPRI (w)][active - 1]) = active;
2369 2500
2370 ev_stop (EV_A_ (W)w); 2501 ev_stop (EV_A_ (W)w);
2371 --idleall; 2502 --idleall;
2372 } 2503 }
2373} 2504}
2390 clear_pending (EV_A_ (W)w); 2521 clear_pending (EV_A_ (W)w);
2391 if (expect_false (!ev_is_active (w))) 2522 if (expect_false (!ev_is_active (w)))
2392 return; 2523 return;
2393 2524
2394 { 2525 {
2395 int active = ((W)w)->active; 2526 int active = ev_active (w);
2527
2396 prepares [active - 1] = prepares [--preparecnt]; 2528 prepares [active - 1] = prepares [--preparecnt];
2397 ((W)prepares [active - 1])->active = active; 2529 ev_active (prepares [active - 1]) = active;
2398 } 2530 }
2399 2531
2400 ev_stop (EV_A_ (W)w); 2532 ev_stop (EV_A_ (W)w);
2401} 2533}
2402 2534
2417 clear_pending (EV_A_ (W)w); 2549 clear_pending (EV_A_ (W)w);
2418 if (expect_false (!ev_is_active (w))) 2550 if (expect_false (!ev_is_active (w)))
2419 return; 2551 return;
2420 2552
2421 { 2553 {
2422 int active = ((W)w)->active; 2554 int active = ev_active (w);
2555
2423 checks [active - 1] = checks [--checkcnt]; 2556 checks [active - 1] = checks [--checkcnt];
2424 ((W)checks [active - 1])->active = active; 2557 ev_active (checks [active - 1]) = active;
2425 } 2558 }
2426 2559
2427 ev_stop (EV_A_ (W)w); 2560 ev_stop (EV_A_ (W)w);
2428} 2561}
2429 2562
2525 clear_pending (EV_A_ (W)w); 2658 clear_pending (EV_A_ (W)w);
2526 if (expect_false (!ev_is_active (w))) 2659 if (expect_false (!ev_is_active (w)))
2527 return; 2660 return;
2528 2661
2529 { 2662 {
2530 int active = ((W)w)->active; 2663 int active = ev_active (w);
2664
2531 forks [active - 1] = forks [--forkcnt]; 2665 forks [active - 1] = forks [--forkcnt];
2532 ((W)forks [active - 1])->active = active; 2666 ev_active (forks [active - 1]) = active;
2533 } 2667 }
2534 2668
2535 ev_stop (EV_A_ (W)w); 2669 ev_stop (EV_A_ (W)w);
2536} 2670}
2537#endif 2671#endif
2556 clear_pending (EV_A_ (W)w); 2690 clear_pending (EV_A_ (W)w);
2557 if (expect_false (!ev_is_active (w))) 2691 if (expect_false (!ev_is_active (w)))
2558 return; 2692 return;
2559 2693
2560 { 2694 {
2561 int active = ((W)w)->active; 2695 int active = ev_active (w);
2696
2562 asyncs [active - 1] = asyncs [--asynccnt]; 2697 asyncs [active - 1] = asyncs [--asynccnt];
2563 ((W)asyncs [active - 1])->active = active; 2698 ev_active (asyncs [active - 1]) = active;
2564 } 2699 }
2565 2700
2566 ev_stop (EV_A_ (W)w); 2701 ev_stop (EV_A_ (W)w);
2567} 2702}
2568 2703

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines