… | |
… | |
117 | # define EV_USE_EPOLL 0 |
117 | # define EV_USE_EPOLL 0 |
118 | # endif |
118 | # endif |
119 | |
119 | |
120 | # if HAVE_LINUX_AIO_ABI_H |
120 | # if HAVE_LINUX_AIO_ABI_H |
121 | # ifndef EV_USE_LINUXAIO |
121 | # ifndef EV_USE_LINUXAIO |
122 | # define EV_USE_LINUXAIO EV_FEATURE_BACKENDS |
122 | # define EV_USE_LINUXAIO 0 /* was: EV_FEATURE_BACKENDS, always off by default */ |
123 | # endif |
123 | # endif |
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 |
… | |
… | |
326 | # define EV_USE_PORT 0 |
344 | # define EV_USE_PORT 0 |
327 | #endif |
345 | #endif |
328 | |
346 | |
329 | #ifndef EV_USE_LINUXAIO |
347 | #ifndef EV_USE_LINUXAIO |
330 | # if __linux /* libev currently assumes linux/aio_abi.h is always available on linux */ |
348 | # if __linux /* libev currently assumes linux/aio_abi.h is always available on linux */ |
331 | # define EV_USE_LINUXAIO 1 |
349 | # define EV_USE_LINUXAIO 0 /* was: 1, always off by default */ |
332 | # else |
350 | # else |
333 | # define EV_USE_LINUXAIO 0 |
351 | # define EV_USE_LINUXAIO 0 |
|
|
352 | # endif |
|
|
353 | #endif |
|
|
354 | |
|
|
355 | #ifndef EV_USE_IOURING |
|
|
356 | # if __linux /* later checks might disable again */ |
|
|
357 | # define EV_USE_IOURING 1 |
|
|
358 | # else |
|
|
359 | # define EV_USE_IOURING 0 |
334 | # endif |
360 | # endif |
335 | #endif |
361 | #endif |
336 | |
362 | |
337 | #ifndef EV_USE_INOTIFY |
363 | #ifndef EV_USE_INOTIFY |
338 | # if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 4)) |
364 | # if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 4)) |
… | |
… | |
361 | #ifndef EV_USE_SIGNALFD |
387 | #ifndef EV_USE_SIGNALFD |
362 | # if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 7)) |
388 | # if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 7)) |
363 | # define EV_USE_SIGNALFD EV_FEATURE_OS |
389 | # define EV_USE_SIGNALFD EV_FEATURE_OS |
364 | # else |
390 | # else |
365 | # 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 |
366 | # endif |
400 | # endif |
367 | #endif |
401 | #endif |
368 | |
402 | |
369 | #if 0 /* debugging */ |
403 | #if 0 /* debugging */ |
370 | # define EV_VERIFY 3 |
404 | # define EV_VERIFY 3 |
… | |
… | |
406 | # include <sys/syscall.h> |
440 | # include <sys/syscall.h> |
407 | # ifdef SYS_clock_gettime |
441 | # ifdef SYS_clock_gettime |
408 | # define clock_gettime(id, ts) syscall (SYS_clock_gettime, (id), (ts)) |
442 | # define clock_gettime(id, ts) syscall (SYS_clock_gettime, (id), (ts)) |
409 | # undef EV_USE_MONOTONIC |
443 | # undef EV_USE_MONOTONIC |
410 | # define EV_USE_MONOTONIC 1 |
444 | # define EV_USE_MONOTONIC 1 |
|
|
445 | # define EV_NEED_SYSCALL 1 |
411 | # else |
446 | # else |
412 | # undef EV_USE_CLOCK_SYSCALL |
447 | # undef EV_USE_CLOCK_SYSCALL |
413 | # define EV_USE_CLOCK_SYSCALL 0 |
448 | # define EV_USE_CLOCK_SYSCALL 0 |
414 | # endif |
449 | # endif |
415 | #endif |
450 | #endif |
… | |
… | |
427 | #endif |
462 | #endif |
428 | |
463 | |
429 | #if !EV_STAT_ENABLE |
464 | #if !EV_STAT_ENABLE |
430 | # undef EV_USE_INOTIFY |
465 | # undef EV_USE_INOTIFY |
431 | # define EV_USE_INOTIFY 0 |
466 | # define EV_USE_INOTIFY 0 |
|
|
467 | #endif |
|
|
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 |
432 | #endif |
475 | #endif |
433 | |
476 | |
434 | #if !EV_USE_NANOSLEEP |
477 | #if !EV_USE_NANOSLEEP |
435 | /* 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 */ |
436 | # if !defined _WIN32 && !defined __hpux |
479 | # if !defined _WIN32 && !defined __hpux |
… | |
… | |
438 | # endif |
481 | # endif |
439 | #endif |
482 | #endif |
440 | |
483 | |
441 | #if EV_USE_LINUXAIO |
484 | #if EV_USE_LINUXAIO |
442 | # include <sys/syscall.h> |
485 | # include <sys/syscall.h> |
443 | # if !SYS_io_getevents || !EV_USE_EPOLL /* ev_linxaio uses ev_poll.c:ev_epoll_create */ |
486 | # if SYS_io_getevents && EV_USE_EPOLL /* linuxaio backend requires epoll backend */ |
|
|
487 | # define EV_NEED_SYSCALL 1 |
|
|
488 | # else |
444 | # undef EV_USE_LINUXAIO |
489 | # undef EV_USE_LINUXAIO |
445 | # define EV_USE_LINUXAIO 0 |
490 | # define EV_USE_LINUXAIO 0 |
|
|
491 | # endif |
|
|
492 | #endif |
|
|
493 | |
|
|
494 | #if EV_USE_IOURING |
|
|
495 | # include <sys/syscall.h> |
|
|
496 | # if !SYS_io_uring_setup && __linux && !__alpha |
|
|
497 | # define SYS_io_uring_setup 425 |
|
|
498 | # define SYS_io_uring_enter 426 |
|
|
499 | # define SYS_io_uring_wregister 427 |
|
|
500 | # endif |
|
|
501 | # if SYS_io_uring_setup && EV_USE_EPOLL /* iouring backend requires epoll backend */ |
|
|
502 | # define EV_NEED_SYSCALL 1 |
|
|
503 | # else |
|
|
504 | # undef EV_USE_IOURING |
|
|
505 | # define EV_USE_IOURING 0 |
446 | # endif |
506 | # endif |
447 | #endif |
507 | #endif |
448 | |
508 | |
449 | #if EV_USE_INOTIFY |
509 | #if EV_USE_INOTIFY |
450 | # include <sys/statfs.h> |
510 | # include <sys/statfs.h> |
… | |
… | |
455 | # define EV_USE_INOTIFY 0 |
515 | # define EV_USE_INOTIFY 0 |
456 | # endif |
516 | # endif |
457 | #endif |
517 | #endif |
458 | |
518 | |
459 | #if EV_USE_EVENTFD |
519 | #if EV_USE_EVENTFD |
460 | /* 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 */ |
461 | # include <stdint.h> |
521 | # include <stdint.h> |
462 | # ifndef EFD_NONBLOCK |
522 | # ifndef EFD_NONBLOCK |
463 | # define EFD_NONBLOCK O_NONBLOCK |
523 | # define EFD_NONBLOCK O_NONBLOCK |
464 | # endif |
524 | # endif |
465 | # ifndef EFD_CLOEXEC |
525 | # ifndef EFD_CLOEXEC |
… | |
… | |
471 | # endif |
531 | # endif |
472 | EV_CPP(extern "C") int (eventfd) (unsigned int initval, int flags); |
532 | EV_CPP(extern "C") int (eventfd) (unsigned int initval, int flags); |
473 | #endif |
533 | #endif |
474 | |
534 | |
475 | #if EV_USE_SIGNALFD |
535 | #if EV_USE_SIGNALFD |
476 | /* 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 */ |
477 | # include <stdint.h> |
537 | # include <stdint.h> |
478 | # ifndef SFD_NONBLOCK |
538 | # ifndef SFD_NONBLOCK |
479 | # define SFD_NONBLOCK O_NONBLOCK |
539 | # define SFD_NONBLOCK O_NONBLOCK |
480 | # endif |
540 | # endif |
481 | # ifndef SFD_CLOEXEC |
541 | # ifndef SFD_CLOEXEC |
… | |
… | |
483 | # define SFD_CLOEXEC O_CLOEXEC |
543 | # define SFD_CLOEXEC O_CLOEXEC |
484 | # else |
544 | # else |
485 | # define SFD_CLOEXEC 02000000 |
545 | # define SFD_CLOEXEC 02000000 |
486 | # endif |
546 | # endif |
487 | # endif |
547 | # endif |
488 | 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); |
489 | |
549 | |
490 | struct signalfd_siginfo |
550 | struct signalfd_siginfo |
491 | { |
551 | { |
492 | uint32_t ssi_signo; |
552 | uint32_t ssi_signo; |
493 | char pad[128 - sizeof (uint32_t)]; |
553 | char pad[128 - sizeof (uint32_t)]; |
494 | }; |
554 | }; |
495 | #endif |
555 | #endif |
496 | |
556 | |
497 | /**/ |
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 |
|
|
565 | #endif |
|
|
566 | |
|
|
567 | /*****************************************************************************/ |
498 | |
568 | |
499 | #if EV_VERIFY >= 3 |
569 | #if EV_VERIFY >= 3 |
500 | # define EV_FREQUENT_CHECK ev_verify (EV_A) |
570 | # define EV_FREQUENT_CHECK ev_verify (EV_A) |
501 | #else |
571 | #else |
502 | # define EV_FREQUENT_CHECK do { } while (0) |
572 | # define EV_FREQUENT_CHECK do { } while (0) |
… | |
… | |
507 | * This value is good at least till the year 4000. |
577 | * This value is good at least till the year 4000. |
508 | */ |
578 | */ |
509 | #define MIN_INTERVAL 0.0001220703125 /* 1/2**13, good till 4000 */ |
579 | #define MIN_INTERVAL 0.0001220703125 /* 1/2**13, good till 4000 */ |
510 | /*#define MIN_INTERVAL 0.00000095367431640625 /* 1/2**20, good till 2200 */ |
580 | /*#define MIN_INTERVAL 0.00000095367431640625 /* 1/2**20, good till 2200 */ |
511 | |
581 | |
512 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
582 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
513 | #define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */ |
583 | #define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */ |
514 | |
584 | |
|
|
585 | /* find a portable timestamp that is "always" in the future but fits into time_t. |
|
|
586 | * this is quite hard, and we are mostly guessing - we handle 32 bit signed/unsigned time_t, |
|
|
587 | * and sizes larger than 32 bit, and maybe the unlikely floating point time_t */ |
|
|
588 | #define EV_TSTAMP_HUGE \ |
|
|
589 | (sizeof (time_t) >= 8 ? 10000000000000. \ |
|
|
590 | : 0 < (time_t)4294967295 ? 4294967295. \ |
|
|
591 | : 2147483647.) \ |
|
|
592 | |
|
|
593 | #ifndef EV_TS_CONST |
|
|
594 | # define EV_TS_CONST(nv) nv |
|
|
595 | # define EV_TS_TO_MSEC(a) a * 1e3 + 0.9999 |
|
|
596 | # define EV_TS_FROM_USEC(us) us * 1e-6 |
515 | #define EV_TV_SET(tv,t) do { tv.tv_sec = (long)t; tv.tv_usec = (long)((t - tv.tv_sec) * 1e6); } while (0) |
597 | # define EV_TV_SET(tv,t) do { tv.tv_sec = (long)t; tv.tv_usec = (long)((t - tv.tv_sec) * 1e6); } while (0) |
516 | #define EV_TS_SET(ts,t) do { ts.tv_sec = (long)t; ts.tv_nsec = (long)((t - ts.tv_sec) * 1e9); } while (0) |
598 | # define EV_TS_SET(ts,t) do { ts.tv_sec = (long)t; ts.tv_nsec = (long)((t - ts.tv_sec) * 1e9); } while (0) |
|
|
599 | # define EV_TV_GET(tv) ((tv).tv_sec + (tv).tv_usec * 1e-6) |
|
|
600 | # define EV_TS_GET(ts) ((ts).tv_sec + (ts).tv_nsec * 1e-9) |
|
|
601 | #endif |
517 | |
602 | |
518 | /* the following is ecb.h embedded into libev - use update_ev_c to update from an external copy */ |
603 | /* the following is ecb.h embedded into libev - use update_ev_c to update from an external copy */ |
519 | /* ECB.H BEGIN */ |
604 | /* ECB.H BEGIN */ |
520 | /* |
605 | /* |
521 | * libecb - http://software.schmorp.de/pkg/libecb |
606 | * libecb - http://software.schmorp.de/pkg/libecb |
… | |
… | |
1558 | # define inline_speed ecb_inline |
1643 | # define inline_speed ecb_inline |
1559 | #else |
1644 | #else |
1560 | # define inline_speed ecb_noinline static |
1645 | # define inline_speed ecb_noinline static |
1561 | #endif |
1646 | #endif |
1562 | |
1647 | |
|
|
1648 | /*****************************************************************************/ |
|
|
1649 | /* raw syscall wrappers */ |
|
|
1650 | |
|
|
1651 | #if EV_NEED_SYSCALL |
|
|
1652 | |
|
|
1653 | #include <sys/syscall.h> |
|
|
1654 | |
|
|
1655 | /* |
|
|
1656 | * define some syscall wrappers for common architectures |
|
|
1657 | * this is mostly for nice looks during debugging, not performance. |
|
|
1658 | * our syscalls return < 0, not == -1, on error. which is good |
|
|
1659 | * enough for linux aio. |
|
|
1660 | * TODO: arm is also common nowadays, maybe even mips and x86 |
|
|
1661 | * TODO: after implementing this, it suddenly looks like overkill, but its hard to remove... |
|
|
1662 | */ |
|
|
1663 | #if __GNUC__ && __linux && ECB_AMD64 && !EV_FEATURE_CODE |
|
|
1664 | /* the costly errno access probably kills this for size optimisation */ |
|
|
1665 | |
|
|
1666 | #define ev_syscall(nr,narg,arg1,arg2,arg3,arg4,arg5,arg6) \ |
|
|
1667 | ({ \ |
|
|
1668 | long res; \ |
|
|
1669 | register unsigned long r6 __asm__ ("r9" ); \ |
|
|
1670 | register unsigned long r5 __asm__ ("r8" ); \ |
|
|
1671 | register unsigned long r4 __asm__ ("r10"); \ |
|
|
1672 | register unsigned long r3 __asm__ ("rdx"); \ |
|
|
1673 | register unsigned long r2 __asm__ ("rsi"); \ |
|
|
1674 | register unsigned long r1 __asm__ ("rdi"); \ |
|
|
1675 | if (narg >= 6) r6 = (unsigned long)(arg6); \ |
|
|
1676 | if (narg >= 5) r5 = (unsigned long)(arg5); \ |
|
|
1677 | if (narg >= 4) r4 = (unsigned long)(arg4); \ |
|
|
1678 | if (narg >= 3) r3 = (unsigned long)(arg3); \ |
|
|
1679 | if (narg >= 2) r2 = (unsigned long)(arg2); \ |
|
|
1680 | if (narg >= 1) r1 = (unsigned long)(arg1); \ |
|
|
1681 | __asm__ __volatile__ ( \ |
|
|
1682 | "syscall\n\t" \ |
|
|
1683 | : "=a" (res) \ |
|
|
1684 | : "0" (nr), "r" (r1), "r" (r2), "r" (r3), "r" (r4), "r" (r5) \ |
|
|
1685 | : "cc", "r11", "cx", "memory"); \ |
|
|
1686 | errno = -res; \ |
|
|
1687 | res; \ |
|
|
1688 | }) |
|
|
1689 | |
|
|
1690 | #endif |
|
|
1691 | |
|
|
1692 | #ifdef ev_syscall |
|
|
1693 | #define ev_syscall0(nr) ev_syscall (nr, 0, 0, 0, 0, 0, 0, 0) |
|
|
1694 | #define ev_syscall1(nr,arg1) ev_syscall (nr, 1, arg1, 0, 0, 0, 0, 0) |
|
|
1695 | #define ev_syscall2(nr,arg1,arg2) ev_syscall (nr, 2, arg1, arg2, 0, 0, 0, 0) |
|
|
1696 | #define ev_syscall3(nr,arg1,arg2,arg3) ev_syscall (nr, 3, arg1, arg2, arg3, 0, 0, 0) |
|
|
1697 | #define ev_syscall4(nr,arg1,arg2,arg3,arg4) ev_syscall (nr, 3, arg1, arg2, arg3, arg4, 0, 0) |
|
|
1698 | #define ev_syscall5(nr,arg1,arg2,arg3,arg4,arg5) ev_syscall (nr, 5, arg1, arg2, arg3, arg4, arg5, 0) |
|
|
1699 | #define ev_syscall6(nr,arg1,arg2,arg3,arg4,arg5,arg6) ev_syscall (nr, 6, arg1, arg2, arg3, arg4, arg5,arg6) |
|
|
1700 | #else |
|
|
1701 | #define ev_syscall0(nr) syscall (nr) |
|
|
1702 | #define ev_syscall1(nr,arg1) syscall (nr, arg1) |
|
|
1703 | #define ev_syscall2(nr,arg1,arg2) syscall (nr, arg1, arg2) |
|
|
1704 | #define ev_syscall3(nr,arg1,arg2,arg3) syscall (nr, arg1, arg2, arg3) |
|
|
1705 | #define ev_syscall4(nr,arg1,arg2,arg3,arg4) syscall (nr, arg1, arg2, arg3, arg4) |
|
|
1706 | #define ev_syscall5(nr,arg1,arg2,arg3,arg4,arg5) syscall (nr, arg1, arg2, arg3, arg4, arg5) |
|
|
1707 | #define ev_syscall6(nr,arg1,arg2,arg3,arg4,arg5,arg6) syscall (nr, arg1, arg2, arg3, arg4, arg5,arg6) |
|
|
1708 | #endif |
|
|
1709 | |
|
|
1710 | #endif |
|
|
1711 | |
|
|
1712 | /*****************************************************************************/ |
|
|
1713 | |
1563 | #define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) |
1714 | #define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) |
1564 | |
1715 | |
1565 | #if EV_MINPRI == EV_MAXPRI |
1716 | #if EV_MINPRI == EV_MAXPRI |
1566 | # define ABSPRI(w) (((W)w), 0) |
1717 | # define ABSPRI(w) (((W)w), 0) |
1567 | #else |
1718 | #else |
… | |
… | |
1626 | const ev_tstamp shift = sizeof (unsigned long) >= 8 ? 10000000000000000000. : 1000000000.; |
1777 | const ev_tstamp shift = sizeof (unsigned long) >= 8 ? 10000000000000000000. : 1000000000.; |
1627 | #else |
1778 | #else |
1628 | const ev_tstamp shift = sizeof (unsigned long) >= 8 ? 18446744073709551616. : 4294967296.; |
1779 | const ev_tstamp shift = sizeof (unsigned long) >= 8 ? 18446744073709551616. : 4294967296.; |
1629 | #endif |
1780 | #endif |
1630 | |
1781 | |
|
|
1782 | /* special treatment for negative arguments */ |
|
|
1783 | if (ecb_expect_false (v < 0.)) |
|
|
1784 | { |
|
|
1785 | ev_tstamp f = -ev_floor (-v); |
|
|
1786 | |
|
|
1787 | return f - (f == v ? 0 : 1); |
|
|
1788 | } |
|
|
1789 | |
1631 | /* argument too large for an unsigned long? */ |
1790 | /* argument too large for an unsigned long? then reduce it */ |
1632 | if (ecb_expect_false (v >= shift)) |
1791 | if (ecb_expect_false (v >= shift)) |
1633 | { |
1792 | { |
1634 | ev_tstamp f; |
1793 | ev_tstamp f; |
1635 | |
1794 | |
1636 | if (v == v - 1.) |
1795 | if (v == v - 1.) |
1637 | return v; /* very large number */ |
1796 | return v; /* very large numbers are assumed to be integer */ |
1638 | |
1797 | |
1639 | f = shift * ev_floor (v * (1. / shift)); |
1798 | f = shift * ev_floor (v * (1. / shift)); |
1640 | return f + ev_floor (v - f); |
1799 | return f + ev_floor (v - f); |
1641 | } |
|
|
1642 | |
|
|
1643 | /* special treatment for negative args? */ |
|
|
1644 | if (ecb_expect_false (v < 0.)) |
|
|
1645 | { |
|
|
1646 | ev_tstamp f = -ev_floor (-v); |
|
|
1647 | |
|
|
1648 | return f - (f == v ? 0 : 1); |
|
|
1649 | } |
1800 | } |
1650 | |
1801 | |
1651 | /* fits into an unsigned long */ |
1802 | /* fits into an unsigned long */ |
1652 | return (unsigned long)v; |
1803 | return (unsigned long)v; |
1653 | } |
1804 | } |
… | |
… | |
1797 | { |
1948 | { |
1798 | WL head; |
1949 | WL head; |
1799 | unsigned char events; /* the events watched for */ |
1950 | unsigned char events; /* the events watched for */ |
1800 | unsigned char reify; /* flag set when this ANFD needs reification (EV_ANFD_REIFY, EV__IOFDSET) */ |
1951 | unsigned char reify; /* flag set when this ANFD needs reification (EV_ANFD_REIFY, EV__IOFDSET) */ |
1801 | unsigned char emask; /* some backends store the actual kernel mask in here */ |
1952 | unsigned char emask; /* some backends store the actual kernel mask in here */ |
1802 | unsigned char unused; |
1953 | unsigned char eflags; /* flags field for use by backends */ |
1803 | #if EV_USE_EPOLL |
1954 | #if EV_USE_EPOLL |
1804 | unsigned int egen; /* generation counter to counter epoll bugs */ |
1955 | unsigned int egen; /* generation counter to counter epoll bugs */ |
1805 | #endif |
1956 | #endif |
1806 | #if EV_SELECT_IS_WINSOCKET || EV_USE_IOCP |
1957 | #if EV_SELECT_IS_WINSOCKET || EV_USE_IOCP |
1807 | SOCKET handle; |
1958 | SOCKET handle; |
… | |
… | |
1861 | static struct ev_loop default_loop_struct; |
2012 | static struct ev_loop default_loop_struct; |
1862 | EV_API_DECL struct ev_loop *ev_default_loop_ptr = 0; /* needs to be initialised to make it a definition despite extern */ |
2013 | EV_API_DECL struct ev_loop *ev_default_loop_ptr = 0; /* needs to be initialised to make it a definition despite extern */ |
1863 | |
2014 | |
1864 | #else |
2015 | #else |
1865 | |
2016 | |
1866 | EV_API_DECL ev_tstamp ev_rt_now = 0; /* needs to be initialised to make it a definition despite extern */ |
2017 | EV_API_DECL ev_tstamp ev_rt_now = EV_TS_CONST (0.); /* needs to be initialised to make it a definition despite extern */ |
1867 | #define VAR(name,decl) static decl; |
2018 | #define VAR(name,decl) static decl; |
1868 | #include "ev_vars.h" |
2019 | #include "ev_vars.h" |
1869 | #undef VAR |
2020 | #undef VAR |
1870 | |
2021 | |
1871 | static int ev_default_loop_ptr; |
2022 | static int ev_default_loop_ptr; |
… | |
… | |
1893 | #if EV_USE_REALTIME |
2044 | #if EV_USE_REALTIME |
1894 | if (ecb_expect_true (have_realtime)) |
2045 | if (ecb_expect_true (have_realtime)) |
1895 | { |
2046 | { |
1896 | struct timespec ts; |
2047 | struct timespec ts; |
1897 | clock_gettime (CLOCK_REALTIME, &ts); |
2048 | clock_gettime (CLOCK_REALTIME, &ts); |
1898 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
2049 | return EV_TS_GET (ts); |
1899 | } |
2050 | } |
1900 | #endif |
2051 | #endif |
1901 | |
2052 | |
|
|
2053 | { |
1902 | struct timeval tv; |
2054 | struct timeval tv; |
1903 | gettimeofday (&tv, 0); |
2055 | gettimeofday (&tv, 0); |
1904 | return tv.tv_sec + tv.tv_usec * 1e-6; |
2056 | return EV_TV_GET (tv); |
|
|
2057 | } |
1905 | } |
2058 | } |
1906 | #endif |
2059 | #endif |
1907 | |
2060 | |
1908 | inline_size ev_tstamp |
2061 | inline_size ev_tstamp |
1909 | get_clock (void) |
2062 | get_clock (void) |
… | |
… | |
1911 | #if EV_USE_MONOTONIC |
2064 | #if EV_USE_MONOTONIC |
1912 | if (ecb_expect_true (have_monotonic)) |
2065 | if (ecb_expect_true (have_monotonic)) |
1913 | { |
2066 | { |
1914 | struct timespec ts; |
2067 | struct timespec ts; |
1915 | clock_gettime (CLOCK_MONOTONIC, &ts); |
2068 | clock_gettime (CLOCK_MONOTONIC, &ts); |
1916 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
2069 | return EV_TS_GET (ts); |
1917 | } |
2070 | } |
1918 | #endif |
2071 | #endif |
1919 | |
2072 | |
1920 | return ev_time (); |
2073 | return ev_time (); |
1921 | } |
2074 | } |
… | |
… | |
1929 | #endif |
2082 | #endif |
1930 | |
2083 | |
1931 | void |
2084 | void |
1932 | ev_sleep (ev_tstamp delay) EV_NOEXCEPT |
2085 | ev_sleep (ev_tstamp delay) EV_NOEXCEPT |
1933 | { |
2086 | { |
1934 | if (delay > 0.) |
2087 | if (delay > EV_TS_CONST (0.)) |
1935 | { |
2088 | { |
1936 | #if EV_USE_NANOSLEEP |
2089 | #if EV_USE_NANOSLEEP |
1937 | struct timespec ts; |
2090 | struct timespec ts; |
1938 | |
2091 | |
1939 | EV_TS_SET (ts, delay); |
2092 | EV_TS_SET (ts, delay); |
1940 | nanosleep (&ts, 0); |
2093 | nanosleep (&ts, 0); |
1941 | #elif defined _WIN32 |
2094 | #elif defined _WIN32 |
1942 | /* maybe this should round up, as ms is very low resolution */ |
2095 | /* maybe this should round up, as ms is very low resolution */ |
1943 | /* compared to select (µs) or nanosleep (ns) */ |
2096 | /* compared to select (µs) or nanosleep (ns) */ |
1944 | Sleep ((unsigned long)(delay * 1e3)); |
2097 | Sleep ((unsigned long)(EV_TS_TO_MSEC (delay))); |
1945 | #else |
2098 | #else |
1946 | struct timeval tv; |
2099 | struct timeval tv; |
1947 | |
2100 | |
1948 | /* here we rely on sys/time.h + sys/types.h + unistd.h providing select */ |
2101 | /* here we rely on sys/time.h + sys/types.h + unistd.h providing select */ |
1949 | /* something not guaranteed by newer posix versions, but guaranteed */ |
2102 | /* something not guaranteed by newer posix versions, but guaranteed */ |
… | |
… | |
2109 | inline_size void |
2262 | inline_size void |
2110 | fd_reify (EV_P) |
2263 | fd_reify (EV_P) |
2111 | { |
2264 | { |
2112 | int i; |
2265 | int i; |
2113 | |
2266 | |
|
|
2267 | /* most backends do not modify the fdchanges list in backend_modfiy. |
|
|
2268 | * except io_uring, which has fixed-size buffers which might force us |
|
|
2269 | * to handle events in backend_modify, causing fdchangesd to be amended, |
|
|
2270 | * which could result in an endless loop. |
|
|
2271 | * to avoid this, we do not dynamically handle fds that were added |
|
|
2272 | * during fd_reify. that menas thast for those backends, fdchangecnt |
|
|
2273 | * might be non-zero during poll, which must cause them to not block. |
|
|
2274 | * to not put too much of a burden on other backends, this detail |
|
|
2275 | * needs to be handled in the backend. |
|
|
2276 | */ |
|
|
2277 | int changecnt = fdchangecnt; |
|
|
2278 | |
2114 | #if EV_SELECT_IS_WINSOCKET || EV_USE_IOCP |
2279 | #if EV_SELECT_IS_WINSOCKET || EV_USE_IOCP |
2115 | for (i = 0; i < fdchangecnt; ++i) |
2280 | for (i = 0; i < changecnt; ++i) |
2116 | { |
2281 | { |
2117 | int fd = fdchanges [i]; |
2282 | int fd = fdchanges [i]; |
2118 | ANFD *anfd = anfds + fd; |
2283 | ANFD *anfd = anfds + fd; |
2119 | |
2284 | |
2120 | if (anfd->reify & EV__IOFDSET && anfd->head) |
2285 | if (anfd->reify & EV__IOFDSET && anfd->head) |
… | |
… | |
2134 | } |
2299 | } |
2135 | } |
2300 | } |
2136 | } |
2301 | } |
2137 | #endif |
2302 | #endif |
2138 | |
2303 | |
2139 | for (i = 0; i < fdchangecnt; ++i) |
2304 | for (i = 0; i < changecnt; ++i) |
2140 | { |
2305 | { |
2141 | int fd = fdchanges [i]; |
2306 | int fd = fdchanges [i]; |
2142 | ANFD *anfd = anfds + fd; |
2307 | ANFD *anfd = anfds + fd; |
2143 | ev_io *w; |
2308 | ev_io *w; |
2144 | |
2309 | |
… | |
… | |
2160 | |
2325 | |
2161 | if (o_reify & EV__IOFDSET) |
2326 | if (o_reify & EV__IOFDSET) |
2162 | backend_modify (EV_A_ fd, o_events, anfd->events); |
2327 | backend_modify (EV_A_ fd, o_events, anfd->events); |
2163 | } |
2328 | } |
2164 | |
2329 | |
|
|
2330 | /* normally, fdchangecnt hasn't changed. if it has, then new fds have been added. |
|
|
2331 | * this is a rare case (see beginning comment in this function), so we copy them to the |
|
|
2332 | * front and hope the backend handles this case. |
|
|
2333 | */ |
|
|
2334 | if (ecb_expect_false (fdchangecnt != changecnt)) |
|
|
2335 | memmove (fdchanges, fdchanges + changecnt, (fdchangecnt - changecnt) * sizeof (*fdchanges)); |
|
|
2336 | |
2165 | fdchangecnt = 0; |
2337 | fdchangecnt -= changecnt; |
2166 | } |
2338 | } |
2167 | |
2339 | |
2168 | /* something about the given fd changed */ |
2340 | /* something about the given fd changed */ |
2169 | inline_size |
2341 | inline_size |
2170 | void |
2342 | void |
… | |
… | |
2299 | |
2471 | |
2300 | /* find minimum child */ |
2472 | /* find minimum child */ |
2301 | if (ecb_expect_true (pos + DHEAP - 1 < E)) |
2473 | if (ecb_expect_true (pos + DHEAP - 1 < E)) |
2302 | { |
2474 | { |
2303 | /* fast path */ (minpos = pos + 0), (minat = ANHE_at (*minpos)); |
2475 | /* fast path */ (minpos = pos + 0), (minat = ANHE_at (*minpos)); |
2304 | if ( ANHE_at (pos [1]) < minat) (minpos = pos + 1), (minat = ANHE_at (*minpos)); |
2476 | if ( minat > ANHE_at (pos [1])) (minpos = pos + 1), (minat = ANHE_at (*minpos)); |
2305 | if ( ANHE_at (pos [2]) < minat) (minpos = pos + 2), (minat = ANHE_at (*minpos)); |
2477 | if ( minat > ANHE_at (pos [2])) (minpos = pos + 2), (minat = ANHE_at (*minpos)); |
2306 | if ( ANHE_at (pos [3]) < minat) (minpos = pos + 3), (minat = ANHE_at (*minpos)); |
2478 | if ( minat > ANHE_at (pos [3])) (minpos = pos + 3), (minat = ANHE_at (*minpos)); |
2307 | } |
2479 | } |
2308 | else if (pos < E) |
2480 | else if (pos < E) |
2309 | { |
2481 | { |
2310 | /* slow path */ (minpos = pos + 0), (minat = ANHE_at (*minpos)); |
2482 | /* slow path */ (minpos = pos + 0), (minat = ANHE_at (*minpos)); |
2311 | if (pos + 1 < E && ANHE_at (pos [1]) < minat) (minpos = pos + 1), (minat = ANHE_at (*minpos)); |
2483 | if (pos + 1 < E && minat > ANHE_at (pos [1])) (minpos = pos + 1), (minat = ANHE_at (*minpos)); |
2312 | if (pos + 2 < E && ANHE_at (pos [2]) < minat) (minpos = pos + 2), (minat = ANHE_at (*minpos)); |
2484 | if (pos + 2 < E && minat > ANHE_at (pos [2])) (minpos = pos + 2), (minat = ANHE_at (*minpos)); |
2313 | if (pos + 3 < E && ANHE_at (pos [3]) < minat) (minpos = pos + 3), (minat = ANHE_at (*minpos)); |
2485 | if (pos + 3 < E && minat > ANHE_at (pos [3])) (minpos = pos + 3), (minat = ANHE_at (*minpos)); |
2314 | } |
2486 | } |
2315 | else |
2487 | else |
2316 | break; |
2488 | break; |
2317 | |
2489 | |
2318 | if (ANHE_at (he) <= minat) |
2490 | if (ANHE_at (he) <= minat) |
… | |
… | |
2326 | |
2498 | |
2327 | heap [k] = he; |
2499 | heap [k] = he; |
2328 | ev_active (ANHE_w (he)) = k; |
2500 | ev_active (ANHE_w (he)) = k; |
2329 | } |
2501 | } |
2330 | |
2502 | |
2331 | #else /* 4HEAP */ |
2503 | #else /* not 4HEAP */ |
2332 | |
2504 | |
2333 | #define HEAP0 1 |
2505 | #define HEAP0 1 |
2334 | #define HPARENT(k) ((k) >> 1) |
2506 | #define HPARENT(k) ((k) >> 1) |
2335 | #define UPHEAP_DONE(p,k) (!(p)) |
2507 | #define UPHEAP_DONE(p,k) (!(p)) |
2336 | |
2508 | |
… | |
… | |
2722 | |
2894 | |
2723 | #endif |
2895 | #endif |
2724 | |
2896 | |
2725 | /*****************************************************************************/ |
2897 | /*****************************************************************************/ |
2726 | |
2898 | |
|
|
2899 | #if EV_USE_TIMERFD |
|
|
2900 | |
|
|
2901 | static void periodics_reschedule (EV_P); |
|
|
2902 | |
|
|
2903 | static void |
|
|
2904 | timerfdcb (EV_P_ ev_io *iow, int revents) |
|
|
2905 | { |
|
|
2906 | struct itimerspec its = { 0 }; |
|
|
2907 | |
|
|
2908 | /* since we can't easily come zup with a (portable) maximum value of time_t, |
|
|
2909 | * we wake up once per month, which hopefully is rare enough to not |
|
|
2910 | * be a problem. */ |
|
|
2911 | its.it_value.tv_sec = ev_rt_now + 86400 * 30; |
|
|
2912 | timerfd_settime (timerfd, TFD_TIMER_ABSTIME | TFD_TIMER_CANCEL_ON_SET, &its, 0); |
|
|
2913 | |
|
|
2914 | ev_rt_now = ev_time (); |
|
|
2915 | /* periodics_reschedule only needs ev_rt_now */ |
|
|
2916 | /* but maybe in the future we want the full treatment. */ |
|
|
2917 | /* |
|
|
2918 | now_floor = EV_TS_CONST (0.); |
|
|
2919 | time_update (EV_A_ EV_TSTAMP_HUGE); |
|
|
2920 | */ |
|
|
2921 | periodics_reschedule (EV_A); |
|
|
2922 | } |
|
|
2923 | |
|
|
2924 | ecb_noinline ecb_cold |
|
|
2925 | static void |
|
|
2926 | evtimerfd_init (EV_P) |
|
|
2927 | { |
|
|
2928 | if (!ev_is_active (&timerfd_w)) |
|
|
2929 | { |
|
|
2930 | timerfd = timerfd_create (CLOCK_REALTIME, TFD_NONBLOCK | TFD_CLOEXEC); |
|
|
2931 | |
|
|
2932 | if (timerfd >= 0) |
|
|
2933 | { |
|
|
2934 | fd_intern (timerfd); /* just to be sure */ |
|
|
2935 | |
|
|
2936 | ev_io_init (&timerfd_w, timerfdcb, timerfd, EV_READ); |
|
|
2937 | ev_set_priority (&timerfd_w, EV_MINPRI); |
|
|
2938 | ev_io_start (EV_A_ &timerfd_w); |
|
|
2939 | ev_unref (EV_A); /* watcher should not keep loop alive */ |
|
|
2940 | |
|
|
2941 | /* (re-) arm timer */ |
|
|
2942 | timerfdcb (EV_A_ 0, 0); |
|
|
2943 | } |
|
|
2944 | } |
|
|
2945 | } |
|
|
2946 | |
|
|
2947 | #endif |
|
|
2948 | |
|
|
2949 | /*****************************************************************************/ |
|
|
2950 | |
2727 | #if EV_USE_IOCP |
2951 | #if EV_USE_IOCP |
2728 | # include "ev_iocp.c" |
2952 | # include "ev_iocp.c" |
2729 | #endif |
2953 | #endif |
2730 | #if EV_USE_PORT |
2954 | #if EV_USE_PORT |
2731 | # include "ev_port.c" |
2955 | # include "ev_port.c" |
… | |
… | |
2736 | #if EV_USE_EPOLL |
2960 | #if EV_USE_EPOLL |
2737 | # include "ev_epoll.c" |
2961 | # include "ev_epoll.c" |
2738 | #endif |
2962 | #endif |
2739 | #if EV_USE_LINUXAIO |
2963 | #if EV_USE_LINUXAIO |
2740 | # include "ev_linuxaio.c" |
2964 | # include "ev_linuxaio.c" |
|
|
2965 | #endif |
|
|
2966 | #if EV_USE_IOURING |
|
|
2967 | # include "ev_iouring.c" |
2741 | #endif |
2968 | #endif |
2742 | #if EV_USE_POLL |
2969 | #if EV_USE_POLL |
2743 | # include "ev_poll.c" |
2970 | # include "ev_poll.c" |
2744 | #endif |
2971 | #endif |
2745 | #if EV_USE_SELECT |
2972 | #if EV_USE_SELECT |
… | |
… | |
2774 | unsigned int |
3001 | unsigned int |
2775 | ev_supported_backends (void) EV_NOEXCEPT |
3002 | ev_supported_backends (void) EV_NOEXCEPT |
2776 | { |
3003 | { |
2777 | unsigned int flags = 0; |
3004 | unsigned int flags = 0; |
2778 | |
3005 | |
2779 | if (EV_USE_PORT ) flags |= EVBACKEND_PORT; |
3006 | if (EV_USE_PORT ) flags |= EVBACKEND_PORT; |
2780 | if (EV_USE_KQUEUE ) flags |= EVBACKEND_KQUEUE; |
3007 | if (EV_USE_KQUEUE ) flags |= EVBACKEND_KQUEUE; |
2781 | if (EV_USE_EPOLL ) flags |= EVBACKEND_EPOLL; |
3008 | if (EV_USE_EPOLL ) flags |= EVBACKEND_EPOLL; |
2782 | if (EV_USE_LINUXAIO) flags |= EVBACKEND_LINUXAIO; |
3009 | if (EV_USE_LINUXAIO ) flags |= EVBACKEND_LINUXAIO; |
2783 | if (EV_USE_POLL ) flags |= EVBACKEND_POLL; |
3010 | if (EV_USE_IOURING && ev_linux_version () >= 0x050601) flags |= EVBACKEND_IOURING; /* 5.6.1+ */ |
2784 | if (EV_USE_SELECT ) flags |= EVBACKEND_SELECT; |
3011 | if (EV_USE_POLL ) flags |= EVBACKEND_POLL; |
2785 | |
3012 | if (EV_USE_SELECT ) flags |= EVBACKEND_SELECT; |
|
|
3013 | |
2786 | return flags; |
3014 | return flags; |
2787 | } |
3015 | } |
2788 | |
3016 | |
2789 | ecb_cold |
3017 | ecb_cold |
2790 | unsigned int |
3018 | unsigned int |
… | |
… | |
2808 | |
3036 | |
2809 | /* TODO: linuxaio is very experimental */ |
3037 | /* TODO: linuxaio is very experimental */ |
2810 | #if !EV_RECOMMEND_LINUXAIO |
3038 | #if !EV_RECOMMEND_LINUXAIO |
2811 | flags &= ~EVBACKEND_LINUXAIO; |
3039 | flags &= ~EVBACKEND_LINUXAIO; |
2812 | #endif |
3040 | #endif |
|
|
3041 | /* TODO: linuxaio is super experimental */ |
|
|
3042 | #if !EV_RECOMMEND_IOURING |
|
|
3043 | flags &= ~EVBACKEND_IOURING; |
|
|
3044 | #endif |
2813 | |
3045 | |
2814 | return flags; |
3046 | return flags; |
2815 | } |
3047 | } |
2816 | |
3048 | |
2817 | ecb_cold |
3049 | ecb_cold |
… | |
… | |
2821 | int flags = EVBACKEND_EPOLL | EVBACKEND_KQUEUE | EVBACKEND_PORT; |
3053 | int flags = EVBACKEND_EPOLL | EVBACKEND_KQUEUE | EVBACKEND_PORT; |
2822 | |
3054 | |
2823 | /* epoll embeddability broken on all linux versions up to at least 2.6.23 */ |
3055 | /* epoll embeddability broken on all linux versions up to at least 2.6.23 */ |
2824 | if (ev_linux_version () < 0x020620) /* disable it on linux < 2.6.32 */ |
3056 | if (ev_linux_version () < 0x020620) /* disable it on linux < 2.6.32 */ |
2825 | flags &= ~EVBACKEND_EPOLL; |
3057 | flags &= ~EVBACKEND_EPOLL; |
|
|
3058 | |
|
|
3059 | /* EVBACKEND_LINUXAIO is theoretically embeddable, but suffers from a performance overhead */ |
|
|
3060 | |
|
|
3061 | /* EVBACKEND_IOURING is practically embeddable, but the current implementation is not |
|
|
3062 | * because our backend_fd is the epoll fd we need as fallback. |
|
|
3063 | * if the kernel ever is fixed, this might change... |
|
|
3064 | */ |
2826 | |
3065 | |
2827 | return flags; |
3066 | return flags; |
2828 | } |
3067 | } |
2829 | |
3068 | |
2830 | unsigned int |
3069 | unsigned int |
… | |
… | |
2948 | fs_fd = flags & EVFLAG_NOINOTIFY ? -1 : -2; |
3187 | fs_fd = flags & EVFLAG_NOINOTIFY ? -1 : -2; |
2949 | #endif |
3188 | #endif |
2950 | #if EV_USE_SIGNALFD |
3189 | #if EV_USE_SIGNALFD |
2951 | sigfd = flags & EVFLAG_SIGNALFD ? -2 : -1; |
3190 | sigfd = flags & EVFLAG_SIGNALFD ? -2 : -1; |
2952 | #endif |
3191 | #endif |
|
|
3192 | #if EV_USE_TIMERFD |
|
|
3193 | timerfd = flags & EVFLAG_NOTIMERFD ? -1 : -2; |
|
|
3194 | #endif |
2953 | |
3195 | |
2954 | if (!(flags & EVBACKEND_MASK)) |
3196 | if (!(flags & EVBACKEND_MASK)) |
2955 | flags |= ev_recommended_backends (); |
3197 | flags |= ev_recommended_backends (); |
2956 | |
3198 | |
2957 | #if EV_USE_IOCP |
3199 | #if EV_USE_IOCP |
… | |
… | |
2960 | #if EV_USE_PORT |
3202 | #if EV_USE_PORT |
2961 | if (!backend && (flags & EVBACKEND_PORT )) backend = port_init (EV_A_ flags); |
3203 | if (!backend && (flags & EVBACKEND_PORT )) backend = port_init (EV_A_ flags); |
2962 | #endif |
3204 | #endif |
2963 | #if EV_USE_KQUEUE |
3205 | #if EV_USE_KQUEUE |
2964 | if (!backend && (flags & EVBACKEND_KQUEUE )) backend = kqueue_init (EV_A_ flags); |
3206 | if (!backend && (flags & EVBACKEND_KQUEUE )) backend = kqueue_init (EV_A_ flags); |
|
|
3207 | #endif |
|
|
3208 | #if EV_USE_IOURING |
|
|
3209 | if (!backend && (flags & EVBACKEND_IOURING )) backend = iouring_init (EV_A_ flags); |
2965 | #endif |
3210 | #endif |
2966 | #if EV_USE_LINUXAIO |
3211 | #if EV_USE_LINUXAIO |
2967 | if (!backend && (flags & EVBACKEND_LINUXAIO)) backend = linuxaio_init (EV_A_ flags); |
3212 | if (!backend && (flags & EVBACKEND_LINUXAIO)) backend = linuxaio_init (EV_A_ flags); |
2968 | #endif |
3213 | #endif |
2969 | #if EV_USE_EPOLL |
3214 | #if EV_USE_EPOLL |
… | |
… | |
3027 | #if EV_USE_SIGNALFD |
3272 | #if EV_USE_SIGNALFD |
3028 | if (ev_is_active (&sigfd_w)) |
3273 | if (ev_is_active (&sigfd_w)) |
3029 | close (sigfd); |
3274 | close (sigfd); |
3030 | #endif |
3275 | #endif |
3031 | |
3276 | |
|
|
3277 | #if EV_USE_TIMERFD |
|
|
3278 | if (ev_is_active (&timerfd_w)) |
|
|
3279 | close (timerfd); |
|
|
3280 | #endif |
|
|
3281 | |
3032 | #if EV_USE_INOTIFY |
3282 | #if EV_USE_INOTIFY |
3033 | if (fs_fd >= 0) |
3283 | if (fs_fd >= 0) |
3034 | close (fs_fd); |
3284 | close (fs_fd); |
3035 | #endif |
3285 | #endif |
3036 | |
3286 | |
… | |
… | |
3043 | #if EV_USE_PORT |
3293 | #if EV_USE_PORT |
3044 | if (backend == EVBACKEND_PORT ) port_destroy (EV_A); |
3294 | if (backend == EVBACKEND_PORT ) port_destroy (EV_A); |
3045 | #endif |
3295 | #endif |
3046 | #if EV_USE_KQUEUE |
3296 | #if EV_USE_KQUEUE |
3047 | if (backend == EVBACKEND_KQUEUE ) kqueue_destroy (EV_A); |
3297 | if (backend == EVBACKEND_KQUEUE ) kqueue_destroy (EV_A); |
|
|
3298 | #endif |
|
|
3299 | #if EV_USE_IOURING |
|
|
3300 | if (backend == EVBACKEND_IOURING ) iouring_destroy (EV_A); |
3048 | #endif |
3301 | #endif |
3049 | #if EV_USE_LINUXAIO |
3302 | #if EV_USE_LINUXAIO |
3050 | if (backend == EVBACKEND_LINUXAIO) linuxaio_destroy (EV_A); |
3303 | if (backend == EVBACKEND_LINUXAIO) linuxaio_destroy (EV_A); |
3051 | #endif |
3304 | #endif |
3052 | #if EV_USE_EPOLL |
3305 | #if EV_USE_EPOLL |
… | |
… | |
3111 | if (backend == EVBACKEND_PORT ) port_fork (EV_A); |
3364 | if (backend == EVBACKEND_PORT ) port_fork (EV_A); |
3112 | #endif |
3365 | #endif |
3113 | #if EV_USE_KQUEUE |
3366 | #if EV_USE_KQUEUE |
3114 | if (backend == EVBACKEND_KQUEUE ) kqueue_fork (EV_A); |
3367 | if (backend == EVBACKEND_KQUEUE ) kqueue_fork (EV_A); |
3115 | #endif |
3368 | #endif |
|
|
3369 | #if EV_USE_IOURING |
|
|
3370 | if (backend == EVBACKEND_IOURING ) iouring_fork (EV_A); |
|
|
3371 | #endif |
3116 | #if EV_USE_LINUXAIO |
3372 | #if EV_USE_LINUXAIO |
3117 | if (backend == EVBACKEND_LINUXAIO) linuxaio_fork (EV_A); |
3373 | if (backend == EVBACKEND_LINUXAIO) linuxaio_fork (EV_A); |
3118 | #endif |
3374 | #endif |
3119 | #if EV_USE_EPOLL |
3375 | #if EV_USE_EPOLL |
3120 | if (backend == EVBACKEND_EPOLL ) epoll_fork (EV_A); |
3376 | if (backend == EVBACKEND_EPOLL ) epoll_fork (EV_A); |
3121 | #endif |
3377 | #endif |
3122 | #if EV_USE_INOTIFY |
3378 | #if EV_USE_INOTIFY |
3123 | infy_fork (EV_A); |
3379 | infy_fork (EV_A); |
3124 | #endif |
3380 | #endif |
3125 | |
3381 | |
|
|
3382 | if (postfork != 2) |
|
|
3383 | { |
|
|
3384 | #if EV_USE_SIGNALFD |
|
|
3385 | /* surprisingly, nothing needs to be done for signalfd, accoridng to docs, it does the right thing on fork */ |
|
|
3386 | #endif |
|
|
3387 | |
|
|
3388 | #if EV_USE_TIMERFD |
|
|
3389 | if (ev_is_active (&timerfd_w)) |
|
|
3390 | { |
|
|
3391 | ev_ref (EV_A); |
|
|
3392 | ev_io_stop (EV_A_ &timerfd_w); |
|
|
3393 | |
|
|
3394 | close (timerfd); |
|
|
3395 | timerfd = -2; |
|
|
3396 | |
|
|
3397 | evtimerfd_init (EV_A); |
|
|
3398 | /* reschedule periodics, in case we missed something */ |
|
|
3399 | ev_feed_event (EV_A_ &timerfd_w, EV_CUSTOM); |
|
|
3400 | } |
|
|
3401 | #endif |
|
|
3402 | |
3126 | #if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE |
3403 | #if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE |
3127 | if (ev_is_active (&pipe_w) && postfork != 2) |
3404 | if (ev_is_active (&pipe_w)) |
3128 | { |
3405 | { |
3129 | /* pipe_write_wanted must be false now, so modifying fd vars should be safe */ |
3406 | /* pipe_write_wanted must be false now, so modifying fd vars should be safe */ |
3130 | |
3407 | |
3131 | ev_ref (EV_A); |
3408 | ev_ref (EV_A); |
3132 | ev_io_stop (EV_A_ &pipe_w); |
3409 | ev_io_stop (EV_A_ &pipe_w); |
3133 | |
3410 | |
3134 | if (evpipe [0] >= 0) |
3411 | if (evpipe [0] >= 0) |
3135 | EV_WIN32_CLOSE_FD (evpipe [0]); |
3412 | EV_WIN32_CLOSE_FD (evpipe [0]); |
3136 | |
3413 | |
3137 | evpipe_init (EV_A); |
3414 | evpipe_init (EV_A); |
3138 | /* iterate over everything, in case we missed something before */ |
3415 | /* iterate over everything, in case we missed something before */ |
3139 | ev_feed_event (EV_A_ &pipe_w, EV_CUSTOM); |
3416 | ev_feed_event (EV_A_ &pipe_w, EV_CUSTOM); |
|
|
3417 | } |
|
|
3418 | #endif |
3140 | } |
3419 | } |
3141 | #endif |
|
|
3142 | |
3420 | |
3143 | postfork = 0; |
3421 | postfork = 0; |
3144 | } |
3422 | } |
3145 | |
3423 | |
3146 | #if EV_MULTIPLICITY |
3424 | #if EV_MULTIPLICITY |
… | |
… | |
3416 | { |
3694 | { |
3417 | ev_at (w) += w->repeat; |
3695 | ev_at (w) += w->repeat; |
3418 | if (ev_at (w) < mn_now) |
3696 | if (ev_at (w) < mn_now) |
3419 | ev_at (w) = mn_now; |
3697 | ev_at (w) = mn_now; |
3420 | |
3698 | |
3421 | assert (("libev: negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
3699 | assert (("libev: negative ev_timer repeat value found while processing timers", w->repeat > EV_TS_CONST (0.))); |
3422 | |
3700 | |
3423 | ANHE_at_cache (timers [HEAP0]); |
3701 | ANHE_at_cache (timers [HEAP0]); |
3424 | downheap (timers, timercnt, HEAP0); |
3702 | downheap (timers, timercnt, HEAP0); |
3425 | } |
3703 | } |
3426 | else |
3704 | else |
… | |
… | |
3557 | |
3835 | |
3558 | mn_now = get_clock (); |
3836 | mn_now = get_clock (); |
3559 | |
3837 | |
3560 | /* only fetch the realtime clock every 0.5*MIN_TIMEJUMP seconds */ |
3838 | /* only fetch the realtime clock every 0.5*MIN_TIMEJUMP seconds */ |
3561 | /* interpolate in the meantime */ |
3839 | /* interpolate in the meantime */ |
3562 | if (ecb_expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) |
3840 | if (ecb_expect_true (mn_now - now_floor < EV_TS_CONST (MIN_TIMEJUMP * .5))) |
3563 | { |
3841 | { |
3564 | ev_rt_now = rtmn_diff + mn_now; |
3842 | ev_rt_now = rtmn_diff + mn_now; |
3565 | return; |
3843 | return; |
3566 | } |
3844 | } |
3567 | |
3845 | |
… | |
… | |
3581 | ev_tstamp diff; |
3859 | ev_tstamp diff; |
3582 | rtmn_diff = ev_rt_now - mn_now; |
3860 | rtmn_diff = ev_rt_now - mn_now; |
3583 | |
3861 | |
3584 | diff = odiff - rtmn_diff; |
3862 | diff = odiff - rtmn_diff; |
3585 | |
3863 | |
3586 | if (ecb_expect_true ((diff < 0. ? -diff : diff) < MIN_TIMEJUMP)) |
3864 | if (ecb_expect_true ((diff < EV_TS_CONST (0.) ? -diff : diff) < EV_TS_CONST (MIN_TIMEJUMP))) |
3587 | return; /* all is well */ |
3865 | return; /* all is well */ |
3588 | |
3866 | |
3589 | ev_rt_now = ev_time (); |
3867 | ev_rt_now = ev_time (); |
3590 | mn_now = get_clock (); |
3868 | mn_now = get_clock (); |
3591 | now_floor = mn_now; |
3869 | now_floor = mn_now; |
… | |
… | |
3600 | else |
3878 | else |
3601 | #endif |
3879 | #endif |
3602 | { |
3880 | { |
3603 | ev_rt_now = ev_time (); |
3881 | ev_rt_now = ev_time (); |
3604 | |
3882 | |
3605 | if (ecb_expect_false (mn_now > ev_rt_now || ev_rt_now > mn_now + max_block + MIN_TIMEJUMP)) |
3883 | if (ecb_expect_false (mn_now > ev_rt_now || ev_rt_now > mn_now + max_block + EV_TS_CONST (MIN_TIMEJUMP))) |
3606 | { |
3884 | { |
3607 | /* adjust timers. this is easy, as the offset is the same for all of them */ |
3885 | /* adjust timers. this is easy, as the offset is the same for all of them */ |
3608 | timers_reschedule (EV_A_ ev_rt_now - mn_now); |
3886 | timers_reschedule (EV_A_ ev_rt_now - mn_now); |
3609 | #if EV_PERIODIC_ENABLE |
3887 | #if EV_PERIODIC_ENABLE |
3610 | periodics_reschedule (EV_A); |
3888 | periodics_reschedule (EV_A); |
… | |
… | |
3679 | |
3957 | |
3680 | /* remember old timestamp for io_blocktime calculation */ |
3958 | /* remember old timestamp for io_blocktime calculation */ |
3681 | ev_tstamp prev_mn_now = mn_now; |
3959 | ev_tstamp prev_mn_now = mn_now; |
3682 | |
3960 | |
3683 | /* update time to cancel out callback processing overhead */ |
3961 | /* update time to cancel out callback processing overhead */ |
3684 | time_update (EV_A_ 1e100); |
3962 | time_update (EV_A_ EV_TS_CONST (EV_TSTAMP_HUGE)); |
3685 | |
3963 | |
3686 | /* from now on, we want a pipe-wake-up */ |
3964 | /* from now on, we want a pipe-wake-up */ |
3687 | pipe_write_wanted = 1; |
3965 | pipe_write_wanted = 1; |
3688 | |
3966 | |
3689 | ECB_MEMORY_FENCE; /* make sure pipe_write_wanted is visible before we check for potential skips */ |
3967 | ECB_MEMORY_FENCE; /* make sure pipe_write_wanted is visible before we check for potential skips */ |
3690 | |
3968 | |
3691 | if (ecb_expect_true (!(flags & EVRUN_NOWAIT || idleall || !activecnt || pipe_write_skipped))) |
3969 | if (ecb_expect_true (!(flags & EVRUN_NOWAIT || idleall || !activecnt || pipe_write_skipped))) |
3692 | { |
3970 | { |
3693 | waittime = MAX_BLOCKTIME; |
3971 | waittime = EV_TS_CONST (MAX_BLOCKTIME); |
3694 | |
3972 | |
3695 | if (timercnt) |
3973 | if (timercnt) |
3696 | { |
3974 | { |
3697 | ev_tstamp to = ANHE_at (timers [HEAP0]) - mn_now; |
3975 | ev_tstamp to = ANHE_at (timers [HEAP0]) - mn_now; |
3698 | if (waittime > to) waittime = to; |
3976 | if (waittime > to) waittime = to; |
… | |
… | |
3708 | |
3986 | |
3709 | /* don't let timeouts decrease the waittime below timeout_blocktime */ |
3987 | /* don't let timeouts decrease the waittime below timeout_blocktime */ |
3710 | if (ecb_expect_false (waittime < timeout_blocktime)) |
3988 | if (ecb_expect_false (waittime < timeout_blocktime)) |
3711 | waittime = timeout_blocktime; |
3989 | waittime = timeout_blocktime; |
3712 | |
3990 | |
3713 | /* at this point, we NEED to wait, so we have to ensure */ |
3991 | /* now there are two more special cases left, either we have |
3714 | /* to pass a minimum nonzero value to the backend */ |
3992 | * already-expired timers, so we should not sleep, or we have timers |
|
|
3993 | * that expire very soon, in which case we need to wait for a minimum |
|
|
3994 | * amount of time for some event loop backends. |
|
|
3995 | */ |
3715 | if (ecb_expect_false (waittime < backend_mintime)) |
3996 | if (ecb_expect_false (waittime < backend_mintime)) |
|
|
3997 | waittime = waittime <= EV_TS_CONST (0.) |
|
|
3998 | ? EV_TS_CONST (0.) |
3716 | waittime = backend_mintime; |
3999 | : backend_mintime; |
3717 | |
4000 | |
3718 | /* extra check because io_blocktime is commonly 0 */ |
4001 | /* extra check because io_blocktime is commonly 0 */ |
3719 | if (ecb_expect_false (io_blocktime)) |
4002 | if (ecb_expect_false (io_blocktime)) |
3720 | { |
4003 | { |
3721 | sleeptime = io_blocktime - (mn_now - prev_mn_now); |
4004 | sleeptime = io_blocktime - (mn_now - prev_mn_now); |
3722 | |
4005 | |
3723 | if (sleeptime > waittime - backend_mintime) |
4006 | if (sleeptime > waittime - backend_mintime) |
3724 | sleeptime = waittime - backend_mintime; |
4007 | sleeptime = waittime - backend_mintime; |
3725 | |
4008 | |
3726 | if (ecb_expect_true (sleeptime > 0.)) |
4009 | if (ecb_expect_true (sleeptime > EV_TS_CONST (0.))) |
3727 | { |
4010 | { |
3728 | ev_sleep (sleeptime); |
4011 | ev_sleep (sleeptime); |
3729 | waittime -= sleeptime; |
4012 | waittime -= sleeptime; |
3730 | } |
4013 | } |
3731 | } |
4014 | } |
… | |
… | |
3805 | } |
4088 | } |
3806 | |
4089 | |
3807 | void |
4090 | void |
3808 | ev_now_update (EV_P) EV_NOEXCEPT |
4091 | ev_now_update (EV_P) EV_NOEXCEPT |
3809 | { |
4092 | { |
3810 | time_update (EV_A_ 1e100); |
4093 | time_update (EV_A_ EV_TSTAMP_HUGE); |
3811 | } |
4094 | } |
3812 | |
4095 | |
3813 | void |
4096 | void |
3814 | ev_suspend (EV_P) EV_NOEXCEPT |
4097 | ev_suspend (EV_P) EV_NOEXCEPT |
3815 | { |
4098 | { |
… | |
… | |
4046 | } |
4329 | } |
4047 | |
4330 | |
4048 | ev_tstamp |
4331 | ev_tstamp |
4049 | ev_timer_remaining (EV_P_ ev_timer *w) EV_NOEXCEPT |
4332 | ev_timer_remaining (EV_P_ ev_timer *w) EV_NOEXCEPT |
4050 | { |
4333 | { |
4051 | return ev_at (w) - (ev_is_active (w) ? mn_now : 0.); |
4334 | return ev_at (w) - (ev_is_active (w) ? mn_now : EV_TS_CONST (0.)); |
4052 | } |
4335 | } |
4053 | |
4336 | |
4054 | #if EV_PERIODIC_ENABLE |
4337 | #if EV_PERIODIC_ENABLE |
4055 | ecb_noinline |
4338 | ecb_noinline |
4056 | void |
4339 | void |
4057 | ev_periodic_start (EV_P_ ev_periodic *w) EV_NOEXCEPT |
4340 | ev_periodic_start (EV_P_ ev_periodic *w) EV_NOEXCEPT |
4058 | { |
4341 | { |
4059 | if (ecb_expect_false (ev_is_active (w))) |
4342 | if (ecb_expect_false (ev_is_active (w))) |
4060 | return; |
4343 | return; |
|
|
4344 | |
|
|
4345 | #if EV_USE_TIMERFD |
|
|
4346 | if (timerfd == -2) |
|
|
4347 | evtimerfd_init (EV_A); |
|
|
4348 | #endif |
4061 | |
4349 | |
4062 | if (w->reschedule_cb) |
4350 | if (w->reschedule_cb) |
4063 | ev_at (w) = w->reschedule_cb (w, ev_rt_now); |
4351 | ev_at (w) = w->reschedule_cb (w, ev_rt_now); |
4064 | else if (w->interval) |
4352 | else if (w->interval) |
4065 | { |
4353 | { |
… | |
… | |
4807 | ev_run (EV_A_ EVRUN_NOWAIT); |
5095 | ev_run (EV_A_ EVRUN_NOWAIT); |
4808 | } |
5096 | } |
4809 | } |
5097 | } |
4810 | } |
5098 | } |
4811 | |
5099 | |
|
|
5100 | #if EV_FORK_ENABLE |
4812 | static void |
5101 | static void |
4813 | embed_fork_cb (EV_P_ ev_fork *fork_w, int revents) |
5102 | embed_fork_cb (EV_P_ ev_fork *fork_w, int revents) |
4814 | { |
5103 | { |
4815 | ev_embed *w = (ev_embed *)(((char *)fork_w) - offsetof (ev_embed, fork)); |
5104 | ev_embed *w = (ev_embed *)(((char *)fork_w) - offsetof (ev_embed, fork)); |
4816 | |
5105 | |
… | |
… | |
4823 | ev_run (EV_A_ EVRUN_NOWAIT); |
5112 | ev_run (EV_A_ EVRUN_NOWAIT); |
4824 | } |
5113 | } |
4825 | |
5114 | |
4826 | ev_embed_start (EV_A_ w); |
5115 | ev_embed_start (EV_A_ w); |
4827 | } |
5116 | } |
|
|
5117 | #endif |
4828 | |
5118 | |
4829 | #if 0 |
5119 | #if 0 |
4830 | static void |
5120 | static void |
4831 | embed_idle_cb (EV_P_ ev_idle *idle, int revents) |
5121 | embed_idle_cb (EV_P_ ev_idle *idle, int revents) |
4832 | { |
5122 | { |
… | |
… | |
4853 | |
5143 | |
4854 | ev_prepare_init (&w->prepare, embed_prepare_cb); |
5144 | ev_prepare_init (&w->prepare, embed_prepare_cb); |
4855 | ev_set_priority (&w->prepare, EV_MINPRI); |
5145 | ev_set_priority (&w->prepare, EV_MINPRI); |
4856 | ev_prepare_start (EV_A_ &w->prepare); |
5146 | ev_prepare_start (EV_A_ &w->prepare); |
4857 | |
5147 | |
|
|
5148 | #if EV_FORK_ENABLE |
4858 | ev_fork_init (&w->fork, embed_fork_cb); |
5149 | ev_fork_init (&w->fork, embed_fork_cb); |
4859 | ev_fork_start (EV_A_ &w->fork); |
5150 | ev_fork_start (EV_A_ &w->fork); |
|
|
5151 | #endif |
4860 | |
5152 | |
4861 | /*ev_idle_init (&w->idle, e,bed_idle_cb);*/ |
5153 | /*ev_idle_init (&w->idle, e,bed_idle_cb);*/ |
4862 | |
5154 | |
4863 | ev_start (EV_A_ (W)w, 1); |
5155 | ev_start (EV_A_ (W)w, 1); |
4864 | |
5156 | |
… | |
… | |
4874 | |
5166 | |
4875 | EV_FREQUENT_CHECK; |
5167 | EV_FREQUENT_CHECK; |
4876 | |
5168 | |
4877 | ev_io_stop (EV_A_ &w->io); |
5169 | ev_io_stop (EV_A_ &w->io); |
4878 | ev_prepare_stop (EV_A_ &w->prepare); |
5170 | ev_prepare_stop (EV_A_ &w->prepare); |
|
|
5171 | #if EV_FORK_ENABLE |
4879 | ev_fork_stop (EV_A_ &w->fork); |
5172 | ev_fork_stop (EV_A_ &w->fork); |
|
|
5173 | #endif |
4880 | |
5174 | |
4881 | ev_stop (EV_A_ (W)w); |
5175 | ev_stop (EV_A_ (W)w); |
4882 | |
5176 | |
4883 | EV_FREQUENT_CHECK; |
5177 | EV_FREQUENT_CHECK; |
4884 | } |
5178 | } |