… | |
… | |
124 | # else |
124 | # else |
125 | # undef EV_USE_LINUXAIO |
125 | # undef EV_USE_LINUXAIO |
126 | # define EV_USE_LINUXAIO 0 |
126 | # define EV_USE_LINUXAIO 0 |
127 | # endif |
127 | # endif |
128 | |
128 | |
|
|
129 | # if HAVE_LINUX_FS_H && HAVE_SYS_TIMERFD_H && HAVE_KERNEL_RWF_T |
|
|
130 | # ifndef EV_USE_IOURING |
|
|
131 | # define EV_USE_IOURING EV_FEATURE_BACKENDS |
|
|
132 | # endif |
|
|
133 | # else |
|
|
134 | # undef EV_USE_IOURING |
|
|
135 | # define EV_USE_IOURING 0 |
|
|
136 | # endif |
|
|
137 | |
129 | # if HAVE_KQUEUE && HAVE_SYS_EVENT_H |
138 | # if HAVE_KQUEUE && HAVE_SYS_EVENT_H |
130 | # ifndef EV_USE_KQUEUE |
139 | # ifndef EV_USE_KQUEUE |
131 | # define EV_USE_KQUEUE EV_FEATURE_BACKENDS |
140 | # define EV_USE_KQUEUE EV_FEATURE_BACKENDS |
132 | # endif |
141 | # endif |
133 | # else |
142 | # else |
… | |
… | |
168 | # endif |
177 | # endif |
169 | # else |
178 | # else |
170 | # undef EV_USE_EVENTFD |
179 | # undef EV_USE_EVENTFD |
171 | # define EV_USE_EVENTFD 0 |
180 | # define EV_USE_EVENTFD 0 |
172 | # endif |
181 | # endif |
173 | |
182 | |
174 | #endif |
183 | #endif |
175 | |
184 | |
176 | /* OS X, in its infinite idiocy, actually HARDCODES |
185 | /* OS X, in its infinite idiocy, actually HARDCODES |
177 | * a limit of 1024 into their select. Where people have brains, |
186 | * a limit of 1024 into their select. Where people have brains, |
178 | * OS X engineers apparently have a vacuum. Or maybe they were |
187 | * OS X engineers apparently have a vacuum. Or maybe they were |
… | |
… | |
332 | # else |
341 | # else |
333 | # define EV_USE_LINUXAIO 0 |
342 | # define EV_USE_LINUXAIO 0 |
334 | # endif |
343 | # endif |
335 | #endif |
344 | #endif |
336 | |
345 | |
|
|
346 | #ifndef EV_USE_IOURING |
|
|
347 | # if __linux /* later checks might disable again */ |
|
|
348 | # define EV_USE_IOURING 1 |
|
|
349 | # else |
|
|
350 | # define EV_USE_IOURING 0 |
|
|
351 | # endif |
|
|
352 | #endif |
|
|
353 | |
337 | #ifndef EV_USE_INOTIFY |
354 | #ifndef EV_USE_INOTIFY |
338 | # if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 4)) |
355 | # if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 4)) |
339 | # define EV_USE_INOTIFY EV_FEATURE_OS |
356 | # define EV_USE_INOTIFY EV_FEATURE_OS |
340 | # else |
357 | # else |
341 | # define EV_USE_INOTIFY 0 |
358 | # define EV_USE_INOTIFY 0 |
… | |
… | |
406 | # include <sys/syscall.h> |
423 | # include <sys/syscall.h> |
407 | # ifdef SYS_clock_gettime |
424 | # ifdef SYS_clock_gettime |
408 | # define clock_gettime(id, ts) syscall (SYS_clock_gettime, (id), (ts)) |
425 | # define clock_gettime(id, ts) syscall (SYS_clock_gettime, (id), (ts)) |
409 | # undef EV_USE_MONOTONIC |
426 | # undef EV_USE_MONOTONIC |
410 | # define EV_USE_MONOTONIC 1 |
427 | # define EV_USE_MONOTONIC 1 |
|
|
428 | # define EV_NEED_SYSCALL 1 |
411 | # else |
429 | # else |
412 | # undef EV_USE_CLOCK_SYSCALL |
430 | # undef EV_USE_CLOCK_SYSCALL |
413 | # define EV_USE_CLOCK_SYSCALL 0 |
431 | # define EV_USE_CLOCK_SYSCALL 0 |
414 | # endif |
432 | # endif |
415 | #endif |
433 | #endif |
… | |
… | |
427 | #endif |
445 | #endif |
428 | |
446 | |
429 | #if !EV_STAT_ENABLE |
447 | #if !EV_STAT_ENABLE |
430 | # undef EV_USE_INOTIFY |
448 | # undef EV_USE_INOTIFY |
431 | # define EV_USE_INOTIFY 0 |
449 | # define EV_USE_INOTIFY 0 |
|
|
450 | #endif |
|
|
451 | |
|
|
452 | #if __linux && EV_USE_IOURING |
|
|
453 | # include <linux/version.h> |
|
|
454 | # if LINUX_VERSION_CODE < KERNEL_VERSION(4,14,0) |
|
|
455 | # undef EV_USE_IOURING |
|
|
456 | # define EV_USE_IOURING 0 |
|
|
457 | # endif |
432 | #endif |
458 | #endif |
433 | |
459 | |
434 | #if !EV_USE_NANOSLEEP |
460 | #if !EV_USE_NANOSLEEP |
435 | /* hp-ux has it in sys/time.h, which we unconditionally include above */ |
461 | /* hp-ux has it in sys/time.h, which we unconditionally include above */ |
436 | # if !defined _WIN32 && !defined __hpux |
462 | # if !defined _WIN32 && !defined __hpux |
… | |
… | |
438 | # endif |
464 | # endif |
439 | #endif |
465 | #endif |
440 | |
466 | |
441 | #if EV_USE_LINUXAIO |
467 | #if EV_USE_LINUXAIO |
442 | # include <sys/syscall.h> |
468 | # include <sys/syscall.h> |
443 | # if !SYS_io_getevents || !EV_USE_EPOLL /* ev_linxaio uses ev_poll.c:ev_epoll_create */ |
469 | # if SYS_io_getevents && EV_USE_EPOLL /* linuxaio backend requires epoll backend */ |
|
|
470 | # define EV_NEED_SYSCALL 1 |
|
|
471 | # else |
444 | # undef EV_USE_LINUXAIO |
472 | # undef EV_USE_LINUXAIO |
445 | # define EV_USE_LINUXAIO 0 |
473 | # define EV_USE_LINUXAIO 0 |
|
|
474 | # endif |
|
|
475 | #endif |
|
|
476 | |
|
|
477 | #if EV_USE_IOURING |
|
|
478 | # include <sys/syscall.h> |
|
|
479 | # if !SYS_io_uring_setup && __linux && !__alpha |
|
|
480 | # define SYS_io_uring_setup 425 |
|
|
481 | # define SYS_io_uring_enter 426 |
|
|
482 | # define SYS_io_uring_wregister 427 |
|
|
483 | # endif |
|
|
484 | # if SYS_io_uring_setup && EV_USE_EPOLL /* iouring backend requires epoll backend */ |
|
|
485 | # define EV_NEED_SYSCALL 1 |
|
|
486 | # else |
|
|
487 | # undef EV_USE_IOURING |
|
|
488 | # define EV_USE_IOURING 0 |
446 | # endif |
489 | # endif |
447 | #endif |
490 | #endif |
448 | |
491 | |
449 | #if EV_USE_INOTIFY |
492 | #if EV_USE_INOTIFY |
450 | # include <sys/statfs.h> |
493 | # include <sys/statfs.h> |
… | |
… | |
492 | uint32_t ssi_signo; |
535 | uint32_t ssi_signo; |
493 | char pad[128 - sizeof (uint32_t)]; |
536 | char pad[128 - sizeof (uint32_t)]; |
494 | }; |
537 | }; |
495 | #endif |
538 | #endif |
496 | |
539 | |
497 | /**/ |
540 | /*****************************************************************************/ |
498 | |
541 | |
499 | #if EV_VERIFY >= 3 |
542 | #if EV_VERIFY >= 3 |
500 | # define EV_FREQUENT_CHECK ev_verify (EV_A) |
543 | # define EV_FREQUENT_CHECK ev_verify (EV_A) |
501 | #else |
544 | #else |
502 | # define EV_FREQUENT_CHECK do { } while (0) |
545 | # define EV_FREQUENT_CHECK do { } while (0) |
… | |
… | |
507 | * This value is good at least till the year 4000. |
550 | * This value is good at least till the year 4000. |
508 | */ |
551 | */ |
509 | #define MIN_INTERVAL 0.0001220703125 /* 1/2**13, good till 4000 */ |
552 | #define MIN_INTERVAL 0.0001220703125 /* 1/2**13, good till 4000 */ |
510 | /*#define MIN_INTERVAL 0.00000095367431640625 /* 1/2**20, good till 2200 */ |
553 | /*#define MIN_INTERVAL 0.00000095367431640625 /* 1/2**20, good till 2200 */ |
511 | |
554 | |
512 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
555 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
513 | #define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */ |
556 | #define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */ |
514 | |
557 | |
|
|
558 | /* find a portable timestamp that is "always" in the future but fits into time_t. |
|
|
559 | * this is quite hard, and we are mostly guessing - we handle 32 bit signed/unsigned time_t, |
|
|
560 | * and sizes larger than 32 bit, and maybe the unlikely floating point time_t */ |
|
|
561 | #define EV_TSTAMP_HUGE \ |
|
|
562 | (sizeof (time_t) >= 8 ? 10000000000000. \ |
|
|
563 | : 0 < (time_t)4294967295 ? 4294967295. \ |
|
|
564 | : 2147483647.) \ |
|
|
565 | |
|
|
566 | #ifndef EV_TS_CONST |
|
|
567 | # define EV_TS_CONST(nv) nv |
|
|
568 | # define EV_TS_TO_MSEC(a) a * 1e3 + 0.9999 |
|
|
569 | # define EV_TS_FROM_USEC(us) us * 1e-6 |
515 | #define EV_TV_SET(tv,t) do { tv.tv_sec = (long)t; tv.tv_usec = (long)((t - tv.tv_sec) * 1e6); } while (0) |
570 | # define EV_TV_SET(tv,t) do { tv.tv_sec = (long)t; tv.tv_usec = (long)((t - tv.tv_sec) * 1e6); } while (0) |
516 | #define EV_TS_SET(ts,t) do { ts.tv_sec = (long)t; ts.tv_nsec = (long)((t - ts.tv_sec) * 1e9); } while (0) |
571 | # define EV_TS_SET(ts,t) do { ts.tv_sec = (long)t; ts.tv_nsec = (long)((t - ts.tv_sec) * 1e9); } while (0) |
|
|
572 | # define EV_TV_GET(tv) ((tv).tv_sec + (tv).tv_usec * 1e-6) |
|
|
573 | # define EV_TS_GET(ts) ((ts).tv_sec + (ts).tv_nsec * 1e-9) |
|
|
574 | #endif |
517 | |
575 | |
518 | /* the following is ecb.h embedded into libev - use update_ev_c to update from an external copy */ |
576 | /* the following is ecb.h embedded into libev - use update_ev_c to update from an external copy */ |
519 | /* ECB.H BEGIN */ |
577 | /* ECB.H BEGIN */ |
520 | /* |
578 | /* |
521 | * libecb - http://software.schmorp.de/pkg/libecb |
579 | * libecb - http://software.schmorp.de/pkg/libecb |
… | |
… | |
1558 | # define inline_speed ecb_inline |
1616 | # define inline_speed ecb_inline |
1559 | #else |
1617 | #else |
1560 | # define inline_speed ecb_noinline static |
1618 | # define inline_speed ecb_noinline static |
1561 | #endif |
1619 | #endif |
1562 | |
1620 | |
|
|
1621 | /*****************************************************************************/ |
|
|
1622 | /* raw syscall wrappers */ |
|
|
1623 | |
|
|
1624 | #if EV_NEED_SYSCALL |
|
|
1625 | |
|
|
1626 | #include <sys/syscall.h> |
|
|
1627 | |
|
|
1628 | /* |
|
|
1629 | * define some syscall wrappers for common architectures |
|
|
1630 | * this is mostly for nice looks during debugging, not performance. |
|
|
1631 | * our syscalls return < 0, not == -1, on error. which is good |
|
|
1632 | * enough for linux aio. |
|
|
1633 | * TODO: arm is also common nowadays, maybe even mips and x86 |
|
|
1634 | * TODO: after implementing this, it suddenly looks like overkill, but its hard to remove... |
|
|
1635 | */ |
|
|
1636 | #if __GNUC__ && __linux && ECB_AMD64 && !defined __OPTIMIZE_SIZE__ |
|
|
1637 | /* the costly errno access probably kills this for size optimisation */ |
|
|
1638 | |
|
|
1639 | #define ev_syscall(nr,narg,arg1,arg2,arg3,arg4,arg5,arg6) \ |
|
|
1640 | ({ \ |
|
|
1641 | long res; \ |
|
|
1642 | register unsigned long r6 __asm__ ("r9" ); \ |
|
|
1643 | register unsigned long r5 __asm__ ("r8" ); \ |
|
|
1644 | register unsigned long r4 __asm__ ("r10"); \ |
|
|
1645 | register unsigned long r3 __asm__ ("rdx"); \ |
|
|
1646 | register unsigned long r2 __asm__ ("rsi"); \ |
|
|
1647 | register unsigned long r1 __asm__ ("rdi"); \ |
|
|
1648 | if (narg >= 6) r6 = (unsigned long)(arg6); \ |
|
|
1649 | if (narg >= 5) r5 = (unsigned long)(arg5); \ |
|
|
1650 | if (narg >= 4) r4 = (unsigned long)(arg4); \ |
|
|
1651 | if (narg >= 3) r3 = (unsigned long)(arg3); \ |
|
|
1652 | if (narg >= 2) r2 = (unsigned long)(arg2); \ |
|
|
1653 | if (narg >= 1) r1 = (unsigned long)(arg1); \ |
|
|
1654 | __asm__ __volatile__ ( \ |
|
|
1655 | "syscall\n\t" \ |
|
|
1656 | : "=a" (res) \ |
|
|
1657 | : "0" (nr), "r" (r1), "r" (r2), "r" (r3), "r" (r4), "r" (r5) \ |
|
|
1658 | : "cc", "r11", "cx", "memory"); \ |
|
|
1659 | errno = -res; \ |
|
|
1660 | res; \ |
|
|
1661 | }) |
|
|
1662 | |
|
|
1663 | #endif |
|
|
1664 | |
|
|
1665 | #ifdef ev_syscall |
|
|
1666 | #define ev_syscall0(nr) ev_syscall (nr, 0, 0, 0, 0, 0, 0, 0) |
|
|
1667 | #define ev_syscall1(nr,arg1) ev_syscall (nr, 1, arg1, 0, 0, 0, 0, 0) |
|
|
1668 | #define ev_syscall2(nr,arg1,arg2) ev_syscall (nr, 2, arg1, arg2, 0, 0, 0, 0) |
|
|
1669 | #define ev_syscall3(nr,arg1,arg2,arg3) ev_syscall (nr, 3, arg1, arg2, arg3, 0, 0, 0) |
|
|
1670 | #define ev_syscall4(nr,arg1,arg2,arg3,arg4) ev_syscall (nr, 3, arg1, arg2, arg3, arg4, 0, 0) |
|
|
1671 | #define ev_syscall5(nr,arg1,arg2,arg3,arg4,arg5) ev_syscall (nr, 5, arg1, arg2, arg3, arg4, arg5, 0) |
|
|
1672 | #define ev_syscall6(nr,arg1,arg2,arg3,arg4,arg5,arg6) ev_syscall (nr, 6, arg1, arg2, arg3, arg4, arg5,arg6) |
|
|
1673 | #else |
|
|
1674 | #define ev_syscall0(nr) syscall (nr) |
|
|
1675 | #define ev_syscall1(nr,arg1) syscall (nr, arg1) |
|
|
1676 | #define ev_syscall2(nr,arg1,arg2) syscall (nr, arg1, arg2) |
|
|
1677 | #define ev_syscall3(nr,arg1,arg2,arg3) syscall (nr, arg1, arg2, arg3) |
|
|
1678 | #define ev_syscall4(nr,arg1,arg2,arg3,arg4) syscall (nr, arg1, arg2, arg3, arg4) |
|
|
1679 | #define ev_syscall5(nr,arg1,arg2,arg3,arg4,arg5) syscall (nr, arg1, arg2, arg3, arg4, arg5) |
|
|
1680 | #define ev_syscall6(nr,arg1,arg2,arg3,arg4,arg5,arg6) syscall (nr, arg1, arg2, arg3, arg4, arg5,arg6) |
|
|
1681 | #endif |
|
|
1682 | |
|
|
1683 | #endif |
|
|
1684 | |
|
|
1685 | /*****************************************************************************/ |
|
|
1686 | |
1563 | #define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) |
1687 | #define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) |
1564 | |
1688 | |
1565 | #if EV_MINPRI == EV_MAXPRI |
1689 | #if EV_MINPRI == EV_MAXPRI |
1566 | # define ABSPRI(w) (((W)w), 0) |
1690 | # define ABSPRI(w) (((W)w), 0) |
1567 | #else |
1691 | #else |
… | |
… | |
1626 | const ev_tstamp shift = sizeof (unsigned long) >= 8 ? 10000000000000000000. : 1000000000.; |
1750 | const ev_tstamp shift = sizeof (unsigned long) >= 8 ? 10000000000000000000. : 1000000000.; |
1627 | #else |
1751 | #else |
1628 | const ev_tstamp shift = sizeof (unsigned long) >= 8 ? 18446744073709551616. : 4294967296.; |
1752 | const ev_tstamp shift = sizeof (unsigned long) >= 8 ? 18446744073709551616. : 4294967296.; |
1629 | #endif |
1753 | #endif |
1630 | |
1754 | |
|
|
1755 | /* special treatment for negative arguments */ |
|
|
1756 | if (ecb_expect_false (v < 0.)) |
|
|
1757 | { |
|
|
1758 | ev_tstamp f = -ev_floor (-v); |
|
|
1759 | |
|
|
1760 | return f - (f == v ? 0 : 1); |
|
|
1761 | } |
|
|
1762 | |
1631 | /* argument too large for an unsigned long? */ |
1763 | /* argument too large for an unsigned long? then reduce it */ |
1632 | if (ecb_expect_false (v >= shift)) |
1764 | if (ecb_expect_false (v >= shift)) |
1633 | { |
1765 | { |
1634 | ev_tstamp f; |
1766 | ev_tstamp f; |
1635 | |
1767 | |
1636 | if (v == v - 1.) |
1768 | if (v == v - 1.) |
1637 | return v; /* very large number */ |
1769 | return v; /* very large numbers are assumed to be integer */ |
1638 | |
1770 | |
1639 | f = shift * ev_floor (v * (1. / shift)); |
1771 | f = shift * ev_floor (v * (1. / shift)); |
1640 | return f + ev_floor (v - f); |
1772 | return f + ev_floor (v - f); |
1641 | } |
|
|
1642 | |
|
|
1643 | /* special treatment for negative args? */ |
|
|
1644 | if (ecb_expect_false (v < 0.)) |
|
|
1645 | { |
|
|
1646 | ev_tstamp f = -ev_floor (-v); |
|
|
1647 | |
|
|
1648 | return f - (f == v ? 0 : 1); |
|
|
1649 | } |
1773 | } |
1650 | |
1774 | |
1651 | /* fits into an unsigned long */ |
1775 | /* fits into an unsigned long */ |
1652 | return (unsigned long)v; |
1776 | return (unsigned long)v; |
1653 | } |
1777 | } |
… | |
… | |
1797 | { |
1921 | { |
1798 | WL head; |
1922 | WL head; |
1799 | unsigned char events; /* the events watched for */ |
1923 | unsigned char events; /* the events watched for */ |
1800 | unsigned char reify; /* flag set when this ANFD needs reification (EV_ANFD_REIFY, EV__IOFDSET) */ |
1924 | unsigned char reify; /* flag set when this ANFD needs reification (EV_ANFD_REIFY, EV__IOFDSET) */ |
1801 | unsigned char emask; /* some backends store the actual kernel mask in here */ |
1925 | unsigned char emask; /* some backends store the actual kernel mask in here */ |
1802 | unsigned char unused; |
1926 | unsigned char eflags; /* flags field for use by backends */ |
1803 | #if EV_USE_EPOLL |
1927 | #if EV_USE_EPOLL |
1804 | unsigned int egen; /* generation counter to counter epoll bugs */ |
1928 | unsigned int egen; /* generation counter to counter epoll bugs */ |
1805 | #endif |
1929 | #endif |
1806 | #if EV_SELECT_IS_WINSOCKET || EV_USE_IOCP |
1930 | #if EV_SELECT_IS_WINSOCKET || EV_USE_IOCP |
1807 | SOCKET handle; |
1931 | SOCKET handle; |
… | |
… | |
1861 | static struct ev_loop default_loop_struct; |
1985 | static struct ev_loop default_loop_struct; |
1862 | EV_API_DECL struct ev_loop *ev_default_loop_ptr = 0; /* needs to be initialised to make it a definition despite extern */ |
1986 | EV_API_DECL struct ev_loop *ev_default_loop_ptr = 0; /* needs to be initialised to make it a definition despite extern */ |
1863 | |
1987 | |
1864 | #else |
1988 | #else |
1865 | |
1989 | |
1866 | EV_API_DECL ev_tstamp ev_rt_now = 0; /* needs to be initialised to make it a definition despite extern */ |
1990 | EV_API_DECL ev_tstamp ev_rt_now = EV_TS_CONST (0.); /* needs to be initialised to make it a definition despite extern */ |
1867 | #define VAR(name,decl) static decl; |
1991 | #define VAR(name,decl) static decl; |
1868 | #include "ev_vars.h" |
1992 | #include "ev_vars.h" |
1869 | #undef VAR |
1993 | #undef VAR |
1870 | |
1994 | |
1871 | static int ev_default_loop_ptr; |
1995 | static int ev_default_loop_ptr; |
… | |
… | |
1893 | #if EV_USE_REALTIME |
2017 | #if EV_USE_REALTIME |
1894 | if (ecb_expect_true (have_realtime)) |
2018 | if (ecb_expect_true (have_realtime)) |
1895 | { |
2019 | { |
1896 | struct timespec ts; |
2020 | struct timespec ts; |
1897 | clock_gettime (CLOCK_REALTIME, &ts); |
2021 | clock_gettime (CLOCK_REALTIME, &ts); |
1898 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
2022 | return EV_TS_GET (ts); |
1899 | } |
2023 | } |
1900 | #endif |
2024 | #endif |
1901 | |
2025 | |
|
|
2026 | { |
1902 | struct timeval tv; |
2027 | struct timeval tv; |
1903 | gettimeofday (&tv, 0); |
2028 | gettimeofday (&tv, 0); |
1904 | return tv.tv_sec + tv.tv_usec * 1e-6; |
2029 | return EV_TV_GET (tv); |
|
|
2030 | } |
1905 | } |
2031 | } |
1906 | #endif |
2032 | #endif |
1907 | |
2033 | |
1908 | inline_size ev_tstamp |
2034 | inline_size ev_tstamp |
1909 | get_clock (void) |
2035 | get_clock (void) |
… | |
… | |
1911 | #if EV_USE_MONOTONIC |
2037 | #if EV_USE_MONOTONIC |
1912 | if (ecb_expect_true (have_monotonic)) |
2038 | if (ecb_expect_true (have_monotonic)) |
1913 | { |
2039 | { |
1914 | struct timespec ts; |
2040 | struct timespec ts; |
1915 | clock_gettime (CLOCK_MONOTONIC, &ts); |
2041 | clock_gettime (CLOCK_MONOTONIC, &ts); |
1916 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
2042 | return EV_TS_GET (ts); |
1917 | } |
2043 | } |
1918 | #endif |
2044 | #endif |
1919 | |
2045 | |
1920 | return ev_time (); |
2046 | return ev_time (); |
1921 | } |
2047 | } |
… | |
… | |
1929 | #endif |
2055 | #endif |
1930 | |
2056 | |
1931 | void |
2057 | void |
1932 | ev_sleep (ev_tstamp delay) EV_NOEXCEPT |
2058 | ev_sleep (ev_tstamp delay) EV_NOEXCEPT |
1933 | { |
2059 | { |
1934 | if (delay > 0.) |
2060 | if (delay > EV_TS_CONST (0.)) |
1935 | { |
2061 | { |
1936 | #if EV_USE_NANOSLEEP |
2062 | #if EV_USE_NANOSLEEP |
1937 | struct timespec ts; |
2063 | struct timespec ts; |
1938 | |
2064 | |
1939 | EV_TS_SET (ts, delay); |
2065 | EV_TS_SET (ts, delay); |
1940 | nanosleep (&ts, 0); |
2066 | nanosleep (&ts, 0); |
1941 | #elif defined _WIN32 |
2067 | #elif defined _WIN32 |
1942 | /* maybe this should round up, as ms is very low resolution */ |
2068 | /* maybe this should round up, as ms is very low resolution */ |
1943 | /* compared to select (µs) or nanosleep (ns) */ |
2069 | /* compared to select (µs) or nanosleep (ns) */ |
1944 | Sleep ((unsigned long)(delay * 1e3)); |
2070 | Sleep ((unsigned long)(EV_TS_TO_MSEC (delay))); |
1945 | #else |
2071 | #else |
1946 | struct timeval tv; |
2072 | struct timeval tv; |
1947 | |
2073 | |
1948 | /* here we rely on sys/time.h + sys/types.h + unistd.h providing select */ |
2074 | /* here we rely on sys/time.h + sys/types.h + unistd.h providing select */ |
1949 | /* something not guaranteed by newer posix versions, but guaranteed */ |
2075 | /* something not guaranteed by newer posix versions, but guaranteed */ |
… | |
… | |
2299 | |
2425 | |
2300 | /* find minimum child */ |
2426 | /* find minimum child */ |
2301 | if (ecb_expect_true (pos + DHEAP - 1 < E)) |
2427 | if (ecb_expect_true (pos + DHEAP - 1 < E)) |
2302 | { |
2428 | { |
2303 | /* fast path */ (minpos = pos + 0), (minat = ANHE_at (*minpos)); |
2429 | /* fast path */ (minpos = pos + 0), (minat = ANHE_at (*minpos)); |
2304 | if ( ANHE_at (pos [1]) < minat) (minpos = pos + 1), (minat = ANHE_at (*minpos)); |
2430 | if ( minat > ANHE_at (pos [1])) (minpos = pos + 1), (minat = ANHE_at (*minpos)); |
2305 | if ( ANHE_at (pos [2]) < minat) (minpos = pos + 2), (minat = ANHE_at (*minpos)); |
2431 | if ( minat > ANHE_at (pos [2])) (minpos = pos + 2), (minat = ANHE_at (*minpos)); |
2306 | if ( ANHE_at (pos [3]) < minat) (minpos = pos + 3), (minat = ANHE_at (*minpos)); |
2432 | if ( minat > ANHE_at (pos [3])) (minpos = pos + 3), (minat = ANHE_at (*minpos)); |
2307 | } |
2433 | } |
2308 | else if (pos < E) |
2434 | else if (pos < E) |
2309 | { |
2435 | { |
2310 | /* slow path */ (minpos = pos + 0), (minat = ANHE_at (*minpos)); |
2436 | /* slow path */ (minpos = pos + 0), (minat = ANHE_at (*minpos)); |
2311 | if (pos + 1 < E && ANHE_at (pos [1]) < minat) (minpos = pos + 1), (minat = ANHE_at (*minpos)); |
2437 | if (pos + 1 < E && minat > ANHE_at (pos [1])) (minpos = pos + 1), (minat = ANHE_at (*minpos)); |
2312 | if (pos + 2 < E && ANHE_at (pos [2]) < minat) (minpos = pos + 2), (minat = ANHE_at (*minpos)); |
2438 | if (pos + 2 < E && minat > ANHE_at (pos [2])) (minpos = pos + 2), (minat = ANHE_at (*minpos)); |
2313 | if (pos + 3 < E && ANHE_at (pos [3]) < minat) (minpos = pos + 3), (minat = ANHE_at (*minpos)); |
2439 | if (pos + 3 < E && minat > ANHE_at (pos [3])) (minpos = pos + 3), (minat = ANHE_at (*minpos)); |
2314 | } |
2440 | } |
2315 | else |
2441 | else |
2316 | break; |
2442 | break; |
2317 | |
2443 | |
2318 | if (ANHE_at (he) <= minat) |
2444 | if (ANHE_at (he) <= minat) |
… | |
… | |
2326 | |
2452 | |
2327 | heap [k] = he; |
2453 | heap [k] = he; |
2328 | ev_active (ANHE_w (he)) = k; |
2454 | ev_active (ANHE_w (he)) = k; |
2329 | } |
2455 | } |
2330 | |
2456 | |
2331 | #else /* 4HEAP */ |
2457 | #else /* not 4HEAP */ |
2332 | |
2458 | |
2333 | #define HEAP0 1 |
2459 | #define HEAP0 1 |
2334 | #define HPARENT(k) ((k) >> 1) |
2460 | #define HPARENT(k) ((k) >> 1) |
2335 | #define UPHEAP_DONE(p,k) (!(p)) |
2461 | #define UPHEAP_DONE(p,k) (!(p)) |
2336 | |
2462 | |
… | |
… | |
2737 | # include "ev_epoll.c" |
2863 | # include "ev_epoll.c" |
2738 | #endif |
2864 | #endif |
2739 | #if EV_USE_LINUXAIO |
2865 | #if EV_USE_LINUXAIO |
2740 | # include "ev_linuxaio.c" |
2866 | # include "ev_linuxaio.c" |
2741 | #endif |
2867 | #endif |
|
|
2868 | #if EV_USE_IOURING |
|
|
2869 | # include "ev_iouring.c" |
|
|
2870 | #endif |
2742 | #if EV_USE_POLL |
2871 | #if EV_USE_POLL |
2743 | # include "ev_poll.c" |
2872 | # include "ev_poll.c" |
2744 | #endif |
2873 | #endif |
2745 | #if EV_USE_SELECT |
2874 | #if EV_USE_SELECT |
2746 | # include "ev_select.c" |
2875 | # include "ev_select.c" |
… | |
… | |
2778 | |
2907 | |
2779 | if (EV_USE_PORT ) flags |= EVBACKEND_PORT; |
2908 | if (EV_USE_PORT ) flags |= EVBACKEND_PORT; |
2780 | if (EV_USE_KQUEUE ) flags |= EVBACKEND_KQUEUE; |
2909 | if (EV_USE_KQUEUE ) flags |= EVBACKEND_KQUEUE; |
2781 | if (EV_USE_EPOLL ) flags |= EVBACKEND_EPOLL; |
2910 | if (EV_USE_EPOLL ) flags |= EVBACKEND_EPOLL; |
2782 | if (EV_USE_LINUXAIO) flags |= EVBACKEND_LINUXAIO; |
2911 | if (EV_USE_LINUXAIO) flags |= EVBACKEND_LINUXAIO; |
|
|
2912 | if (EV_USE_IOURING ) flags |= EVBACKEND_IOURING; |
2783 | if (EV_USE_POLL ) flags |= EVBACKEND_POLL; |
2913 | if (EV_USE_POLL ) flags |= EVBACKEND_POLL; |
2784 | if (EV_USE_SELECT ) flags |= EVBACKEND_SELECT; |
2914 | if (EV_USE_SELECT ) flags |= EVBACKEND_SELECT; |
2785 | |
2915 | |
2786 | return flags; |
2916 | return flags; |
2787 | } |
2917 | } |
… | |
… | |
2808 | |
2938 | |
2809 | /* TODO: linuxaio is very experimental */ |
2939 | /* TODO: linuxaio is very experimental */ |
2810 | #if !EV_RECOMMEND_LINUXAIO |
2940 | #if !EV_RECOMMEND_LINUXAIO |
2811 | flags &= ~EVBACKEND_LINUXAIO; |
2941 | flags &= ~EVBACKEND_LINUXAIO; |
2812 | #endif |
2942 | #endif |
|
|
2943 | /* TODO: linuxaio is super experimental */ |
|
|
2944 | #if !EV_RECOMMEND_IOURING |
|
|
2945 | flags &= ~EVBACKEND_IOURING; |
|
|
2946 | #endif |
2813 | |
2947 | |
2814 | return flags; |
2948 | return flags; |
2815 | } |
2949 | } |
2816 | |
2950 | |
2817 | ecb_cold |
2951 | ecb_cold |
… | |
… | |
2821 | int flags = EVBACKEND_EPOLL | EVBACKEND_KQUEUE | EVBACKEND_PORT; |
2955 | int flags = EVBACKEND_EPOLL | EVBACKEND_KQUEUE | EVBACKEND_PORT; |
2822 | |
2956 | |
2823 | /* epoll embeddability broken on all linux versions up to at least 2.6.23 */ |
2957 | /* epoll embeddability broken on all linux versions up to at least 2.6.23 */ |
2824 | if (ev_linux_version () < 0x020620) /* disable it on linux < 2.6.32 */ |
2958 | if (ev_linux_version () < 0x020620) /* disable it on linux < 2.6.32 */ |
2825 | flags &= ~EVBACKEND_EPOLL; |
2959 | flags &= ~EVBACKEND_EPOLL; |
|
|
2960 | |
|
|
2961 | /* EVBACKEND_LINUXAIO is theoretically embeddable, but suffers from a performance overhead */ |
|
|
2962 | |
|
|
2963 | /* EVBACKEND_IOURING is practically embeddable, but the current implementation is not |
|
|
2964 | * because our backend_fd is the epoll fd we need as fallback. |
|
|
2965 | * if the kernel ever is fixed, this might change... |
|
|
2966 | */ |
2826 | |
2967 | |
2827 | return flags; |
2968 | return flags; |
2828 | } |
2969 | } |
2829 | |
2970 | |
2830 | unsigned int |
2971 | unsigned int |
… | |
… | |
2961 | if (!backend && (flags & EVBACKEND_PORT )) backend = port_init (EV_A_ flags); |
3102 | if (!backend && (flags & EVBACKEND_PORT )) backend = port_init (EV_A_ flags); |
2962 | #endif |
3103 | #endif |
2963 | #if EV_USE_KQUEUE |
3104 | #if EV_USE_KQUEUE |
2964 | if (!backend && (flags & EVBACKEND_KQUEUE )) backend = kqueue_init (EV_A_ flags); |
3105 | if (!backend && (flags & EVBACKEND_KQUEUE )) backend = kqueue_init (EV_A_ flags); |
2965 | #endif |
3106 | #endif |
|
|
3107 | #if EV_USE_IOURING |
|
|
3108 | if (!backend && (flags & EVBACKEND_IOURING )) backend = iouring_init (EV_A_ flags); |
|
|
3109 | #endif |
2966 | #if EV_USE_LINUXAIO |
3110 | #if EV_USE_LINUXAIO |
2967 | if (!backend && (flags & EVBACKEND_LINUXAIO)) backend = linuxaio_init (EV_A_ flags); |
3111 | if (!backend && (flags & EVBACKEND_LINUXAIO)) backend = linuxaio_init (EV_A_ flags); |
2968 | #endif |
3112 | #endif |
2969 | #if EV_USE_EPOLL |
3113 | #if EV_USE_EPOLL |
2970 | if (!backend && (flags & EVBACKEND_EPOLL )) backend = epoll_init (EV_A_ flags); |
3114 | if (!backend && (flags & EVBACKEND_EPOLL )) backend = epoll_init (EV_A_ flags); |
… | |
… | |
3043 | #if EV_USE_PORT |
3187 | #if EV_USE_PORT |
3044 | if (backend == EVBACKEND_PORT ) port_destroy (EV_A); |
3188 | if (backend == EVBACKEND_PORT ) port_destroy (EV_A); |
3045 | #endif |
3189 | #endif |
3046 | #if EV_USE_KQUEUE |
3190 | #if EV_USE_KQUEUE |
3047 | if (backend == EVBACKEND_KQUEUE ) kqueue_destroy (EV_A); |
3191 | if (backend == EVBACKEND_KQUEUE ) kqueue_destroy (EV_A); |
|
|
3192 | #endif |
|
|
3193 | #if EV_USE_IOURING |
|
|
3194 | if (backend == EVBACKEND_IOURING ) iouring_destroy (EV_A); |
3048 | #endif |
3195 | #endif |
3049 | #if EV_USE_LINUXAIO |
3196 | #if EV_USE_LINUXAIO |
3050 | if (backend == EVBACKEND_LINUXAIO) linuxaio_destroy (EV_A); |
3197 | if (backend == EVBACKEND_LINUXAIO) linuxaio_destroy (EV_A); |
3051 | #endif |
3198 | #endif |
3052 | #if EV_USE_EPOLL |
3199 | #if EV_USE_EPOLL |
… | |
… | |
3110 | #if EV_USE_PORT |
3257 | #if EV_USE_PORT |
3111 | if (backend == EVBACKEND_PORT ) port_fork (EV_A); |
3258 | if (backend == EVBACKEND_PORT ) port_fork (EV_A); |
3112 | #endif |
3259 | #endif |
3113 | #if EV_USE_KQUEUE |
3260 | #if EV_USE_KQUEUE |
3114 | if (backend == EVBACKEND_KQUEUE ) kqueue_fork (EV_A); |
3261 | if (backend == EVBACKEND_KQUEUE ) kqueue_fork (EV_A); |
|
|
3262 | #endif |
|
|
3263 | #if EV_USE_IOURING |
|
|
3264 | if (backend == EVBACKEND_IOURING ) iouring_fork (EV_A); |
3115 | #endif |
3265 | #endif |
3116 | #if EV_USE_LINUXAIO |
3266 | #if EV_USE_LINUXAIO |
3117 | if (backend == EVBACKEND_LINUXAIO) linuxaio_fork (EV_A); |
3267 | if (backend == EVBACKEND_LINUXAIO) linuxaio_fork (EV_A); |
3118 | #endif |
3268 | #endif |
3119 | #if EV_USE_EPOLL |
3269 | #if EV_USE_EPOLL |
… | |
… | |
3416 | { |
3566 | { |
3417 | ev_at (w) += w->repeat; |
3567 | ev_at (w) += w->repeat; |
3418 | if (ev_at (w) < mn_now) |
3568 | if (ev_at (w) < mn_now) |
3419 | ev_at (w) = mn_now; |
3569 | ev_at (w) = mn_now; |
3420 | |
3570 | |
3421 | assert (("libev: negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
3571 | assert (("libev: negative ev_timer repeat value found while processing timers", w->repeat > EV_TS_CONST (0.))); |
3422 | |
3572 | |
3423 | ANHE_at_cache (timers [HEAP0]); |
3573 | ANHE_at_cache (timers [HEAP0]); |
3424 | downheap (timers, timercnt, HEAP0); |
3574 | downheap (timers, timercnt, HEAP0); |
3425 | } |
3575 | } |
3426 | else |
3576 | else |
… | |
… | |
3557 | |
3707 | |
3558 | mn_now = get_clock (); |
3708 | mn_now = get_clock (); |
3559 | |
3709 | |
3560 | /* only fetch the realtime clock every 0.5*MIN_TIMEJUMP seconds */ |
3710 | /* only fetch the realtime clock every 0.5*MIN_TIMEJUMP seconds */ |
3561 | /* interpolate in the meantime */ |
3711 | /* interpolate in the meantime */ |
3562 | if (ecb_expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) |
3712 | if (ecb_expect_true (mn_now - now_floor < EV_TS_CONST (MIN_TIMEJUMP * .5))) |
3563 | { |
3713 | { |
3564 | ev_rt_now = rtmn_diff + mn_now; |
3714 | ev_rt_now = rtmn_diff + mn_now; |
3565 | return; |
3715 | return; |
3566 | } |
3716 | } |
3567 | |
3717 | |
… | |
… | |
3581 | ev_tstamp diff; |
3731 | ev_tstamp diff; |
3582 | rtmn_diff = ev_rt_now - mn_now; |
3732 | rtmn_diff = ev_rt_now - mn_now; |
3583 | |
3733 | |
3584 | diff = odiff - rtmn_diff; |
3734 | diff = odiff - rtmn_diff; |
3585 | |
3735 | |
3586 | if (ecb_expect_true ((diff < 0. ? -diff : diff) < MIN_TIMEJUMP)) |
3736 | if (ecb_expect_true ((diff < EV_TS_CONST (0.) ? -diff : diff) < EV_TS_CONST (MIN_TIMEJUMP))) |
3587 | return; /* all is well */ |
3737 | return; /* all is well */ |
3588 | |
3738 | |
3589 | ev_rt_now = ev_time (); |
3739 | ev_rt_now = ev_time (); |
3590 | mn_now = get_clock (); |
3740 | mn_now = get_clock (); |
3591 | now_floor = mn_now; |
3741 | now_floor = mn_now; |
… | |
… | |
3600 | else |
3750 | else |
3601 | #endif |
3751 | #endif |
3602 | { |
3752 | { |
3603 | ev_rt_now = ev_time (); |
3753 | ev_rt_now = ev_time (); |
3604 | |
3754 | |
3605 | if (ecb_expect_false (mn_now > ev_rt_now || ev_rt_now > mn_now + max_block + MIN_TIMEJUMP)) |
3755 | if (ecb_expect_false (mn_now > ev_rt_now || ev_rt_now > mn_now + max_block + EV_TS_CONST (MIN_TIMEJUMP))) |
3606 | { |
3756 | { |
3607 | /* adjust timers. this is easy, as the offset is the same for all of them */ |
3757 | /* adjust timers. this is easy, as the offset is the same for all of them */ |
3608 | timers_reschedule (EV_A_ ev_rt_now - mn_now); |
3758 | timers_reschedule (EV_A_ ev_rt_now - mn_now); |
3609 | #if EV_PERIODIC_ENABLE |
3759 | #if EV_PERIODIC_ENABLE |
3610 | periodics_reschedule (EV_A); |
3760 | periodics_reschedule (EV_A); |
… | |
… | |
3679 | |
3829 | |
3680 | /* remember old timestamp for io_blocktime calculation */ |
3830 | /* remember old timestamp for io_blocktime calculation */ |
3681 | ev_tstamp prev_mn_now = mn_now; |
3831 | ev_tstamp prev_mn_now = mn_now; |
3682 | |
3832 | |
3683 | /* update time to cancel out callback processing overhead */ |
3833 | /* update time to cancel out callback processing overhead */ |
3684 | time_update (EV_A_ 1e100); |
3834 | time_update (EV_A_ EV_TS_CONST (EV_TSTAMP_HUGE)); |
3685 | |
3835 | |
3686 | /* from now on, we want a pipe-wake-up */ |
3836 | /* from now on, we want a pipe-wake-up */ |
3687 | pipe_write_wanted = 1; |
3837 | pipe_write_wanted = 1; |
3688 | |
3838 | |
3689 | ECB_MEMORY_FENCE; /* make sure pipe_write_wanted is visible before we check for potential skips */ |
3839 | ECB_MEMORY_FENCE; /* make sure pipe_write_wanted is visible before we check for potential skips */ |
3690 | |
3840 | |
3691 | if (ecb_expect_true (!(flags & EVRUN_NOWAIT || idleall || !activecnt || pipe_write_skipped))) |
3841 | if (ecb_expect_true (!(flags & EVRUN_NOWAIT || idleall || !activecnt || pipe_write_skipped))) |
3692 | { |
3842 | { |
3693 | waittime = MAX_BLOCKTIME; |
3843 | waittime = EV_TS_CONST (MAX_BLOCKTIME); |
3694 | |
3844 | |
3695 | if (timercnt) |
3845 | if (timercnt) |
3696 | { |
3846 | { |
3697 | ev_tstamp to = ANHE_at (timers [HEAP0]) - mn_now; |
3847 | ev_tstamp to = ANHE_at (timers [HEAP0]) - mn_now; |
3698 | if (waittime > to) waittime = to; |
3848 | if (waittime > to) waittime = to; |
… | |
… | |
3708 | |
3858 | |
3709 | /* don't let timeouts decrease the waittime below timeout_blocktime */ |
3859 | /* don't let timeouts decrease the waittime below timeout_blocktime */ |
3710 | if (ecb_expect_false (waittime < timeout_blocktime)) |
3860 | if (ecb_expect_false (waittime < timeout_blocktime)) |
3711 | waittime = timeout_blocktime; |
3861 | waittime = timeout_blocktime; |
3712 | |
3862 | |
3713 | /* at this point, we NEED to wait, so we have to ensure */ |
3863 | /* now there are two more special cases left, either we have |
3714 | /* to pass a minimum nonzero value to the backend */ |
3864 | * already-expired timers, so we should not sleep, or we have timers |
|
|
3865 | * that expire very soon, in which case we need to wait for a minimum |
|
|
3866 | * amount of time for some event loop backends. |
|
|
3867 | */ |
3715 | if (ecb_expect_false (waittime < backend_mintime)) |
3868 | if (ecb_expect_false (waittime < backend_mintime)) |
|
|
3869 | waittime = waittime <= EV_TS_CONST (0.) |
|
|
3870 | ? EV_TS_CONST (0.) |
3716 | waittime = backend_mintime; |
3871 | : backend_mintime; |
3717 | |
3872 | |
3718 | /* extra check because io_blocktime is commonly 0 */ |
3873 | /* extra check because io_blocktime is commonly 0 */ |
3719 | if (ecb_expect_false (io_blocktime)) |
3874 | if (ecb_expect_false (io_blocktime)) |
3720 | { |
3875 | { |
3721 | sleeptime = io_blocktime - (mn_now - prev_mn_now); |
3876 | sleeptime = io_blocktime - (mn_now - prev_mn_now); |
3722 | |
3877 | |
3723 | if (sleeptime > waittime - backend_mintime) |
3878 | if (sleeptime > waittime - backend_mintime) |
3724 | sleeptime = waittime - backend_mintime; |
3879 | sleeptime = waittime - backend_mintime; |
3725 | |
3880 | |
3726 | if (ecb_expect_true (sleeptime > 0.)) |
3881 | if (ecb_expect_true (sleeptime > EV_TS_CONST (0.))) |
3727 | { |
3882 | { |
3728 | ev_sleep (sleeptime); |
3883 | ev_sleep (sleeptime); |
3729 | waittime -= sleeptime; |
3884 | waittime -= sleeptime; |
3730 | } |
3885 | } |
3731 | } |
3886 | } |
… | |
… | |
3805 | } |
3960 | } |
3806 | |
3961 | |
3807 | void |
3962 | void |
3808 | ev_now_update (EV_P) EV_NOEXCEPT |
3963 | ev_now_update (EV_P) EV_NOEXCEPT |
3809 | { |
3964 | { |
3810 | time_update (EV_A_ 1e100); |
3965 | time_update (EV_A_ EV_TSTAMP_HUGE); |
3811 | } |
3966 | } |
3812 | |
3967 | |
3813 | void |
3968 | void |
3814 | ev_suspend (EV_P) EV_NOEXCEPT |
3969 | ev_suspend (EV_P) EV_NOEXCEPT |
3815 | { |
3970 | { |
… | |
… | |
4046 | } |
4201 | } |
4047 | |
4202 | |
4048 | ev_tstamp |
4203 | ev_tstamp |
4049 | ev_timer_remaining (EV_P_ ev_timer *w) EV_NOEXCEPT |
4204 | ev_timer_remaining (EV_P_ ev_timer *w) EV_NOEXCEPT |
4050 | { |
4205 | { |
4051 | return ev_at (w) - (ev_is_active (w) ? mn_now : 0.); |
4206 | return ev_at (w) - (ev_is_active (w) ? mn_now : EV_TS_CONST (0.)); |
4052 | } |
4207 | } |
4053 | |
4208 | |
4054 | #if EV_PERIODIC_ENABLE |
4209 | #if EV_PERIODIC_ENABLE |
4055 | ecb_noinline |
4210 | ecb_noinline |
4056 | void |
4211 | void |