… | |
… | |
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> |
… | |
… | |
164 | #endif |
164 | #endif |
165 | |
165 | |
166 | /* this block tries to deduce configuration from header-defined symbols and defaults */ |
166 | /* this block tries to deduce configuration from header-defined symbols and defaults */ |
167 | |
167 | |
168 | #ifndef EV_USE_MONOTONIC |
168 | #ifndef EV_USE_MONOTONIC |
|
|
169 | # if defined (_POSIX_MONOTONIC_CLOCK) && _POSIX_MONOTONIC_CLOCK >= 0 |
|
|
170 | # define EV_USE_MONOTONIC 1 |
|
|
171 | # else |
169 | # define EV_USE_MONOTONIC 0 |
172 | # define EV_USE_MONOTONIC 0 |
|
|
173 | # endif |
170 | #endif |
174 | #endif |
171 | |
175 | |
172 | #ifndef EV_USE_REALTIME |
176 | #ifndef EV_USE_REALTIME |
173 | # define EV_USE_REALTIME 0 |
177 | # define EV_USE_REALTIME 0 |
174 | #endif |
178 | #endif |
175 | |
179 | |
176 | #ifndef EV_USE_NANOSLEEP |
180 | #ifndef EV_USE_NANOSLEEP |
|
|
181 | # if _POSIX_C_SOURCE >= 199309L |
|
|
182 | # define EV_USE_NANOSLEEP 1 |
|
|
183 | # else |
177 | # define EV_USE_NANOSLEEP 0 |
184 | # define EV_USE_NANOSLEEP 0 |
|
|
185 | # endif |
178 | #endif |
186 | #endif |
179 | |
187 | |
180 | #ifndef EV_USE_SELECT |
188 | #ifndef EV_USE_SELECT |
181 | # define EV_USE_SELECT 1 |
189 | # define EV_USE_SELECT 1 |
182 | #endif |
190 | #endif |
… | |
… | |
236 | # define EV_USE_EVENTFD 0 |
244 | # define EV_USE_EVENTFD 0 |
237 | # endif |
245 | # endif |
238 | #endif |
246 | #endif |
239 | |
247 | |
240 | #if 0 /* debugging */ |
248 | #if 0 /* debugging */ |
241 | # define EV_VERIFY 1 |
249 | # define EV_VERIFY 3 |
242 | # define EV_USE_4HEAP 1 |
250 | # define EV_USE_4HEAP 1 |
243 | # define EV_HEAP_CACHE_AT 1 |
251 | # define EV_HEAP_CACHE_AT 1 |
|
|
252 | #endif |
|
|
253 | |
|
|
254 | #ifndef EV_VERIFY |
|
|
255 | # define EV_VERIFY !EV_MINIMAL |
244 | #endif |
256 | #endif |
245 | |
257 | |
246 | #ifndef EV_USE_4HEAP |
258 | #ifndef EV_USE_4HEAP |
247 | # define EV_USE_4HEAP !EV_MINIMAL |
259 | # define EV_USE_4HEAP !EV_MINIMAL |
248 | #endif |
260 | #endif |
… | |
… | |
294 | # endif |
306 | # endif |
295 | #endif |
307 | #endif |
296 | |
308 | |
297 | /**/ |
309 | /**/ |
298 | |
310 | |
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 |
311 | #if EV_VERIFY >= 3 |
305 | # define EV_FREQUENT_CHECK ev_loop_verify (EV_A) |
312 | # define EV_FREQUENT_CHECK ev_loop_verify (EV_A) |
306 | #else |
313 | #else |
307 | # define EV_FREQUENT_CHECK do { } while (0) |
314 | # define EV_FREQUENT_CHECK do { } while (0) |
308 | #endif |
315 | #endif |
309 | |
316 | |
… | |
… | |
700 | events |= (unsigned char)w->events; |
707 | events |= (unsigned char)w->events; |
701 | |
708 | |
702 | #if EV_SELECT_IS_WINSOCKET |
709 | #if EV_SELECT_IS_WINSOCKET |
703 | if (events) |
710 | if (events) |
704 | { |
711 | { |
705 | unsigned long argp; |
712 | unsigned long arg; |
706 | #ifdef EV_FD_TO_WIN32_HANDLE |
713 | #ifdef EV_FD_TO_WIN32_HANDLE |
707 | anfd->handle = EV_FD_TO_WIN32_HANDLE (fd); |
714 | anfd->handle = EV_FD_TO_WIN32_HANDLE (fd); |
708 | #else |
715 | #else |
709 | anfd->handle = _get_osfhandle (fd); |
716 | anfd->handle = _get_osfhandle (fd); |
710 | #endif |
717 | #endif |
711 | assert (("libev only supports socket fds in this configuration", ioctlsocket (anfd->handle, FIONREAD, &argp) == 0)); |
718 | assert (("libev only supports socket fds in this configuration", ioctlsocket (anfd->handle, FIONREAD, &arg) == 0)); |
712 | } |
719 | } |
713 | #endif |
720 | #endif |
714 | |
721 | |
715 | { |
722 | { |
716 | unsigned char o_events = anfd->events; |
723 | unsigned char o_events = anfd->events; |
… | |
… | |
769 | { |
776 | { |
770 | int fd; |
777 | int fd; |
771 | |
778 | |
772 | for (fd = 0; fd < anfdmax; ++fd) |
779 | for (fd = 0; fd < anfdmax; ++fd) |
773 | if (anfds [fd].events) |
780 | if (anfds [fd].events) |
774 | if (!fd_valid (fd) == -1 && errno == EBADF) |
781 | if (!fd_valid (fd) && errno == EBADF) |
775 | fd_kill (EV_A_ fd); |
782 | fd_kill (EV_A_ fd); |
776 | } |
783 | } |
777 | |
784 | |
778 | /* called on ENOMEM in select/poll to kill some fds and retry */ |
785 | /* called on ENOMEM in select/poll to kill some fds and retry */ |
779 | static void noinline |
786 | static void noinline |
… | |
… | |
938 | /* rebuild the heap: this function is used only once and executed rarely */ |
945 | /* rebuild the heap: this function is used only once and executed rarely */ |
939 | void inline_size |
946 | void inline_size |
940 | reheap (ANHE *heap, int N) |
947 | reheap (ANHE *heap, int N) |
941 | { |
948 | { |
942 | int i; |
949 | int i; |
|
|
950 | |
943 | /* we don't use floyds algorithm, upheap is simpler and is more cache-efficient */ |
951 | /* 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 */ |
952 | /* also, this is easy to implement and correct for both 2-heaps and 4-heaps */ |
945 | for (i = 0; i < N; ++i) |
953 | for (i = 0; i < N; ++i) |
946 | upheap (heap, i + HEAP0); |
954 | upheap (heap, i + HEAP0); |
947 | } |
955 | } |
948 | |
956 | |
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 | |
|
|
964 | /*****************************************************************************/ |
957 | /*****************************************************************************/ |
965 | |
958 | |
966 | typedef struct |
959 | typedef struct |
967 | { |
960 | { |
968 | WL head; |
961 | WL head; |
… | |
… | |
990 | |
983 | |
991 | void inline_speed |
984 | void inline_speed |
992 | fd_intern (int fd) |
985 | fd_intern (int fd) |
993 | { |
986 | { |
994 | #ifdef _WIN32 |
987 | #ifdef _WIN32 |
995 | int arg = 1; |
988 | unsigned long arg = 1; |
996 | ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg); |
989 | ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg); |
997 | #else |
990 | #else |
998 | fcntl (fd, F_SETFD, FD_CLOEXEC); |
991 | fcntl (fd, F_SETFD, FD_CLOEXEC); |
999 | fcntl (fd, F_SETFL, O_NONBLOCK); |
992 | fcntl (fd, F_SETFL, O_NONBLOCK); |
1000 | #endif |
993 | #endif |
… | |
… | |
1484 | |
1477 | |
1485 | postfork = 0; |
1478 | postfork = 0; |
1486 | } |
1479 | } |
1487 | |
1480 | |
1488 | #if EV_MULTIPLICITY |
1481 | #if EV_MULTIPLICITY |
|
|
1482 | |
1489 | struct ev_loop * |
1483 | struct ev_loop * |
1490 | ev_loop_new (unsigned int flags) |
1484 | ev_loop_new (unsigned int flags) |
1491 | { |
1485 | { |
1492 | struct ev_loop *loop = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop)); |
1486 | struct ev_loop *loop = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop)); |
1493 | |
1487 | |
… | |
… | |
1513 | { |
1507 | { |
1514 | postfork = 1; /* must be in line with ev_default_fork */ |
1508 | postfork = 1; /* must be in line with ev_default_fork */ |
1515 | } |
1509 | } |
1516 | |
1510 | |
1517 | #if EV_VERIFY |
1511 | #if EV_VERIFY |
1518 | static void |
1512 | void noinline |
|
|
1513 | verify_watcher (EV_P_ W w) |
|
|
1514 | { |
|
|
1515 | assert (("watcher has invalid priority", ABSPRI (w) >= 0 && ABSPRI (w) < NUMPRI)); |
|
|
1516 | |
|
|
1517 | if (w->pending) |
|
|
1518 | assert (("pending watcher not on pending queue", pendings [ABSPRI (w)][w->pending - 1].w == w)); |
|
|
1519 | } |
|
|
1520 | |
|
|
1521 | static void noinline |
|
|
1522 | verify_heap (EV_P_ ANHE *heap, int N) |
|
|
1523 | { |
|
|
1524 | int i; |
|
|
1525 | |
|
|
1526 | for (i = HEAP0; i < N + HEAP0; ++i) |
|
|
1527 | { |
|
|
1528 | assert (("active index mismatch in heap", ev_active (ANHE_w (heap [i])) == i)); |
|
|
1529 | assert (("heap condition violated", i == HEAP0 || ANHE_at (heap [HPARENT (i)]) <= ANHE_at (heap [i]))); |
|
|
1530 | assert (("heap at cache mismatch", ANHE_at (heap [i]) == ev_at (ANHE_w (heap [i])))); |
|
|
1531 | |
|
|
1532 | verify_watcher (EV_A_ (W)ANHE_w (heap [i])); |
|
|
1533 | } |
|
|
1534 | } |
|
|
1535 | |
|
|
1536 | static void noinline |
1519 | array_check (W **ws, int cnt) |
1537 | array_verify (EV_P_ W *ws, int cnt) |
1520 | { |
1538 | { |
1521 | while (cnt--) |
1539 | while (cnt--) |
|
|
1540 | { |
1522 | assert (("active index mismatch", ev_active (ws [cnt]) == cnt + 1)); |
1541 | assert (("active index mismatch", ev_active (ws [cnt]) == cnt + 1)); |
|
|
1542 | verify_watcher (EV_A_ ws [cnt]); |
|
|
1543 | } |
1523 | } |
1544 | } |
|
|
1545 | #endif |
1524 | |
1546 | |
1525 | static void |
1547 | void |
1526 | ev_loop_verify (EV_P) |
1548 | ev_loop_verify (EV_P) |
1527 | { |
1549 | { |
|
|
1550 | #if EV_VERIFY |
1528 | int i; |
1551 | int i; |
|
|
1552 | WL w; |
1529 | |
1553 | |
|
|
1554 | assert (activecnt >= -1); |
|
|
1555 | |
|
|
1556 | assert (fdchangemax >= fdchangecnt); |
|
|
1557 | for (i = 0; i < fdchangecnt; ++i) |
|
|
1558 | assert (("negative fd in fdchanges", fdchanges [i] >= 0)); |
|
|
1559 | |
|
|
1560 | assert (anfdmax >= 0); |
|
|
1561 | for (i = 0; i < anfdmax; ++i) |
|
|
1562 | for (w = anfds [i].head; w; w = w->next) |
|
|
1563 | { |
|
|
1564 | verify_watcher (EV_A_ (W)w); |
|
|
1565 | assert (("inactive fd watcher on anfd list", ev_active (w) == 1)); |
|
|
1566 | assert (("fd mismatch between watcher and anfd", ((ev_io *)w)->fd == i)); |
|
|
1567 | } |
|
|
1568 | |
|
|
1569 | assert (timermax >= timercnt); |
1530 | checkheap (timers, timercnt); |
1570 | verify_heap (EV_A_ timers, timercnt); |
|
|
1571 | |
1531 | #if EV_PERIODIC_ENABLE |
1572 | #if EV_PERIODIC_ENABLE |
|
|
1573 | assert (periodicmax >= periodiccnt); |
1532 | checkheap (periodics, periodiccnt); |
1574 | verify_heap (EV_A_ periodics, periodiccnt); |
1533 | #endif |
1575 | #endif |
1534 | |
1576 | |
|
|
1577 | for (i = NUMPRI; i--; ) |
|
|
1578 | { |
|
|
1579 | assert (pendingmax [i] >= pendingcnt [i]); |
1535 | #if EV_IDLE_ENABLE |
1580 | #if EV_IDLE_ENABLE |
1536 | for (i = NUMPRI; i--; ) |
1581 | assert (idleall >= 0); |
|
|
1582 | assert (idlemax [i] >= idlecnt [i]); |
1537 | array_check ((W **)idles [i], idlecnt [i]); |
1583 | array_verify (EV_A_ (W *)idles [i], idlecnt [i]); |
1538 | #endif |
1584 | #endif |
|
|
1585 | } |
|
|
1586 | |
1539 | #if EV_FORK_ENABLE |
1587 | #if EV_FORK_ENABLE |
|
|
1588 | assert (forkmax >= forkcnt); |
1540 | array_check ((W **)forks, forkcnt); |
1589 | array_verify (EV_A_ (W *)forks, forkcnt); |
1541 | #endif |
1590 | #endif |
1542 | array_check ((W **)prepares, preparecnt); |
1591 | |
1543 | array_check ((W **)checks, checkcnt); |
|
|
1544 | #if EV_ASYNC_ENABLE |
1592 | #if EV_ASYNC_ENABLE |
|
|
1593 | assert (asyncmax >= asynccnt); |
1545 | array_check ((W **)asyncs, asynccnt); |
1594 | array_verify (EV_A_ (W *)asyncs, asynccnt); |
|
|
1595 | #endif |
|
|
1596 | |
|
|
1597 | assert (preparemax >= preparecnt); |
|
|
1598 | array_verify (EV_A_ (W *)prepares, preparecnt); |
|
|
1599 | |
|
|
1600 | assert (checkmax >= checkcnt); |
|
|
1601 | array_verify (EV_A_ (W *)checks, checkcnt); |
|
|
1602 | |
|
|
1603 | # if 0 |
|
|
1604 | for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next) |
|
|
1605 | for (signum = signalmax; signum--; ) if (signals [signum].gotsig) |
1546 | #endif |
1606 | # endif |
1547 | } |
|
|
1548 | #endif |
1607 | #endif |
|
|
1608 | } |
1549 | |
1609 | |
1550 | #endif |
1610 | #endif /* multiplicity */ |
1551 | |
1611 | |
1552 | #if EV_MULTIPLICITY |
1612 | #if EV_MULTIPLICITY |
1553 | struct ev_loop * |
1613 | struct ev_loop * |
1554 | ev_default_loop_init (unsigned int flags) |
1614 | ev_default_loop_init (unsigned int flags) |
1555 | #else |
1615 | #else |
… | |
… | |
1620 | void inline_speed |
1680 | void inline_speed |
1621 | call_pending (EV_P) |
1681 | call_pending (EV_P) |
1622 | { |
1682 | { |
1623 | int pri; |
1683 | int pri; |
1624 | |
1684 | |
1625 | EV_FREQUENT_CHECK; |
|
|
1626 | |
|
|
1627 | for (pri = NUMPRI; pri--; ) |
1685 | for (pri = NUMPRI; pri--; ) |
1628 | while (pendingcnt [pri]) |
1686 | while (pendingcnt [pri]) |
1629 | { |
1687 | { |
1630 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
1688 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
1631 | |
1689 | |
… | |
… | |
1633 | { |
1691 | { |
1634 | /*assert (("non-pending watcher on pending list", p->w->pending));*/ |
1692 | /*assert (("non-pending watcher on pending list", p->w->pending));*/ |
1635 | |
1693 | |
1636 | p->w->pending = 0; |
1694 | p->w->pending = 0; |
1637 | EV_CB_INVOKE (p->w, p->events); |
1695 | EV_CB_INVOKE (p->w, p->events); |
|
|
1696 | EV_FREQUENT_CHECK; |
1638 | } |
1697 | } |
1639 | } |
1698 | } |
1640 | |
|
|
1641 | EV_FREQUENT_CHECK; |
|
|
1642 | } |
1699 | } |
1643 | |
1700 | |
1644 | #if EV_IDLE_ENABLE |
1701 | #if EV_IDLE_ENABLE |
1645 | void inline_size |
1702 | void inline_size |
1646 | idle_reify (EV_P) |
1703 | idle_reify (EV_P) |
… | |
… | |
1698 | #if EV_PERIODIC_ENABLE |
1755 | #if EV_PERIODIC_ENABLE |
1699 | void inline_size |
1756 | void inline_size |
1700 | periodics_reify (EV_P) |
1757 | periodics_reify (EV_P) |
1701 | { |
1758 | { |
1702 | EV_FREQUENT_CHECK; |
1759 | EV_FREQUENT_CHECK; |
|
|
1760 | |
1703 | while (periodiccnt && ANHE_at (periodics [HEAP0]) < ev_rt_now) |
1761 | while (periodiccnt && ANHE_at (periodics [HEAP0]) < ev_rt_now) |
1704 | { |
1762 | { |
1705 | ev_periodic *w = (ev_periodic *)ANHE_w (periodics [HEAP0]); |
1763 | ev_periodic *w = (ev_periodic *)ANHE_w (periodics [HEAP0]); |
1706 | |
1764 | |
1707 | /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/ |
1765 | /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/ |
… | |
… | |
1713 | |
1771 | |
1714 | assert (("ev_periodic reschedule callback returned time in the past", ev_at (w) >= ev_rt_now)); |
1772 | assert (("ev_periodic reschedule callback returned time in the past", ev_at (w) >= ev_rt_now)); |
1715 | |
1773 | |
1716 | ANHE_at_cache (periodics [HEAP0]); |
1774 | ANHE_at_cache (periodics [HEAP0]); |
1717 | downheap (periodics, periodiccnt, HEAP0); |
1775 | downheap (periodics, periodiccnt, HEAP0); |
1718 | EV_FREQUENT_CHECK; |
|
|
1719 | } |
1776 | } |
1720 | else if (w->interval) |
1777 | else if (w->interval) |
1721 | { |
1778 | { |
1722 | ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; |
1779 | 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 */ |
1780 | /* if next trigger time is not sufficiently in the future, put it there */ |
… | |
… | |
1859 | |
1916 | |
1860 | call_pending (EV_A); /* in case we recurse, ensure ordering stays nice and clean */ |
1917 | call_pending (EV_A); /* in case we recurse, ensure ordering stays nice and clean */ |
1861 | |
1918 | |
1862 | do |
1919 | do |
1863 | { |
1920 | { |
|
|
1921 | #if EV_VERIFY >= 2 |
|
|
1922 | ev_loop_verify (EV_A); |
|
|
1923 | #endif |
|
|
1924 | |
1864 | #ifndef _WIN32 |
1925 | #ifndef _WIN32 |
1865 | if (expect_false (curpid)) /* penalise the forking check even more */ |
1926 | if (expect_false (curpid)) /* penalise the forking check even more */ |
1866 | if (expect_false (getpid () != curpid)) |
1927 | if (expect_false (getpid () != curpid)) |
1867 | { |
1928 | { |
1868 | curpid = getpid (); |
1929 | curpid = getpid (); |
… | |
… | |
2506 | } |
2567 | } |
2507 | |
2568 | |
2508 | } |
2569 | } |
2509 | } |
2570 | } |
2510 | |
2571 | |
|
|
2572 | #endif |
|
|
2573 | |
|
|
2574 | #ifdef _WIN32 |
|
|
2575 | # define EV_LSTAT(p,b) _stati64 (p, b) |
|
|
2576 | #else |
|
|
2577 | # define EV_LSTAT(p,b) lstat (p, b) |
2511 | #endif |
2578 | #endif |
2512 | |
2579 | |
2513 | void |
2580 | void |
2514 | ev_stat_stat (EV_P_ ev_stat *w) |
2581 | ev_stat_stat (EV_P_ ev_stat *w) |
2515 | { |
2582 | { |