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