… | |
… | |
325 | |
325 | |
326 | typedef ev_watcher *W; |
326 | typedef ev_watcher *W; |
327 | typedef ev_watcher_list *WL; |
327 | typedef ev_watcher_list *WL; |
328 | typedef ev_watcher_time *WT; |
328 | typedef 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 */ |
… | |
… | |
519 | } |
520 | } |
520 | } |
521 | } |
521 | |
522 | |
522 | /*****************************************************************************/ |
523 | /*****************************************************************************/ |
523 | |
524 | |
|
|
525 | #define MALLOC_ROUND 4096 /* prefer to allocate in chunks of this size, must be 2**n and >> 4 longs */ |
|
|
526 | |
524 | int inline_size |
527 | int inline_size |
525 | array_nextsize (int elem, int cur, int cnt) |
528 | array_nextsize (int elem, int cur, int cnt) |
526 | { |
529 | { |
527 | int ncur = cur + 1; |
530 | int ncur = cur + 1; |
528 | |
531 | |
529 | do |
532 | do |
530 | ncur <<= 1; |
533 | ncur <<= 1; |
531 | while (cnt > ncur); |
534 | while (cnt > ncur); |
532 | |
535 | |
533 | /* 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 */ |
534 | if (elem * ncur > 4096) |
537 | if (elem * ncur > MALLOC_ROUND - sizeof (void *) * 4) |
535 | { |
538 | { |
536 | ncur *= elem; |
539 | ncur *= elem; |
537 | ncur = (ncur + elem + 4095 + sizeof (void *) * 4) & ~4095; |
540 | ncur = (ncur + elem + (MALLOC_ROUND - 1) + sizeof (void *) * 4) & ~(MALLOC_ROUND - 1); |
538 | ncur = ncur - sizeof (void *) * 4; |
541 | ncur = ncur - sizeof (void *) * 4; |
539 | ncur /= elem; |
542 | ncur /= elem; |
540 | } |
543 | } |
541 | |
544 | |
542 | return ncur; |
545 | return ncur; |
… | |
… | |
756 | } |
759 | } |
757 | } |
760 | } |
758 | |
761 | |
759 | /*****************************************************************************/ |
762 | /*****************************************************************************/ |
760 | |
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 DHEAP 4 |
|
|
774 | #define HEAP0 (DHEAP - 1) /* index of first element in heap */ |
|
|
775 | |
761 | /* towards the root */ |
776 | /* towards the root */ |
762 | void inline_speed |
777 | void inline_speed |
763 | upheap (WT *heap, int k) |
778 | upheap (WT *heap, int k) |
764 | { |
779 | { |
765 | WT w = heap [k]; |
780 | WT w = heap [k]; |
766 | |
781 | |
767 | for (;;) |
782 | for (;;) |
768 | { |
783 | { |
|
|
784 | int p = ((k - HEAP0 - 1) / DHEAP) + HEAP0; |
|
|
785 | |
|
|
786 | if (p == k || heap [p]->at <= w->at) |
|
|
787 | break; |
|
|
788 | |
|
|
789 | heap [k] = heap [p]; |
|
|
790 | ev_active (heap [k]) = k; |
|
|
791 | k = p; |
|
|
792 | } |
|
|
793 | |
|
|
794 | heap [k] = w; |
|
|
795 | ev_active (heap [k]) = k; |
|
|
796 | } |
|
|
797 | |
|
|
798 | /* away from the root */ |
|
|
799 | void inline_speed |
|
|
800 | downheap (WT *heap, int N, int k) |
|
|
801 | { |
|
|
802 | WT w = heap [k]; |
|
|
803 | WT *E = heap + N + HEAP0; |
|
|
804 | |
|
|
805 | for (;;) |
|
|
806 | { |
|
|
807 | ev_tstamp minat; |
|
|
808 | WT *minpos; |
|
|
809 | WT *pos = heap + DHEAP * (k - HEAP0) + HEAP0; |
|
|
810 | |
|
|
811 | // find minimum child |
|
|
812 | if (expect_true (pos + DHEAP - 1 < E)) |
|
|
813 | { |
|
|
814 | /* fast path */ |
|
|
815 | (minpos = pos + 0), (minat = (*minpos)->at); |
|
|
816 | if (pos [1]->at < minat) (minpos = pos + 1), (minat = (*minpos)->at); |
|
|
817 | if (pos [2]->at < minat) (minpos = pos + 2), (minat = (*minpos)->at); |
|
|
818 | if (pos [3]->at < minat) (minpos = pos + 3), (minat = (*minpos)->at); |
|
|
819 | } |
|
|
820 | else |
|
|
821 | { |
|
|
822 | /* slow path */ |
|
|
823 | if (pos >= E) |
|
|
824 | break; |
|
|
825 | (minpos = pos + 0), (minat = (*minpos)->at); |
|
|
826 | if (pos + 1 < E && pos [1]->at < minat) (minpos = pos + 1), (minat = (*minpos)->at); |
|
|
827 | if (pos + 2 < E && pos [2]->at < minat) (minpos = pos + 2), (minat = (*minpos)->at); |
|
|
828 | if (pos + 3 < E && pos [3]->at < minat) (minpos = pos + 3), (minat = (*minpos)->at); |
|
|
829 | } |
|
|
830 | |
|
|
831 | if (w->at <= minat) |
|
|
832 | break; |
|
|
833 | |
|
|
834 | ev_active (*minpos) = k; |
|
|
835 | heap [k] = *minpos; |
|
|
836 | |
|
|
837 | k = minpos - heap; |
|
|
838 | } |
|
|
839 | |
|
|
840 | heap [k] = w; |
|
|
841 | ev_active (heap [k]) = k; |
|
|
842 | } |
|
|
843 | |
|
|
844 | #else // 4HEAP |
|
|
845 | |
|
|
846 | #define HEAP0 1 |
|
|
847 | |
|
|
848 | /* towards the root */ |
|
|
849 | void inline_speed |
|
|
850 | upheap (WT *heap, int k) |
|
|
851 | { |
|
|
852 | WT w = heap [k]; |
|
|
853 | |
|
|
854 | for (;;) |
|
|
855 | { |
769 | int p = k >> 1; |
856 | int p = k >> 1; |
770 | |
857 | |
771 | /* maybe we could use a dummy element at heap [0]? */ |
858 | /* maybe we could use a dummy element at heap [0]? */ |
772 | if (!p || heap [p]->at <= w->at) |
859 | if (!p || heap [p]->at <= w->at) |
773 | break; |
860 | break; |
774 | |
861 | |
775 | heap [k] = heap [p]; |
862 | heap [k] = heap [p]; |
776 | ((W)heap [k])->active = k; |
863 | ev_active (heap [k]) = k; |
777 | k = p; |
864 | k = p; |
778 | } |
865 | } |
779 | |
866 | |
780 | heap [k] = w; |
867 | heap [k] = w; |
781 | ((W)heap [k])->active = k; |
868 | ev_active (heap [k]) = k; |
782 | } |
869 | } |
783 | |
870 | |
784 | /* away from the root */ |
871 | /* away from the root */ |
785 | void inline_speed |
872 | void inline_speed |
786 | downheap (WT *heap, int N, int k) |
873 | downheap (WT *heap, int N, int k) |
… | |
… | |
792 | int c = k << 1; |
879 | int c = k << 1; |
793 | |
880 | |
794 | if (c > N) |
881 | if (c > N) |
795 | break; |
882 | break; |
796 | |
883 | |
797 | c += c < N && heap [c]->at > heap [c + 1]->at |
884 | c += c + 1 < N && heap [c]->at > heap [c + 1]->at |
798 | ? 1 : 0; |
885 | ? 1 : 0; |
799 | |
886 | |
800 | if (w->at <= heap [c]->at) |
887 | if (w->at <= heap [c]->at) |
801 | break; |
888 | break; |
802 | |
889 | |
803 | heap [k] = heap [c]; |
890 | heap [k] = heap [c]; |
804 | ((W)heap [k])->active = k; |
891 | ((W)heap [k])->active = k; |
805 | |
892 | |
806 | k = c; |
893 | k = c; |
807 | } |
894 | } |
808 | |
895 | |
809 | heap [k] = w; |
896 | heap [k] = w; |
810 | ((W)heap [k])->active = k; |
897 | ev_active (heap [k]) = k; |
811 | } |
898 | } |
|
|
899 | #endif |
812 | |
900 | |
813 | void inline_size |
901 | void inline_size |
814 | adjustheap (WT *heap, int N, int k) |
902 | adjustheap (WT *heap, int N, int k) |
815 | { |
903 | { |
816 | upheap (heap, k); |
904 | upheap (heap, k); |
… | |
… | |
911 | pipecb (EV_P_ ev_io *iow, int revents) |
999 | pipecb (EV_P_ ev_io *iow, int revents) |
912 | { |
1000 | { |
913 | #if EV_USE_EVENTFD |
1001 | #if EV_USE_EVENTFD |
914 | if (evfd >= 0) |
1002 | if (evfd >= 0) |
915 | { |
1003 | { |
916 | uint64_t counter = 1; |
1004 | uint64_t counter; |
917 | read (evfd, &counter, sizeof (uint64_t)); |
1005 | read (evfd, &counter, sizeof (uint64_t)); |
918 | } |
1006 | } |
919 | else |
1007 | else |
920 | #endif |
1008 | #endif |
921 | { |
1009 | { |
… | |
… | |
1367 | void |
1455 | void |
1368 | ev_loop_fork (EV_P) |
1456 | ev_loop_fork (EV_P) |
1369 | { |
1457 | { |
1370 | postfork = 1; /* must be in line with ev_default_fork */ |
1458 | postfork = 1; /* must be in line with ev_default_fork */ |
1371 | } |
1459 | } |
1372 | |
|
|
1373 | #endif |
1460 | #endif |
1374 | |
1461 | |
1375 | #if EV_MULTIPLICITY |
1462 | #if EV_MULTIPLICITY |
1376 | struct ev_loop * |
1463 | struct ev_loop * |
1377 | ev_default_loop_init (unsigned int flags) |
1464 | ev_default_loop_init (unsigned int flags) |
… | |
… | |
1458 | EV_CB_INVOKE (p->w, p->events); |
1545 | EV_CB_INVOKE (p->w, p->events); |
1459 | } |
1546 | } |
1460 | } |
1547 | } |
1461 | } |
1548 | } |
1462 | |
1549 | |
|
|
1550 | #if EV_IDLE_ENABLE |
|
|
1551 | void inline_size |
|
|
1552 | idle_reify (EV_P) |
|
|
1553 | { |
|
|
1554 | if (expect_false (idleall)) |
|
|
1555 | { |
|
|
1556 | int pri; |
|
|
1557 | |
|
|
1558 | for (pri = NUMPRI; pri--; ) |
|
|
1559 | { |
|
|
1560 | if (pendingcnt [pri]) |
|
|
1561 | break; |
|
|
1562 | |
|
|
1563 | if (idlecnt [pri]) |
|
|
1564 | { |
|
|
1565 | queue_events (EV_A_ (W *)idles [pri], idlecnt [pri], EV_IDLE); |
|
|
1566 | break; |
|
|
1567 | } |
|
|
1568 | } |
|
|
1569 | } |
|
|
1570 | } |
|
|
1571 | #endif |
|
|
1572 | |
1463 | void inline_size |
1573 | void inline_size |
1464 | timers_reify (EV_P) |
1574 | timers_reify (EV_P) |
1465 | { |
1575 | { |
1466 | while (timercnt && ev_at (timers [1]) <= mn_now) |
1576 | while (timercnt && ev_at (timers [HEAP0]) <= mn_now) |
1467 | { |
1577 | { |
1468 | ev_timer *w = (ev_timer *)timers [1]; |
1578 | ev_timer *w = (ev_timer *)timers [HEAP0]; |
1469 | |
1579 | |
1470 | /*assert (("inactive timer on timer heap detected", ev_is_active (w)));*/ |
1580 | /*assert (("inactive timer on timer heap detected", ev_is_active (w)));*/ |
1471 | |
1581 | |
1472 | /* first reschedule or stop timer */ |
1582 | /* first reschedule or stop timer */ |
1473 | if (w->repeat) |
1583 | if (w->repeat) |
… | |
… | |
1476 | |
1586 | |
1477 | ev_at (w) += w->repeat; |
1587 | ev_at (w) += w->repeat; |
1478 | if (ev_at (w) < mn_now) |
1588 | if (ev_at (w) < mn_now) |
1479 | ev_at (w) = mn_now; |
1589 | ev_at (w) = mn_now; |
1480 | |
1590 | |
1481 | downheap (timers, timercnt, 1); |
1591 | downheap (timers, timercnt, HEAP0); |
1482 | } |
1592 | } |
1483 | else |
1593 | else |
1484 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
1594 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
1485 | |
1595 | |
1486 | ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); |
1596 | ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); |
… | |
… | |
1489 | |
1599 | |
1490 | #if EV_PERIODIC_ENABLE |
1600 | #if EV_PERIODIC_ENABLE |
1491 | void inline_size |
1601 | void inline_size |
1492 | periodics_reify (EV_P) |
1602 | periodics_reify (EV_P) |
1493 | { |
1603 | { |
1494 | while (periodiccnt && ev_at (periodics [1]) <= ev_rt_now) |
1604 | while (periodiccnt && ev_at (periodics [HEAP0]) <= ev_rt_now) |
1495 | { |
1605 | { |
1496 | ev_periodic *w = (ev_periodic *)periodics [1]; |
1606 | ev_periodic *w = (ev_periodic *)periodics [HEAP0]; |
1497 | |
1607 | |
1498 | /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/ |
1608 | /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/ |
1499 | |
1609 | |
1500 | /* first reschedule or stop timer */ |
1610 | /* first reschedule or stop timer */ |
1501 | if (w->reschedule_cb) |
1611 | if (w->reschedule_cb) |
… | |
… | |
1507 | else if (w->interval) |
1617 | else if (w->interval) |
1508 | { |
1618 | { |
1509 | ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; |
1619 | 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; |
1620 | 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)); |
1621 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ev_at (w) > ev_rt_now)); |
1512 | downheap (periodics, periodiccnt, 1); |
1622 | downheap (periodics, periodiccnt, HEAP0); |
1513 | } |
1623 | } |
1514 | else |
1624 | else |
1515 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
1625 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
1516 | |
1626 | |
1517 | ev_feed_event (EV_A_ (W)w, EV_PERIODIC); |
1627 | ev_feed_event (EV_A_ (W)w, EV_PERIODIC); |
… | |
… | |
1522 | periodics_reschedule (EV_P) |
1632 | periodics_reschedule (EV_P) |
1523 | { |
1633 | { |
1524 | int i; |
1634 | int i; |
1525 | |
1635 | |
1526 | /* adjust periodics after time jump */ |
1636 | /* adjust periodics after time jump */ |
1527 | for (i = 0; i < periodiccnt; ++i) |
1637 | for (i = 1; i <= periodiccnt; ++i) |
1528 | { |
1638 | { |
1529 | ev_periodic *w = (ev_periodic *)periodics [i]; |
1639 | ev_periodic *w = (ev_periodic *)periodics [i]; |
1530 | |
1640 | |
1531 | if (w->reschedule_cb) |
1641 | if (w->reschedule_cb) |
1532 | ev_at (w) = w->reschedule_cb (w, ev_rt_now); |
1642 | ev_at (w) = w->reschedule_cb (w, ev_rt_now); |
1533 | else if (w->interval) |
1643 | else if (w->interval) |
1534 | ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; |
1644 | ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; |
1535 | } |
1645 | } |
1536 | |
1646 | |
1537 | /* now rebuild the heap */ |
1647 | /* now rebuild the heap */ |
1538 | for (i = periodiccnt >> 1; i--; ) |
1648 | for (i = periodiccnt >> 1; --i; ) |
1539 | downheap (periodics, periodiccnt, i); |
1649 | downheap (periodics, periodiccnt, i + HEAP0); |
1540 | } |
|
|
1541 | #endif |
|
|
1542 | |
|
|
1543 | #if EV_IDLE_ENABLE |
|
|
1544 | void inline_size |
|
|
1545 | idle_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 | } |
1650 | } |
1564 | #endif |
1651 | #endif |
1565 | |
1652 | |
1566 | void inline_speed |
1653 | void inline_speed |
1567 | time_update (EV_P_ ev_tstamp max_block) |
1654 | time_update (EV_P_ ev_tstamp max_block) |
… | |
… | |
1596 | */ |
1683 | */ |
1597 | for (i = 4; --i; ) |
1684 | for (i = 4; --i; ) |
1598 | { |
1685 | { |
1599 | rtmn_diff = ev_rt_now - mn_now; |
1686 | rtmn_diff = ev_rt_now - mn_now; |
1600 | |
1687 | |
1601 | if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) |
1688 | if (expect_true (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP)) |
1602 | return; /* all is well */ |
1689 | return; /* all is well */ |
1603 | |
1690 | |
1604 | ev_rt_now = ev_time (); |
1691 | ev_rt_now = ev_time (); |
1605 | mn_now = get_clock (); |
1692 | mn_now = get_clock (); |
1606 | now_floor = mn_now; |
1693 | now_floor = mn_now; |
… | |
… | |
1702 | |
1789 | |
1703 | waittime = MAX_BLOCKTIME; |
1790 | waittime = MAX_BLOCKTIME; |
1704 | |
1791 | |
1705 | if (timercnt) |
1792 | if (timercnt) |
1706 | { |
1793 | { |
1707 | ev_tstamp to = ev_at (timers [1]) - mn_now + backend_fudge; |
1794 | ev_tstamp to = ev_at (timers [HEAP0]) - mn_now + backend_fudge; |
1708 | if (waittime > to) waittime = to; |
1795 | if (waittime > to) waittime = to; |
1709 | } |
1796 | } |
1710 | |
1797 | |
1711 | #if EV_PERIODIC_ENABLE |
1798 | #if EV_PERIODIC_ENABLE |
1712 | if (periodiccnt) |
1799 | if (periodiccnt) |
1713 | { |
1800 | { |
1714 | ev_tstamp to = ev_at (periodics [1]) - ev_rt_now + backend_fudge; |
1801 | ev_tstamp to = ev_at (periodics [HEAP0]) - ev_rt_now + backend_fudge; |
1715 | if (waittime > to) waittime = to; |
1802 | if (waittime > to) waittime = to; |
1716 | } |
1803 | } |
1717 | #endif |
1804 | #endif |
1718 | |
1805 | |
1719 | if (expect_false (waittime < timeout_blocktime)) |
1806 | if (expect_false (waittime < timeout_blocktime)) |
… | |
… | |
1889 | |
1976 | |
1890 | ev_at (w) += mn_now; |
1977 | ev_at (w) += mn_now; |
1891 | |
1978 | |
1892 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
1979 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
1893 | |
1980 | |
1894 | ev_start (EV_A_ (W)w, ++timercnt); |
1981 | ev_start (EV_A_ (W)w, ++timercnt + HEAP0 - 1); |
1895 | array_needsize (WT, timers, timermax, timercnt + 1, EMPTY2); |
1982 | array_needsize (WT, timers, timermax, timercnt + HEAP0, EMPTY2); |
1896 | timers [timercnt] = (WT)w; |
1983 | timers [ev_active (w)] = (WT)w; |
1897 | upheap (timers, timercnt); |
1984 | upheap (timers, ev_active (w)); |
1898 | |
1985 | |
1899 | /*assert (("internal timer heap corruption", timers [((W)w)->active] == w));*/ |
1986 | /*assert (("internal timer heap corruption", timers [ev_active (w)] == w));*/ |
1900 | } |
1987 | } |
1901 | |
1988 | |
1902 | void noinline |
1989 | void noinline |
1903 | ev_timer_stop (EV_P_ ev_timer *w) |
1990 | ev_timer_stop (EV_P_ ev_timer *w) |
1904 | { |
1991 | { |
1905 | clear_pending (EV_A_ (W)w); |
1992 | clear_pending (EV_A_ (W)w); |
1906 | if (expect_false (!ev_is_active (w))) |
1993 | if (expect_false (!ev_is_active (w))) |
1907 | return; |
1994 | return; |
1908 | |
1995 | |
1909 | assert (("internal timer heap corruption", timers [((W)w)->active] == (WT)w)); |
|
|
1910 | |
|
|
1911 | { |
1996 | { |
1912 | int active = ((W)w)->active; |
1997 | int active = ev_active (w); |
1913 | |
1998 | |
|
|
1999 | assert (("internal timer heap corruption", timers [active] == (WT)w)); |
|
|
2000 | |
1914 | if (expect_true (active < timercnt)) |
2001 | if (expect_true (active < timercnt + HEAP0 - 1)) |
1915 | { |
2002 | { |
1916 | timers [active] = timers [timercnt]; |
2003 | timers [active] = timers [timercnt + HEAP0 - 1]; |
1917 | adjustheap (timers, timercnt, active); |
2004 | adjustheap (timers, timercnt, active); |
1918 | } |
2005 | } |
1919 | |
2006 | |
1920 | --timercnt; |
2007 | --timercnt; |
1921 | } |
2008 | } |
… | |
… | |
1931 | if (ev_is_active (w)) |
2018 | if (ev_is_active (w)) |
1932 | { |
2019 | { |
1933 | if (w->repeat) |
2020 | if (w->repeat) |
1934 | { |
2021 | { |
1935 | ev_at (w) = mn_now + w->repeat; |
2022 | ev_at (w) = mn_now + w->repeat; |
1936 | adjustheap (timers, timercnt, ((W)w)->active); |
2023 | adjustheap (timers, timercnt, ev_active (w)); |
1937 | } |
2024 | } |
1938 | else |
2025 | else |
1939 | ev_timer_stop (EV_A_ w); |
2026 | ev_timer_stop (EV_A_ w); |
1940 | } |
2027 | } |
1941 | else if (w->repeat) |
2028 | else if (w->repeat) |
1942 | { |
2029 | { |
1943 | w->at = w->repeat; |
2030 | ev_at (w) = w->repeat; |
1944 | ev_timer_start (EV_A_ w); |
2031 | ev_timer_start (EV_A_ w); |
1945 | } |
2032 | } |
1946 | } |
2033 | } |
1947 | |
2034 | |
1948 | #if EV_PERIODIC_ENABLE |
2035 | #if EV_PERIODIC_ENABLE |
… | |
… | |
1961 | ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; |
2048 | ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; |
1962 | } |
2049 | } |
1963 | else |
2050 | else |
1964 | ev_at (w) = w->offset; |
2051 | ev_at (w) = w->offset; |
1965 | |
2052 | |
1966 | ev_start (EV_A_ (W)w, ++periodiccnt); |
2053 | ev_start (EV_A_ (W)w, ++periodiccnt + HEAP0 - 1); |
1967 | array_needsize (WT, periodics, periodicmax, periodiccnt + 1, EMPTY2); |
2054 | array_needsize (WT, periodics, periodicmax, periodiccnt + HEAP0, EMPTY2); |
1968 | periodics [periodiccnt] = (WT)w; |
2055 | periodics [ev_active (w)] = (WT)w; |
1969 | upheap (periodics, periodiccnt); |
2056 | upheap (periodics, ev_active (w)); |
1970 | |
2057 | |
1971 | /*assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));*/ |
2058 | /*assert (("internal periodic heap corruption", periodics [ev_active (w)] == w));*/ |
1972 | } |
2059 | } |
1973 | |
2060 | |
1974 | void noinline |
2061 | void noinline |
1975 | ev_periodic_stop (EV_P_ ev_periodic *w) |
2062 | ev_periodic_stop (EV_P_ ev_periodic *w) |
1976 | { |
2063 | { |
1977 | clear_pending (EV_A_ (W)w); |
2064 | clear_pending (EV_A_ (W)w); |
1978 | if (expect_false (!ev_is_active (w))) |
2065 | if (expect_false (!ev_is_active (w))) |
1979 | return; |
2066 | return; |
1980 | |
2067 | |
1981 | assert (("internal periodic heap corruption", periodics [((W)w)->active] == (WT)w)); |
|
|
1982 | |
|
|
1983 | { |
2068 | { |
1984 | int active = ((W)w)->active; |
2069 | int active = ev_active (w); |
1985 | |
2070 | |
|
|
2071 | assert (("internal periodic heap corruption", periodics [active] == (WT)w)); |
|
|
2072 | |
1986 | if (expect_true (active < periodiccnt)) |
2073 | if (expect_true (active < periodiccnt + HEAP0 - 1)) |
1987 | { |
2074 | { |
1988 | periodics [active] = periodics [periodiccnt]; |
2075 | periodics [active] = periodics [periodiccnt + HEAP0 - 1]; |
1989 | adjustheap (periodics, periodiccnt, active); |
2076 | adjustheap (periodics, periodiccnt, active); |
1990 | } |
2077 | } |
1991 | |
2078 | |
1992 | --periodiccnt; |
2079 | --periodiccnt; |
1993 | } |
2080 | } |
… | |
… | |
2113 | if (w->wd < 0) |
2200 | if (w->wd < 0) |
2114 | { |
2201 | { |
2115 | ev_timer_start (EV_A_ &w->timer); /* this is not race-free, so we still need to recheck periodically */ |
2202 | ev_timer_start (EV_A_ &w->timer); /* this is not race-free, so we still need to recheck periodically */ |
2116 | |
2203 | |
2117 | /* monitor some parent directory for speedup hints */ |
2204 | /* monitor some parent directory for speedup hints */ |
|
|
2205 | /* note that exceeding the hardcoded limit is not a correctness issue, */ |
|
|
2206 | /* but an efficiency issue only */ |
2118 | if ((errno == ENOENT || errno == EACCES) && strlen (w->path) < 4096) |
2207 | if ((errno == ENOENT || errno == EACCES) && strlen (w->path) < 4096) |
2119 | { |
2208 | { |
2120 | char path [4096]; |
2209 | char path [4096]; |
2121 | strcpy (path, w->path); |
2210 | strcpy (path, w->path); |
2122 | |
2211 | |
… | |
… | |
2367 | clear_pending (EV_A_ (W)w); |
2456 | clear_pending (EV_A_ (W)w); |
2368 | if (expect_false (!ev_is_active (w))) |
2457 | if (expect_false (!ev_is_active (w))) |
2369 | return; |
2458 | return; |
2370 | |
2459 | |
2371 | { |
2460 | { |
2372 | int active = ((W)w)->active; |
2461 | int active = ev_active (w); |
2373 | |
2462 | |
2374 | idles [ABSPRI (w)][active - 1] = idles [ABSPRI (w)][--idlecnt [ABSPRI (w)]]; |
2463 | idles [ABSPRI (w)][active - 1] = idles [ABSPRI (w)][--idlecnt [ABSPRI (w)]]; |
2375 | ((W)idles [ABSPRI (w)][active - 1])->active = active; |
2464 | ev_active (idles [ABSPRI (w)][active - 1]) = active; |
2376 | |
2465 | |
2377 | ev_stop (EV_A_ (W)w); |
2466 | ev_stop (EV_A_ (W)w); |
2378 | --idleall; |
2467 | --idleall; |
2379 | } |
2468 | } |
2380 | } |
2469 | } |
… | |
… | |
2397 | clear_pending (EV_A_ (W)w); |
2486 | clear_pending (EV_A_ (W)w); |
2398 | if (expect_false (!ev_is_active (w))) |
2487 | if (expect_false (!ev_is_active (w))) |
2399 | return; |
2488 | return; |
2400 | |
2489 | |
2401 | { |
2490 | { |
2402 | int active = ((W)w)->active; |
2491 | int active = ev_active (w); |
|
|
2492 | |
2403 | prepares [active - 1] = prepares [--preparecnt]; |
2493 | prepares [active - 1] = prepares [--preparecnt]; |
2404 | ((W)prepares [active - 1])->active = active; |
2494 | ev_active (prepares [active - 1]) = active; |
2405 | } |
2495 | } |
2406 | |
2496 | |
2407 | ev_stop (EV_A_ (W)w); |
2497 | ev_stop (EV_A_ (W)w); |
2408 | } |
2498 | } |
2409 | |
2499 | |
… | |
… | |
2424 | clear_pending (EV_A_ (W)w); |
2514 | clear_pending (EV_A_ (W)w); |
2425 | if (expect_false (!ev_is_active (w))) |
2515 | if (expect_false (!ev_is_active (w))) |
2426 | return; |
2516 | return; |
2427 | |
2517 | |
2428 | { |
2518 | { |
2429 | int active = ((W)w)->active; |
2519 | int active = ev_active (w); |
|
|
2520 | |
2430 | checks [active - 1] = checks [--checkcnt]; |
2521 | checks [active - 1] = checks [--checkcnt]; |
2431 | ((W)checks [active - 1])->active = active; |
2522 | ev_active (checks [active - 1]) = active; |
2432 | } |
2523 | } |
2433 | |
2524 | |
2434 | ev_stop (EV_A_ (W)w); |
2525 | ev_stop (EV_A_ (W)w); |
2435 | } |
2526 | } |
2436 | |
2527 | |
… | |
… | |
2532 | clear_pending (EV_A_ (W)w); |
2623 | clear_pending (EV_A_ (W)w); |
2533 | if (expect_false (!ev_is_active (w))) |
2624 | if (expect_false (!ev_is_active (w))) |
2534 | return; |
2625 | return; |
2535 | |
2626 | |
2536 | { |
2627 | { |
2537 | int active = ((W)w)->active; |
2628 | int active = ev_active (w); |
|
|
2629 | |
2538 | forks [active - 1] = forks [--forkcnt]; |
2630 | forks [active - 1] = forks [--forkcnt]; |
2539 | ((W)forks [active - 1])->active = active; |
2631 | ev_active (forks [active - 1]) = active; |
2540 | } |
2632 | } |
2541 | |
2633 | |
2542 | ev_stop (EV_A_ (W)w); |
2634 | ev_stop (EV_A_ (W)w); |
2543 | } |
2635 | } |
2544 | #endif |
2636 | #endif |
… | |
… | |
2563 | clear_pending (EV_A_ (W)w); |
2655 | clear_pending (EV_A_ (W)w); |
2564 | if (expect_false (!ev_is_active (w))) |
2656 | if (expect_false (!ev_is_active (w))) |
2565 | return; |
2657 | return; |
2566 | |
2658 | |
2567 | { |
2659 | { |
2568 | int active = ((W)w)->active; |
2660 | int active = ev_active (w); |
|
|
2661 | |
2569 | asyncs [active - 1] = asyncs [--asynccnt]; |
2662 | asyncs [active - 1] = asyncs [--asynccnt]; |
2570 | ((W)asyncs [active - 1])->active = active; |
2663 | ev_active (asyncs [active - 1]) = active; |
2571 | } |
2664 | } |
2572 | |
2665 | |
2573 | ev_stop (EV_A_ (W)w); |
2666 | ev_stop (EV_A_ (W)w); |
2574 | } |
2667 | } |
2575 | |
2668 | |