… | |
… | |
457 | # endif |
457 | # endif |
458 | #endif |
458 | #endif |
459 | |
459 | |
460 | #if EV_USE_IOURING |
460 | #if EV_USE_IOURING |
461 | # include <sys/syscall.h> |
461 | # include <sys/syscall.h> |
462 | # if !__alpha && !SYS_io_uring_setup |
462 | # if !SYS_io_uring_setup && __linux && !__alpha |
463 | # define SYS_io_uring_setup 425 |
463 | # define SYS_io_uring_setup 425 |
464 | # define SYS_io_uring_enter 426 |
464 | # define SYS_io_uring_enter 426 |
465 | # define SYS_io_uring_wregister 427 |
465 | # define SYS_io_uring_wregister 427 |
466 | # endif |
466 | # endif |
467 | # if SYS_io_uring_setup && EV_USE_EPOLL /* iouring backend requires epoll backend */ |
467 | # if SYS_io_uring_setup && EV_USE_EPOLL /* iouring backend requires epoll backend */ |
… | |
… | |
536 | /*#define MIN_INTERVAL 0.00000095367431640625 /* 1/2**20, good till 2200 */ |
536 | /*#define MIN_INTERVAL 0.00000095367431640625 /* 1/2**20, good till 2200 */ |
537 | |
537 | |
538 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
538 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
539 | #define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */ |
539 | #define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */ |
540 | |
540 | |
541 | /* find a portable timestamp that is "alawys" in the future but fits into time_t. |
541 | /* find a portable timestamp that is "always" in the future but fits into time_t. |
542 | * this is quite hard, and we are mostly guessing - we handle 32 bit signed/unsigned time_t, |
542 | * this is quite hard, and we are mostly guessing - we handle 32 bit signed/unsigned time_t, |
543 | * and sizes large than 32 bit, but and maybe the unlikely loating point time_t */ |
543 | * and sizes larger than 32 bit, and maybe the unlikely floating point time_t */ |
544 | #define EV_TSTAMP_HUGE \ |
544 | #define EV_TSTAMP_HUGE \ |
545 | (sizeof (time_t) >= 8 ? 10000000000000. \ |
545 | (sizeof (time_t) >= 8 ? 10000000000000. \ |
546 | : 0 < (time_t)4294967295 ? 4294967295. \ |
546 | : 0 < (time_t)4294967295 ? 4294967295. \ |
547 | : 2147483647.) \ |
547 | : 2147483647.) \ |
548 | |
548 | |
|
|
549 | #ifndef EV_TS_CONST |
|
|
550 | # define EV_TS_CONST(nv) nv |
|
|
551 | # define EV_TS_TO_MSEC(a) a * 1e3 + 0.9999 |
|
|
552 | # define EV_TS_FROM_USEC(us) us * 1e-6 |
549 | #define EV_TV_SET(tv,t) do { tv.tv_sec = (long)t; tv.tv_usec = (long)((t - tv.tv_sec) * 1e6); } while (0) |
553 | # define EV_TV_SET(tv,t) do { tv.tv_sec = (long)t; tv.tv_usec = (long)((t - tv.tv_sec) * 1e6); } while (0) |
550 | #define EV_TS_SET(ts,t) do { ts.tv_sec = (long)t; ts.tv_nsec = (long)((t - ts.tv_sec) * 1e9); } while (0) |
554 | # define EV_TS_SET(ts,t) do { ts.tv_sec = (long)t; ts.tv_nsec = (long)((t - ts.tv_sec) * 1e9); } while (0) |
|
|
555 | # define EV_TV_GET(tv) ((tv).tv_sec + (tv).tv_usec * 1e-6) |
|
|
556 | # define EV_TS_GET(ts) ((ts).tv_sec + (ts).tv_nsec * 1e-9) |
|
|
557 | #endif |
551 | |
558 | |
552 | /* the following is ecb.h embedded into libev - use update_ev_c to update from an external copy */ |
559 | /* the following is ecb.h embedded into libev - use update_ev_c to update from an external copy */ |
553 | /* ECB.H BEGIN */ |
560 | /* ECB.H BEGIN */ |
554 | /* |
561 | /* |
555 | * libecb - http://software.schmorp.de/pkg/libecb |
562 | * libecb - http://software.schmorp.de/pkg/libecb |
… | |
… | |
1726 | const ev_tstamp shift = sizeof (unsigned long) >= 8 ? 10000000000000000000. : 1000000000.; |
1733 | const ev_tstamp shift = sizeof (unsigned long) >= 8 ? 10000000000000000000. : 1000000000.; |
1727 | #else |
1734 | #else |
1728 | const ev_tstamp shift = sizeof (unsigned long) >= 8 ? 18446744073709551616. : 4294967296.; |
1735 | const ev_tstamp shift = sizeof (unsigned long) >= 8 ? 18446744073709551616. : 4294967296.; |
1729 | #endif |
1736 | #endif |
1730 | |
1737 | |
|
|
1738 | /* special treatment for negative arguments */ |
|
|
1739 | if (ecb_expect_false (v < 0.)) |
|
|
1740 | { |
|
|
1741 | ev_tstamp f = -ev_floor (-v); |
|
|
1742 | |
|
|
1743 | return f - (f == v ? 0 : 1); |
|
|
1744 | } |
|
|
1745 | |
1731 | /* argument too large for an unsigned long? */ |
1746 | /* argument too large for an unsigned long? then reduce it */ |
1732 | if (ecb_expect_false (v >= shift)) |
1747 | if (ecb_expect_false (v >= shift)) |
1733 | { |
1748 | { |
1734 | ev_tstamp f; |
1749 | ev_tstamp f; |
1735 | |
1750 | |
1736 | if (v == v - 1.) |
1751 | if (v == v - 1.) |
1737 | return v; /* very large number */ |
1752 | return v; /* very large numbers are assumed to be integer */ |
1738 | |
1753 | |
1739 | f = shift * ev_floor (v * (1. / shift)); |
1754 | f = shift * ev_floor (v * (1. / shift)); |
1740 | return f + ev_floor (v - f); |
1755 | return f + ev_floor (v - f); |
1741 | } |
|
|
1742 | |
|
|
1743 | /* special treatment for negative args? */ |
|
|
1744 | if (ecb_expect_false (v < 0.)) |
|
|
1745 | { |
|
|
1746 | ev_tstamp f = -ev_floor (-v); |
|
|
1747 | |
|
|
1748 | return f - (f == v ? 0 : 1); |
|
|
1749 | } |
1756 | } |
1750 | |
1757 | |
1751 | /* fits into an unsigned long */ |
1758 | /* fits into an unsigned long */ |
1752 | return (unsigned long)v; |
1759 | return (unsigned long)v; |
1753 | } |
1760 | } |
… | |
… | |
1961 | static struct ev_loop default_loop_struct; |
1968 | static struct ev_loop default_loop_struct; |
1962 | EV_API_DECL struct ev_loop *ev_default_loop_ptr = 0; /* needs to be initialised to make it a definition despite extern */ |
1969 | EV_API_DECL struct ev_loop *ev_default_loop_ptr = 0; /* needs to be initialised to make it a definition despite extern */ |
1963 | |
1970 | |
1964 | #else |
1971 | #else |
1965 | |
1972 | |
1966 | EV_API_DECL ev_tstamp ev_rt_now = 0; /* needs to be initialised to make it a definition despite extern */ |
1973 | EV_API_DECL ev_tstamp ev_rt_now = EV_TS_CONST (0.); /* needs to be initialised to make it a definition despite extern */ |
1967 | #define VAR(name,decl) static decl; |
1974 | #define VAR(name,decl) static decl; |
1968 | #include "ev_vars.h" |
1975 | #include "ev_vars.h" |
1969 | #undef VAR |
1976 | #undef VAR |
1970 | |
1977 | |
1971 | static int ev_default_loop_ptr; |
1978 | static int ev_default_loop_ptr; |
… | |
… | |
1993 | #if EV_USE_REALTIME |
2000 | #if EV_USE_REALTIME |
1994 | if (ecb_expect_true (have_realtime)) |
2001 | if (ecb_expect_true (have_realtime)) |
1995 | { |
2002 | { |
1996 | struct timespec ts; |
2003 | struct timespec ts; |
1997 | clock_gettime (CLOCK_REALTIME, &ts); |
2004 | clock_gettime (CLOCK_REALTIME, &ts); |
1998 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
2005 | return EV_TS_GET (ts); |
1999 | } |
2006 | } |
2000 | #endif |
2007 | #endif |
2001 | |
2008 | |
2002 | struct timeval tv; |
2009 | struct timeval tv; |
2003 | gettimeofday (&tv, 0); |
2010 | gettimeofday (&tv, 0); |
2004 | return tv.tv_sec + tv.tv_usec * 1e-6; |
2011 | return EV_TV_GET (tv); |
2005 | } |
2012 | } |
2006 | #endif |
2013 | #endif |
2007 | |
2014 | |
2008 | inline_size ev_tstamp |
2015 | inline_size ev_tstamp |
2009 | get_clock (void) |
2016 | get_clock (void) |
… | |
… | |
2011 | #if EV_USE_MONOTONIC |
2018 | #if EV_USE_MONOTONIC |
2012 | if (ecb_expect_true (have_monotonic)) |
2019 | if (ecb_expect_true (have_monotonic)) |
2013 | { |
2020 | { |
2014 | struct timespec ts; |
2021 | struct timespec ts; |
2015 | clock_gettime (CLOCK_MONOTONIC, &ts); |
2022 | clock_gettime (CLOCK_MONOTONIC, &ts); |
2016 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
2023 | return EV_TS_GET (ts); |
2017 | } |
2024 | } |
2018 | #endif |
2025 | #endif |
2019 | |
2026 | |
2020 | return ev_time (); |
2027 | return ev_time (); |
2021 | } |
2028 | } |
… | |
… | |
2029 | #endif |
2036 | #endif |
2030 | |
2037 | |
2031 | void |
2038 | void |
2032 | ev_sleep (ev_tstamp delay) EV_NOEXCEPT |
2039 | ev_sleep (ev_tstamp delay) EV_NOEXCEPT |
2033 | { |
2040 | { |
2034 | if (delay > 0.) |
2041 | if (delay > EV_TS_CONST (0.)) |
2035 | { |
2042 | { |
2036 | #if EV_USE_NANOSLEEP |
2043 | #if EV_USE_NANOSLEEP |
2037 | struct timespec ts; |
2044 | struct timespec ts; |
2038 | |
2045 | |
2039 | EV_TS_SET (ts, delay); |
2046 | EV_TS_SET (ts, delay); |
2040 | nanosleep (&ts, 0); |
2047 | nanosleep (&ts, 0); |
2041 | #elif defined _WIN32 |
2048 | #elif defined _WIN32 |
2042 | /* maybe this should round up, as ms is very low resolution */ |
2049 | /* maybe this should round up, as ms is very low resolution */ |
2043 | /* compared to select (µs) or nanosleep (ns) */ |
2050 | /* compared to select (µs) or nanosleep (ns) */ |
2044 | Sleep ((unsigned long)(delay * 1e3)); |
2051 | Sleep ((unsigned long)(EV_TS_TO_MSEC (delay))); |
2045 | #else |
2052 | #else |
2046 | struct timeval tv; |
2053 | struct timeval tv; |
2047 | |
2054 | |
2048 | /* here we rely on sys/time.h + sys/types.h + unistd.h providing select */ |
2055 | /* here we rely on sys/time.h + sys/types.h + unistd.h providing select */ |
2049 | /* something not guaranteed by newer posix versions, but guaranteed */ |
2056 | /* something not guaranteed by newer posix versions, but guaranteed */ |
… | |
… | |
2399 | |
2406 | |
2400 | /* find minimum child */ |
2407 | /* find minimum child */ |
2401 | if (ecb_expect_true (pos + DHEAP - 1 < E)) |
2408 | if (ecb_expect_true (pos + DHEAP - 1 < E)) |
2402 | { |
2409 | { |
2403 | /* fast path */ (minpos = pos + 0), (minat = ANHE_at (*minpos)); |
2410 | /* fast path */ (minpos = pos + 0), (minat = ANHE_at (*minpos)); |
2404 | if ( ANHE_at (pos [1]) < minat) (minpos = pos + 1), (minat = ANHE_at (*minpos)); |
2411 | if ( minat > ANHE_at (pos [1])) (minpos = pos + 1), (minat = ANHE_at (*minpos)); |
2405 | if ( ANHE_at (pos [2]) < minat) (minpos = pos + 2), (minat = ANHE_at (*minpos)); |
2412 | if ( minat > ANHE_at (pos [2])) (minpos = pos + 2), (minat = ANHE_at (*minpos)); |
2406 | if ( ANHE_at (pos [3]) < minat) (minpos = pos + 3), (minat = ANHE_at (*minpos)); |
2413 | if ( minat > ANHE_at (pos [3])) (minpos = pos + 3), (minat = ANHE_at (*minpos)); |
2407 | } |
2414 | } |
2408 | else if (pos < E) |
2415 | else if (pos < E) |
2409 | { |
2416 | { |
2410 | /* slow path */ (minpos = pos + 0), (minat = ANHE_at (*minpos)); |
2417 | /* slow path */ (minpos = pos + 0), (minat = ANHE_at (*minpos)); |
2411 | if (pos + 1 < E && ANHE_at (pos [1]) < minat) (minpos = pos + 1), (minat = ANHE_at (*minpos)); |
2418 | if (pos + 1 < E && minat > ANHE_at (pos [1])) (minpos = pos + 1), (minat = ANHE_at (*minpos)); |
2412 | if (pos + 2 < E && ANHE_at (pos [2]) < minat) (minpos = pos + 2), (minat = ANHE_at (*minpos)); |
2419 | if (pos + 2 < E && minat > ANHE_at (pos [2])) (minpos = pos + 2), (minat = ANHE_at (*minpos)); |
2413 | if (pos + 3 < E && ANHE_at (pos [3]) < minat) (minpos = pos + 3), (minat = ANHE_at (*minpos)); |
2420 | if (pos + 3 < E && minat > ANHE_at (pos [3])) (minpos = pos + 3), (minat = ANHE_at (*minpos)); |
2414 | } |
2421 | } |
2415 | else |
2422 | else |
2416 | break; |
2423 | break; |
2417 | |
2424 | |
2418 | if (ANHE_at (he) <= minat) |
2425 | if (ANHE_at (he) <= minat) |
… | |
… | |
2426 | |
2433 | |
2427 | heap [k] = he; |
2434 | heap [k] = he; |
2428 | ev_active (ANHE_w (he)) = k; |
2435 | ev_active (ANHE_w (he)) = k; |
2429 | } |
2436 | } |
2430 | |
2437 | |
2431 | #else /* 4HEAP */ |
2438 | #else /* not 4HEAP */ |
2432 | |
2439 | |
2433 | #define HEAP0 1 |
2440 | #define HEAP0 1 |
2434 | #define HPARENT(k) ((k) >> 1) |
2441 | #define HPARENT(k) ((k) >> 1) |
2435 | #define UPHEAP_DONE(p,k) (!(p)) |
2442 | #define UPHEAP_DONE(p,k) (!(p)) |
2436 | |
2443 | |
… | |
… | |
3540 | { |
3547 | { |
3541 | ev_at (w) += w->repeat; |
3548 | ev_at (w) += w->repeat; |
3542 | if (ev_at (w) < mn_now) |
3549 | if (ev_at (w) < mn_now) |
3543 | ev_at (w) = mn_now; |
3550 | ev_at (w) = mn_now; |
3544 | |
3551 | |
3545 | assert (("libev: negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
3552 | assert (("libev: negative ev_timer repeat value found while processing timers", w->repeat > EV_TS_CONST (0.))); |
3546 | |
3553 | |
3547 | ANHE_at_cache (timers [HEAP0]); |
3554 | ANHE_at_cache (timers [HEAP0]); |
3548 | downheap (timers, timercnt, HEAP0); |
3555 | downheap (timers, timercnt, HEAP0); |
3549 | } |
3556 | } |
3550 | else |
3557 | else |
… | |
… | |
3681 | |
3688 | |
3682 | mn_now = get_clock (); |
3689 | mn_now = get_clock (); |
3683 | |
3690 | |
3684 | /* only fetch the realtime clock every 0.5*MIN_TIMEJUMP seconds */ |
3691 | /* only fetch the realtime clock every 0.5*MIN_TIMEJUMP seconds */ |
3685 | /* interpolate in the meantime */ |
3692 | /* interpolate in the meantime */ |
3686 | if (ecb_expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) |
3693 | if (ecb_expect_true (mn_now - now_floor < EV_TS_CONST (MIN_TIMEJUMP * .5))) |
3687 | { |
3694 | { |
3688 | ev_rt_now = rtmn_diff + mn_now; |
3695 | ev_rt_now = rtmn_diff + mn_now; |
3689 | return; |
3696 | return; |
3690 | } |
3697 | } |
3691 | |
3698 | |
… | |
… | |
3705 | ev_tstamp diff; |
3712 | ev_tstamp diff; |
3706 | rtmn_diff = ev_rt_now - mn_now; |
3713 | rtmn_diff = ev_rt_now - mn_now; |
3707 | |
3714 | |
3708 | diff = odiff - rtmn_diff; |
3715 | diff = odiff - rtmn_diff; |
3709 | |
3716 | |
3710 | if (ecb_expect_true ((diff < 0. ? -diff : diff) < MIN_TIMEJUMP)) |
3717 | if (ecb_expect_true ((diff < EV_TS_CONST (0.) ? -diff : diff) < EV_TS_CONST (MIN_TIMEJUMP))) |
3711 | return; /* all is well */ |
3718 | return; /* all is well */ |
3712 | |
3719 | |
3713 | ev_rt_now = ev_time (); |
3720 | ev_rt_now = ev_time (); |
3714 | mn_now = get_clock (); |
3721 | mn_now = get_clock (); |
3715 | now_floor = mn_now; |
3722 | now_floor = mn_now; |
… | |
… | |
3724 | else |
3731 | else |
3725 | #endif |
3732 | #endif |
3726 | { |
3733 | { |
3727 | ev_rt_now = ev_time (); |
3734 | ev_rt_now = ev_time (); |
3728 | |
3735 | |
3729 | if (ecb_expect_false (mn_now > ev_rt_now || ev_rt_now > mn_now + max_block + MIN_TIMEJUMP)) |
3736 | if (ecb_expect_false (mn_now > ev_rt_now || ev_rt_now > mn_now + max_block + EV_TS_CONST (MIN_TIMEJUMP))) |
3730 | { |
3737 | { |
3731 | /* adjust timers. this is easy, as the offset is the same for all of them */ |
3738 | /* adjust timers. this is easy, as the offset is the same for all of them */ |
3732 | timers_reschedule (EV_A_ ev_rt_now - mn_now); |
3739 | timers_reschedule (EV_A_ ev_rt_now - mn_now); |
3733 | #if EV_PERIODIC_ENABLE |
3740 | #if EV_PERIODIC_ENABLE |
3734 | periodics_reschedule (EV_A); |
3741 | periodics_reschedule (EV_A); |
… | |
… | |
3803 | |
3810 | |
3804 | /* remember old timestamp for io_blocktime calculation */ |
3811 | /* remember old timestamp for io_blocktime calculation */ |
3805 | ev_tstamp prev_mn_now = mn_now; |
3812 | ev_tstamp prev_mn_now = mn_now; |
3806 | |
3813 | |
3807 | /* update time to cancel out callback processing overhead */ |
3814 | /* update time to cancel out callback processing overhead */ |
3808 | time_update (EV_A_ 1e100); |
3815 | time_update (EV_A_ EV_TS_CONST (EV_TSTAMP_HUGE)); |
3809 | |
3816 | |
3810 | /* from now on, we want a pipe-wake-up */ |
3817 | /* from now on, we want a pipe-wake-up */ |
3811 | pipe_write_wanted = 1; |
3818 | pipe_write_wanted = 1; |
3812 | |
3819 | |
3813 | ECB_MEMORY_FENCE; /* make sure pipe_write_wanted is visible before we check for potential skips */ |
3820 | ECB_MEMORY_FENCE; /* make sure pipe_write_wanted is visible before we check for potential skips */ |
3814 | |
3821 | |
3815 | if (ecb_expect_true (!(flags & EVRUN_NOWAIT || idleall || !activecnt || pipe_write_skipped))) |
3822 | if (ecb_expect_true (!(flags & EVRUN_NOWAIT || idleall || !activecnt || pipe_write_skipped))) |
3816 | { |
3823 | { |
3817 | waittime = MAX_BLOCKTIME; |
3824 | waittime = EV_TS_CONST (MAX_BLOCKTIME); |
3818 | |
3825 | |
3819 | if (timercnt) |
3826 | if (timercnt) |
3820 | { |
3827 | { |
3821 | ev_tstamp to = ANHE_at (timers [HEAP0]) - mn_now; |
3828 | ev_tstamp to = ANHE_at (timers [HEAP0]) - mn_now; |
3822 | if (waittime > to) waittime = to; |
3829 | if (waittime > to) waittime = to; |
… | |
… | |
3845 | sleeptime = io_blocktime - (mn_now - prev_mn_now); |
3852 | sleeptime = io_blocktime - (mn_now - prev_mn_now); |
3846 | |
3853 | |
3847 | if (sleeptime > waittime - backend_mintime) |
3854 | if (sleeptime > waittime - backend_mintime) |
3848 | sleeptime = waittime - backend_mintime; |
3855 | sleeptime = waittime - backend_mintime; |
3849 | |
3856 | |
3850 | if (ecb_expect_true (sleeptime > 0.)) |
3857 | if (ecb_expect_true (sleeptime > EV_TS_CONST (0.))) |
3851 | { |
3858 | { |
3852 | ev_sleep (sleeptime); |
3859 | ev_sleep (sleeptime); |
3853 | waittime -= sleeptime; |
3860 | waittime -= sleeptime; |
3854 | } |
3861 | } |
3855 | } |
3862 | } |
… | |
… | |
3929 | } |
3936 | } |
3930 | |
3937 | |
3931 | void |
3938 | void |
3932 | ev_now_update (EV_P) EV_NOEXCEPT |
3939 | ev_now_update (EV_P) EV_NOEXCEPT |
3933 | { |
3940 | { |
3934 | time_update (EV_A_ 1e100); |
3941 | time_update (EV_A_ EV_TSTAMP_HUGE); |
3935 | } |
3942 | } |
3936 | |
3943 | |
3937 | void |
3944 | void |
3938 | ev_suspend (EV_P) EV_NOEXCEPT |
3945 | ev_suspend (EV_P) EV_NOEXCEPT |
3939 | { |
3946 | { |
… | |
… | |
4170 | } |
4177 | } |
4171 | |
4178 | |
4172 | ev_tstamp |
4179 | ev_tstamp |
4173 | ev_timer_remaining (EV_P_ ev_timer *w) EV_NOEXCEPT |
4180 | ev_timer_remaining (EV_P_ ev_timer *w) EV_NOEXCEPT |
4174 | { |
4181 | { |
4175 | return ev_at (w) - (ev_is_active (w) ? mn_now : 0.); |
4182 | return ev_at (w) - (ev_is_active (w) ? mn_now : EV_TS_CONST (0.)); |
4176 | } |
4183 | } |
4177 | |
4184 | |
4178 | #if EV_PERIODIC_ENABLE |
4185 | #if EV_PERIODIC_ENABLE |
4179 | ecb_noinline |
4186 | ecb_noinline |
4180 | void |
4187 | void |