… | |
… | |
57 | # endif |
57 | # endif |
58 | # ifndef EV_USE_MONOTONIC |
58 | # ifndef EV_USE_MONOTONIC |
59 | # define EV_USE_MONOTONIC 1 |
59 | # define EV_USE_MONOTONIC 1 |
60 | # endif |
60 | # endif |
61 | # endif |
61 | # endif |
|
|
62 | # elif !defined(EV_USE_CLOCK_SYSCALL) |
|
|
63 | # define EV_USE_CLOCK_SYSCALL 0 |
62 | # endif |
64 | # endif |
63 | |
65 | |
64 | # if HAVE_CLOCK_GETTIME |
66 | # if HAVE_CLOCK_GETTIME |
65 | # ifndef EV_USE_MONOTONIC |
67 | # ifndef EV_USE_MONOTONIC |
66 | # define EV_USE_MONOTONIC 1 |
68 | # define EV_USE_MONOTONIC 1 |
… | |
… | |
282 | |
284 | |
283 | #ifndef EV_HEAP_CACHE_AT |
285 | #ifndef EV_HEAP_CACHE_AT |
284 | # define EV_HEAP_CACHE_AT !EV_MINIMAL |
286 | # define EV_HEAP_CACHE_AT !EV_MINIMAL |
285 | #endif |
287 | #endif |
286 | |
288 | |
|
|
289 | /* on linux, we can use a (slow) syscall to avoid a dependency on pthread, */ |
|
|
290 | /* which makes programs even slower. might work on other unices, too. */ |
|
|
291 | #if EV_USE_CLOCK_SYSCALL |
|
|
292 | # include <syscall.h> |
|
|
293 | # ifdef SYS_clock_gettime |
|
|
294 | # define clock_gettime(id, ts) syscall (SYS_clock_gettime, (id), (ts)) |
|
|
295 | # undef EV_USE_MONOTONIC |
|
|
296 | # define EV_USE_MONOTONIC 1 |
|
|
297 | # else |
|
|
298 | # undef EV_USE_CLOCK_SYSCALL |
|
|
299 | # define EV_USE_CLOCK_SYSCALL 0 |
|
|
300 | # endif |
|
|
301 | #endif |
|
|
302 | |
287 | /* this block fixes any misconfiguration where we know we run into trouble otherwise */ |
303 | /* this block fixes any misconfiguration where we know we run into trouble otherwise */ |
288 | |
304 | |
289 | #ifndef CLOCK_MONOTONIC |
305 | #ifndef CLOCK_MONOTONIC |
290 | # undef EV_USE_MONOTONIC |
306 | # undef EV_USE_MONOTONIC |
291 | # define EV_USE_MONOTONIC 0 |
307 | # define EV_USE_MONOTONIC 0 |
… | |
… | |
320 | |
336 | |
321 | #if EV_SELECT_IS_WINSOCKET |
337 | #if EV_SELECT_IS_WINSOCKET |
322 | # include <winsock.h> |
338 | # include <winsock.h> |
323 | #endif |
339 | #endif |
324 | |
340 | |
325 | /* on linux, we can use a (slow) syscall to avoid a dependency on pthread, */ |
|
|
326 | /* which makes programs even slower. might work on other unices, too. */ |
|
|
327 | #if EV_USE_CLOCK_SYSCALL |
|
|
328 | # include <syscall.h> |
|
|
329 | # define clock_gettime(id, ts) syscall (SYS_clock_gettime, (id), (ts)) |
|
|
330 | # undef EV_USE_MONOTONIC |
|
|
331 | # define EV_USE_MONOTONIC 1 |
|
|
332 | #endif |
|
|
333 | |
|
|
334 | #if EV_USE_EVENTFD |
341 | #if EV_USE_EVENTFD |
335 | /* our minimum requirement is glibc 2.7 which has the stub, but not the header */ |
342 | /* our minimum requirement is glibc 2.7 which has the stub, but not the header */ |
336 | # include <stdint.h> |
343 | # include <stdint.h> |
337 | # ifdef __cplusplus |
344 | # ifdef __cplusplus |
338 | extern "C" { |
345 | extern "C" { |
… | |
… | |
384 | # define inline_speed static noinline |
391 | # define inline_speed static noinline |
385 | #else |
392 | #else |
386 | # define inline_speed static inline |
393 | # define inline_speed static inline |
387 | #endif |
394 | #endif |
388 | |
395 | |
389 | #define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) |
396 | #define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) |
|
|
397 | |
|
|
398 | #if EV_MINPRI == EV_MAXPRI |
|
|
399 | # define ABSPRI(w) (((W)w), 0) |
|
|
400 | #else |
390 | #define ABSPRI(w) (((W)w)->priority - EV_MINPRI) |
401 | # define ABSPRI(w) (((W)w)->priority - EV_MINPRI) |
|
|
402 | #endif |
391 | |
403 | |
392 | #define EMPTY /* required for microsofts broken pseudo-c compiler */ |
404 | #define EMPTY /* required for microsofts broken pseudo-c compiler */ |
393 | #define EMPTY2(a,b) /* used to suppress some warnings */ |
405 | #define EMPTY2(a,b) /* used to suppress some warnings */ |
394 | |
406 | |
395 | typedef ev_watcher *W; |
407 | typedef ev_watcher *W; |
… | |
… | |
478 | #define ev_malloc(size) ev_realloc (0, (size)) |
490 | #define ev_malloc(size) ev_realloc (0, (size)) |
479 | #define ev_free(ptr) ev_realloc ((ptr), 0) |
491 | #define ev_free(ptr) ev_realloc ((ptr), 0) |
480 | |
492 | |
481 | /*****************************************************************************/ |
493 | /*****************************************************************************/ |
482 | |
494 | |
|
|
495 | /* set in reify when reification needed */ |
|
|
496 | #define EV_ANFD_REIFY 1 |
|
|
497 | |
483 | /* file descriptor info structure */ |
498 | /* file descriptor info structure */ |
484 | typedef struct |
499 | typedef struct |
485 | { |
500 | { |
486 | WL head; |
501 | WL head; |
487 | unsigned char events; /* the events watched for */ |
502 | unsigned char events; /* the events watched for */ |
488 | unsigned char reify; /* flag set when this ANFD needs reification */ |
503 | unsigned char reify; /* flag set when this ANFD needs reification (EV_ANFD_REIFY, EV__IOFDSET) */ |
489 | unsigned char emask; /* the epoll backend stores the actual kernel mask in here */ |
504 | unsigned char emask; /* the epoll backend stores the actual kernel mask in here */ |
490 | unsigned char unused; |
505 | unsigned char unused; |
491 | #if EV_USE_EPOLL |
506 | #if EV_USE_EPOLL |
492 | unsigned int egen; /* generation counter to counter epoll bugs */ |
507 | unsigned int egen; /* generation counter to counter epoll bugs */ |
493 | #endif |
508 | #endif |
… | |
… | |
555 | |
570 | |
556 | static int ev_default_loop_ptr; |
571 | static int ev_default_loop_ptr; |
557 | |
572 | |
558 | #endif |
573 | #endif |
559 | |
574 | |
|
|
575 | #if EV_MINIMAL < 2 |
|
|
576 | # define EV_RELEASE_CB if (expect_false (release_cb)) release_cb (EV_A) |
|
|
577 | # define EV_ACQUIRE_CB if (expect_false (acquire_cb)) acquire_cb (EV_A) |
|
|
578 | # define EV_INVOKE_PENDING invoke_cb (EV_A) |
|
|
579 | #else |
|
|
580 | # define EV_RELEASE_CB (void)0 |
|
|
581 | # define EV_ACQUIRE_CB (void)0 |
|
|
582 | # define EV_INVOKE_PENDING ev_invoke_pending (EV_A) |
|
|
583 | #endif |
|
|
584 | |
|
|
585 | #define EVUNLOOP_RECURSE 0x80 |
|
|
586 | |
560 | /*****************************************************************************/ |
587 | /*****************************************************************************/ |
561 | |
588 | |
|
|
589 | #ifndef EV_HAVE_EV_TIME |
562 | ev_tstamp |
590 | ev_tstamp |
563 | ev_time (void) |
591 | ev_time (void) |
564 | { |
592 | { |
565 | #if EV_USE_REALTIME |
593 | #if EV_USE_REALTIME |
566 | if (expect_true (have_realtime)) |
594 | if (expect_true (have_realtime)) |
… | |
… | |
573 | |
601 | |
574 | struct timeval tv; |
602 | struct timeval tv; |
575 | gettimeofday (&tv, 0); |
603 | gettimeofday (&tv, 0); |
576 | return tv.tv_sec + tv.tv_usec * 1e-6; |
604 | return tv.tv_sec + tv.tv_usec * 1e-6; |
577 | } |
605 | } |
|
|
606 | #endif |
578 | |
607 | |
579 | inline_size ev_tstamp |
608 | inline_size ev_tstamp |
580 | get_clock (void) |
609 | get_clock (void) |
581 | { |
610 | { |
582 | #if EV_USE_MONOTONIC |
611 | #if EV_USE_MONOTONIC |
… | |
… | |
618 | |
647 | |
619 | tv.tv_sec = (time_t)delay; |
648 | tv.tv_sec = (time_t)delay; |
620 | tv.tv_usec = (long)((delay - (ev_tstamp)(tv.tv_sec)) * 1e6); |
649 | tv.tv_usec = (long)((delay - (ev_tstamp)(tv.tv_sec)) * 1e6); |
621 | |
650 | |
622 | /* here we rely on sys/time.h + sys/types.h + unistd.h providing select */ |
651 | /* here we rely on sys/time.h + sys/types.h + unistd.h providing select */ |
623 | /* somehting nto guaranteed by newer posix versions, but guaranteed */ |
652 | /* somehting not guaranteed by newer posix versions, but guaranteed */ |
624 | /* by older ones */ |
653 | /* by older ones */ |
625 | select (0, 0, 0, 0, &tv); |
654 | select (0, 0, 0, 0, &tv); |
626 | #endif |
655 | #endif |
627 | } |
656 | } |
628 | } |
657 | } |
… | |
… | |
736 | } |
765 | } |
737 | |
766 | |
738 | /*****************************************************************************/ |
767 | /*****************************************************************************/ |
739 | |
768 | |
740 | inline_speed void |
769 | inline_speed void |
741 | fd_event (EV_P_ int fd, int revents) |
770 | fd_event_nc (EV_P_ int fd, int revents) |
742 | { |
771 | { |
743 | ANFD *anfd = anfds + fd; |
772 | ANFD *anfd = anfds + fd; |
744 | ev_io *w; |
773 | ev_io *w; |
745 | |
774 | |
746 | for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next) |
775 | for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next) |
… | |
… | |
750 | if (ev) |
779 | if (ev) |
751 | ev_feed_event (EV_A_ (W)w, ev); |
780 | ev_feed_event (EV_A_ (W)w, ev); |
752 | } |
781 | } |
753 | } |
782 | } |
754 | |
783 | |
|
|
784 | /* do not submit kernel events for fds that have reify set */ |
|
|
785 | /* because that means they changed while we were polling for new events */ |
|
|
786 | inline_speed void |
|
|
787 | fd_event (EV_P_ int fd, int revents) |
|
|
788 | { |
|
|
789 | ANFD *anfd = anfds + fd; |
|
|
790 | |
|
|
791 | if (expect_true (!anfd->reify)) |
|
|
792 | fd_event_nc (EV_A_ fd, revents); |
|
|
793 | } |
|
|
794 | |
755 | void |
795 | void |
756 | ev_feed_fd_event (EV_P_ int fd, int revents) |
796 | ev_feed_fd_event (EV_P_ int fd, int revents) |
757 | { |
797 | { |
758 | if (fd >= 0 && fd < anfdmax) |
798 | if (fd >= 0 && fd < anfdmax) |
759 | fd_event (EV_A_ fd, revents); |
799 | fd_event_nc (EV_A_ fd, revents); |
760 | } |
800 | } |
761 | |
801 | |
762 | /* make sure the external fd watch events are in-sync */ |
802 | /* make sure the external fd watch events are in-sync */ |
763 | /* with the kernel/libev internal state */ |
803 | /* with the kernel/libev internal state */ |
764 | inline_size void |
804 | inline_size void |
… | |
… | |
879 | for (fd = 0; fd < anfdmax; ++fd) |
919 | for (fd = 0; fd < anfdmax; ++fd) |
880 | if (anfds [fd].events) |
920 | if (anfds [fd].events) |
881 | { |
921 | { |
882 | anfds [fd].events = 0; |
922 | anfds [fd].events = 0; |
883 | anfds [fd].emask = 0; |
923 | anfds [fd].emask = 0; |
884 | fd_change (EV_A_ fd, EV__IOFDSET | 1); |
924 | fd_change (EV_A_ fd, EV__IOFDSET | EV_ANFD_REIFY); |
885 | } |
925 | } |
886 | } |
926 | } |
887 | |
927 | |
888 | /*****************************************************************************/ |
928 | /*****************************************************************************/ |
889 | |
929 | |
… | |
… | |
1343 | ev_backend (EV_P) |
1383 | ev_backend (EV_P) |
1344 | { |
1384 | { |
1345 | return backend; |
1385 | return backend; |
1346 | } |
1386 | } |
1347 | |
1387 | |
|
|
1388 | #if EV_MINIMAL < 2 |
1348 | unsigned int |
1389 | unsigned int |
1349 | ev_loop_count (EV_P) |
1390 | ev_loop_count (EV_P) |
1350 | { |
1391 | { |
1351 | return loop_count; |
1392 | return loop_count; |
1352 | } |
1393 | } |
1353 | |
1394 | |
|
|
1395 | unsigned int |
|
|
1396 | ev_loop_depth (EV_P) |
|
|
1397 | { |
|
|
1398 | return loop_depth; |
|
|
1399 | } |
|
|
1400 | |
1354 | void |
1401 | void |
1355 | ev_set_io_collect_interval (EV_P_ ev_tstamp interval) |
1402 | ev_set_io_collect_interval (EV_P_ ev_tstamp interval) |
1356 | { |
1403 | { |
1357 | io_blocktime = interval; |
1404 | io_blocktime = interval; |
1358 | } |
1405 | } |
… | |
… | |
1360 | void |
1407 | void |
1361 | ev_set_timeout_collect_interval (EV_P_ ev_tstamp interval) |
1408 | ev_set_timeout_collect_interval (EV_P_ ev_tstamp interval) |
1362 | { |
1409 | { |
1363 | timeout_blocktime = interval; |
1410 | timeout_blocktime = interval; |
1364 | } |
1411 | } |
|
|
1412 | |
|
|
1413 | void |
|
|
1414 | ev_set_userdata (EV_P_ void *data) |
|
|
1415 | { |
|
|
1416 | userdata = data; |
|
|
1417 | } |
|
|
1418 | |
|
|
1419 | void * |
|
|
1420 | ev_userdata (EV_P) |
|
|
1421 | { |
|
|
1422 | return userdata; |
|
|
1423 | } |
|
|
1424 | |
|
|
1425 | void ev_set_invoke_pending_cb (EV_P_ void (*invoke_pending_cb)(EV_P)) |
|
|
1426 | { |
|
|
1427 | invoke_cb = invoke_pending_cb; |
|
|
1428 | } |
|
|
1429 | |
|
|
1430 | void ev_set_loop_release_cb (EV_P_ void (*release)(EV_P), void (*acquire)(EV_P)) |
|
|
1431 | { |
|
|
1432 | release_cb = release; |
|
|
1433 | acquire_cb = acquire; |
|
|
1434 | } |
|
|
1435 | #endif |
1365 | |
1436 | |
1366 | /* initialise a loop structure, must be zero-initialised */ |
1437 | /* initialise a loop structure, must be zero-initialised */ |
1367 | static void noinline |
1438 | static void noinline |
1368 | loop_init (EV_P_ unsigned int flags) |
1439 | loop_init (EV_P_ unsigned int flags) |
1369 | { |
1440 | { |
… | |
… | |
1391 | |
1462 | |
1392 | ev_rt_now = ev_time (); |
1463 | ev_rt_now = ev_time (); |
1393 | mn_now = get_clock (); |
1464 | mn_now = get_clock (); |
1394 | now_floor = mn_now; |
1465 | now_floor = mn_now; |
1395 | rtmn_diff = ev_rt_now - mn_now; |
1466 | rtmn_diff = ev_rt_now - mn_now; |
|
|
1467 | #if EV_MINIMAL < 2 |
|
|
1468 | invoke_cb = ev_invoke_pending; |
|
|
1469 | #endif |
1396 | |
1470 | |
1397 | io_blocktime = 0.; |
1471 | io_blocktime = 0.; |
1398 | timeout_blocktime = 0.; |
1472 | timeout_blocktime = 0.; |
1399 | backend = 0; |
1473 | backend = 0; |
1400 | backend_fd = -1; |
1474 | backend_fd = -1; |
… | |
… | |
1594 | void |
1668 | void |
1595 | ev_loop_fork (EV_P) |
1669 | ev_loop_fork (EV_P) |
1596 | { |
1670 | { |
1597 | postfork = 1; /* must be in line with ev_default_fork */ |
1671 | postfork = 1; /* must be in line with ev_default_fork */ |
1598 | } |
1672 | } |
|
|
1673 | #endif /* multiplicity */ |
1599 | |
1674 | |
1600 | #if EV_VERIFY |
1675 | #if EV_VERIFY |
1601 | static void noinline |
1676 | static void noinline |
1602 | verify_watcher (EV_P_ W w) |
1677 | verify_watcher (EV_P_ W w) |
1603 | { |
1678 | { |
… | |
… | |
1631 | verify_watcher (EV_A_ ws [cnt]); |
1706 | verify_watcher (EV_A_ ws [cnt]); |
1632 | } |
1707 | } |
1633 | } |
1708 | } |
1634 | #endif |
1709 | #endif |
1635 | |
1710 | |
|
|
1711 | #if EV_MINIMAL < 2 |
1636 | void |
1712 | void |
1637 | ev_loop_verify (EV_P) |
1713 | ev_loop_verify (EV_P) |
1638 | { |
1714 | { |
1639 | #if EV_VERIFY |
1715 | #if EV_VERIFY |
1640 | int i; |
1716 | int i; |
… | |
… | |
1693 | for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next) |
1769 | for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next) |
1694 | for (signum = signalmax; signum--; ) if (signals [signum].gotsig) |
1770 | for (signum = signalmax; signum--; ) if (signals [signum].gotsig) |
1695 | # endif |
1771 | # endif |
1696 | #endif |
1772 | #endif |
1697 | } |
1773 | } |
1698 | |
1774 | #endif |
1699 | #endif /* multiplicity */ |
|
|
1700 | |
1775 | |
1701 | #if EV_MULTIPLICITY |
1776 | #if EV_MULTIPLICITY |
1702 | struct ev_loop * |
1777 | struct ev_loop * |
1703 | ev_default_loop_init (unsigned int flags) |
1778 | ev_default_loop_init (unsigned int flags) |
1704 | #else |
1779 | #else |
… | |
… | |
1765 | ev_invoke (EV_P_ void *w, int revents) |
1840 | ev_invoke (EV_P_ void *w, int revents) |
1766 | { |
1841 | { |
1767 | EV_CB_INVOKE ((W)w, revents); |
1842 | EV_CB_INVOKE ((W)w, revents); |
1768 | } |
1843 | } |
1769 | |
1844 | |
1770 | inline_speed void |
1845 | unsigned int |
1771 | call_pending (EV_P) |
1846 | ev_pending_count (EV_P) |
|
|
1847 | { |
|
|
1848 | int pri; |
|
|
1849 | unsigned int count = 0; |
|
|
1850 | |
|
|
1851 | for (pri = NUMPRI; pri--; ) |
|
|
1852 | count += pendingcnt [pri]; |
|
|
1853 | |
|
|
1854 | return count; |
|
|
1855 | } |
|
|
1856 | |
|
|
1857 | void noinline |
|
|
1858 | ev_invoke_pending (EV_P) |
1772 | { |
1859 | { |
1773 | int pri; |
1860 | int pri; |
1774 | |
1861 | |
1775 | for (pri = NUMPRI; pri--; ) |
1862 | for (pri = NUMPRI; pri--; ) |
1776 | while (pendingcnt [pri]) |
1863 | while (pendingcnt [pri]) |
… | |
… | |
1948 | /* fetch new monotonic and realtime times from the kernel */ |
2035 | /* fetch new monotonic and realtime times from the kernel */ |
1949 | /* also detetc if there was a timejump, and act accordingly */ |
2036 | /* also detetc if there was a timejump, and act accordingly */ |
1950 | inline_speed void |
2037 | inline_speed void |
1951 | time_update (EV_P_ ev_tstamp max_block) |
2038 | time_update (EV_P_ ev_tstamp max_block) |
1952 | { |
2039 | { |
1953 | int i; |
|
|
1954 | |
|
|
1955 | #if EV_USE_MONOTONIC |
2040 | #if EV_USE_MONOTONIC |
1956 | if (expect_true (have_monotonic)) |
2041 | if (expect_true (have_monotonic)) |
1957 | { |
2042 | { |
|
|
2043 | int i; |
1958 | ev_tstamp odiff = rtmn_diff; |
2044 | ev_tstamp odiff = rtmn_diff; |
1959 | |
2045 | |
1960 | mn_now = get_clock (); |
2046 | mn_now = get_clock (); |
1961 | |
2047 | |
1962 | /* only fetch the realtime clock every 0.5*MIN_TIMEJUMP seconds */ |
2048 | /* only fetch the realtime clock every 0.5*MIN_TIMEJUMP seconds */ |
… | |
… | |
2012 | |
2098 | |
2013 | mn_now = ev_rt_now; |
2099 | mn_now = ev_rt_now; |
2014 | } |
2100 | } |
2015 | } |
2101 | } |
2016 | |
2102 | |
2017 | static int loop_done; |
|
|
2018 | |
|
|
2019 | void |
2103 | void |
2020 | ev_loop (EV_P_ int flags) |
2104 | ev_loop (EV_P_ int flags) |
2021 | { |
2105 | { |
|
|
2106 | #if EV_MINIMAL < 2 |
|
|
2107 | ++loop_depth; |
|
|
2108 | #endif |
|
|
2109 | |
|
|
2110 | assert (("libev: ev_loop recursion during release detected", loop_done != EVUNLOOP_RECURSE)); |
|
|
2111 | |
2022 | loop_done = EVUNLOOP_CANCEL; |
2112 | loop_done = EVUNLOOP_CANCEL; |
2023 | |
2113 | |
2024 | call_pending (EV_A); /* in case we recurse, ensure ordering stays nice and clean */ |
2114 | EV_INVOKE_PENDING; /* in case we recurse, ensure ordering stays nice and clean */ |
2025 | |
2115 | |
2026 | do |
2116 | do |
2027 | { |
2117 | { |
2028 | #if EV_VERIFY >= 2 |
2118 | #if EV_VERIFY >= 2 |
2029 | ev_loop_verify (EV_A); |
2119 | ev_loop_verify (EV_A); |
… | |
… | |
2042 | /* we might have forked, so queue fork handlers */ |
2132 | /* we might have forked, so queue fork handlers */ |
2043 | if (expect_false (postfork)) |
2133 | if (expect_false (postfork)) |
2044 | if (forkcnt) |
2134 | if (forkcnt) |
2045 | { |
2135 | { |
2046 | queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK); |
2136 | queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK); |
2047 | call_pending (EV_A); |
2137 | EV_INVOKE_PENDING; |
2048 | } |
2138 | } |
2049 | #endif |
2139 | #endif |
2050 | |
2140 | |
2051 | /* queue prepare watchers (and execute them) */ |
2141 | /* queue prepare watchers (and execute them) */ |
2052 | if (expect_false (preparecnt)) |
2142 | if (expect_false (preparecnt)) |
2053 | { |
2143 | { |
2054 | queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); |
2144 | queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); |
2055 | call_pending (EV_A); |
2145 | EV_INVOKE_PENDING; |
2056 | } |
2146 | } |
|
|
2147 | |
|
|
2148 | if (expect_false (loop_done)) |
|
|
2149 | break; |
2057 | |
2150 | |
2058 | /* we might have forked, so reify kernel state if necessary */ |
2151 | /* we might have forked, so reify kernel state if necessary */ |
2059 | if (expect_false (postfork)) |
2152 | if (expect_false (postfork)) |
2060 | loop_fork (EV_A); |
2153 | loop_fork (EV_A); |
2061 | |
2154 | |
… | |
… | |
2067 | ev_tstamp waittime = 0.; |
2160 | ev_tstamp waittime = 0.; |
2068 | ev_tstamp sleeptime = 0.; |
2161 | ev_tstamp sleeptime = 0.; |
2069 | |
2162 | |
2070 | if (expect_true (!(flags & EVLOOP_NONBLOCK || idleall || !activecnt))) |
2163 | if (expect_true (!(flags & EVLOOP_NONBLOCK || idleall || !activecnt))) |
2071 | { |
2164 | { |
|
|
2165 | /* remember old timestamp for io_blocktime calculation */ |
|
|
2166 | ev_tstamp prev_mn_now = mn_now; |
|
|
2167 | |
2072 | /* update time to cancel out callback processing overhead */ |
2168 | /* update time to cancel out callback processing overhead */ |
2073 | time_update (EV_A_ 1e100); |
2169 | time_update (EV_A_ 1e100); |
2074 | |
2170 | |
2075 | waittime = MAX_BLOCKTIME; |
2171 | waittime = MAX_BLOCKTIME; |
2076 | |
2172 | |
… | |
… | |
2086 | ev_tstamp to = ANHE_at (periodics [HEAP0]) - ev_rt_now + backend_fudge; |
2182 | ev_tstamp to = ANHE_at (periodics [HEAP0]) - ev_rt_now + backend_fudge; |
2087 | if (waittime > to) waittime = to; |
2183 | if (waittime > to) waittime = to; |
2088 | } |
2184 | } |
2089 | #endif |
2185 | #endif |
2090 | |
2186 | |
|
|
2187 | /* don't let timeouts decrease the waittime below timeout_blocktime */ |
2091 | if (expect_false (waittime < timeout_blocktime)) |
2188 | if (expect_false (waittime < timeout_blocktime)) |
2092 | waittime = timeout_blocktime; |
2189 | waittime = timeout_blocktime; |
2093 | |
2190 | |
2094 | sleeptime = waittime - backend_fudge; |
2191 | /* extra check because io_blocktime is commonly 0 */ |
2095 | |
|
|
2096 | if (expect_true (sleeptime > io_blocktime)) |
2192 | if (expect_false (io_blocktime)) |
2097 | sleeptime = io_blocktime; |
|
|
2098 | |
|
|
2099 | if (sleeptime) |
|
|
2100 | { |
2193 | { |
|
|
2194 | sleeptime = io_blocktime - (mn_now - prev_mn_now); |
|
|
2195 | |
|
|
2196 | if (sleeptime > waittime - backend_fudge) |
|
|
2197 | sleeptime = waittime - backend_fudge; |
|
|
2198 | |
|
|
2199 | if (expect_true (sleeptime > 0.)) |
|
|
2200 | { |
2101 | ev_sleep (sleeptime); |
2201 | ev_sleep (sleeptime); |
2102 | waittime -= sleeptime; |
2202 | waittime -= sleeptime; |
|
|
2203 | } |
2103 | } |
2204 | } |
2104 | } |
2205 | } |
2105 | |
2206 | |
|
|
2207 | #if EV_MINIMAL < 2 |
2106 | ++loop_count; |
2208 | ++loop_count; |
|
|
2209 | #endif |
|
|
2210 | assert ((loop_done = EVUNLOOP_RECURSE, 1)); /* assert for side effect */ |
2107 | backend_poll (EV_A_ waittime); |
2211 | backend_poll (EV_A_ waittime); |
|
|
2212 | assert ((loop_done = EVUNLOOP_CANCEL, 1)); /* assert for side effect */ |
2108 | |
2213 | |
2109 | /* update ev_rt_now, do magic */ |
2214 | /* update ev_rt_now, do magic */ |
2110 | time_update (EV_A_ waittime + sleeptime); |
2215 | time_update (EV_A_ waittime + sleeptime); |
2111 | } |
2216 | } |
2112 | |
2217 | |
… | |
… | |
2123 | |
2228 | |
2124 | /* queue check watchers, to be executed first */ |
2229 | /* queue check watchers, to be executed first */ |
2125 | if (expect_false (checkcnt)) |
2230 | if (expect_false (checkcnt)) |
2126 | queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); |
2231 | queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); |
2127 | |
2232 | |
2128 | call_pending (EV_A); |
2233 | EV_INVOKE_PENDING; |
2129 | } |
2234 | } |
2130 | while (expect_true ( |
2235 | while (expect_true ( |
2131 | activecnt |
2236 | activecnt |
2132 | && !loop_done |
2237 | && !loop_done |
2133 | && !(flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK)) |
2238 | && !(flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK)) |
2134 | )); |
2239 | )); |
2135 | |
2240 | |
2136 | if (loop_done == EVUNLOOP_ONE) |
2241 | if (loop_done == EVUNLOOP_ONE) |
2137 | loop_done = EVUNLOOP_CANCEL; |
2242 | loop_done = EVUNLOOP_CANCEL; |
|
|
2243 | |
|
|
2244 | #if EV_MINIMAL < 2 |
|
|
2245 | --loop_depth; |
|
|
2246 | #endif |
2138 | } |
2247 | } |
2139 | |
2248 | |
2140 | void |
2249 | void |
2141 | ev_unloop (EV_P_ int how) |
2250 | ev_unloop (EV_P_ int how) |
2142 | { |
2251 | { |
… | |
… | |
2234 | } |
2343 | } |
2235 | |
2344 | |
2236 | inline_size void |
2345 | inline_size void |
2237 | pri_adjust (EV_P_ W w) |
2346 | pri_adjust (EV_P_ W w) |
2238 | { |
2347 | { |
2239 | int pri = w->priority; |
2348 | int pri = ev_priority (w); |
2240 | pri = pri < EV_MINPRI ? EV_MINPRI : pri; |
2349 | pri = pri < EV_MINPRI ? EV_MINPRI : pri; |
2241 | pri = pri > EV_MAXPRI ? EV_MAXPRI : pri; |
2350 | pri = pri > EV_MAXPRI ? EV_MAXPRI : pri; |
2242 | w->priority = pri; |
2351 | ev_set_priority (w, pri); |
2243 | } |
2352 | } |
2244 | |
2353 | |
2245 | inline_speed void |
2354 | inline_speed void |
2246 | ev_start (EV_P_ W w, int active) |
2355 | ev_start (EV_P_ W w, int active) |
2247 | { |
2356 | { |
… | |
… | |
2274 | |
2383 | |
2275 | ev_start (EV_A_ (W)w, 1); |
2384 | ev_start (EV_A_ (W)w, 1); |
2276 | array_needsize (ANFD, anfds, anfdmax, fd + 1, array_init_zero); |
2385 | array_needsize (ANFD, anfds, anfdmax, fd + 1, array_init_zero); |
2277 | wlist_add (&anfds[fd].head, (WL)w); |
2386 | wlist_add (&anfds[fd].head, (WL)w); |
2278 | |
2387 | |
2279 | fd_change (EV_A_ fd, w->events & EV__IOFDSET | 1); |
2388 | fd_change (EV_A_ fd, w->events & EV__IOFDSET | EV_ANFD_REIFY); |
2280 | w->events &= ~EV__IOFDSET; |
2389 | w->events &= ~EV__IOFDSET; |
2281 | |
2390 | |
2282 | EV_FREQUENT_CHECK; |
2391 | EV_FREQUENT_CHECK; |
2283 | } |
2392 | } |
2284 | |
2393 | |
… | |
… | |
2488 | if (!((WL)w)->next) |
2597 | if (!((WL)w)->next) |
2489 | { |
2598 | { |
2490 | #if _WIN32 |
2599 | #if _WIN32 |
2491 | signal (w->signum, ev_sighandler); |
2600 | signal (w->signum, ev_sighandler); |
2492 | #else |
2601 | #else |
2493 | struct sigaction sa; |
2602 | struct sigaction sa = { }; |
2494 | sa.sa_handler = ev_sighandler; |
2603 | sa.sa_handler = ev_sighandler; |
2495 | sigfillset (&sa.sa_mask); |
2604 | sigfillset (&sa.sa_mask); |
2496 | sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ |
2605 | sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ |
2497 | sigaction (w->signum, &sa, 0); |
2606 | sigaction (w->signum, &sa, 0); |
2498 | #endif |
2607 | #endif |