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