… | |
… | |
126 | # define EV_USE_EVENTFD 1 |
126 | # define EV_USE_EVENTFD 1 |
127 | # else |
127 | # else |
128 | # define EV_USE_EVENTFD 0 |
128 | # define EV_USE_EVENTFD 0 |
129 | # endif |
129 | # endif |
130 | # endif |
130 | # endif |
131 | |
131 | |
132 | #endif |
132 | #endif |
133 | |
133 | |
134 | #include <math.h> |
134 | #include <math.h> |
135 | #include <stdlib.h> |
135 | #include <stdlib.h> |
136 | #include <fcntl.h> |
136 | #include <fcntl.h> |
… | |
… | |
235 | # else |
235 | # else |
236 | # define EV_USE_EVENTFD 0 |
236 | # define EV_USE_EVENTFD 0 |
237 | # endif |
237 | # endif |
238 | #endif |
238 | #endif |
239 | |
239 | |
|
|
240 | #if 0 /* debugging */ |
|
|
241 | # define EV_VERIFY 3 |
|
|
242 | # define EV_USE_4HEAP 1 |
|
|
243 | # define EV_HEAP_CACHE_AT 1 |
|
|
244 | #endif |
|
|
245 | |
|
|
246 | #ifndef EV_VERIFY |
|
|
247 | # define EV_VERIFY !EV_MINIMAL |
|
|
248 | #endif |
|
|
249 | |
240 | #ifndef EV_USE_4HEAP |
250 | #ifndef EV_USE_4HEAP |
241 | # define EV_USE_4HEAP !EV_MINIMAL |
251 | # define EV_USE_4HEAP !EV_MINIMAL |
242 | #endif |
252 | #endif |
243 | |
253 | |
244 | #ifndef EV_HEAP_CACHE_AT |
254 | #ifndef EV_HEAP_CACHE_AT |
… | |
… | |
288 | # endif |
298 | # endif |
289 | #endif |
299 | #endif |
290 | |
300 | |
291 | /**/ |
301 | /**/ |
292 | |
302 | |
293 | /* undefined or zero: no verification done or available */ |
|
|
294 | /* 1 or higher: ev_loop_verify function available */ |
|
|
295 | /* 2 or higher: ev_loop_verify is called frequently */ |
|
|
296 | #define EV_VERIFY 1 |
|
|
297 | |
|
|
298 | #if EV_VERIFY > 1 |
303 | #if EV_VERIFY >= 3 |
299 | # define EV_FREQUENT_CHECK ev_loop_verify (EV_A) |
304 | # define EV_FREQUENT_CHECK ev_loop_verify (EV_A) |
300 | #else |
305 | #else |
301 | # define EV_FREQUENT_CHECK do { } while (0) |
306 | # define EV_FREQUENT_CHECK do { } while (0) |
302 | #endif |
307 | #endif |
303 | |
308 | |
… | |
… | |
932 | /* rebuild the heap: this function is used only once and executed rarely */ |
937 | /* rebuild the heap: this function is used only once and executed rarely */ |
933 | void inline_size |
938 | void inline_size |
934 | reheap (ANHE *heap, int N) |
939 | reheap (ANHE *heap, int N) |
935 | { |
940 | { |
936 | int i; |
941 | int i; |
|
|
942 | |
937 | /* we don't use floyds algorithm, upheap is simpler and is more cache-efficient */ |
943 | /* we don't use floyds algorithm, upheap is simpler and is more cache-efficient */ |
938 | /* also, this is easy to implement and correct for both 2-heaps and 4-heaps */ |
944 | /* also, this is easy to implement and correct for both 2-heaps and 4-heaps */ |
939 | for (i = 0; i < N; ++i) |
945 | for (i = 0; i < N; ++i) |
940 | upheap (heap, i + HEAP0); |
946 | upheap (heap, i + HEAP0); |
941 | } |
947 | } |
942 | |
|
|
943 | #if EV_VERIFY |
|
|
944 | static void |
|
|
945 | checkheap (ANHE *heap, int N) |
|
|
946 | { |
|
|
947 | int i; |
|
|
948 | |
|
|
949 | for (i = HEAP0; i < N + HEAP0; ++i) |
|
|
950 | { |
|
|
951 | assert (("active index mismatch in heap", ev_active (ANHE_w (heap [i])) == i)); |
|
|
952 | assert (("heap condition violated", i == HEAP0 || ANHE_at (heap [HPARENT (i)]) <= ANHE_at (heap [i]))); |
|
|
953 | assert (("heap at cache mismatch", ANHE_at (heap [i]) == ev_at (ANHE_w (heap [i])))); |
|
|
954 | } |
|
|
955 | } |
|
|
956 | #endif |
|
|
957 | |
948 | |
958 | /*****************************************************************************/ |
949 | /*****************************************************************************/ |
959 | |
950 | |
960 | typedef struct |
951 | typedef struct |
961 | { |
952 | { |
… | |
… | |
1478 | |
1469 | |
1479 | postfork = 0; |
1470 | postfork = 0; |
1480 | } |
1471 | } |
1481 | |
1472 | |
1482 | #if EV_MULTIPLICITY |
1473 | #if EV_MULTIPLICITY |
|
|
1474 | |
1483 | struct ev_loop * |
1475 | struct ev_loop * |
1484 | ev_loop_new (unsigned int flags) |
1476 | ev_loop_new (unsigned int flags) |
1485 | { |
1477 | { |
1486 | struct ev_loop *loop = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop)); |
1478 | struct ev_loop *loop = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop)); |
1487 | |
1479 | |
… | |
… | |
1507 | { |
1499 | { |
1508 | postfork = 1; /* must be in line with ev_default_fork */ |
1500 | postfork = 1; /* must be in line with ev_default_fork */ |
1509 | } |
1501 | } |
1510 | |
1502 | |
1511 | #if EV_VERIFY |
1503 | #if EV_VERIFY |
1512 | static void |
1504 | void noinline |
|
|
1505 | verify_watcher (EV_P_ W w) |
|
|
1506 | { |
|
|
1507 | assert (("watcher has invalid priority", ABSPRI (w) >= 0 && ABSPRI (w) < NUMPRI)); |
|
|
1508 | |
|
|
1509 | if (w->pending) |
|
|
1510 | assert (("pending watcher not on pending queue", pendings [ABSPRI (w)][w->pending - 1].w == w)); |
|
|
1511 | } |
|
|
1512 | |
|
|
1513 | static void noinline |
|
|
1514 | verify_heap (EV_P_ ANHE *heap, int N) |
|
|
1515 | { |
|
|
1516 | int i; |
|
|
1517 | |
|
|
1518 | for (i = HEAP0; i < N + HEAP0; ++i) |
|
|
1519 | { |
|
|
1520 | assert (("active index mismatch in heap", ev_active (ANHE_w (heap [i])) == i)); |
|
|
1521 | assert (("heap condition violated", i == HEAP0 || ANHE_at (heap [HPARENT (i)]) <= ANHE_at (heap [i]))); |
|
|
1522 | assert (("heap at cache mismatch", ANHE_at (heap [i]) == ev_at (ANHE_w (heap [i])))); |
|
|
1523 | |
|
|
1524 | verify_watcher (EV_A_ (W)ANHE_w (heap [i])); |
|
|
1525 | } |
|
|
1526 | } |
|
|
1527 | |
|
|
1528 | static void noinline |
1513 | array_check (W **ws, int cnt) |
1529 | array_verify (EV_P_ W *ws, int cnt) |
1514 | { |
1530 | { |
1515 | while (cnt--) |
1531 | while (cnt--) |
|
|
1532 | { |
1516 | assert (("active index mismatch", ev_active (ws [cnt]) == cnt + 1)); |
1533 | assert (("active index mismatch", ev_active (ws [cnt]) == cnt + 1)); |
|
|
1534 | verify_watcher (EV_A_ ws [cnt]); |
|
|
1535 | } |
1517 | } |
1536 | } |
|
|
1537 | #endif |
1518 | |
1538 | |
1519 | static void |
1539 | void |
1520 | ev_loop_verify (EV_P) |
1540 | ev_loop_verify (EV_P) |
1521 | { |
1541 | { |
|
|
1542 | #if EV_VERIFY |
1522 | int i; |
1543 | int i; |
|
|
1544 | WL w; |
1523 | |
1545 | |
|
|
1546 | assert (activecnt >= -1); |
|
|
1547 | |
|
|
1548 | assert (fdchangemax >= fdchangecnt); |
|
|
1549 | for (i = 0; i < fdchangecnt; ++i) |
|
|
1550 | assert (("negative fd in fdchanges", fdchanges [i] >= 0)); |
|
|
1551 | |
|
|
1552 | assert (anfdmax >= 0); |
|
|
1553 | for (i = 0; i < anfdmax; ++i) |
|
|
1554 | for (w = anfds [i].head; w; w = w->next) |
|
|
1555 | { |
|
|
1556 | verify_watcher (EV_A_ (W)w); |
|
|
1557 | assert (("inactive fd watcher on anfd list", ev_active (w) == 1)); |
|
|
1558 | assert (("fd mismatch between watcher and anfd", ((ev_io *)w)->fd == i)); |
|
|
1559 | } |
|
|
1560 | |
|
|
1561 | assert (timermax >= timercnt); |
1524 | checkheap (timers, timercnt); |
1562 | verify_heap (EV_A_ timers, timercnt); |
|
|
1563 | |
1525 | #if EV_PERIODIC_ENABLE |
1564 | #if EV_PERIODIC_ENABLE |
|
|
1565 | assert (periodicmax >= periodiccnt); |
1526 | checkheap (periodics, periodiccnt); |
1566 | verify_heap (EV_A_ periodics, periodiccnt); |
1527 | #endif |
1567 | #endif |
1528 | |
1568 | |
|
|
1569 | for (i = NUMPRI; i--; ) |
|
|
1570 | { |
|
|
1571 | assert (pendingmax [i] >= pendingcnt [i]); |
1529 | #if EV_IDLE_ENABLE |
1572 | #if EV_IDLE_ENABLE |
1530 | for (i = NUMPRI; i--; ) |
1573 | assert (idlemax [i] >= idlecnt [i]); |
1531 | array_check ((W **)idles [i], idlecnt [i]); |
1574 | array_verify (EV_A_ (W *)idles [i], idlecnt [i]); |
1532 | #endif |
1575 | #endif |
|
|
1576 | } |
|
|
1577 | |
1533 | #if EV_FORK_ENABLE |
1578 | #if EV_FORK_ENABLE |
|
|
1579 | assert (forkmax >= forkcnt); |
1534 | array_check ((W **)forks, forkcnt); |
1580 | array_verify (EV_A_ (W *)forks, forkcnt); |
1535 | #endif |
1581 | #endif |
1536 | array_check ((W **)prepares, preparecnt); |
1582 | |
1537 | array_check ((W **)checks, checkcnt); |
|
|
1538 | #if EV_ASYNC_ENABLE |
1583 | #if EV_ASYNC_ENABLE |
|
|
1584 | assert (asyncmax >= asynccnt); |
1539 | array_check ((W **)asyncs, asynccnt); |
1585 | array_verify (EV_A_ (W *)asyncs, asynccnt); |
|
|
1586 | #endif |
|
|
1587 | |
|
|
1588 | assert (preparemax >= preparecnt); |
|
|
1589 | array_verify (EV_A_ (W *)prepares, preparecnt); |
|
|
1590 | |
|
|
1591 | assert (checkmax >= checkcnt); |
|
|
1592 | array_verify (EV_A_ (W *)checks, checkcnt); |
|
|
1593 | |
|
|
1594 | # if 0 |
|
|
1595 | for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next) |
|
|
1596 | for (signum = signalmax; signum--; ) if (signals [signum].gotsig) |
1540 | #endif |
1597 | # endif |
1541 | } |
|
|
1542 | #endif |
1598 | #endif |
|
|
1599 | } |
1543 | |
1600 | |
1544 | #endif |
1601 | #endif /* multiplicity */ |
1545 | |
1602 | |
1546 | #if EV_MULTIPLICITY |
1603 | #if EV_MULTIPLICITY |
1547 | struct ev_loop * |
1604 | struct ev_loop * |
1548 | ev_default_loop_init (unsigned int flags) |
1605 | ev_default_loop_init (unsigned int flags) |
1549 | #else |
1606 | #else |
… | |
… | |
1614 | void inline_speed |
1671 | void inline_speed |
1615 | call_pending (EV_P) |
1672 | call_pending (EV_P) |
1616 | { |
1673 | { |
1617 | int pri; |
1674 | int pri; |
1618 | |
1675 | |
1619 | EV_FREQUENT_CHECK; |
|
|
1620 | |
|
|
1621 | for (pri = NUMPRI; pri--; ) |
1676 | for (pri = NUMPRI; pri--; ) |
1622 | while (pendingcnt [pri]) |
1677 | while (pendingcnt [pri]) |
1623 | { |
1678 | { |
1624 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
1679 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
1625 | |
1680 | |
… | |
… | |
1627 | { |
1682 | { |
1628 | /*assert (("non-pending watcher on pending list", p->w->pending));*/ |
1683 | /*assert (("non-pending watcher on pending list", p->w->pending));*/ |
1629 | |
1684 | |
1630 | p->w->pending = 0; |
1685 | p->w->pending = 0; |
1631 | EV_CB_INVOKE (p->w, p->events); |
1686 | EV_CB_INVOKE (p->w, p->events); |
|
|
1687 | EV_FREQUENT_CHECK; |
1632 | } |
1688 | } |
1633 | } |
1689 | } |
1634 | |
|
|
1635 | EV_FREQUENT_CHECK; |
|
|
1636 | } |
1690 | } |
1637 | |
1691 | |
1638 | #if EV_IDLE_ENABLE |
1692 | #if EV_IDLE_ENABLE |
1639 | void inline_size |
1693 | void inline_size |
1640 | idle_reify (EV_P) |
1694 | idle_reify (EV_P) |
… | |
… | |
1692 | #if EV_PERIODIC_ENABLE |
1746 | #if EV_PERIODIC_ENABLE |
1693 | void inline_size |
1747 | void inline_size |
1694 | periodics_reify (EV_P) |
1748 | periodics_reify (EV_P) |
1695 | { |
1749 | { |
1696 | EV_FREQUENT_CHECK; |
1750 | EV_FREQUENT_CHECK; |
|
|
1751 | |
1697 | while (periodiccnt && ANHE_at (periodics [HEAP0]) < ev_rt_now) |
1752 | while (periodiccnt && ANHE_at (periodics [HEAP0]) < ev_rt_now) |
1698 | { |
1753 | { |
1699 | ev_periodic *w = (ev_periodic *)ANHE_w (periodics [HEAP0]); |
1754 | ev_periodic *w = (ev_periodic *)ANHE_w (periodics [HEAP0]); |
1700 | |
1755 | |
1701 | /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/ |
1756 | /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/ |
… | |
… | |
1707 | |
1762 | |
1708 | assert (("ev_periodic reschedule callback returned time in the past", ev_at (w) >= ev_rt_now)); |
1763 | assert (("ev_periodic reschedule callback returned time in the past", ev_at (w) >= ev_rt_now)); |
1709 | |
1764 | |
1710 | ANHE_at_cache (periodics [HEAP0]); |
1765 | ANHE_at_cache (periodics [HEAP0]); |
1711 | downheap (periodics, periodiccnt, HEAP0); |
1766 | downheap (periodics, periodiccnt, HEAP0); |
1712 | EV_FREQUENT_CHECK; |
|
|
1713 | } |
1767 | } |
1714 | else if (w->interval) |
1768 | else if (w->interval) |
1715 | { |
1769 | { |
1716 | ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; |
1770 | ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; |
1717 | /* if next trigger time is not sufficiently in the future, put it there */ |
1771 | /* if next trigger time is not sufficiently in the future, put it there */ |
… | |
… | |
1853 | |
1907 | |
1854 | call_pending (EV_A); /* in case we recurse, ensure ordering stays nice and clean */ |
1908 | call_pending (EV_A); /* in case we recurse, ensure ordering stays nice and clean */ |
1855 | |
1909 | |
1856 | do |
1910 | do |
1857 | { |
1911 | { |
|
|
1912 | #if EV_VERIFY >= 2 |
|
|
1913 | ev_loop_verify (EV_A); |
|
|
1914 | #endif |
|
|
1915 | |
1858 | #ifndef _WIN32 |
1916 | #ifndef _WIN32 |
1859 | if (expect_false (curpid)) /* penalise the forking check even more */ |
1917 | if (expect_false (curpid)) /* penalise the forking check even more */ |
1860 | if (expect_false (getpid () != curpid)) |
1918 | if (expect_false (getpid () != curpid)) |
1861 | { |
1919 | { |
1862 | curpid = getpid (); |
1920 | curpid = getpid (); |