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.510 by root, Wed Aug 28 09:45:49 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) */
514 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.) \
548
549#ifndef EV_TS_CONST
550# define EV_TS_CONST(nv) nv
551# define EV_TS_TO_MSEC(a) a * 1e3 + 0.9999
552# 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) 553# 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) 554# define EV_TS_SET(ts,t) do { ts.tv_sec = (long)t; ts.tv_nsec = (long)((t - ts.tv_sec) * 1e9); } while (0)
555# define EV_TV_GET(tv) ((tv).tv_sec + (tv).tv_usec * 1e-6)
556# define EV_TS_GET(ts) ((ts).tv_sec + (ts).tv_nsec * 1e-9)
557#endif
517 558
518/* the following is ecb.h embedded into libev - use update_ev_c to update from an external copy */ 559/* the following is ecb.h embedded into libev - use update_ev_c to update from an external copy */
519/* ECB.H BEGIN */ 560/* ECB.H BEGIN */
520/* 561/*
521 * libecb - http://software.schmorp.de/pkg/libecb 562 * libecb - http://software.schmorp.de/pkg/libecb
1558# define inline_speed ecb_inline 1599# define inline_speed ecb_inline
1559#else 1600#else
1560# define inline_speed ecb_noinline static 1601# define inline_speed ecb_noinline static
1561#endif 1602#endif
1562 1603
1604/*****************************************************************************/
1605/* raw syscall wrappers */
1606
1607#if EV_NEED_SYSCALL
1608
1609#include <sys/syscall.h>
1610
1611/*
1612 * define some syscall wrappers for common architectures
1613 * this is mostly for nice looks during debugging, not performance.
1614 * our syscalls return < 0, not == -1, on error. which is good
1615 * enough for linux aio.
1616 * TODO: arm is also common nowadays, maybe even mips and x86
1617 * TODO: after implementing this, it suddenly looks like overkill, but its hard to remove...
1618 */
1619#if __GNUC__ && __linux && ECB_AMD64 && !defined __OPTIMIZE_SIZE__
1620 /* the costly errno access probably kills this for size optimisation */
1621
1622 #define ev_syscall(nr,narg,arg1,arg2,arg3,arg4,arg5,arg6) \
1623 ({ \
1624 long res; \
1625 register unsigned long r6 __asm__ ("r9" ); \
1626 register unsigned long r5 __asm__ ("r8" ); \
1627 register unsigned long r4 __asm__ ("r10"); \
1628 register unsigned long r3 __asm__ ("rdx"); \
1629 register unsigned long r2 __asm__ ("rsi"); \
1630 register unsigned long r1 __asm__ ("rdi"); \
1631 if (narg >= 6) r6 = (unsigned long)(arg6); \
1632 if (narg >= 5) r5 = (unsigned long)(arg5); \
1633 if (narg >= 4) r4 = (unsigned long)(arg4); \
1634 if (narg >= 3) r3 = (unsigned long)(arg3); \
1635 if (narg >= 2) r2 = (unsigned long)(arg2); \
1636 if (narg >= 1) r1 = (unsigned long)(arg1); \
1637 __asm__ __volatile__ ( \
1638 "syscall\n\t" \
1639 : "=a" (res) \
1640 : "0" (nr), "r" (r1), "r" (r2), "r" (r3), "r" (r4), "r" (r5) \
1641 : "cc", "r11", "cx", "memory"); \
1642 errno = -res; \
1643 res; \
1644 })
1645
1646#endif
1647
1648#ifdef ev_syscall
1649 #define ev_syscall0(nr) ev_syscall (nr, 0, 0, 0, 0, 0, 0, 0)
1650 #define ev_syscall1(nr,arg1) ev_syscall (nr, 1, arg1, 0, 0, 0, 0, 0)
1651 #define ev_syscall2(nr,arg1,arg2) ev_syscall (nr, 2, arg1, arg2, 0, 0, 0, 0)
1652 #define ev_syscall3(nr,arg1,arg2,arg3) ev_syscall (nr, 3, arg1, arg2, arg3, 0, 0, 0)
1653 #define ev_syscall4(nr,arg1,arg2,arg3,arg4) ev_syscall (nr, 3, arg1, arg2, arg3, arg4, 0, 0)
1654 #define ev_syscall5(nr,arg1,arg2,arg3,arg4,arg5) ev_syscall (nr, 5, arg1, arg2, arg3, arg4, arg5, 0)
1655 #define ev_syscall6(nr,arg1,arg2,arg3,arg4,arg5,arg6) ev_syscall (nr, 6, arg1, arg2, arg3, arg4, arg5,arg6)
1656#else
1657 #define ev_syscall0(nr) syscall (nr)
1658 #define ev_syscall1(nr,arg1) syscall (nr, arg1)
1659 #define ev_syscall2(nr,arg1,arg2) syscall (nr, arg1, arg2)
1660 #define ev_syscall3(nr,arg1,arg2,arg3) syscall (nr, arg1, arg2, arg3)
1661 #define ev_syscall4(nr,arg1,arg2,arg3,arg4) syscall (nr, arg1, arg2, arg3, arg4)
1662 #define ev_syscall5(nr,arg1,arg2,arg3,arg4,arg5) syscall (nr, arg1, arg2, arg3, arg4, arg5)
1663 #define ev_syscall6(nr,arg1,arg2,arg3,arg4,arg5,arg6) syscall (nr, arg1, arg2, arg3, arg4, arg5,arg6)
1664#endif
1665
1666#endif
1667
1668/*****************************************************************************/
1669
1563#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) 1670#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1)
1564 1671
1565#if EV_MINPRI == EV_MAXPRI 1672#if EV_MINPRI == EV_MAXPRI
1566# define ABSPRI(w) (((W)w), 0) 1673# define ABSPRI(w) (((W)w), 0)
1567#else 1674#else
1626 const ev_tstamp shift = sizeof (unsigned long) >= 8 ? 10000000000000000000. : 1000000000.; 1733 const ev_tstamp shift = sizeof (unsigned long) >= 8 ? 10000000000000000000. : 1000000000.;
1627#else 1734#else
1628 const ev_tstamp shift = sizeof (unsigned long) >= 8 ? 18446744073709551616. : 4294967296.; 1735 const ev_tstamp shift = sizeof (unsigned long) >= 8 ? 18446744073709551616. : 4294967296.;
1629#endif 1736#endif
1630 1737
1738 /* special treatment for negative arguments */
1739 if (ecb_expect_false (v < 0.))
1740 {
1741 ev_tstamp f = -ev_floor (-v);
1742
1743 return f - (f == v ? 0 : 1);
1744 }
1745
1631 /* argument too large for an unsigned long? */ 1746 /* argument too large for an unsigned long? then reduce it */
1632 if (ecb_expect_false (v >= shift)) 1747 if (ecb_expect_false (v >= shift))
1633 { 1748 {
1634 ev_tstamp f; 1749 ev_tstamp f;
1635 1750
1636 if (v == v - 1.) 1751 if (v == v - 1.)
1637 return v; /* very large number */ 1752 return v; /* very large numbers are assumed to be integer */
1638 1753
1639 f = shift * ev_floor (v * (1. / shift)); 1754 f = shift * ev_floor (v * (1. / shift));
1640 return f + ev_floor (v - f); 1755 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 } 1756 }
1650 1757
1651 /* fits into an unsigned long */ 1758 /* fits into an unsigned long */
1652 return (unsigned long)v; 1759 return (unsigned long)v;
1653} 1760}
1797{ 1904{
1798 WL head; 1905 WL head;
1799 unsigned char events; /* the events watched for */ 1906 unsigned char events; /* the events watched for */
1800 unsigned char reify; /* flag set when this ANFD needs reification (EV_ANFD_REIFY, EV__IOFDSET) */ 1907 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 */ 1908 unsigned char emask; /* some backends store the actual kernel mask in here */
1802 unsigned char unused; 1909 unsigned char eflags; /* flags field for use by backends */
1803#if EV_USE_EPOLL 1910#if EV_USE_EPOLL
1804 unsigned int egen; /* generation counter to counter epoll bugs */ 1911 unsigned int egen; /* generation counter to counter epoll bugs */
1805#endif 1912#endif
1806#if EV_SELECT_IS_WINSOCKET || EV_USE_IOCP 1913#if EV_SELECT_IS_WINSOCKET || EV_USE_IOCP
1807 SOCKET handle; 1914 SOCKET handle;
1861 static struct ev_loop default_loop_struct; 1968 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 */ 1969 EV_API_DECL struct ev_loop *ev_default_loop_ptr = 0; /* needs to be initialised to make it a definition despite extern */
1863 1970
1864#else 1971#else
1865 1972
1866 EV_API_DECL ev_tstamp ev_rt_now = 0; /* needs to be initialised to make it a definition despite extern */ 1973 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; 1974 #define VAR(name,decl) static decl;
1868 #include "ev_vars.h" 1975 #include "ev_vars.h"
1869 #undef VAR 1976 #undef VAR
1870 1977
1871 static int ev_default_loop_ptr; 1978 static int ev_default_loop_ptr;
1893#if EV_USE_REALTIME 2000#if EV_USE_REALTIME
1894 if (ecb_expect_true (have_realtime)) 2001 if (ecb_expect_true (have_realtime))
1895 { 2002 {
1896 struct timespec ts; 2003 struct timespec ts;
1897 clock_gettime (CLOCK_REALTIME, &ts); 2004 clock_gettime (CLOCK_REALTIME, &ts);
1898 return ts.tv_sec + ts.tv_nsec * 1e-9; 2005 return EV_TS_GET (ts);
1899 } 2006 }
1900#endif 2007#endif
1901 2008
2009 {
1902 struct timeval tv; 2010 struct timeval tv;
1903 gettimeofday (&tv, 0); 2011 gettimeofday (&tv, 0);
1904 return tv.tv_sec + tv.tv_usec * 1e-6; 2012 return EV_TV_GET (tv);
2013 }
1905} 2014}
1906#endif 2015#endif
1907 2016
1908inline_size ev_tstamp 2017inline_size ev_tstamp
1909get_clock (void) 2018get_clock (void)
1911#if EV_USE_MONOTONIC 2020#if EV_USE_MONOTONIC
1912 if (ecb_expect_true (have_monotonic)) 2021 if (ecb_expect_true (have_monotonic))
1913 { 2022 {
1914 struct timespec ts; 2023 struct timespec ts;
1915 clock_gettime (CLOCK_MONOTONIC, &ts); 2024 clock_gettime (CLOCK_MONOTONIC, &ts);
1916 return ts.tv_sec + ts.tv_nsec * 1e-9; 2025 return EV_TS_GET (ts);
1917 } 2026 }
1918#endif 2027#endif
1919 2028
1920 return ev_time (); 2029 return ev_time ();
1921} 2030}
1929#endif 2038#endif
1930 2039
1931void 2040void
1932ev_sleep (ev_tstamp delay) EV_NOEXCEPT 2041ev_sleep (ev_tstamp delay) EV_NOEXCEPT
1933{ 2042{
1934 if (delay > 0.) 2043 if (delay > EV_TS_CONST (0.))
1935 { 2044 {
1936#if EV_USE_NANOSLEEP 2045#if EV_USE_NANOSLEEP
1937 struct timespec ts; 2046 struct timespec ts;
1938 2047
1939 EV_TS_SET (ts, delay); 2048 EV_TS_SET (ts, delay);
1940 nanosleep (&ts, 0); 2049 nanosleep (&ts, 0);
1941#elif defined _WIN32 2050#elif defined _WIN32
1942 /* maybe this should round up, as ms is very low resolution */ 2051 /* maybe this should round up, as ms is very low resolution */
1943 /* compared to select (µs) or nanosleep (ns) */ 2052 /* compared to select (µs) or nanosleep (ns) */
1944 Sleep ((unsigned long)(delay * 1e3)); 2053 Sleep ((unsigned long)(EV_TS_TO_MSEC (delay)));
1945#else 2054#else
1946 struct timeval tv; 2055 struct timeval tv;
1947 2056
1948 /* here we rely on sys/time.h + sys/types.h + unistd.h providing select */ 2057 /* here we rely on sys/time.h + sys/types.h + unistd.h providing select */
1949 /* something not guaranteed by newer posix versions, but guaranteed */ 2058 /* something not guaranteed by newer posix versions, but guaranteed */
2299 2408
2300 /* find minimum child */ 2409 /* find minimum child */
2301 if (ecb_expect_true (pos + DHEAP - 1 < E)) 2410 if (ecb_expect_true (pos + DHEAP - 1 < E))
2302 { 2411 {
2303 /* fast path */ (minpos = pos + 0), (minat = ANHE_at (*minpos)); 2412 /* fast path */ (minpos = pos + 0), (minat = ANHE_at (*minpos));
2304 if ( ANHE_at (pos [1]) < minat) (minpos = pos + 1), (minat = ANHE_at (*minpos)); 2413 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)); 2414 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)); 2415 if ( minat > ANHE_at (pos [3])) (minpos = pos + 3), (minat = ANHE_at (*minpos));
2307 } 2416 }
2308 else if (pos < E) 2417 else if (pos < E)
2309 { 2418 {
2310 /* slow path */ (minpos = pos + 0), (minat = ANHE_at (*minpos)); 2419 /* 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)); 2420 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)); 2421 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)); 2422 if (pos + 3 < E && minat > ANHE_at (pos [3])) (minpos = pos + 3), (minat = ANHE_at (*minpos));
2314 } 2423 }
2315 else 2424 else
2316 break; 2425 break;
2317 2426
2318 if (ANHE_at (he) <= minat) 2427 if (ANHE_at (he) <= minat)
2326 2435
2327 heap [k] = he; 2436 heap [k] = he;
2328 ev_active (ANHE_w (he)) = k; 2437 ev_active (ANHE_w (he)) = k;
2329} 2438}
2330 2439
2331#else /* 4HEAP */ 2440#else /* not 4HEAP */
2332 2441
2333#define HEAP0 1 2442#define HEAP0 1
2334#define HPARENT(k) ((k) >> 1) 2443#define HPARENT(k) ((k) >> 1)
2335#define UPHEAP_DONE(p,k) (!(p)) 2444#define UPHEAP_DONE(p,k) (!(p))
2336 2445
2737# include "ev_epoll.c" 2846# include "ev_epoll.c"
2738#endif 2847#endif
2739#if EV_USE_LINUXAIO 2848#if EV_USE_LINUXAIO
2740# include "ev_linuxaio.c" 2849# include "ev_linuxaio.c"
2741#endif 2850#endif
2851#if EV_USE_IOURING
2852# include "ev_iouring.c"
2853#endif
2742#if EV_USE_POLL 2854#if EV_USE_POLL
2743# include "ev_poll.c" 2855# include "ev_poll.c"
2744#endif 2856#endif
2745#if EV_USE_SELECT 2857#if EV_USE_SELECT
2746# include "ev_select.c" 2858# include "ev_select.c"
2778 2890
2779 if (EV_USE_PORT ) flags |= EVBACKEND_PORT; 2891 if (EV_USE_PORT ) flags |= EVBACKEND_PORT;
2780 if (EV_USE_KQUEUE ) flags |= EVBACKEND_KQUEUE; 2892 if (EV_USE_KQUEUE ) flags |= EVBACKEND_KQUEUE;
2781 if (EV_USE_EPOLL ) flags |= EVBACKEND_EPOLL; 2893 if (EV_USE_EPOLL ) flags |= EVBACKEND_EPOLL;
2782 if (EV_USE_LINUXAIO) flags |= EVBACKEND_LINUXAIO; 2894 if (EV_USE_LINUXAIO) flags |= EVBACKEND_LINUXAIO;
2895 if (EV_USE_IOURING ) flags |= EVBACKEND_IOURING;
2783 if (EV_USE_POLL ) flags |= EVBACKEND_POLL; 2896 if (EV_USE_POLL ) flags |= EVBACKEND_POLL;
2784 if (EV_USE_SELECT ) flags |= EVBACKEND_SELECT; 2897 if (EV_USE_SELECT ) flags |= EVBACKEND_SELECT;
2785 2898
2786 return flags; 2899 return flags;
2787} 2900}
2808 2921
2809 /* TODO: linuxaio is very experimental */ 2922 /* TODO: linuxaio is very experimental */
2810#if !EV_RECOMMEND_LINUXAIO 2923#if !EV_RECOMMEND_LINUXAIO
2811 flags &= ~EVBACKEND_LINUXAIO; 2924 flags &= ~EVBACKEND_LINUXAIO;
2812#endif 2925#endif
2926 /* TODO: linuxaio is super experimental */
2927#if !EV_RECOMMEND_IOURING
2928 flags &= ~EVBACKEND_IOURING;
2929#endif
2813 2930
2814 return flags; 2931 return flags;
2815} 2932}
2816 2933
2817ecb_cold 2934ecb_cold
2821 int flags = EVBACKEND_EPOLL | EVBACKEND_KQUEUE | EVBACKEND_PORT; 2938 int flags = EVBACKEND_EPOLL | EVBACKEND_KQUEUE | EVBACKEND_PORT;
2822 2939
2823 /* epoll embeddability broken on all linux versions up to at least 2.6.23 */ 2940 /* 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 */ 2941 if (ev_linux_version () < 0x020620) /* disable it on linux < 2.6.32 */
2825 flags &= ~EVBACKEND_EPOLL; 2942 flags &= ~EVBACKEND_EPOLL;
2943
2944 /* EVBACKEND_LINUXAIO is theoretically embeddable, but suffers from a performance overhead */
2945
2946 /* EVBACKEND_IOURING is practically embeddable, but the current implementation is not
2947 * because our backend_fd is the epoll fd we need as fallback.
2948 * if the kernel ever is fixed, this might change...
2949 */
2826 2950
2827 return flags; 2951 return flags;
2828} 2952}
2829 2953
2830unsigned int 2954unsigned int
2961 if (!backend && (flags & EVBACKEND_PORT )) backend = port_init (EV_A_ flags); 3085 if (!backend && (flags & EVBACKEND_PORT )) backend = port_init (EV_A_ flags);
2962#endif 3086#endif
2963#if EV_USE_KQUEUE 3087#if EV_USE_KQUEUE
2964 if (!backend && (flags & EVBACKEND_KQUEUE )) backend = kqueue_init (EV_A_ flags); 3088 if (!backend && (flags & EVBACKEND_KQUEUE )) backend = kqueue_init (EV_A_ flags);
2965#endif 3089#endif
3090#if EV_USE_IOURING
3091 if (!backend && (flags & EVBACKEND_IOURING )) backend = iouring_init (EV_A_ flags);
3092#endif
2966#if EV_USE_LINUXAIO 3093#if EV_USE_LINUXAIO
2967 if (!backend && (flags & EVBACKEND_LINUXAIO)) backend = linuxaio_init (EV_A_ flags); 3094 if (!backend && (flags & EVBACKEND_LINUXAIO)) backend = linuxaio_init (EV_A_ flags);
2968#endif 3095#endif
2969#if EV_USE_EPOLL 3096#if EV_USE_EPOLL
2970 if (!backend && (flags & EVBACKEND_EPOLL )) backend = epoll_init (EV_A_ flags); 3097 if (!backend && (flags & EVBACKEND_EPOLL )) backend = epoll_init (EV_A_ flags);
3043#if EV_USE_PORT 3170#if EV_USE_PORT
3044 if (backend == EVBACKEND_PORT ) port_destroy (EV_A); 3171 if (backend == EVBACKEND_PORT ) port_destroy (EV_A);
3045#endif 3172#endif
3046#if EV_USE_KQUEUE 3173#if EV_USE_KQUEUE
3047 if (backend == EVBACKEND_KQUEUE ) kqueue_destroy (EV_A); 3174 if (backend == EVBACKEND_KQUEUE ) kqueue_destroy (EV_A);
3175#endif
3176#if EV_USE_IOURING
3177 if (backend == EVBACKEND_IOURING ) iouring_destroy (EV_A);
3048#endif 3178#endif
3049#if EV_USE_LINUXAIO 3179#if EV_USE_LINUXAIO
3050 if (backend == EVBACKEND_LINUXAIO) linuxaio_destroy (EV_A); 3180 if (backend == EVBACKEND_LINUXAIO) linuxaio_destroy (EV_A);
3051#endif 3181#endif
3052#if EV_USE_EPOLL 3182#if EV_USE_EPOLL
3110#if EV_USE_PORT 3240#if EV_USE_PORT
3111 if (backend == EVBACKEND_PORT ) port_fork (EV_A); 3241 if (backend == EVBACKEND_PORT ) port_fork (EV_A);
3112#endif 3242#endif
3113#if EV_USE_KQUEUE 3243#if EV_USE_KQUEUE
3114 if (backend == EVBACKEND_KQUEUE ) kqueue_fork (EV_A); 3244 if (backend == EVBACKEND_KQUEUE ) kqueue_fork (EV_A);
3245#endif
3246#if EV_USE_IOURING
3247 if (backend == EVBACKEND_IOURING ) iouring_fork (EV_A);
3115#endif 3248#endif
3116#if EV_USE_LINUXAIO 3249#if EV_USE_LINUXAIO
3117 if (backend == EVBACKEND_LINUXAIO) linuxaio_fork (EV_A); 3250 if (backend == EVBACKEND_LINUXAIO) linuxaio_fork (EV_A);
3118#endif 3251#endif
3119#if EV_USE_EPOLL 3252#if EV_USE_EPOLL
3416 { 3549 {
3417 ev_at (w) += w->repeat; 3550 ev_at (w) += w->repeat;
3418 if (ev_at (w) < mn_now) 3551 if (ev_at (w) < mn_now)
3419 ev_at (w) = mn_now; 3552 ev_at (w) = mn_now;
3420 3553
3421 assert (("libev: negative ev_timer repeat value found while processing timers", w->repeat > 0.)); 3554 assert (("libev: negative ev_timer repeat value found while processing timers", w->repeat > EV_TS_CONST (0.)));
3422 3555
3423 ANHE_at_cache (timers [HEAP0]); 3556 ANHE_at_cache (timers [HEAP0]);
3424 downheap (timers, timercnt, HEAP0); 3557 downheap (timers, timercnt, HEAP0);
3425 } 3558 }
3426 else 3559 else
3557 3690
3558 mn_now = get_clock (); 3691 mn_now = get_clock ();
3559 3692
3560 /* only fetch the realtime clock every 0.5*MIN_TIMEJUMP seconds */ 3693 /* only fetch the realtime clock every 0.5*MIN_TIMEJUMP seconds */
3561 /* interpolate in the meantime */ 3694 /* interpolate in the meantime */
3562 if (ecb_expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) 3695 if (ecb_expect_true (mn_now - now_floor < EV_TS_CONST (MIN_TIMEJUMP * .5)))
3563 { 3696 {
3564 ev_rt_now = rtmn_diff + mn_now; 3697 ev_rt_now = rtmn_diff + mn_now;
3565 return; 3698 return;
3566 } 3699 }
3567 3700
3581 ev_tstamp diff; 3714 ev_tstamp diff;
3582 rtmn_diff = ev_rt_now - mn_now; 3715 rtmn_diff = ev_rt_now - mn_now;
3583 3716
3584 diff = odiff - rtmn_diff; 3717 diff = odiff - rtmn_diff;
3585 3718
3586 if (ecb_expect_true ((diff < 0. ? -diff : diff) < MIN_TIMEJUMP)) 3719 if (ecb_expect_true ((diff < EV_TS_CONST (0.) ? -diff : diff) < EV_TS_CONST (MIN_TIMEJUMP)))
3587 return; /* all is well */ 3720 return; /* all is well */
3588 3721
3589 ev_rt_now = ev_time (); 3722 ev_rt_now = ev_time ();
3590 mn_now = get_clock (); 3723 mn_now = get_clock ();
3591 now_floor = mn_now; 3724 now_floor = mn_now;
3600 else 3733 else
3601#endif 3734#endif
3602 { 3735 {
3603 ev_rt_now = ev_time (); 3736 ev_rt_now = ev_time ();
3604 3737
3605 if (ecb_expect_false (mn_now > ev_rt_now || ev_rt_now > mn_now + max_block + MIN_TIMEJUMP)) 3738 if (ecb_expect_false (mn_now > ev_rt_now || ev_rt_now > mn_now + max_block + EV_TS_CONST (MIN_TIMEJUMP)))
3606 { 3739 {
3607 /* adjust timers. this is easy, as the offset is the same for all of them */ 3740 /* 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); 3741 timers_reschedule (EV_A_ ev_rt_now - mn_now);
3609#if EV_PERIODIC_ENABLE 3742#if EV_PERIODIC_ENABLE
3610 periodics_reschedule (EV_A); 3743 periodics_reschedule (EV_A);
3679 3812
3680 /* remember old timestamp for io_blocktime calculation */ 3813 /* remember old timestamp for io_blocktime calculation */
3681 ev_tstamp prev_mn_now = mn_now; 3814 ev_tstamp prev_mn_now = mn_now;
3682 3815
3683 /* update time to cancel out callback processing overhead */ 3816 /* update time to cancel out callback processing overhead */
3684 time_update (EV_A_ 1e100); 3817 time_update (EV_A_ EV_TS_CONST (EV_TSTAMP_HUGE));
3685 3818
3686 /* from now on, we want a pipe-wake-up */ 3819 /* from now on, we want a pipe-wake-up */
3687 pipe_write_wanted = 1; 3820 pipe_write_wanted = 1;
3688 3821
3689 ECB_MEMORY_FENCE; /* make sure pipe_write_wanted is visible before we check for potential skips */ 3822 ECB_MEMORY_FENCE; /* make sure pipe_write_wanted is visible before we check for potential skips */
3690 3823
3691 if (ecb_expect_true (!(flags & EVRUN_NOWAIT || idleall || !activecnt || pipe_write_skipped))) 3824 if (ecb_expect_true (!(flags & EVRUN_NOWAIT || idleall || !activecnt || pipe_write_skipped)))
3692 { 3825 {
3693 waittime = MAX_BLOCKTIME; 3826 waittime = EV_TS_CONST (MAX_BLOCKTIME);
3694 3827
3695 if (timercnt) 3828 if (timercnt)
3696 { 3829 {
3697 ev_tstamp to = ANHE_at (timers [HEAP0]) - mn_now; 3830 ev_tstamp to = ANHE_at (timers [HEAP0]) - mn_now;
3698 if (waittime > to) waittime = to; 3831 if (waittime > to) waittime = to;
3721 sleeptime = io_blocktime - (mn_now - prev_mn_now); 3854 sleeptime = io_blocktime - (mn_now - prev_mn_now);
3722 3855
3723 if (sleeptime > waittime - backend_mintime) 3856 if (sleeptime > waittime - backend_mintime)
3724 sleeptime = waittime - backend_mintime; 3857 sleeptime = waittime - backend_mintime;
3725 3858
3726 if (ecb_expect_true (sleeptime > 0.)) 3859 if (ecb_expect_true (sleeptime > EV_TS_CONST (0.)))
3727 { 3860 {
3728 ev_sleep (sleeptime); 3861 ev_sleep (sleeptime);
3729 waittime -= sleeptime; 3862 waittime -= sleeptime;
3730 } 3863 }
3731 } 3864 }
3805} 3938}
3806 3939
3807void 3940void
3808ev_now_update (EV_P) EV_NOEXCEPT 3941ev_now_update (EV_P) EV_NOEXCEPT
3809{ 3942{
3810 time_update (EV_A_ 1e100); 3943 time_update (EV_A_ EV_TSTAMP_HUGE);
3811} 3944}
3812 3945
3813void 3946void
3814ev_suspend (EV_P) EV_NOEXCEPT 3947ev_suspend (EV_P) EV_NOEXCEPT
3815{ 3948{
4046} 4179}
4047 4180
4048ev_tstamp 4181ev_tstamp
4049ev_timer_remaining (EV_P_ ev_timer *w) EV_NOEXCEPT 4182ev_timer_remaining (EV_P_ ev_timer *w) EV_NOEXCEPT
4050{ 4183{
4051 return ev_at (w) - (ev_is_active (w) ? mn_now : 0.); 4184 return ev_at (w) - (ev_is_active (w) ? mn_now : EV_TS_CONST (0.));
4052} 4185}
4053 4186
4054#if EV_PERIODIC_ENABLE 4187#if EV_PERIODIC_ENABLE
4055ecb_noinline 4188ecb_noinline
4056void 4189void

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines