… | |
… | |
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 |
… | |
… | |
333 | # define EV_USE_LINUXAIO 0 |
342 | # define EV_USE_LINUXAIO 0 |
334 | # endif |
343 | # endif |
335 | #endif |
344 | #endif |
336 | |
345 | |
337 | #ifndef EV_USE_IOURING |
346 | #ifndef EV_USE_IOURING |
338 | # if __linux |
347 | # if __linux /* later checks might disable again */ |
339 | # define EV_USE_IOURING 0 |
348 | # define EV_USE_IOURING 1 |
340 | # else |
349 | # else |
341 | # define EV_USE_IOURING 0 |
350 | # define EV_USE_IOURING 0 |
342 | # endif |
351 | # endif |
343 | #endif |
352 | #endif |
344 | |
353 | |
… | |
… | |
438 | #if !EV_STAT_ENABLE |
447 | #if !EV_STAT_ENABLE |
439 | # undef EV_USE_INOTIFY |
448 | # undef EV_USE_INOTIFY |
440 | # define EV_USE_INOTIFY 0 |
449 | # define EV_USE_INOTIFY 0 |
441 | #endif |
450 | #endif |
442 | |
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 |
|
|
458 | #endif |
|
|
459 | |
443 | #if !EV_USE_NANOSLEEP |
460 | #if !EV_USE_NANOSLEEP |
444 | /* 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 */ |
445 | # if !defined _WIN32 && !defined __hpux |
462 | # if !defined _WIN32 && !defined __hpux |
446 | # include <sys/select.h> |
463 | # include <sys/select.h> |
447 | # endif |
464 | # endif |
448 | #endif |
465 | #endif |
449 | |
466 | |
450 | #if EV_USE_LINUXAIO |
467 | #if EV_USE_LINUXAIO |
451 | # include <sys/syscall.h> |
468 | # include <sys/syscall.h> |
452 | # 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 |
453 | # undef EV_USE_LINUXAIO |
472 | # undef EV_USE_LINUXAIO |
454 | # define EV_USE_LINUXAIO 0 |
473 | # define EV_USE_LINUXAIO 0 |
455 | # else |
|
|
456 | # define EV_NEED_SYSCALL 1 |
|
|
457 | # endif |
474 | # endif |
458 | #endif |
475 | #endif |
459 | |
476 | |
460 | #if EV_USE_IOURING |
477 | #if EV_USE_IOURING |
461 | # include <sys/syscall.h> |
478 | # include <sys/syscall.h> |
462 | # if !__alpha && !SYS_io_uring_setup |
479 | # if !SYS_io_uring_setup && __linux && !__alpha |
463 | # define SYS_io_uring_setup 425 |
480 | # define SYS_io_uring_setup 425 |
464 | # define SYS_io_uring_enter 426 |
481 | # define SYS_io_uring_enter 426 |
465 | # define SYS_io_uring_wregister 427 |
482 | # define SYS_io_uring_wregister 427 |
466 | # endif |
483 | # endif |
467 | # if SYS_io_uring_setup |
484 | # if SYS_io_uring_setup && EV_USE_EPOLL /* iouring backend requires epoll backend */ |
468 | # define EV_NEED_SYSCALL 1 |
485 | # define EV_NEED_SYSCALL 1 |
469 | # else |
486 | # else |
470 | # undef EV_USE_IOURING |
487 | # undef EV_USE_IOURING |
471 | # define EV_USE_IOURING 0 |
488 | # define EV_USE_IOURING 0 |
472 | # endif |
489 | # endif |
… | |
… | |
520 | }; |
537 | }; |
521 | #endif |
538 | #endif |
522 | |
539 | |
523 | /*****************************************************************************/ |
540 | /*****************************************************************************/ |
524 | |
541 | |
525 | #if EV_NEED_SYSCALL |
|
|
526 | |
|
|
527 | #include <sys/syscall.h> |
|
|
528 | |
|
|
529 | /* |
|
|
530 | * define some syscall wrappers for common architectures |
|
|
531 | * this is mostly for nice looks during debugging, not performance. |
|
|
532 | * our syscalls return < 0, not == -1, on error. which is good |
|
|
533 | * enough for linux aio. |
|
|
534 | * TODO: arm is also common nowadays, maybe even mips and x86 |
|
|
535 | * TODO: after implementing this, it suddenly looks like overkill, but its hard to remove... |
|
|
536 | */ |
|
|
537 | #if __GNUC__ && __linux && ECB_AMD64 && !defined __OPTIMIZE_SIZE__ |
|
|
538 | /* the costly errno access probably kills this for size optimisation */ |
|
|
539 | |
|
|
540 | #define ev_syscall(nr,narg,arg1,arg2,arg3,arg4,arg5) \ |
|
|
541 | ({ \ |
|
|
542 | long res; \ |
|
|
543 | register unsigned long r5 __asm__ ("r8" ); \ |
|
|
544 | register unsigned long r4 __asm__ ("r10"); \ |
|
|
545 | register unsigned long r3 __asm__ ("rdx"); \ |
|
|
546 | register unsigned long r2 __asm__ ("rsi"); \ |
|
|
547 | register unsigned long r1 __asm__ ("rdi"); \ |
|
|
548 | if (narg >= 5) r5 = (unsigned long)(arg5); \ |
|
|
549 | if (narg >= 4) r4 = (unsigned long)(arg4); \ |
|
|
550 | if (narg >= 3) r3 = (unsigned long)(arg3); \ |
|
|
551 | if (narg >= 2) r2 = (unsigned long)(arg2); \ |
|
|
552 | if (narg >= 1) r1 = (unsigned long)(arg1); \ |
|
|
553 | __asm__ __volatile__ ( \ |
|
|
554 | "syscall\n\t" \ |
|
|
555 | : "=a" (res) \ |
|
|
556 | : "0" (nr), "r" (r1), "r" (r2), "r" (r3), "r" (r4), "r" (r5) \ |
|
|
557 | : "cc", "r11", "cx", "memory"); \ |
|
|
558 | errno = -res; \ |
|
|
559 | res; \ |
|
|
560 | }) |
|
|
561 | |
|
|
562 | #endif |
|
|
563 | |
|
|
564 | #ifdef ev_syscall |
|
|
565 | #define ev_syscall0(nr) ev_syscall (nr, 0, 0, 0, 0, 0, 0 |
|
|
566 | #define ev_syscall1(nr,arg1) ev_syscall (nr, 1, arg1, 0, 0, 0, 0) |
|
|
567 | #define ev_syscall2(nr,arg1,arg2) ev_syscall (nr, 2, arg1, arg2, 0, 0, 0) |
|
|
568 | #define ev_syscall3(nr,arg1,arg2,arg3) ev_syscall (nr, 3, arg1, arg2, arg3, 0, 0) |
|
|
569 | #define ev_syscall4(nr,arg1,arg2,arg3,arg4) ev_syscall (nr, 3, arg1, arg2, arg3, arg4, 0) |
|
|
570 | #define ev_syscall5(nr,arg1,arg2,arg3,arg4,arg5) ev_syscall (nr, 5, arg1, arg2, arg3, arg4, arg5) |
|
|
571 | #else |
|
|
572 | #define ev_syscall0(nr) syscall (nr) |
|
|
573 | #define ev_syscall1(nr,arg1) syscall (nr, arg1) |
|
|
574 | #define ev_syscall2(nr,arg1,arg2) syscall (nr, arg1, arg2) |
|
|
575 | #define ev_syscall3(nr,arg1,arg2,arg3) syscall (nr, arg1, arg2, arg3) |
|
|
576 | #define ev_syscall4(nr,arg1,arg2,arg3,arg4) syscall (nr, arg1, arg2, arg3, arg4) |
|
|
577 | #define ev_syscall5(nr,arg1,arg2,arg3,arg4,arg5) syscall (nr, arg1, arg2, arg3, arg4, arg5) |
|
|
578 | #endif |
|
|
579 | |
|
|
580 | #endif |
|
|
581 | |
|
|
582 | /*****************************************************************************/ |
|
|
583 | |
|
|
584 | #if EV_VERIFY >= 3 |
542 | #if EV_VERIFY >= 3 |
585 | # define EV_FREQUENT_CHECK ev_verify (EV_A) |
543 | # define EV_FREQUENT_CHECK ev_verify (EV_A) |
586 | #else |
544 | #else |
587 | # define EV_FREQUENT_CHECK do { } while (0) |
545 | # define EV_FREQUENT_CHECK do { } while (0) |
588 | #endif |
546 | #endif |
… | |
… | |
592 | * This value is good at least till the year 4000. |
550 | * This value is good at least till the year 4000. |
593 | */ |
551 | */ |
594 | #define MIN_INTERVAL 0.0001220703125 /* 1/2**13, good till 4000 */ |
552 | #define MIN_INTERVAL 0.0001220703125 /* 1/2**13, good till 4000 */ |
595 | /*#define MIN_INTERVAL 0.00000095367431640625 /* 1/2**20, good till 2200 */ |
553 | /*#define MIN_INTERVAL 0.00000095367431640625 /* 1/2**20, good till 2200 */ |
596 | |
554 | |
597 | #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) */ |
598 | #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) */ |
599 | |
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 |
600 | #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) |
601 | #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 |
602 | |
575 | |
603 | /* 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 */ |
604 | /* ECB.H BEGIN */ |
577 | /* ECB.H BEGIN */ |
605 | /* |
578 | /* |
606 | * libecb - http://software.schmorp.de/pkg/libecb |
579 | * libecb - http://software.schmorp.de/pkg/libecb |
… | |
… | |
1643 | # define inline_speed ecb_inline |
1616 | # define inline_speed ecb_inline |
1644 | #else |
1617 | #else |
1645 | # define inline_speed ecb_noinline static |
1618 | # define inline_speed ecb_noinline static |
1646 | #endif |
1619 | #endif |
1647 | |
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 | |
1648 | #define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) |
1687 | #define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) |
1649 | |
1688 | |
1650 | #if EV_MINPRI == EV_MAXPRI |
1689 | #if EV_MINPRI == EV_MAXPRI |
1651 | # define ABSPRI(w) (((W)w), 0) |
1690 | # define ABSPRI(w) (((W)w), 0) |
1652 | #else |
1691 | #else |
… | |
… | |
1711 | const ev_tstamp shift = sizeof (unsigned long) >= 8 ? 10000000000000000000. : 1000000000.; |
1750 | const ev_tstamp shift = sizeof (unsigned long) >= 8 ? 10000000000000000000. : 1000000000.; |
1712 | #else |
1751 | #else |
1713 | const ev_tstamp shift = sizeof (unsigned long) >= 8 ? 18446744073709551616. : 4294967296.; |
1752 | const ev_tstamp shift = sizeof (unsigned long) >= 8 ? 18446744073709551616. : 4294967296.; |
1714 | #endif |
1753 | #endif |
1715 | |
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 | |
1716 | /* argument too large for an unsigned long? */ |
1763 | /* argument too large for an unsigned long? then reduce it */ |
1717 | if (ecb_expect_false (v >= shift)) |
1764 | if (ecb_expect_false (v >= shift)) |
1718 | { |
1765 | { |
1719 | ev_tstamp f; |
1766 | ev_tstamp f; |
1720 | |
1767 | |
1721 | if (v == v - 1.) |
1768 | if (v == v - 1.) |
1722 | return v; /* very large number */ |
1769 | return v; /* very large numbers are assumed to be integer */ |
1723 | |
1770 | |
1724 | f = shift * ev_floor (v * (1. / shift)); |
1771 | f = shift * ev_floor (v * (1. / shift)); |
1725 | return f + ev_floor (v - f); |
1772 | return f + ev_floor (v - f); |
1726 | } |
|
|
1727 | |
|
|
1728 | /* special treatment for negative args? */ |
|
|
1729 | if (ecb_expect_false (v < 0.)) |
|
|
1730 | { |
|
|
1731 | ev_tstamp f = -ev_floor (-v); |
|
|
1732 | |
|
|
1733 | return f - (f == v ? 0 : 1); |
|
|
1734 | } |
1773 | } |
1735 | |
1774 | |
1736 | /* fits into an unsigned long */ |
1775 | /* fits into an unsigned long */ |
1737 | return (unsigned long)v; |
1776 | return (unsigned long)v; |
1738 | } |
1777 | } |
… | |
… | |
1882 | { |
1921 | { |
1883 | WL head; |
1922 | WL head; |
1884 | unsigned char events; /* the events watched for */ |
1923 | unsigned char events; /* the events watched for */ |
1885 | 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) */ |
1886 | 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 */ |
1887 | unsigned char unused; |
1926 | unsigned char eflags; /* flags field for use by backends */ |
1888 | #if EV_USE_EPOLL |
1927 | #if EV_USE_EPOLL |
1889 | unsigned int egen; /* generation counter to counter epoll bugs */ |
1928 | unsigned int egen; /* generation counter to counter epoll bugs */ |
1890 | #endif |
1929 | #endif |
1891 | #if EV_SELECT_IS_WINSOCKET || EV_USE_IOCP |
1930 | #if EV_SELECT_IS_WINSOCKET || EV_USE_IOCP |
1892 | SOCKET handle; |
1931 | SOCKET handle; |
… | |
… | |
1946 | static struct ev_loop default_loop_struct; |
1985 | static struct ev_loop default_loop_struct; |
1947 | 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 */ |
1948 | |
1987 | |
1949 | #else |
1988 | #else |
1950 | |
1989 | |
1951 | 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 */ |
1952 | #define VAR(name,decl) static decl; |
1991 | #define VAR(name,decl) static decl; |
1953 | #include "ev_vars.h" |
1992 | #include "ev_vars.h" |
1954 | #undef VAR |
1993 | #undef VAR |
1955 | |
1994 | |
1956 | static int ev_default_loop_ptr; |
1995 | static int ev_default_loop_ptr; |
… | |
… | |
1978 | #if EV_USE_REALTIME |
2017 | #if EV_USE_REALTIME |
1979 | if (ecb_expect_true (have_realtime)) |
2018 | if (ecb_expect_true (have_realtime)) |
1980 | { |
2019 | { |
1981 | struct timespec ts; |
2020 | struct timespec ts; |
1982 | clock_gettime (CLOCK_REALTIME, &ts); |
2021 | clock_gettime (CLOCK_REALTIME, &ts); |
1983 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
2022 | return EV_TS_GET (ts); |
1984 | } |
2023 | } |
1985 | #endif |
2024 | #endif |
1986 | |
2025 | |
|
|
2026 | { |
1987 | struct timeval tv; |
2027 | struct timeval tv; |
1988 | gettimeofday (&tv, 0); |
2028 | gettimeofday (&tv, 0); |
1989 | return tv.tv_sec + tv.tv_usec * 1e-6; |
2029 | return EV_TV_GET (tv); |
|
|
2030 | } |
1990 | } |
2031 | } |
1991 | #endif |
2032 | #endif |
1992 | |
2033 | |
1993 | inline_size ev_tstamp |
2034 | inline_size ev_tstamp |
1994 | get_clock (void) |
2035 | get_clock (void) |
… | |
… | |
1996 | #if EV_USE_MONOTONIC |
2037 | #if EV_USE_MONOTONIC |
1997 | if (ecb_expect_true (have_monotonic)) |
2038 | if (ecb_expect_true (have_monotonic)) |
1998 | { |
2039 | { |
1999 | struct timespec ts; |
2040 | struct timespec ts; |
2000 | clock_gettime (CLOCK_MONOTONIC, &ts); |
2041 | clock_gettime (CLOCK_MONOTONIC, &ts); |
2001 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
2042 | return EV_TS_GET (ts); |
2002 | } |
2043 | } |
2003 | #endif |
2044 | #endif |
2004 | |
2045 | |
2005 | return ev_time (); |
2046 | return ev_time (); |
2006 | } |
2047 | } |
… | |
… | |
2014 | #endif |
2055 | #endif |
2015 | |
2056 | |
2016 | void |
2057 | void |
2017 | ev_sleep (ev_tstamp delay) EV_NOEXCEPT |
2058 | ev_sleep (ev_tstamp delay) EV_NOEXCEPT |
2018 | { |
2059 | { |
2019 | if (delay > 0.) |
2060 | if (delay > EV_TS_CONST (0.)) |
2020 | { |
2061 | { |
2021 | #if EV_USE_NANOSLEEP |
2062 | #if EV_USE_NANOSLEEP |
2022 | struct timespec ts; |
2063 | struct timespec ts; |
2023 | |
2064 | |
2024 | EV_TS_SET (ts, delay); |
2065 | EV_TS_SET (ts, delay); |
2025 | nanosleep (&ts, 0); |
2066 | nanosleep (&ts, 0); |
2026 | #elif defined _WIN32 |
2067 | #elif defined _WIN32 |
2027 | /* maybe this should round up, as ms is very low resolution */ |
2068 | /* maybe this should round up, as ms is very low resolution */ |
2028 | /* compared to select (µs) or nanosleep (ns) */ |
2069 | /* compared to select (µs) or nanosleep (ns) */ |
2029 | Sleep ((unsigned long)(delay * 1e3)); |
2070 | Sleep ((unsigned long)(EV_TS_TO_MSEC (delay))); |
2030 | #else |
2071 | #else |
2031 | struct timeval tv; |
2072 | struct timeval tv; |
2032 | |
2073 | |
2033 | /* 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 */ |
2034 | /* something not guaranteed by newer posix versions, but guaranteed */ |
2075 | /* something not guaranteed by newer posix versions, but guaranteed */ |
… | |
… | |
2384 | |
2425 | |
2385 | /* find minimum child */ |
2426 | /* find minimum child */ |
2386 | if (ecb_expect_true (pos + DHEAP - 1 < E)) |
2427 | if (ecb_expect_true (pos + DHEAP - 1 < E)) |
2387 | { |
2428 | { |
2388 | /* fast path */ (minpos = pos + 0), (minat = ANHE_at (*minpos)); |
2429 | /* fast path */ (minpos = pos + 0), (minat = ANHE_at (*minpos)); |
2389 | 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)); |
2390 | 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)); |
2391 | 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)); |
2392 | } |
2433 | } |
2393 | else if (pos < E) |
2434 | else if (pos < E) |
2394 | { |
2435 | { |
2395 | /* slow path */ (minpos = pos + 0), (minat = ANHE_at (*minpos)); |
2436 | /* slow path */ (minpos = pos + 0), (minat = ANHE_at (*minpos)); |
2396 | 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)); |
2397 | 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)); |
2398 | 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)); |
2399 | } |
2440 | } |
2400 | else |
2441 | else |
2401 | break; |
2442 | break; |
2402 | |
2443 | |
2403 | if (ANHE_at (he) <= minat) |
2444 | if (ANHE_at (he) <= minat) |
… | |
… | |
2411 | |
2452 | |
2412 | heap [k] = he; |
2453 | heap [k] = he; |
2413 | ev_active (ANHE_w (he)) = k; |
2454 | ev_active (ANHE_w (he)) = k; |
2414 | } |
2455 | } |
2415 | |
2456 | |
2416 | #else /* 4HEAP */ |
2457 | #else /* not 4HEAP */ |
2417 | |
2458 | |
2418 | #define HEAP0 1 |
2459 | #define HEAP0 1 |
2419 | #define HPARENT(k) ((k) >> 1) |
2460 | #define HPARENT(k) ((k) >> 1) |
2420 | #define UPHEAP_DONE(p,k) (!(p)) |
2461 | #define UPHEAP_DONE(p,k) (!(p)) |
2421 | |
2462 | |
… | |
… | |
2915 | |
2956 | |
2916 | /* 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 */ |
2917 | 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 */ |
2918 | flags &= ~EVBACKEND_EPOLL; |
2959 | flags &= ~EVBACKEND_EPOLL; |
2919 | |
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 | */ |
|
|
2967 | |
2920 | return flags; |
2968 | return flags; |
2921 | } |
2969 | } |
2922 | |
2970 | |
2923 | unsigned int |
2971 | unsigned int |
2924 | ev_backend (EV_P) EV_NOEXCEPT |
2972 | ev_backend (EV_P) EV_NOEXCEPT |
… | |
… | |
3518 | { |
3566 | { |
3519 | ev_at (w) += w->repeat; |
3567 | ev_at (w) += w->repeat; |
3520 | if (ev_at (w) < mn_now) |
3568 | if (ev_at (w) < mn_now) |
3521 | ev_at (w) = mn_now; |
3569 | ev_at (w) = mn_now; |
3522 | |
3570 | |
3523 | 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.))); |
3524 | |
3572 | |
3525 | ANHE_at_cache (timers [HEAP0]); |
3573 | ANHE_at_cache (timers [HEAP0]); |
3526 | downheap (timers, timercnt, HEAP0); |
3574 | downheap (timers, timercnt, HEAP0); |
3527 | } |
3575 | } |
3528 | else |
3576 | else |
… | |
… | |
3659 | |
3707 | |
3660 | mn_now = get_clock (); |
3708 | mn_now = get_clock (); |
3661 | |
3709 | |
3662 | /* only fetch the realtime clock every 0.5*MIN_TIMEJUMP seconds */ |
3710 | /* only fetch the realtime clock every 0.5*MIN_TIMEJUMP seconds */ |
3663 | /* interpolate in the meantime */ |
3711 | /* interpolate in the meantime */ |
3664 | 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))) |
3665 | { |
3713 | { |
3666 | ev_rt_now = rtmn_diff + mn_now; |
3714 | ev_rt_now = rtmn_diff + mn_now; |
3667 | return; |
3715 | return; |
3668 | } |
3716 | } |
3669 | |
3717 | |
… | |
… | |
3683 | ev_tstamp diff; |
3731 | ev_tstamp diff; |
3684 | rtmn_diff = ev_rt_now - mn_now; |
3732 | rtmn_diff = ev_rt_now - mn_now; |
3685 | |
3733 | |
3686 | diff = odiff - rtmn_diff; |
3734 | diff = odiff - rtmn_diff; |
3687 | |
3735 | |
3688 | 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))) |
3689 | return; /* all is well */ |
3737 | return; /* all is well */ |
3690 | |
3738 | |
3691 | ev_rt_now = ev_time (); |
3739 | ev_rt_now = ev_time (); |
3692 | mn_now = get_clock (); |
3740 | mn_now = get_clock (); |
3693 | now_floor = mn_now; |
3741 | now_floor = mn_now; |
… | |
… | |
3702 | else |
3750 | else |
3703 | #endif |
3751 | #endif |
3704 | { |
3752 | { |
3705 | ev_rt_now = ev_time (); |
3753 | ev_rt_now = ev_time (); |
3706 | |
3754 | |
3707 | 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))) |
3708 | { |
3756 | { |
3709 | /* 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 */ |
3710 | timers_reschedule (EV_A_ ev_rt_now - mn_now); |
3758 | timers_reschedule (EV_A_ ev_rt_now - mn_now); |
3711 | #if EV_PERIODIC_ENABLE |
3759 | #if EV_PERIODIC_ENABLE |
3712 | periodics_reschedule (EV_A); |
3760 | periodics_reschedule (EV_A); |
… | |
… | |
3781 | |
3829 | |
3782 | /* remember old timestamp for io_blocktime calculation */ |
3830 | /* remember old timestamp for io_blocktime calculation */ |
3783 | ev_tstamp prev_mn_now = mn_now; |
3831 | ev_tstamp prev_mn_now = mn_now; |
3784 | |
3832 | |
3785 | /* update time to cancel out callback processing overhead */ |
3833 | /* update time to cancel out callback processing overhead */ |
3786 | time_update (EV_A_ 1e100); |
3834 | time_update (EV_A_ EV_TS_CONST (EV_TSTAMP_HUGE)); |
3787 | |
3835 | |
3788 | /* from now on, we want a pipe-wake-up */ |
3836 | /* from now on, we want a pipe-wake-up */ |
3789 | pipe_write_wanted = 1; |
3837 | pipe_write_wanted = 1; |
3790 | |
3838 | |
3791 | 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 */ |
3792 | |
3840 | |
3793 | 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))) |
3794 | { |
3842 | { |
3795 | waittime = MAX_BLOCKTIME; |
3843 | waittime = EV_TS_CONST (MAX_BLOCKTIME); |
3796 | |
3844 | |
3797 | if (timercnt) |
3845 | if (timercnt) |
3798 | { |
3846 | { |
3799 | ev_tstamp to = ANHE_at (timers [HEAP0]) - mn_now; |
3847 | ev_tstamp to = ANHE_at (timers [HEAP0]) - mn_now; |
3800 | if (waittime > to) waittime = to; |
3848 | if (waittime > to) waittime = to; |
… | |
… | |
3823 | sleeptime = io_blocktime - (mn_now - prev_mn_now); |
3871 | sleeptime = io_blocktime - (mn_now - prev_mn_now); |
3824 | |
3872 | |
3825 | if (sleeptime > waittime - backend_mintime) |
3873 | if (sleeptime > waittime - backend_mintime) |
3826 | sleeptime = waittime - backend_mintime; |
3874 | sleeptime = waittime - backend_mintime; |
3827 | |
3875 | |
3828 | if (ecb_expect_true (sleeptime > 0.)) |
3876 | if (ecb_expect_true (sleeptime > EV_TS_CONST (0.))) |
3829 | { |
3877 | { |
3830 | ev_sleep (sleeptime); |
3878 | ev_sleep (sleeptime); |
3831 | waittime -= sleeptime; |
3879 | waittime -= sleeptime; |
3832 | } |
3880 | } |
3833 | } |
3881 | } |
… | |
… | |
3907 | } |
3955 | } |
3908 | |
3956 | |
3909 | void |
3957 | void |
3910 | ev_now_update (EV_P) EV_NOEXCEPT |
3958 | ev_now_update (EV_P) EV_NOEXCEPT |
3911 | { |
3959 | { |
3912 | time_update (EV_A_ 1e100); |
3960 | time_update (EV_A_ EV_TSTAMP_HUGE); |
3913 | } |
3961 | } |
3914 | |
3962 | |
3915 | void |
3963 | void |
3916 | ev_suspend (EV_P) EV_NOEXCEPT |
3964 | ev_suspend (EV_P) EV_NOEXCEPT |
3917 | { |
3965 | { |
… | |
… | |
4148 | } |
4196 | } |
4149 | |
4197 | |
4150 | ev_tstamp |
4198 | ev_tstamp |
4151 | ev_timer_remaining (EV_P_ ev_timer *w) EV_NOEXCEPT |
4199 | ev_timer_remaining (EV_P_ ev_timer *w) EV_NOEXCEPT |
4152 | { |
4200 | { |
4153 | return ev_at (w) - (ev_is_active (w) ? mn_now : 0.); |
4201 | return ev_at (w) - (ev_is_active (w) ? mn_now : EV_TS_CONST (0.)); |
4154 | } |
4202 | } |
4155 | |
4203 | |
4156 | #if EV_PERIODIC_ENABLE |
4204 | #if EV_PERIODIC_ENABLE |
4157 | ecb_noinline |
4205 | ecb_noinline |
4158 | void |
4206 | void |