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

Comparing libev/ev.c (file contents):
Revision 1.510 by root, Wed Aug 28 09:45:49 2019 UTC vs.
Revision 1.517 by root, Tue Dec 24 13:52:58 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
183# if HAVE_SYS_TIMERFD_H
184# ifndef EV_USE_TIMERFD
185# define EV_USE_TIMERFD EV_FEATURE_OS
186# endif
187# else
188# undef EV_USE_TIMERFD
189# define EV_USE_TIMERFD 0
190# endif
191
174#endif 192#endif
175 193
176/* OS X, in its infinite idiocy, actually HARDCODES 194/* OS X, in its infinite idiocy, actually HARDCODES
177 * a limit of 1024 into their select. Where people have brains, 195 * a limit of 1024 into their select. Where people have brains,
178 * OS X engineers apparently have a vacuum. Or maybe they were 196 * OS X engineers apparently have a vacuum. Or maybe they were
333# define EV_USE_LINUXAIO 0 351# define EV_USE_LINUXAIO 0
334# endif 352# endif
335#endif 353#endif
336 354
337#ifndef EV_USE_IOURING 355#ifndef EV_USE_IOURING
338# if __linux 356# if __linux /* later checks might disable again */
339# define EV_USE_IOURING 0 357# define EV_USE_IOURING 1
340# else 358# else
341# define EV_USE_IOURING 0 359# define EV_USE_IOURING 0
342# endif 360# endif
343#endif 361#endif
344 362
369#ifndef EV_USE_SIGNALFD 387#ifndef EV_USE_SIGNALFD
370# if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 7)) 388# if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 7))
371# define EV_USE_SIGNALFD EV_FEATURE_OS 389# define EV_USE_SIGNALFD EV_FEATURE_OS
372# else 390# else
373# define EV_USE_SIGNALFD 0 391# define EV_USE_SIGNALFD 0
392# endif
393#endif
394
395#ifndef EV_USE_TIMERFD
396# if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 8))
397# define EV_USE_TIMERFD EV_FEATURE_OS
398# else
399# define EV_USE_TIMERFD 0
374# endif 400# endif
375#endif 401#endif
376 402
377#if 0 /* debugging */ 403#if 0 /* debugging */
378# define EV_VERIFY 3 404# define EV_VERIFY 3
438#if !EV_STAT_ENABLE 464#if !EV_STAT_ENABLE
439# undef EV_USE_INOTIFY 465# undef EV_USE_INOTIFY
440# define EV_USE_INOTIFY 0 466# define EV_USE_INOTIFY 0
441#endif 467#endif
442 468
469#if __linux && EV_USE_IOURING
470# include <linux/version.h>
471# if LINUX_VERSION_CODE < KERNEL_VERSION(4,14,0)
472# undef EV_USE_IOURING
473# define EV_USE_IOURING 0
474# endif
475#endif
476
443#if !EV_USE_NANOSLEEP 477#if !EV_USE_NANOSLEEP
444/* hp-ux has it in sys/time.h, which we unconditionally include above */ 478/* hp-ux has it in sys/time.h, which we unconditionally include above */
445# if !defined _WIN32 && !defined __hpux 479# if !defined _WIN32 && !defined __hpux
446# include <sys/select.h> 480# include <sys/select.h>
447# endif 481# endif
481# define EV_USE_INOTIFY 0 515# define EV_USE_INOTIFY 0
482# endif 516# endif
483#endif 517#endif
484 518
485#if EV_USE_EVENTFD 519#if EV_USE_EVENTFD
486/* our minimum requirement is glibc 2.7 which has the stub, but not the header */ 520/* our minimum requirement is glibc 2.7 which has the stub, but not the full header */
487# include <stdint.h> 521# include <stdint.h>
488# ifndef EFD_NONBLOCK 522# ifndef EFD_NONBLOCK
489# define EFD_NONBLOCK O_NONBLOCK 523# define EFD_NONBLOCK O_NONBLOCK
490# endif 524# endif
491# ifndef EFD_CLOEXEC 525# ifndef EFD_CLOEXEC
497# endif 531# endif
498EV_CPP(extern "C") int (eventfd) (unsigned int initval, int flags); 532EV_CPP(extern "C") int (eventfd) (unsigned int initval, int flags);
499#endif 533#endif
500 534
501#if EV_USE_SIGNALFD 535#if EV_USE_SIGNALFD
502/* our minimum requirement is glibc 2.7 which has the stub, but not the header */ 536/* our minimum requirement is glibc 2.7 which has the stub, but not the full header */
503# include <stdint.h> 537# include <stdint.h>
504# ifndef SFD_NONBLOCK 538# ifndef SFD_NONBLOCK
505# define SFD_NONBLOCK O_NONBLOCK 539# define SFD_NONBLOCK O_NONBLOCK
506# endif 540# endif
507# ifndef SFD_CLOEXEC 541# ifndef SFD_CLOEXEC
509# define SFD_CLOEXEC O_CLOEXEC 543# define SFD_CLOEXEC O_CLOEXEC
510# else 544# else
511# define SFD_CLOEXEC 02000000 545# define SFD_CLOEXEC 02000000
512# endif 546# endif
513# endif 547# endif
514EV_CPP (extern "C") int signalfd (int fd, const sigset_t *mask, int flags); 548EV_CPP (extern "C") int (signalfd) (int fd, const sigset_t *mask, int flags);
515 549
516struct signalfd_siginfo 550struct signalfd_siginfo
517{ 551{
518 uint32_t ssi_signo; 552 uint32_t ssi_signo;
519 char pad[128 - sizeof (uint32_t)]; 553 char pad[128 - sizeof (uint32_t)];
520}; 554};
555#endif
556
557/* for timerfd, libev core requires TFD_TIMER_CANCEL_ON_SET &c */
558#if EV_USE_TIMERFD
559# include <sys/timerfd.h>
560/* timerfd is only used for periodics */
561# if !(defined (TFD_TIMER_CANCEL_ON_SET) && defined (TFD_CLOEXEC) && defined (TFD_NONBLOCK)) || !EV_PERIODIC_ENABLE
562# undef EV_USE_TIMERFD
563# define EV_USE_TIMERFD 0
564# endif
521#endif 565#endif
522 566
523/*****************************************************************************/ 567/*****************************************************************************/
524 568
525#if EV_VERIFY >= 3 569#if EV_VERIFY >= 3
2831 2875
2832#endif 2876#endif
2833 2877
2834/*****************************************************************************/ 2878/*****************************************************************************/
2835 2879
2880#if EV_USE_TIMERFD
2881
2882static void periodics_reschedule (EV_P);
2883
2884static void
2885timerfdcb (EV_P_ ev_io *iow, int revents)
2886{
2887 struct itimerspec its = { 0 };
2888
2889 /* since we can't easily come zup with a (portable) maximum value of time_t,
2890 * we wake up once per month, which hopefully is rare enough to not
2891 * be a problem. */
2892 its.it_value.tv_sec = ev_rt_now + 86400 * 30;
2893 timerfd_settime (timerfd, TFD_TIMER_ABSTIME | TFD_TIMER_CANCEL_ON_SET, &its, 0);
2894
2895 ev_rt_now = ev_time ();
2896 /* periodics_reschedule only needs ev_rt_now */
2897 /* but maybe in the future we want the full treatment. */
2898 /*
2899 now_floor = EV_TS_CONST (0.);
2900 time_update (EV_A_ EV_TSTAMP_HUGE);
2901 */
2902 periodics_reschedule (EV_A);
2903}
2904
2905ecb_noinline ecb_cold
2906static void
2907evtimerfd_init (EV_P)
2908{
2909 if (!ev_is_active (&timerfd_w))
2910 {
2911 timerfd = timerfd_create (CLOCK_REALTIME, TFD_NONBLOCK | TFD_CLOEXEC);
2912
2913 if (timerfd >= 0)
2914 {
2915 fd_intern (timerfd); /* just to be sure */
2916
2917 ev_io_init (&timerfd_w, timerfdcb, timerfd, EV_READ);
2918 ev_set_priority (&timerfd_w, EV_MINPRI);
2919 ev_io_start (EV_A_ &timerfd_w);
2920 ev_unref (EV_A); /* watcher should not keep loop alive */
2921
2922 /* (re-) arm timer */
2923 timerfdcb (EV_A_ 0, 0);
2924 }
2925 }
2926}
2927
2928#endif
2929
2930/*****************************************************************************/
2931
2836#if EV_USE_IOCP 2932#if EV_USE_IOCP
2837# include "ev_iocp.c" 2933# include "ev_iocp.c"
2838#endif 2934#endif
2839#if EV_USE_PORT 2935#if EV_USE_PORT
2840# include "ev_port.c" 2936# include "ev_port.c"
3072 fs_fd = flags & EVFLAG_NOINOTIFY ? -1 : -2; 3168 fs_fd = flags & EVFLAG_NOINOTIFY ? -1 : -2;
3073#endif 3169#endif
3074#if EV_USE_SIGNALFD 3170#if EV_USE_SIGNALFD
3075 sigfd = flags & EVFLAG_SIGNALFD ? -2 : -1; 3171 sigfd = flags & EVFLAG_SIGNALFD ? -2 : -1;
3076#endif 3172#endif
3173#if EV_USE_TIMERFD
3174 timerfd = flags & EVFLAG_NOTIMERFD ? -1 : -2;
3175#endif
3077 3176
3078 if (!(flags & EVBACKEND_MASK)) 3177 if (!(flags & EVBACKEND_MASK))
3079 flags |= ev_recommended_backends (); 3178 flags |= ev_recommended_backends ();
3080 3179
3081#if EV_USE_IOCP 3180#if EV_USE_IOCP
3152 } 3251 }
3153 3252
3154#if EV_USE_SIGNALFD 3253#if EV_USE_SIGNALFD
3155 if (ev_is_active (&sigfd_w)) 3254 if (ev_is_active (&sigfd_w))
3156 close (sigfd); 3255 close (sigfd);
3256#endif
3257
3258#if EV_USE_TIMERFD
3259 if (ev_is_active (&timerfd_w))
3260 close (timerfd);
3157#endif 3261#endif
3158 3262
3159#if EV_USE_INOTIFY 3263#if EV_USE_INOTIFY
3160 if (fs_fd >= 0) 3264 if (fs_fd >= 0)
3161 close (fs_fd); 3265 close (fs_fd);
3254#endif 3358#endif
3255#if EV_USE_INOTIFY 3359#if EV_USE_INOTIFY
3256 infy_fork (EV_A); 3360 infy_fork (EV_A);
3257#endif 3361#endif
3258 3362
3363 if (postfork != 2)
3364 {
3365 #if EV_USE_SIGNALFD
3366 /* surprisingly, nothing needs to be done for signalfd, accoridng to docs, it does the right thing on fork */
3367 #endif
3368
3369 #if EV_USE_TIMERFD
3370 if (ev_is_active (&timerfd_w))
3371 {
3372 ev_ref (EV_A);
3373 ev_io_stop (EV_A_ &timerfd_w);
3374
3375 close (timerfd);
3376 timerfd = -2;
3377
3378 evtimerfd_init (EV_A);
3379 /* reschedule periodics, in case we missed something */
3380 ev_feed_event (EV_A_ &timerfd_w, EV_CUSTOM);
3381 }
3382 #endif
3383
3259#if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE 3384 #if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE
3260 if (ev_is_active (&pipe_w) && postfork != 2) 3385 if (ev_is_active (&pipe_w))
3261 { 3386 {
3262 /* pipe_write_wanted must be false now, so modifying fd vars should be safe */ 3387 /* pipe_write_wanted must be false now, so modifying fd vars should be safe */
3263 3388
3264 ev_ref (EV_A); 3389 ev_ref (EV_A);
3265 ev_io_stop (EV_A_ &pipe_w); 3390 ev_io_stop (EV_A_ &pipe_w);
3266 3391
3267 if (evpipe [0] >= 0) 3392 if (evpipe [0] >= 0)
3268 EV_WIN32_CLOSE_FD (evpipe [0]); 3393 EV_WIN32_CLOSE_FD (evpipe [0]);
3269 3394
3270 evpipe_init (EV_A); 3395 evpipe_init (EV_A);
3271 /* iterate over everything, in case we missed something before */ 3396 /* iterate over everything, in case we missed something before */
3272 ev_feed_event (EV_A_ &pipe_w, EV_CUSTOM); 3397 ev_feed_event (EV_A_ &pipe_w, EV_CUSTOM);
3398 }
3399 #endif
3273 } 3400 }
3274#endif
3275 3401
3276 postfork = 0; 3402 postfork = 0;
3277} 3403}
3278 3404
3279#if EV_MULTIPLICITY 3405#if EV_MULTIPLICITY
3841 3967
3842 /* don't let timeouts decrease the waittime below timeout_blocktime */ 3968 /* don't let timeouts decrease the waittime below timeout_blocktime */
3843 if (ecb_expect_false (waittime < timeout_blocktime)) 3969 if (ecb_expect_false (waittime < timeout_blocktime))
3844 waittime = timeout_blocktime; 3970 waittime = timeout_blocktime;
3845 3971
3846 /* at this point, we NEED to wait, so we have to ensure */ 3972 /* now there are two more special cases left, either we have
3847 /* to pass a minimum nonzero value to the backend */ 3973 * already-expired timers, so we should not sleep, or we have timers
3974 * that expire very soon, in which case we need to wait for a minimum
3975 * amount of time for some event loop backends.
3976 */
3848 if (ecb_expect_false (waittime < backend_mintime)) 3977 if (ecb_expect_false (waittime < backend_mintime))
3978 waittime = waittime <= EV_TS_CONST (0.)
3979 ? EV_TS_CONST (0.)
3849 waittime = backend_mintime; 3980 : backend_mintime;
3850 3981
3851 /* extra check because io_blocktime is commonly 0 */ 3982 /* extra check because io_blocktime is commonly 0 */
3852 if (ecb_expect_false (io_blocktime)) 3983 if (ecb_expect_false (io_blocktime))
3853 { 3984 {
3854 sleeptime = io_blocktime - (mn_now - prev_mn_now); 3985 sleeptime = io_blocktime - (mn_now - prev_mn_now);
4189void 4320void
4190ev_periodic_start (EV_P_ ev_periodic *w) EV_NOEXCEPT 4321ev_periodic_start (EV_P_ ev_periodic *w) EV_NOEXCEPT
4191{ 4322{
4192 if (ecb_expect_false (ev_is_active (w))) 4323 if (ecb_expect_false (ev_is_active (w)))
4193 return; 4324 return;
4325
4326#if EV_USE_TIMERFD
4327 if (timerfd == -2)
4328 evtimerfd_init (EV_A);
4329#endif
4194 4330
4195 if (w->reschedule_cb) 4331 if (w->reschedule_cb)
4196 ev_at (w) = w->reschedule_cb (w, ev_rt_now); 4332 ev_at (w) = w->reschedule_cb (w, ev_rt_now);
4197 else if (w->interval) 4333 else if (w->interval)
4198 { 4334 {
4940 ev_run (EV_A_ EVRUN_NOWAIT); 5076 ev_run (EV_A_ EVRUN_NOWAIT);
4941 } 5077 }
4942 } 5078 }
4943} 5079}
4944 5080
5081#if EV_FORK_ENABLE
4945static void 5082static void
4946embed_fork_cb (EV_P_ ev_fork *fork_w, int revents) 5083embed_fork_cb (EV_P_ ev_fork *fork_w, int revents)
4947{ 5084{
4948 ev_embed *w = (ev_embed *)(((char *)fork_w) - offsetof (ev_embed, fork)); 5085 ev_embed *w = (ev_embed *)(((char *)fork_w) - offsetof (ev_embed, fork));
4949 5086
4956 ev_run (EV_A_ EVRUN_NOWAIT); 5093 ev_run (EV_A_ EVRUN_NOWAIT);
4957 } 5094 }
4958 5095
4959 ev_embed_start (EV_A_ w); 5096 ev_embed_start (EV_A_ w);
4960} 5097}
5098#endif
4961 5099
4962#if 0 5100#if 0
4963static void 5101static void
4964embed_idle_cb (EV_P_ ev_idle *idle, int revents) 5102embed_idle_cb (EV_P_ ev_idle *idle, int revents)
4965{ 5103{
4986 5124
4987 ev_prepare_init (&w->prepare, embed_prepare_cb); 5125 ev_prepare_init (&w->prepare, embed_prepare_cb);
4988 ev_set_priority (&w->prepare, EV_MINPRI); 5126 ev_set_priority (&w->prepare, EV_MINPRI);
4989 ev_prepare_start (EV_A_ &w->prepare); 5127 ev_prepare_start (EV_A_ &w->prepare);
4990 5128
5129#if EV_FORK_ENABLE
4991 ev_fork_init (&w->fork, embed_fork_cb); 5130 ev_fork_init (&w->fork, embed_fork_cb);
4992 ev_fork_start (EV_A_ &w->fork); 5131 ev_fork_start (EV_A_ &w->fork);
5132#endif
4993 5133
4994 /*ev_idle_init (&w->idle, e,bed_idle_cb);*/ 5134 /*ev_idle_init (&w->idle, e,bed_idle_cb);*/
4995 5135
4996 ev_start (EV_A_ (W)w, 1); 5136 ev_start (EV_A_ (W)w, 1);
4997 5137
5007 5147
5008 EV_FREQUENT_CHECK; 5148 EV_FREQUENT_CHECK;
5009 5149
5010 ev_io_stop (EV_A_ &w->io); 5150 ev_io_stop (EV_A_ &w->io);
5011 ev_prepare_stop (EV_A_ &w->prepare); 5151 ev_prepare_stop (EV_A_ &w->prepare);
5152#if EV_FORK_ENABLE
5012 ev_fork_stop (EV_A_ &w->fork); 5153 ev_fork_stop (EV_A_ &w->fork);
5154#endif
5013 5155
5014 ev_stop (EV_A_ (W)w); 5156 ev_stop (EV_A_ (W)w);
5015 5157
5016 EV_FREQUENT_CHECK; 5158 EV_FREQUENT_CHECK;
5017} 5159}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines