… | |
… | |
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> |
… | |
… | |
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 */ |
240 | #if 0 /* debugging */ |
241 | # define EV_VERIFY 1 |
241 | # define EV_VERIFY 3 |
242 | # define EV_USE_4HEAP 1 |
242 | # define EV_USE_4HEAP 1 |
243 | # define EV_HEAP_CACHE_AT 1 |
243 | # define EV_HEAP_CACHE_AT 1 |
|
|
244 | #endif |
|
|
245 | |
|
|
246 | #ifndef EV_VERIFY |
|
|
247 | # define EV_VERIFY !EV_MINIMAL |
244 | #endif |
248 | #endif |
245 | |
249 | |
246 | #ifndef EV_USE_4HEAP |
250 | #ifndef EV_USE_4HEAP |
247 | # define EV_USE_4HEAP !EV_MINIMAL |
251 | # define EV_USE_4HEAP !EV_MINIMAL |
248 | #endif |
252 | #endif |
… | |
… | |
294 | # endif |
298 | # endif |
295 | #endif |
299 | #endif |
296 | |
300 | |
297 | /**/ |
301 | /**/ |
298 | |
302 | |
299 | /* EV_VERIFY: enable internal consistency checks |
|
|
300 | * undefined or zero: no verification done or available |
|
|
301 | * 1 or higher: ev_loop_verify function available |
|
|
302 | * 2 or higher: ev_loop_verify is called frequently |
|
|
303 | */ |
|
|
304 | #if EV_VERIFY >= 1 |
303 | #if EV_VERIFY >= 3 |
305 | # define EV_FREQUENT_CHECK ev_loop_verify (EV_A) |
304 | # define EV_FREQUENT_CHECK ev_loop_verify (EV_A) |
306 | #else |
305 | #else |
307 | # define EV_FREQUENT_CHECK do { } while (0) |
306 | # define EV_FREQUENT_CHECK do { } while (0) |
308 | #endif |
307 | #endif |
309 | |
308 | |
… | |
… | |
938 | /* 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 */ |
939 | void inline_size |
938 | void inline_size |
940 | reheap (ANHE *heap, int N) |
939 | reheap (ANHE *heap, int N) |
941 | { |
940 | { |
942 | int i; |
941 | int i; |
|
|
942 | |
943 | /* 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 */ |
944 | /* 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 */ |
945 | for (i = 0; i < N; ++i) |
945 | for (i = 0; i < N; ++i) |
946 | upheap (heap, i + HEAP0); |
946 | upheap (heap, i + HEAP0); |
947 | } |
947 | } |
948 | |
|
|
949 | #if EV_VERIFY |
|
|
950 | static void |
|
|
951 | checkheap (ANHE *heap, int N) |
|
|
952 | { |
|
|
953 | int i; |
|
|
954 | |
|
|
955 | for (i = HEAP0; i < N + HEAP0; ++i) |
|
|
956 | { |
|
|
957 | assert (("active index mismatch in heap", ev_active (ANHE_w (heap [i])) == i)); |
|
|
958 | assert (("heap condition violated", i == HEAP0 || ANHE_at (heap [HPARENT (i)]) <= ANHE_at (heap [i]))); |
|
|
959 | assert (("heap at cache mismatch", ANHE_at (heap [i]) == ev_at (ANHE_w (heap [i])))); |
|
|
960 | } |
|
|
961 | } |
|
|
962 | #endif |
|
|
963 | |
948 | |
964 | /*****************************************************************************/ |
949 | /*****************************************************************************/ |
965 | |
950 | |
966 | typedef struct |
951 | typedef struct |
967 | { |
952 | { |
… | |
… | |
1484 | |
1469 | |
1485 | postfork = 0; |
1470 | postfork = 0; |
1486 | } |
1471 | } |
1487 | |
1472 | |
1488 | #if EV_MULTIPLICITY |
1473 | #if EV_MULTIPLICITY |
|
|
1474 | |
1489 | struct ev_loop * |
1475 | struct ev_loop * |
1490 | ev_loop_new (unsigned int flags) |
1476 | ev_loop_new (unsigned int flags) |
1491 | { |
1477 | { |
1492 | 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)); |
1493 | |
1479 | |
… | |
… | |
1513 | { |
1499 | { |
1514 | postfork = 1; /* must be in line with ev_default_fork */ |
1500 | postfork = 1; /* must be in line with ev_default_fork */ |
1515 | } |
1501 | } |
1516 | |
1502 | |
1517 | #if EV_VERIFY |
1503 | #if EV_VERIFY |
1518 | 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 |
1519 | array_check (W **ws, int cnt) |
1529 | array_verify (EV_P_ W *ws, int cnt) |
1520 | { |
1530 | { |
1521 | while (cnt--) |
1531 | while (cnt--) |
|
|
1532 | { |
1522 | 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 | } |
1523 | } |
1536 | } |
|
|
1537 | #endif |
1524 | |
1538 | |
1525 | static void |
1539 | void |
1526 | ev_loop_verify (EV_P) |
1540 | ev_loop_verify (EV_P) |
1527 | { |
1541 | { |
|
|
1542 | #if EV_VERIFY |
1528 | int i; |
1543 | int i; |
|
|
1544 | WL w; |
1529 | |
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); |
1530 | checkheap (timers, timercnt); |
1562 | verify_heap (EV_A_ timers, timercnt); |
|
|
1563 | |
1531 | #if EV_PERIODIC_ENABLE |
1564 | #if EV_PERIODIC_ENABLE |
|
|
1565 | assert (periodicmax >= periodiccnt); |
1532 | checkheap (periodics, periodiccnt); |
1566 | verify_heap (EV_A_ periodics, periodiccnt); |
1533 | #endif |
1567 | #endif |
1534 | |
1568 | |
|
|
1569 | for (i = NUMPRI; i--; ) |
|
|
1570 | { |
|
|
1571 | assert (pendingmax [i] >= pendingcnt [i]); |
1535 | #if EV_IDLE_ENABLE |
1572 | #if EV_IDLE_ENABLE |
1536 | for (i = NUMPRI; i--; ) |
1573 | assert (idleall >= 0); |
|
|
1574 | assert (idlemax [i] >= idlecnt [i]); |
1537 | array_check ((W **)idles [i], idlecnt [i]); |
1575 | array_verify (EV_A_ (W *)idles [i], idlecnt [i]); |
1538 | #endif |
1576 | #endif |
|
|
1577 | } |
|
|
1578 | |
1539 | #if EV_FORK_ENABLE |
1579 | #if EV_FORK_ENABLE |
|
|
1580 | assert (forkmax >= forkcnt); |
1540 | array_check ((W **)forks, forkcnt); |
1581 | array_verify (EV_A_ (W *)forks, forkcnt); |
1541 | #endif |
1582 | #endif |
1542 | array_check ((W **)prepares, preparecnt); |
1583 | |
1543 | array_check ((W **)checks, checkcnt); |
|
|
1544 | #if EV_ASYNC_ENABLE |
1584 | #if EV_ASYNC_ENABLE |
|
|
1585 | assert (asyncmax >= asynccnt); |
1545 | array_check ((W **)asyncs, asynccnt); |
1586 | array_verify (EV_A_ (W *)asyncs, asynccnt); |
|
|
1587 | #endif |
|
|
1588 | |
|
|
1589 | assert (preparemax >= preparecnt); |
|
|
1590 | array_verify (EV_A_ (W *)prepares, preparecnt); |
|
|
1591 | |
|
|
1592 | assert (checkmax >= checkcnt); |
|
|
1593 | array_verify (EV_A_ (W *)checks, checkcnt); |
|
|
1594 | |
|
|
1595 | # if 0 |
|
|
1596 | for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next) |
|
|
1597 | for (signum = signalmax; signum--; ) if (signals [signum].gotsig) |
1546 | #endif |
1598 | # endif |
1547 | } |
|
|
1548 | #endif |
1599 | #endif |
|
|
1600 | } |
1549 | |
1601 | |
1550 | #endif |
1602 | #endif /* multiplicity */ |
1551 | |
1603 | |
1552 | #if EV_MULTIPLICITY |
1604 | #if EV_MULTIPLICITY |
1553 | struct ev_loop * |
1605 | struct ev_loop * |
1554 | ev_default_loop_init (unsigned int flags) |
1606 | ev_default_loop_init (unsigned int flags) |
1555 | #else |
1607 | #else |
… | |
… | |
1620 | void inline_speed |
1672 | void inline_speed |
1621 | call_pending (EV_P) |
1673 | call_pending (EV_P) |
1622 | { |
1674 | { |
1623 | int pri; |
1675 | int pri; |
1624 | |
1676 | |
1625 | EV_FREQUENT_CHECK; |
|
|
1626 | |
|
|
1627 | for (pri = NUMPRI; pri--; ) |
1677 | for (pri = NUMPRI; pri--; ) |
1628 | while (pendingcnt [pri]) |
1678 | while (pendingcnt [pri]) |
1629 | { |
1679 | { |
1630 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
1680 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
1631 | |
1681 | |
… | |
… | |
1633 | { |
1683 | { |
1634 | /*assert (("non-pending watcher on pending list", p->w->pending));*/ |
1684 | /*assert (("non-pending watcher on pending list", p->w->pending));*/ |
1635 | |
1685 | |
1636 | p->w->pending = 0; |
1686 | p->w->pending = 0; |
1637 | EV_CB_INVOKE (p->w, p->events); |
1687 | EV_CB_INVOKE (p->w, p->events); |
|
|
1688 | EV_FREQUENT_CHECK; |
1638 | } |
1689 | } |
1639 | } |
1690 | } |
1640 | |
|
|
1641 | EV_FREQUENT_CHECK; |
|
|
1642 | } |
1691 | } |
1643 | |
1692 | |
1644 | #if EV_IDLE_ENABLE |
1693 | #if EV_IDLE_ENABLE |
1645 | void inline_size |
1694 | void inline_size |
1646 | idle_reify (EV_P) |
1695 | idle_reify (EV_P) |
… | |
… | |
1698 | #if EV_PERIODIC_ENABLE |
1747 | #if EV_PERIODIC_ENABLE |
1699 | void inline_size |
1748 | void inline_size |
1700 | periodics_reify (EV_P) |
1749 | periodics_reify (EV_P) |
1701 | { |
1750 | { |
1702 | EV_FREQUENT_CHECK; |
1751 | EV_FREQUENT_CHECK; |
|
|
1752 | |
1703 | while (periodiccnt && ANHE_at (periodics [HEAP0]) < ev_rt_now) |
1753 | while (periodiccnt && ANHE_at (periodics [HEAP0]) < ev_rt_now) |
1704 | { |
1754 | { |
1705 | ev_periodic *w = (ev_periodic *)ANHE_w (periodics [HEAP0]); |
1755 | ev_periodic *w = (ev_periodic *)ANHE_w (periodics [HEAP0]); |
1706 | |
1756 | |
1707 | /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/ |
1757 | /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/ |
… | |
… | |
1713 | |
1763 | |
1714 | assert (("ev_periodic reschedule callback returned time in the past", ev_at (w) >= ev_rt_now)); |
1764 | assert (("ev_periodic reschedule callback returned time in the past", ev_at (w) >= ev_rt_now)); |
1715 | |
1765 | |
1716 | ANHE_at_cache (periodics [HEAP0]); |
1766 | ANHE_at_cache (periodics [HEAP0]); |
1717 | downheap (periodics, periodiccnt, HEAP0); |
1767 | downheap (periodics, periodiccnt, HEAP0); |
1718 | EV_FREQUENT_CHECK; |
|
|
1719 | } |
1768 | } |
1720 | else if (w->interval) |
1769 | else if (w->interval) |
1721 | { |
1770 | { |
1722 | ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; |
1771 | ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; |
1723 | /* if next trigger time is not sufficiently in the future, put it there */ |
1772 | /* if next trigger time is not sufficiently in the future, put it there */ |
… | |
… | |
1859 | |
1908 | |
1860 | call_pending (EV_A); /* in case we recurse, ensure ordering stays nice and clean */ |
1909 | call_pending (EV_A); /* in case we recurse, ensure ordering stays nice and clean */ |
1861 | |
1910 | |
1862 | do |
1911 | do |
1863 | { |
1912 | { |
|
|
1913 | #if EV_VERIFY >= 2 |
|
|
1914 | ev_loop_verify (EV_A); |
|
|
1915 | #endif |
|
|
1916 | |
1864 | #ifndef _WIN32 |
1917 | #ifndef _WIN32 |
1865 | if (expect_false (curpid)) /* penalise the forking check even more */ |
1918 | if (expect_false (curpid)) /* penalise the forking check even more */ |
1866 | if (expect_false (getpid () != curpid)) |
1919 | if (expect_false (getpid () != curpid)) |
1867 | { |
1920 | { |
1868 | curpid = getpid (); |
1921 | curpid = getpid (); |