… | |
… | |
178 | # else |
178 | # else |
179 | # undef EV_USE_EVENTFD |
179 | # undef EV_USE_EVENTFD |
180 | # define EV_USE_EVENTFD 0 |
180 | # define EV_USE_EVENTFD 0 |
181 | # endif |
181 | # endif |
182 | |
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 | |
183 | #endif |
192 | #endif |
184 | |
193 | |
185 | /* OS X, in its infinite idiocy, actually HARDCODES |
194 | /* OS X, in its infinite idiocy, actually HARDCODES |
186 | * a limit of 1024 into their select. Where people have brains, |
195 | * a limit of 1024 into their select. Where people have brains, |
187 | * OS X engineers apparently have a vacuum. Or maybe they were |
196 | * OS X engineers apparently have a vacuum. Or maybe they were |
… | |
… | |
381 | # else |
390 | # else |
382 | # define EV_USE_SIGNALFD 0 |
391 | # define EV_USE_SIGNALFD 0 |
383 | # endif |
392 | # endif |
384 | #endif |
393 | #endif |
385 | |
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 |
|
|
400 | # endif |
|
|
401 | #endif |
|
|
402 | |
386 | #if 0 /* debugging */ |
403 | #if 0 /* debugging */ |
387 | # define EV_VERIFY 3 |
404 | # define EV_VERIFY 3 |
388 | # define EV_USE_4HEAP 1 |
405 | # define EV_USE_4HEAP 1 |
389 | # define EV_HEAP_CACHE_AT 1 |
406 | # define EV_HEAP_CACHE_AT 1 |
390 | #endif |
407 | #endif |
… | |
… | |
498 | # define EV_USE_INOTIFY 0 |
515 | # define EV_USE_INOTIFY 0 |
499 | # endif |
516 | # endif |
500 | #endif |
517 | #endif |
501 | |
518 | |
502 | #if EV_USE_EVENTFD |
519 | #if EV_USE_EVENTFD |
503 | /* 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 */ |
504 | # include <stdint.h> |
521 | # include <stdint.h> |
505 | # ifndef EFD_NONBLOCK |
522 | # ifndef EFD_NONBLOCK |
506 | # define EFD_NONBLOCK O_NONBLOCK |
523 | # define EFD_NONBLOCK O_NONBLOCK |
507 | # endif |
524 | # endif |
508 | # ifndef EFD_CLOEXEC |
525 | # ifndef EFD_CLOEXEC |
… | |
… | |
514 | # endif |
531 | # endif |
515 | EV_CPP(extern "C") int (eventfd) (unsigned int initval, int flags); |
532 | EV_CPP(extern "C") int (eventfd) (unsigned int initval, int flags); |
516 | #endif |
533 | #endif |
517 | |
534 | |
518 | #if EV_USE_SIGNALFD |
535 | #if EV_USE_SIGNALFD |
519 | /* 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 */ |
520 | # include <stdint.h> |
537 | # include <stdint.h> |
521 | # ifndef SFD_NONBLOCK |
538 | # ifndef SFD_NONBLOCK |
522 | # define SFD_NONBLOCK O_NONBLOCK |
539 | # define SFD_NONBLOCK O_NONBLOCK |
523 | # endif |
540 | # endif |
524 | # ifndef SFD_CLOEXEC |
541 | # ifndef SFD_CLOEXEC |
… | |
… | |
526 | # define SFD_CLOEXEC O_CLOEXEC |
543 | # define SFD_CLOEXEC O_CLOEXEC |
527 | # else |
544 | # else |
528 | # define SFD_CLOEXEC 02000000 |
545 | # define SFD_CLOEXEC 02000000 |
529 | # endif |
546 | # endif |
530 | # endif |
547 | # endif |
531 | EV_CPP (extern "C") int signalfd (int fd, const sigset_t *mask, int flags); |
548 | EV_CPP (extern "C") int (signalfd) (int fd, const sigset_t *mask, int flags); |
532 | |
549 | |
533 | struct signalfd_siginfo |
550 | struct signalfd_siginfo |
534 | { |
551 | { |
535 | uint32_t ssi_signo; |
552 | uint32_t ssi_signo; |
536 | char pad[128 - sizeof (uint32_t)]; |
553 | char pad[128 - sizeof (uint32_t)]; |
537 | }; |
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 |
538 | #endif |
565 | #endif |
539 | |
566 | |
540 | /*****************************************************************************/ |
567 | /*****************************************************************************/ |
541 | |
568 | |
542 | #if EV_VERIFY >= 3 |
569 | #if EV_VERIFY >= 3 |
… | |
… | |
2848 | |
2875 | |
2849 | #endif |
2876 | #endif |
2850 | |
2877 | |
2851 | /*****************************************************************************/ |
2878 | /*****************************************************************************/ |
2852 | |
2879 | |
|
|
2880 | #if EV_USE_TIMERFD |
|
|
2881 | |
|
|
2882 | static void periodics_reschedule (EV_P); |
|
|
2883 | |
|
|
2884 | static void |
|
|
2885 | timerfdcb (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 | |
|
|
2905 | ecb_noinline ecb_cold |
|
|
2906 | static void |
|
|
2907 | evtimerfd_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 | |
2853 | #if EV_USE_IOCP |
2932 | #if EV_USE_IOCP |
2854 | # include "ev_iocp.c" |
2933 | # include "ev_iocp.c" |
2855 | #endif |
2934 | #endif |
2856 | #if EV_USE_PORT |
2935 | #if EV_USE_PORT |
2857 | # include "ev_port.c" |
2936 | # include "ev_port.c" |
… | |
… | |
3089 | fs_fd = flags & EVFLAG_NOINOTIFY ? -1 : -2; |
3168 | fs_fd = flags & EVFLAG_NOINOTIFY ? -1 : -2; |
3090 | #endif |
3169 | #endif |
3091 | #if EV_USE_SIGNALFD |
3170 | #if EV_USE_SIGNALFD |
3092 | sigfd = flags & EVFLAG_SIGNALFD ? -2 : -1; |
3171 | sigfd = flags & EVFLAG_SIGNALFD ? -2 : -1; |
3093 | #endif |
3172 | #endif |
|
|
3173 | #if EV_USE_TIMERFD |
|
|
3174 | timerfd = flags & EVFLAG_NOTIMERFD ? -1 : -2; |
|
|
3175 | #endif |
3094 | |
3176 | |
3095 | if (!(flags & EVBACKEND_MASK)) |
3177 | if (!(flags & EVBACKEND_MASK)) |
3096 | flags |= ev_recommended_backends (); |
3178 | flags |= ev_recommended_backends (); |
3097 | |
3179 | |
3098 | #if EV_USE_IOCP |
3180 | #if EV_USE_IOCP |
… | |
… | |
3169 | } |
3251 | } |
3170 | |
3252 | |
3171 | #if EV_USE_SIGNALFD |
3253 | #if EV_USE_SIGNALFD |
3172 | if (ev_is_active (&sigfd_w)) |
3254 | if (ev_is_active (&sigfd_w)) |
3173 | close (sigfd); |
3255 | close (sigfd); |
|
|
3256 | #endif |
|
|
3257 | |
|
|
3258 | #if EV_USE_TIMERFD |
|
|
3259 | if (ev_is_active (&timerfd_w)) |
|
|
3260 | close (timerfd); |
3174 | #endif |
3261 | #endif |
3175 | |
3262 | |
3176 | #if EV_USE_INOTIFY |
3263 | #if EV_USE_INOTIFY |
3177 | if (fs_fd >= 0) |
3264 | if (fs_fd >= 0) |
3178 | close (fs_fd); |
3265 | close (fs_fd); |
… | |
… | |
3271 | #endif |
3358 | #endif |
3272 | #if EV_USE_INOTIFY |
3359 | #if EV_USE_INOTIFY |
3273 | infy_fork (EV_A); |
3360 | infy_fork (EV_A); |
3274 | #endif |
3361 | #endif |
3275 | |
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 | |
3276 | #if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE |
3384 | #if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE |
3277 | if (ev_is_active (&pipe_w) && postfork != 2) |
3385 | if (ev_is_active (&pipe_w)) |
3278 | { |
3386 | { |
3279 | /* 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 */ |
3280 | |
3388 | |
3281 | ev_ref (EV_A); |
3389 | ev_ref (EV_A); |
3282 | ev_io_stop (EV_A_ &pipe_w); |
3390 | ev_io_stop (EV_A_ &pipe_w); |
3283 | |
3391 | |
3284 | if (evpipe [0] >= 0) |
3392 | if (evpipe [0] >= 0) |
3285 | EV_WIN32_CLOSE_FD (evpipe [0]); |
3393 | EV_WIN32_CLOSE_FD (evpipe [0]); |
3286 | |
3394 | |
3287 | evpipe_init (EV_A); |
3395 | evpipe_init (EV_A); |
3288 | /* iterate over everything, in case we missed something before */ |
3396 | /* iterate over everything, in case we missed something before */ |
3289 | ev_feed_event (EV_A_ &pipe_w, EV_CUSTOM); |
3397 | ev_feed_event (EV_A_ &pipe_w, EV_CUSTOM); |
|
|
3398 | } |
|
|
3399 | #endif |
3290 | } |
3400 | } |
3291 | #endif |
|
|
3292 | |
3401 | |
3293 | postfork = 0; |
3402 | postfork = 0; |
3294 | } |
3403 | } |
3295 | |
3404 | |
3296 | #if EV_MULTIPLICITY |
3405 | #if EV_MULTIPLICITY |
… | |
… | |
3858 | |
3967 | |
3859 | /* don't let timeouts decrease the waittime below timeout_blocktime */ |
3968 | /* don't let timeouts decrease the waittime below timeout_blocktime */ |
3860 | if (ecb_expect_false (waittime < timeout_blocktime)) |
3969 | if (ecb_expect_false (waittime < timeout_blocktime)) |
3861 | waittime = timeout_blocktime; |
3970 | waittime = timeout_blocktime; |
3862 | |
3971 | |
3863 | /* at this point, we NEED to wait, so we have to ensure */ |
3972 | /* now there are two more special cases left, either we have |
3864 | /* 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 | */ |
3865 | 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.) |
3866 | waittime = backend_mintime; |
3980 | : backend_mintime; |
3867 | |
3981 | |
3868 | /* extra check because io_blocktime is commonly 0 */ |
3982 | /* extra check because io_blocktime is commonly 0 */ |
3869 | if (ecb_expect_false (io_blocktime)) |
3983 | if (ecb_expect_false (io_blocktime)) |
3870 | { |
3984 | { |
3871 | sleeptime = io_blocktime - (mn_now - prev_mn_now); |
3985 | sleeptime = io_blocktime - (mn_now - prev_mn_now); |
… | |
… | |
4206 | void |
4320 | void |
4207 | ev_periodic_start (EV_P_ ev_periodic *w) EV_NOEXCEPT |
4321 | ev_periodic_start (EV_P_ ev_periodic *w) EV_NOEXCEPT |
4208 | { |
4322 | { |
4209 | if (ecb_expect_false (ev_is_active (w))) |
4323 | if (ecb_expect_false (ev_is_active (w))) |
4210 | return; |
4324 | return; |
|
|
4325 | |
|
|
4326 | #if EV_USE_TIMERFD |
|
|
4327 | if (timerfd == -2) |
|
|
4328 | evtimerfd_init (EV_A); |
|
|
4329 | #endif |
4211 | |
4330 | |
4212 | if (w->reschedule_cb) |
4331 | if (w->reschedule_cb) |
4213 | ev_at (w) = w->reschedule_cb (w, ev_rt_now); |
4332 | ev_at (w) = w->reschedule_cb (w, ev_rt_now); |
4214 | else if (w->interval) |
4333 | else if (w->interval) |
4215 | { |
4334 | { |