ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libev/ev.c
(Generate patch)

Comparing libev/ev.c (file contents):
Revision 1.508 by root, Thu Jul 11 08:29:08 2019 UTC vs.
Revision 1.513 by root, Fri Dec 20 05:20:23 2019 UTC

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
436#endif 445#endif
437 446
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
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
441#endif 458#endif
442 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
544#define EV_TSTAMP_HUGE \ 561#define EV_TSTAMP_HUGE \
545 (sizeof (time_t) >= 8 ? 10000000000000. \ 562 (sizeof (time_t) >= 8 ? 10000000000000. \
546 : 0 < (time_t)4294967295 ? 4294967295. \ 563 : 0 < (time_t)4294967295 ? 4294967295. \
547 : 2147483647.) \ 564 : 2147483647.) \
548 565
566#ifndef EV_TS_CONST
567# define EV_TS_CONST(nv) nv
549#define EV_TS_TO_MSEC(a) a * 1e3 + 0.9999 568# define EV_TS_TO_MSEC(a) a * 1e3 + 0.9999
550#define EV_TS_FROM_USEC(us) us * 1e-6 569# define EV_TS_FROM_USEC(us) us * 1e-6
551#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)
552#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)
553#define EV_TV_GET(tv) ((tv).tv_sec + (tv).tv_usec * 1e-6) 572# define EV_TV_GET(tv) ((tv).tv_sec + (tv).tv_usec * 1e-6)
554#define EV_TS_GET(ts) ((ts).tv_sec + (ts).tv_nsec * 1e-9) 573# define EV_TS_GET(ts) ((ts).tv_sec + (ts).tv_nsec * 1e-9)
574#endif
555 575
556/* 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 */
557/* ECB.H BEGIN */ 577/* ECB.H BEGIN */
558/* 578/*
559 * libecb - http://software.schmorp.de/pkg/libecb 579 * libecb - http://software.schmorp.de/pkg/libecb
1965 static struct ev_loop default_loop_struct; 1985 static struct ev_loop default_loop_struct;
1966 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 */
1967 1987
1968#else 1988#else
1969 1989
1970 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 */
1971 #define VAR(name,decl) static decl; 1991 #define VAR(name,decl) static decl;
1972 #include "ev_vars.h" 1992 #include "ev_vars.h"
1973 #undef VAR 1993 #undef VAR
1974 1994
1975 static int ev_default_loop_ptr; 1995 static int ev_default_loop_ptr;
2001 clock_gettime (CLOCK_REALTIME, &ts); 2021 clock_gettime (CLOCK_REALTIME, &ts);
2002 return EV_TS_GET (ts); 2022 return EV_TS_GET (ts);
2003 } 2023 }
2004#endif 2024#endif
2005 2025
2026 {
2006 struct timeval tv; 2027 struct timeval tv;
2007 gettimeofday (&tv, 0); 2028 gettimeofday (&tv, 0);
2008 return EV_TV_GET (tv); 2029 return EV_TV_GET (tv);
2030 }
2009} 2031}
2010#endif 2032#endif
2011 2033
2012inline_size ev_tstamp 2034inline_size ev_tstamp
2013get_clock (void) 2035get_clock (void)
2033#endif 2055#endif
2034 2056
2035void 2057void
2036ev_sleep (ev_tstamp delay) EV_NOEXCEPT 2058ev_sleep (ev_tstamp delay) EV_NOEXCEPT
2037{ 2059{
2038 if (delay > 0.) 2060 if (delay > EV_TS_CONST (0.))
2039 { 2061 {
2040#if EV_USE_NANOSLEEP 2062#if EV_USE_NANOSLEEP
2041 struct timespec ts; 2063 struct timespec ts;
2042 2064
2043 EV_TS_SET (ts, delay); 2065 EV_TS_SET (ts, delay);
3544 { 3566 {
3545 ev_at (w) += w->repeat; 3567 ev_at (w) += w->repeat;
3546 if (ev_at (w) < mn_now) 3568 if (ev_at (w) < mn_now)
3547 ev_at (w) = mn_now; 3569 ev_at (w) = mn_now;
3548 3570
3549 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.)));
3550 3572
3551 ANHE_at_cache (timers [HEAP0]); 3573 ANHE_at_cache (timers [HEAP0]);
3552 downheap (timers, timercnt, HEAP0); 3574 downheap (timers, timercnt, HEAP0);
3553 } 3575 }
3554 else 3576 else
3685 3707
3686 mn_now = get_clock (); 3708 mn_now = get_clock ();
3687 3709
3688 /* only fetch the realtime clock every 0.5*MIN_TIMEJUMP seconds */ 3710 /* only fetch the realtime clock every 0.5*MIN_TIMEJUMP seconds */
3689 /* interpolate in the meantime */ 3711 /* interpolate in the meantime */
3690 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)))
3691 { 3713 {
3692 ev_rt_now = rtmn_diff + mn_now; 3714 ev_rt_now = rtmn_diff + mn_now;
3693 return; 3715 return;
3694 } 3716 }
3695 3717
3709 ev_tstamp diff; 3731 ev_tstamp diff;
3710 rtmn_diff = ev_rt_now - mn_now; 3732 rtmn_diff = ev_rt_now - mn_now;
3711 3733
3712 diff = odiff - rtmn_diff; 3734 diff = odiff - rtmn_diff;
3713 3735
3714 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)))
3715 return; /* all is well */ 3737 return; /* all is well */
3716 3738
3717 ev_rt_now = ev_time (); 3739 ev_rt_now = ev_time ();
3718 mn_now = get_clock (); 3740 mn_now = get_clock ();
3719 now_floor = mn_now; 3741 now_floor = mn_now;
3728 else 3750 else
3729#endif 3751#endif
3730 { 3752 {
3731 ev_rt_now = ev_time (); 3753 ev_rt_now = ev_time ();
3732 3754
3733 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)))
3734 { 3756 {
3735 /* 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 */
3736 timers_reschedule (EV_A_ ev_rt_now - mn_now); 3758 timers_reschedule (EV_A_ ev_rt_now - mn_now);
3737#if EV_PERIODIC_ENABLE 3759#if EV_PERIODIC_ENABLE
3738 periodics_reschedule (EV_A); 3760 periodics_reschedule (EV_A);
3807 3829
3808 /* remember old timestamp for io_blocktime calculation */ 3830 /* remember old timestamp for io_blocktime calculation */
3809 ev_tstamp prev_mn_now = mn_now; 3831 ev_tstamp prev_mn_now = mn_now;
3810 3832
3811 /* update time to cancel out callback processing overhead */ 3833 /* update time to cancel out callback processing overhead */
3812 time_update (EV_A_ 1e100); 3834 time_update (EV_A_ EV_TS_CONST (EV_TSTAMP_HUGE));
3813 3835
3814 /* from now on, we want a pipe-wake-up */ 3836 /* from now on, we want a pipe-wake-up */
3815 pipe_write_wanted = 1; 3837 pipe_write_wanted = 1;
3816 3838
3817 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 */
3818 3840
3819 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)))
3820 { 3842 {
3821 waittime = MAX_BLOCKTIME; 3843 waittime = EV_TS_CONST (MAX_BLOCKTIME);
3822 3844
3823 if (timercnt) 3845 if (timercnt)
3824 { 3846 {
3825 ev_tstamp to = ANHE_at (timers [HEAP0]) - mn_now; 3847 ev_tstamp to = ANHE_at (timers [HEAP0]) - mn_now;
3826 if (waittime > to) waittime = to; 3848 if (waittime > to) waittime = to;
3836 3858
3837 /* don't let timeouts decrease the waittime below timeout_blocktime */ 3859 /* don't let timeouts decrease the waittime below timeout_blocktime */
3838 if (ecb_expect_false (waittime < timeout_blocktime)) 3860 if (ecb_expect_false (waittime < timeout_blocktime))
3839 waittime = timeout_blocktime; 3861 waittime = timeout_blocktime;
3840 3862
3841 /* at this point, we NEED to wait, so we have to ensure */ 3863 /* now there are two more special cases left, either we have
3842 /* 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 weait for a minimum
3866 * amount of time for some event loop backends
3867 */
3843 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.)
3844 waittime = backend_mintime; 3871 : backend_mintime;
3845 3872
3846 /* extra check because io_blocktime is commonly 0 */ 3873 /* extra check because io_blocktime is commonly 0 */
3847 if (ecb_expect_false (io_blocktime)) 3874 if (ecb_expect_false (io_blocktime))
3848 { 3875 {
3849 sleeptime = io_blocktime - (mn_now - prev_mn_now); 3876 sleeptime = io_blocktime - (mn_now - prev_mn_now);
3850 3877
3851 if (sleeptime > waittime - backend_mintime) 3878 if (sleeptime > waittime - backend_mintime)
3852 sleeptime = waittime - backend_mintime; 3879 sleeptime = waittime - backend_mintime;
3853 3880
3854 if (ecb_expect_true (sleeptime > 0.)) 3881 if (ecb_expect_true (sleeptime > EV_TS_CONST (0.)))
3855 { 3882 {
3856 ev_sleep (sleeptime); 3883 ev_sleep (sleeptime);
3857 waittime -= sleeptime; 3884 waittime -= sleeptime;
3858 } 3885 }
3859 } 3886 }
3933} 3960}
3934 3961
3935void 3962void
3936ev_now_update (EV_P) EV_NOEXCEPT 3963ev_now_update (EV_P) EV_NOEXCEPT
3937{ 3964{
3938 time_update (EV_A_ 1e100); 3965 time_update (EV_A_ EV_TSTAMP_HUGE);
3939} 3966}
3940 3967
3941void 3968void
3942ev_suspend (EV_P) EV_NOEXCEPT 3969ev_suspend (EV_P) EV_NOEXCEPT
3943{ 3970{
4174} 4201}
4175 4202
4176ev_tstamp 4203ev_tstamp
4177ev_timer_remaining (EV_P_ ev_timer *w) EV_NOEXCEPT 4204ev_timer_remaining (EV_P_ ev_timer *w) EV_NOEXCEPT
4178{ 4205{
4179 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.));
4180} 4207}
4181 4208
4182#if EV_PERIODIC_ENABLE 4209#if EV_PERIODIC_ENABLE
4183ecb_noinline 4210ecb_noinline
4184void 4211void

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines