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

Comparing libev/ev.c (file contents):
Revision 1.500 by root, Mon Jul 1 20:47:37 2019 UTC vs.
Revision 1.505 by root, Wed Jul 10 14:25:35 2019 UTC

332# else 332# else
333# define EV_USE_LINUXAIO 0 333# define EV_USE_LINUXAIO 0
334# endif 334# endif
335#endif 335#endif
336 336
337#ifndef EV_USE_IOURING
338# if __linux
339# define EV_USE_IOURING 0
340# else
341# define EV_USE_IOURING 0
342# endif
343#endif
344
337#ifndef EV_USE_INOTIFY 345#ifndef EV_USE_INOTIFY
338# if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 4)) 346# if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 4))
339# define EV_USE_INOTIFY EV_FEATURE_OS 347# define EV_USE_INOTIFY EV_FEATURE_OS
340# else 348# else
341# define EV_USE_INOTIFY 0 349# define EV_USE_INOTIFY 0
406# include <sys/syscall.h> 414# include <sys/syscall.h>
407# ifdef SYS_clock_gettime 415# ifdef SYS_clock_gettime
408# define clock_gettime(id, ts) syscall (SYS_clock_gettime, (id), (ts)) 416# define clock_gettime(id, ts) syscall (SYS_clock_gettime, (id), (ts))
409# undef EV_USE_MONOTONIC 417# undef EV_USE_MONOTONIC
410# define EV_USE_MONOTONIC 1 418# define EV_USE_MONOTONIC 1
419# define EV_NEED_SYSCALL 1
411# else 420# else
412# undef EV_USE_CLOCK_SYSCALL 421# undef EV_USE_CLOCK_SYSCALL
413# define EV_USE_CLOCK_SYSCALL 0 422# define EV_USE_CLOCK_SYSCALL 0
414# endif 423# endif
415#endif 424#endif
438# endif 447# endif
439#endif 448#endif
440 449
441#if EV_USE_LINUXAIO 450#if EV_USE_LINUXAIO
442# include <sys/syscall.h> 451# include <sys/syscall.h>
443# if !SYS_io_getevents || !EV_USE_EPOLL /* ev_linxaio uses ev_poll.c:ev_epoll_create */ 452# if SYS_io_getevents && EV_USE_EPOLL /* linuxaio backend requires epoll backend */
453# define EV_NEED_SYSCALL 1
454# else
444# undef EV_USE_LINUXAIO 455# undef EV_USE_LINUXAIO
445# define EV_USE_LINUXAIO 0 456# define EV_USE_LINUXAIO 0
457# endif
458#endif
459
460#if EV_USE_IOURING
461# include <sys/syscall.h>
462# if !SYS_io_uring_setup && __linux && !__alpha
463# define SYS_io_uring_setup 425
464# define SYS_io_uring_enter 426
465# define SYS_io_uring_wregister 427
466# endif
467# if SYS_io_uring_setup && EV_USE_EPOLL /* iouring backend requires epoll backend */
468# define EV_NEED_SYSCALL 1
469# else
470# undef EV_USE_IOURING
471# define EV_USE_IOURING 0
446# endif 472# endif
447#endif 473#endif
448 474
449#if EV_USE_INOTIFY 475#if EV_USE_INOTIFY
450# include <sys/statfs.h> 476# include <sys/statfs.h>
492 uint32_t ssi_signo; 518 uint32_t ssi_signo;
493 char pad[128 - sizeof (uint32_t)]; 519 char pad[128 - sizeof (uint32_t)];
494}; 520};
495#endif 521#endif
496 522
497/**/ 523/*****************************************************************************/
498 524
499#if EV_VERIFY >= 3 525#if EV_VERIFY >= 3
500# define EV_FREQUENT_CHECK ev_verify (EV_A) 526# define EV_FREQUENT_CHECK ev_verify (EV_A)
501#else 527#else
502# define EV_FREQUENT_CHECK do { } while (0) 528# define EV_FREQUENT_CHECK do { } while (0)
507 * This value is good at least till the year 4000. 533 * This value is good at least till the year 4000.
508 */ 534 */
509#define MIN_INTERVAL 0.0001220703125 /* 1/2**13, good till 4000 */ 535#define MIN_INTERVAL 0.0001220703125 /* 1/2**13, good till 4000 */
510/*#define MIN_INTERVAL 0.00000095367431640625 /* 1/2**20, good till 2200 */ 536/*#define MIN_INTERVAL 0.00000095367431640625 /* 1/2**20, good till 2200 */
511 537
512#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) */
513#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
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,
543 * and sizes larger than 32 bit, and maybe the unlikely floating point time_t */
544#define EV_TSTAMP_HUGE \
545 (sizeof (time_t) >= 8 ? 10000000000000. \
546 : 0 < (time_t)4294967295 ? 4294967295. \
547 : 2147483647.) \
514 548
515#define EV_TV_SET(tv,t) do { tv.tv_sec = (long)t; tv.tv_usec = (long)((t - tv.tv_sec) * 1e6); } while (0) 549#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) 550#define EV_TS_SET(ts,t) do { ts.tv_sec = (long)t; ts.tv_nsec = (long)((t - ts.tv_sec) * 1e9); } while (0)
551#define EV_TV_GET(tv) ((tv).tv_sec + (tv).tv_usec * 1e6)
552#define EV_TS_GET(ts) ((ts).tv_sec + (ts).tv_nsec * 1e9)
517 553
518/* the following is ecb.h embedded into libev - use update_ev_c to update from an external copy */ 554/* the following is ecb.h embedded into libev - use update_ev_c to update from an external copy */
519/* ECB.H BEGIN */ 555/* ECB.H BEGIN */
520/* 556/*
521 * libecb - http://software.schmorp.de/pkg/libecb 557 * libecb - http://software.schmorp.de/pkg/libecb
1558# define inline_speed ecb_inline 1594# define inline_speed ecb_inline
1559#else 1595#else
1560# define inline_speed ecb_noinline static 1596# define inline_speed ecb_noinline static
1561#endif 1597#endif
1562 1598
1599/*****************************************************************************/
1600/* raw syscall wrappers */
1601
1602#if EV_NEED_SYSCALL
1603
1604#include <sys/syscall.h>
1605
1606/*
1607 * define some syscall wrappers for common architectures
1608 * this is mostly for nice looks during debugging, not performance.
1609 * our syscalls return < 0, not == -1, on error. which is good
1610 * enough for linux aio.
1611 * TODO: arm is also common nowadays, maybe even mips and x86
1612 * TODO: after implementing this, it suddenly looks like overkill, but its hard to remove...
1613 */
1614#if __GNUC__ && __linux && ECB_AMD64 && !defined __OPTIMIZE_SIZE__
1615 /* the costly errno access probably kills this for size optimisation */
1616
1617 #define ev_syscall(nr,narg,arg1,arg2,arg3,arg4,arg5,arg6) \
1618 ({ \
1619 long res; \
1620 register unsigned long r6 __asm__ ("r9" ); \
1621 register unsigned long r5 __asm__ ("r8" ); \
1622 register unsigned long r4 __asm__ ("r10"); \
1623 register unsigned long r3 __asm__ ("rdx"); \
1624 register unsigned long r2 __asm__ ("rsi"); \
1625 register unsigned long r1 __asm__ ("rdi"); \
1626 if (narg >= 6) r6 = (unsigned long)(arg6); \
1627 if (narg >= 5) r5 = (unsigned long)(arg5); \
1628 if (narg >= 4) r4 = (unsigned long)(arg4); \
1629 if (narg >= 3) r3 = (unsigned long)(arg3); \
1630 if (narg >= 2) r2 = (unsigned long)(arg2); \
1631 if (narg >= 1) r1 = (unsigned long)(arg1); \
1632 __asm__ __volatile__ ( \
1633 "syscall\n\t" \
1634 : "=a" (res) \
1635 : "0" (nr), "r" (r1), "r" (r2), "r" (r3), "r" (r4), "r" (r5) \
1636 : "cc", "r11", "cx", "memory"); \
1637 errno = -res; \
1638 res; \
1639 })
1640
1641#endif
1642
1643#ifdef ev_syscall
1644 #define ev_syscall0(nr) ev_syscall (nr, 0, 0, 0, 0, 0, 0, 0)
1645 #define ev_syscall1(nr,arg1) ev_syscall (nr, 1, arg1, 0, 0, 0, 0, 0)
1646 #define ev_syscall2(nr,arg1,arg2) ev_syscall (nr, 2, arg1, arg2, 0, 0, 0, 0)
1647 #define ev_syscall3(nr,arg1,arg2,arg3) ev_syscall (nr, 3, arg1, arg2, arg3, 0, 0, 0)
1648 #define ev_syscall4(nr,arg1,arg2,arg3,arg4) ev_syscall (nr, 3, arg1, arg2, arg3, arg4, 0, 0)
1649 #define ev_syscall5(nr,arg1,arg2,arg3,arg4,arg5) ev_syscall (nr, 5, arg1, arg2, arg3, arg4, arg5, 0)
1650 #define ev_syscall6(nr,arg1,arg2,arg3,arg4,arg5,arg6) ev_syscall (nr, 6, arg1, arg2, arg3, arg4, arg5,arg6)
1651#else
1652 #define ev_syscall0(nr) syscall (nr)
1653 #define ev_syscall1(nr,arg1) syscall (nr, arg1)
1654 #define ev_syscall2(nr,arg1,arg2) syscall (nr, arg1, arg2)
1655 #define ev_syscall3(nr,arg1,arg2,arg3) syscall (nr, arg1, arg2, arg3)
1656 #define ev_syscall4(nr,arg1,arg2,arg3,arg4) syscall (nr, arg1, arg2, arg3, arg4)
1657 #define ev_syscall5(nr,arg1,arg2,arg3,arg4,arg5) syscall (nr, arg1, arg2, arg3, arg4, arg5)
1658 #define ev_syscall6(nr,arg1,arg2,arg3,arg4,arg5,arg6) syscall (nr, arg1, arg2, arg3, arg4, arg5,arg6)
1659#endif
1660
1661#endif
1662
1663/*****************************************************************************/
1664
1563#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) 1665#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1)
1564 1666
1565#if EV_MINPRI == EV_MAXPRI 1667#if EV_MINPRI == EV_MAXPRI
1566# define ABSPRI(w) (((W)w), 0) 1668# define ABSPRI(w) (((W)w), 0)
1567#else 1669#else
1626 const ev_tstamp shift = sizeof (unsigned long) >= 8 ? 10000000000000000000. : 1000000000.; 1728 const ev_tstamp shift = sizeof (unsigned long) >= 8 ? 10000000000000000000. : 1000000000.;
1627#else 1729#else
1628 const ev_tstamp shift = sizeof (unsigned long) >= 8 ? 18446744073709551616. : 4294967296.; 1730 const ev_tstamp shift = sizeof (unsigned long) >= 8 ? 18446744073709551616. : 4294967296.;
1629#endif 1731#endif
1630 1732
1733 /* special treatment for negative arguments */
1734 if (ecb_expect_false (v < 0.))
1735 {
1736 ev_tstamp f = -ev_floor (-v);
1737
1738 return f - (f == v ? 0 : 1);
1739 }
1740
1631 /* argument too large for an unsigned long? */ 1741 /* argument too large for an unsigned long? then reduce it */
1632 if (ecb_expect_false (v >= shift)) 1742 if (ecb_expect_false (v >= shift))
1633 { 1743 {
1634 ev_tstamp f; 1744 ev_tstamp f;
1635 1745
1636 if (v == v - 1.) 1746 if (v == v - 1.)
1637 return v; /* very large number */ 1747 return v; /* very large numbers are assumed to be integer */
1638 1748
1639 f = shift * ev_floor (v * (1. / shift)); 1749 f = shift * ev_floor (v * (1. / shift));
1640 return f + ev_floor (v - f); 1750 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 } 1751 }
1650 1752
1651 /* fits into an unsigned long */ 1753 /* fits into an unsigned long */
1652 return (unsigned long)v; 1754 return (unsigned long)v;
1653} 1755}
1797{ 1899{
1798 WL head; 1900 WL head;
1799 unsigned char events; /* the events watched for */ 1901 unsigned char events; /* the events watched for */
1800 unsigned char reify; /* flag set when this ANFD needs reification (EV_ANFD_REIFY, EV__IOFDSET) */ 1902 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 */ 1903 unsigned char emask; /* some backends store the actual kernel mask in here */
1802 unsigned char unused; 1904 unsigned char eflags; /* flags field for use by backends */
1803#if EV_USE_EPOLL 1905#if EV_USE_EPOLL
1804 unsigned int egen; /* generation counter to counter epoll bugs */ 1906 unsigned int egen; /* generation counter to counter epoll bugs */
1805#endif 1907#endif
1806#if EV_SELECT_IS_WINSOCKET || EV_USE_IOCP 1908#if EV_SELECT_IS_WINSOCKET || EV_USE_IOCP
1807 SOCKET handle; 1909 SOCKET handle;
1893#if EV_USE_REALTIME 1995#if EV_USE_REALTIME
1894 if (ecb_expect_true (have_realtime)) 1996 if (ecb_expect_true (have_realtime))
1895 { 1997 {
1896 struct timespec ts; 1998 struct timespec ts;
1897 clock_gettime (CLOCK_REALTIME, &ts); 1999 clock_gettime (CLOCK_REALTIME, &ts);
1898 return ts.tv_sec + ts.tv_nsec * 1e-9; 2000 return EV_TS_GET (ts);
1899 } 2001 }
1900#endif 2002#endif
1901 2003
1902 struct timeval tv; 2004 struct timeval tv;
1903 gettimeofday (&tv, 0); 2005 gettimeofday (&tv, 0);
1904 return tv.tv_sec + tv.tv_usec * 1e-6; 2006 return EV_TV_GET (tv);
1905} 2007}
1906#endif 2008#endif
1907 2009
1908inline_size ev_tstamp 2010inline_size ev_tstamp
1909get_clock (void) 2011get_clock (void)
1911#if EV_USE_MONOTONIC 2013#if EV_USE_MONOTONIC
1912 if (ecb_expect_true (have_monotonic)) 2014 if (ecb_expect_true (have_monotonic))
1913 { 2015 {
1914 struct timespec ts; 2016 struct timespec ts;
1915 clock_gettime (CLOCK_MONOTONIC, &ts); 2017 clock_gettime (CLOCK_MONOTONIC, &ts);
1916 return ts.tv_sec + ts.tv_nsec * 1e-9; 2018 return EV_TS_GET (ts);
1917 } 2019 }
1918#endif 2020#endif
1919 2021
1920 return ev_time (); 2022 return ev_time ();
1921} 2023}
2737# include "ev_epoll.c" 2839# include "ev_epoll.c"
2738#endif 2840#endif
2739#if EV_USE_LINUXAIO 2841#if EV_USE_LINUXAIO
2740# include "ev_linuxaio.c" 2842# include "ev_linuxaio.c"
2741#endif 2843#endif
2844#if EV_USE_IOURING
2845# include "ev_iouring.c"
2846#endif
2742#if EV_USE_POLL 2847#if EV_USE_POLL
2743# include "ev_poll.c" 2848# include "ev_poll.c"
2744#endif 2849#endif
2745#if EV_USE_SELECT 2850#if EV_USE_SELECT
2746# include "ev_select.c" 2851# include "ev_select.c"
2778 2883
2779 if (EV_USE_PORT ) flags |= EVBACKEND_PORT; 2884 if (EV_USE_PORT ) flags |= EVBACKEND_PORT;
2780 if (EV_USE_KQUEUE ) flags |= EVBACKEND_KQUEUE; 2885 if (EV_USE_KQUEUE ) flags |= EVBACKEND_KQUEUE;
2781 if (EV_USE_EPOLL ) flags |= EVBACKEND_EPOLL; 2886 if (EV_USE_EPOLL ) flags |= EVBACKEND_EPOLL;
2782 if (EV_USE_LINUXAIO) flags |= EVBACKEND_LINUXAIO; 2887 if (EV_USE_LINUXAIO) flags |= EVBACKEND_LINUXAIO;
2888 if (EV_USE_IOURING ) flags |= EVBACKEND_IOURING;
2783 if (EV_USE_POLL ) flags |= EVBACKEND_POLL; 2889 if (EV_USE_POLL ) flags |= EVBACKEND_POLL;
2784 if (EV_USE_SELECT ) flags |= EVBACKEND_SELECT; 2890 if (EV_USE_SELECT ) flags |= EVBACKEND_SELECT;
2785 2891
2786 return flags; 2892 return flags;
2787} 2893}
2808 2914
2809 /* TODO: linuxaio is very experimental */ 2915 /* TODO: linuxaio is very experimental */
2810#if !EV_RECOMMEND_LINUXAIO 2916#if !EV_RECOMMEND_LINUXAIO
2811 flags &= ~EVBACKEND_LINUXAIO; 2917 flags &= ~EVBACKEND_LINUXAIO;
2812#endif 2918#endif
2919 /* TODO: linuxaio is super experimental */
2920#if !EV_RECOMMEND_IOURING
2921 flags &= ~EVBACKEND_IOURING;
2922#endif
2813 2923
2814 return flags; 2924 return flags;
2815} 2925}
2816 2926
2817ecb_cold 2927ecb_cold
2821 int flags = EVBACKEND_EPOLL | EVBACKEND_KQUEUE | EVBACKEND_PORT; 2931 int flags = EVBACKEND_EPOLL | EVBACKEND_KQUEUE | EVBACKEND_PORT;
2822 2932
2823 /* epoll embeddability broken on all linux versions up to at least 2.6.23 */ 2933 /* 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 */ 2934 if (ev_linux_version () < 0x020620) /* disable it on linux < 2.6.32 */
2825 flags &= ~EVBACKEND_EPOLL; 2935 flags &= ~EVBACKEND_EPOLL;
2936
2937 /* EVBACKEND_LINUXAIO is theoretically embeddable, but suffers from a performance overhead */
2938
2939 /* EVBACKEND_IOURING is practically embeddable, but the current implementation is not
2940 * because our backend_fd is the epoll fd we need as fallback.
2941 * if the kernel ever is fixed, this might change...
2942 */
2826 2943
2827 return flags; 2944 return flags;
2828} 2945}
2829 2946
2830unsigned int 2947unsigned int
2961 if (!backend && (flags & EVBACKEND_PORT )) backend = port_init (EV_A_ flags); 3078 if (!backend && (flags & EVBACKEND_PORT )) backend = port_init (EV_A_ flags);
2962#endif 3079#endif
2963#if EV_USE_KQUEUE 3080#if EV_USE_KQUEUE
2964 if (!backend && (flags & EVBACKEND_KQUEUE )) backend = kqueue_init (EV_A_ flags); 3081 if (!backend && (flags & EVBACKEND_KQUEUE )) backend = kqueue_init (EV_A_ flags);
2965#endif 3082#endif
3083#if EV_USE_IOURING
3084 if (!backend && (flags & EVBACKEND_IOURING )) backend = iouring_init (EV_A_ flags);
3085#endif
2966#if EV_USE_LINUXAIO 3086#if EV_USE_LINUXAIO
2967 if (!backend && (flags & EVBACKEND_LINUXAIO)) backend = linuxaio_init (EV_A_ flags); 3087 if (!backend && (flags & EVBACKEND_LINUXAIO)) backend = linuxaio_init (EV_A_ flags);
2968#endif 3088#endif
2969#if EV_USE_EPOLL 3089#if EV_USE_EPOLL
2970 if (!backend && (flags & EVBACKEND_EPOLL )) backend = epoll_init (EV_A_ flags); 3090 if (!backend && (flags & EVBACKEND_EPOLL )) backend = epoll_init (EV_A_ flags);
3043#if EV_USE_PORT 3163#if EV_USE_PORT
3044 if (backend == EVBACKEND_PORT ) port_destroy (EV_A); 3164 if (backend == EVBACKEND_PORT ) port_destroy (EV_A);
3045#endif 3165#endif
3046#if EV_USE_KQUEUE 3166#if EV_USE_KQUEUE
3047 if (backend == EVBACKEND_KQUEUE ) kqueue_destroy (EV_A); 3167 if (backend == EVBACKEND_KQUEUE ) kqueue_destroy (EV_A);
3168#endif
3169#if EV_USE_IOURING
3170 if (backend == EVBACKEND_IOURING ) iouring_destroy (EV_A);
3048#endif 3171#endif
3049#if EV_USE_LINUXAIO 3172#if EV_USE_LINUXAIO
3050 if (backend == EVBACKEND_LINUXAIO) linuxaio_destroy (EV_A); 3173 if (backend == EVBACKEND_LINUXAIO) linuxaio_destroy (EV_A);
3051#endif 3174#endif
3052#if EV_USE_EPOLL 3175#if EV_USE_EPOLL
3110#if EV_USE_PORT 3233#if EV_USE_PORT
3111 if (backend == EVBACKEND_PORT ) port_fork (EV_A); 3234 if (backend == EVBACKEND_PORT ) port_fork (EV_A);
3112#endif 3235#endif
3113#if EV_USE_KQUEUE 3236#if EV_USE_KQUEUE
3114 if (backend == EVBACKEND_KQUEUE ) kqueue_fork (EV_A); 3237 if (backend == EVBACKEND_KQUEUE ) kqueue_fork (EV_A);
3238#endif
3239#if EV_USE_IOURING
3240 if (backend == EVBACKEND_IOURING ) iouring_fork (EV_A);
3115#endif 3241#endif
3116#if EV_USE_LINUXAIO 3242#if EV_USE_LINUXAIO
3117 if (backend == EVBACKEND_LINUXAIO) linuxaio_fork (EV_A); 3243 if (backend == EVBACKEND_LINUXAIO) linuxaio_fork (EV_A);
3118#endif 3244#endif
3119#if EV_USE_EPOLL 3245#if EV_USE_EPOLL

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines