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

Comparing libev/ev.c (file contents):
Revision 1.498 by root, Wed Jun 26 00:01:46 2019 UTC vs.
Revision 1.511 by root, Fri Nov 22 14:32:13 2019 UTC

124# else 124# else
125# undef EV_USE_LINUXAIO 125# undef EV_USE_LINUXAIO
126# define EV_USE_LINUXAIO 0 126# define EV_USE_LINUXAIO 0
127# endif 127# endif
128 128
129# if HAVE_LINUX_FS_H && HAVE_SYS_TIMERFD_H && HAVE_KERNEL_RWF_T
130# ifndef EV_USE_IOURING
131# define EV_USE_IOURING EV_FEATURE_BACKENDS
132# endif
133# else
134# undef EV_USE_IOURING
135# define EV_USE_IOURING 0
136# endif
137
129# if HAVE_KQUEUE && HAVE_SYS_EVENT_H 138# if HAVE_KQUEUE && HAVE_SYS_EVENT_H
130# ifndef EV_USE_KQUEUE 139# ifndef EV_USE_KQUEUE
131# define EV_USE_KQUEUE EV_FEATURE_BACKENDS 140# define EV_USE_KQUEUE EV_FEATURE_BACKENDS
132# endif 141# endif
133# else 142# else
168# endif 177# endif
169# else 178# else
170# undef EV_USE_EVENTFD 179# undef EV_USE_EVENTFD
171# define EV_USE_EVENTFD 0 180# define EV_USE_EVENTFD 0
172# endif 181# endif
173 182
174#endif 183#endif
175 184
176/* OS X, in its infinite idiocy, actually HARDCODES 185/* OS X, in its infinite idiocy, actually HARDCODES
177 * a limit of 1024 into their select. Where people have brains, 186 * a limit of 1024 into their select. Where people have brains,
178 * OS X engineers apparently have a vacuum. Or maybe they were 187 * OS X engineers apparently have a vacuum. Or maybe they were
332# else 341# else
333# define EV_USE_LINUXAIO 0 342# define EV_USE_LINUXAIO 0
334# endif 343# endif
335#endif 344#endif
336 345
346#ifndef EV_USE_IOURING
347# if __linux /* later checks might disable again */
348# define EV_USE_IOURING 1
349# else
350# define EV_USE_IOURING 0
351# endif
352#endif
353
337#ifndef EV_USE_INOTIFY 354#ifndef EV_USE_INOTIFY
338# if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 4)) 355# if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 4))
339# define EV_USE_INOTIFY EV_FEATURE_OS 356# define EV_USE_INOTIFY EV_FEATURE_OS
340# else 357# else
341# define EV_USE_INOTIFY 0 358# define EV_USE_INOTIFY 0
406# include <sys/syscall.h> 423# include <sys/syscall.h>
407# ifdef SYS_clock_gettime 424# ifdef SYS_clock_gettime
408# define clock_gettime(id, ts) syscall (SYS_clock_gettime, (id), (ts)) 425# define clock_gettime(id, ts) syscall (SYS_clock_gettime, (id), (ts))
409# undef EV_USE_MONOTONIC 426# undef EV_USE_MONOTONIC
410# define EV_USE_MONOTONIC 1 427# define EV_USE_MONOTONIC 1
428# define EV_NEED_SYSCALL 1
411# else 429# else
412# undef EV_USE_CLOCK_SYSCALL 430# undef EV_USE_CLOCK_SYSCALL
413# define EV_USE_CLOCK_SYSCALL 0 431# define EV_USE_CLOCK_SYSCALL 0
414# endif 432# endif
415#endif 433#endif
427#endif 445#endif
428 446
429#if !EV_STAT_ENABLE 447#if !EV_STAT_ENABLE
430# undef EV_USE_INOTIFY 448# undef EV_USE_INOTIFY
431# define EV_USE_INOTIFY 0 449# define EV_USE_INOTIFY 0
450#endif
451
452#if __linux && EV_USE_IOURING
453# include <linux/fs.h>
454# ifndef RWF_SYNC
455# undef EV_USE_IOURING
456# define EV_USE_IOURING 0
457# endif
432#endif 458#endif
433 459
434#if !EV_USE_NANOSLEEP 460#if !EV_USE_NANOSLEEP
435/* hp-ux has it in sys/time.h, which we unconditionally include above */ 461/* hp-ux has it in sys/time.h, which we unconditionally include above */
436# if !defined _WIN32 && !defined __hpux 462# if !defined _WIN32 && !defined __hpux
438# endif 464# endif
439#endif 465#endif
440 466
441#if EV_USE_LINUXAIO 467#if EV_USE_LINUXAIO
442# include <sys/syscall.h> 468# include <sys/syscall.h>
443# if !SYS_io_getevents || !EV_USE_EPOLL /* ev_linxaio uses ev_poll.c:ev_epoll_create */ 469# if SYS_io_getevents && EV_USE_EPOLL /* linuxaio backend requires epoll backend */
470# define EV_NEED_SYSCALL 1
471# else
444# undef EV_USE_LINUXAIO 472# undef EV_USE_LINUXAIO
445# define EV_USE_LINUXAIO 0 473# define EV_USE_LINUXAIO 0
474# endif
475#endif
476
477#if EV_USE_IOURING
478# include <sys/syscall.h>
479# if !SYS_io_uring_setup && __linux && !__alpha
480# define SYS_io_uring_setup 425
481# define SYS_io_uring_enter 426
482# define SYS_io_uring_wregister 427
483# endif
484# if SYS_io_uring_setup && EV_USE_EPOLL /* iouring backend requires epoll backend */
485# define EV_NEED_SYSCALL 1
486# else
487# undef EV_USE_IOURING
488# define EV_USE_IOURING 0
446# endif 489# endif
447#endif 490#endif
448 491
449#if EV_USE_INOTIFY 492#if EV_USE_INOTIFY
450# include <sys/statfs.h> 493# include <sys/statfs.h>
492 uint32_t ssi_signo; 535 uint32_t ssi_signo;
493 char pad[128 - sizeof (uint32_t)]; 536 char pad[128 - sizeof (uint32_t)];
494}; 537};
495#endif 538#endif
496 539
497/**/ 540/*****************************************************************************/
498 541
499#if EV_VERIFY >= 3 542#if EV_VERIFY >= 3
500# define EV_FREQUENT_CHECK ev_verify (EV_A) 543# define EV_FREQUENT_CHECK ev_verify (EV_A)
501#else 544#else
502# define EV_FREQUENT_CHECK do { } while (0) 545# define EV_FREQUENT_CHECK do { } while (0)
507 * This value is good at least till the year 4000. 550 * This value is good at least till the year 4000.
508 */ 551 */
509#define MIN_INTERVAL 0.0001220703125 /* 1/2**13, good till 4000 */ 552#define MIN_INTERVAL 0.0001220703125 /* 1/2**13, good till 4000 */
510/*#define MIN_INTERVAL 0.00000095367431640625 /* 1/2**20, good till 2200 */ 553/*#define MIN_INTERVAL 0.00000095367431640625 /* 1/2**20, good till 2200 */
511 554
512#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ 555#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) */ 556#define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */
514 557
558/* find a portable timestamp that is "always" in the future but fits into time_t.
559 * this is quite hard, and we are mostly guessing - we handle 32 bit signed/unsigned time_t,
560 * and sizes larger than 32 bit, and maybe the unlikely floating point time_t */
561#define EV_TSTAMP_HUGE \
562 (sizeof (time_t) >= 8 ? 10000000000000. \
563 : 0 < (time_t)4294967295 ? 4294967295. \
564 : 2147483647.) \
565
566#ifndef EV_TS_CONST
567# define EV_TS_CONST(nv) nv
568# define EV_TS_TO_MSEC(a) a * 1e3 + 0.9999
569# 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) 570# 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) 571# define EV_TS_SET(ts,t) do { ts.tv_sec = (long)t; ts.tv_nsec = (long)((t - ts.tv_sec) * 1e9); } while (0)
572# define EV_TV_GET(tv) ((tv).tv_sec + (tv).tv_usec * 1e-6)
573# define EV_TS_GET(ts) ((ts).tv_sec + (ts).tv_nsec * 1e-9)
574#endif
517 575
518/* the following is ecb.h embedded into libev - use update_ev_c to update from an external copy */ 576/* the following is ecb.h embedded into libev - use update_ev_c to update from an external copy */
519/* ECB.H BEGIN */ 577/* ECB.H BEGIN */
520/* 578/*
521 * libecb - http://software.schmorp.de/pkg/libecb 579 * libecb - http://software.schmorp.de/pkg/libecb
1536/* ECB.H END */ 1594/* ECB.H END */
1537 1595
1538#if ECB_MEMORY_FENCE_NEEDS_PTHREADS 1596#if ECB_MEMORY_FENCE_NEEDS_PTHREADS
1539/* if your architecture doesn't need memory fences, e.g. because it is 1597/* if your architecture doesn't need memory fences, e.g. because it is
1540 * single-cpu/core, or if you use libev in a project that doesn't use libev 1598 * single-cpu/core, or if you use libev in a project that doesn't use libev
1541 * from multiple threads, then you can define ECB_AVOID_PTHREADS when compiling 1599 * from multiple threads, then you can define ECB_NO_THREADS when compiling
1542 * libev, in which cases the memory fences become nops. 1600 * libev, in which cases the memory fences become nops.
1543 * alternatively, you can remove this #error and link against libpthread, 1601 * alternatively, you can remove this #error and link against libpthread,
1544 * which will then provide the memory fences. 1602 * which will then provide the memory fences.
1545 */ 1603 */
1546# error "memory fences not defined for your architecture, please report" 1604# error "memory fences not defined for your architecture, please report"
1550# define ECB_MEMORY_FENCE do { } while (0) 1608# define ECB_MEMORY_FENCE do { } while (0)
1551# define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE 1609# define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE
1552# define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE 1610# define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE
1553#endif 1611#endif
1554 1612
1555#define expect_false(cond) ecb_expect_false (cond)
1556#define expect_true(cond) ecb_expect_true (cond)
1557#define noinline ecb_noinline
1558
1559#define inline_size ecb_inline 1613#define inline_size ecb_inline
1560 1614
1561#if EV_FEATURE_CODE 1615#if EV_FEATURE_CODE
1562# define inline_speed ecb_inline 1616# define inline_speed ecb_inline
1563#else 1617#else
1564# define inline_speed noinline static 1618# define inline_speed ecb_noinline static
1565#endif 1619#endif
1620
1621/*****************************************************************************/
1622/* raw syscall wrappers */
1623
1624#if EV_NEED_SYSCALL
1625
1626#include <sys/syscall.h>
1627
1628/*
1629 * define some syscall wrappers for common architectures
1630 * this is mostly for nice looks during debugging, not performance.
1631 * our syscalls return < 0, not == -1, on error. which is good
1632 * enough for linux aio.
1633 * TODO: arm is also common nowadays, maybe even mips and x86
1634 * TODO: after implementing this, it suddenly looks like overkill, but its hard to remove...
1635 */
1636#if __GNUC__ && __linux && ECB_AMD64 && !defined __OPTIMIZE_SIZE__
1637 /* the costly errno access probably kills this for size optimisation */
1638
1639 #define ev_syscall(nr,narg,arg1,arg2,arg3,arg4,arg5,arg6) \
1640 ({ \
1641 long res; \
1642 register unsigned long r6 __asm__ ("r9" ); \
1643 register unsigned long r5 __asm__ ("r8" ); \
1644 register unsigned long r4 __asm__ ("r10"); \
1645 register unsigned long r3 __asm__ ("rdx"); \
1646 register unsigned long r2 __asm__ ("rsi"); \
1647 register unsigned long r1 __asm__ ("rdi"); \
1648 if (narg >= 6) r6 = (unsigned long)(arg6); \
1649 if (narg >= 5) r5 = (unsigned long)(arg5); \
1650 if (narg >= 4) r4 = (unsigned long)(arg4); \
1651 if (narg >= 3) r3 = (unsigned long)(arg3); \
1652 if (narg >= 2) r2 = (unsigned long)(arg2); \
1653 if (narg >= 1) r1 = (unsigned long)(arg1); \
1654 __asm__ __volatile__ ( \
1655 "syscall\n\t" \
1656 : "=a" (res) \
1657 : "0" (nr), "r" (r1), "r" (r2), "r" (r3), "r" (r4), "r" (r5) \
1658 : "cc", "r11", "cx", "memory"); \
1659 errno = -res; \
1660 res; \
1661 })
1662
1663#endif
1664
1665#ifdef ev_syscall
1666 #define ev_syscall0(nr) ev_syscall (nr, 0, 0, 0, 0, 0, 0, 0)
1667 #define ev_syscall1(nr,arg1) ev_syscall (nr, 1, arg1, 0, 0, 0, 0, 0)
1668 #define ev_syscall2(nr,arg1,arg2) ev_syscall (nr, 2, arg1, arg2, 0, 0, 0, 0)
1669 #define ev_syscall3(nr,arg1,arg2,arg3) ev_syscall (nr, 3, arg1, arg2, arg3, 0, 0, 0)
1670 #define ev_syscall4(nr,arg1,arg2,arg3,arg4) ev_syscall (nr, 3, arg1, arg2, arg3, arg4, 0, 0)
1671 #define ev_syscall5(nr,arg1,arg2,arg3,arg4,arg5) ev_syscall (nr, 5, arg1, arg2, arg3, arg4, arg5, 0)
1672 #define ev_syscall6(nr,arg1,arg2,arg3,arg4,arg5,arg6) ev_syscall (nr, 6, arg1, arg2, arg3, arg4, arg5,arg6)
1673#else
1674 #define ev_syscall0(nr) syscall (nr)
1675 #define ev_syscall1(nr,arg1) syscall (nr, arg1)
1676 #define ev_syscall2(nr,arg1,arg2) syscall (nr, arg1, arg2)
1677 #define ev_syscall3(nr,arg1,arg2,arg3) syscall (nr, arg1, arg2, arg3)
1678 #define ev_syscall4(nr,arg1,arg2,arg3,arg4) syscall (nr, arg1, arg2, arg3, arg4)
1679 #define ev_syscall5(nr,arg1,arg2,arg3,arg4,arg5) syscall (nr, arg1, arg2, arg3, arg4, arg5)
1680 #define ev_syscall6(nr,arg1,arg2,arg3,arg4,arg5,arg6) syscall (nr, arg1, arg2, arg3, arg4, arg5,arg6)
1681#endif
1682
1683#endif
1684
1685/*****************************************************************************/
1566 1686
1567#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) 1687#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1)
1568 1688
1569#if EV_MINPRI == EV_MAXPRI 1689#if EV_MINPRI == EV_MAXPRI
1570# define ABSPRI(w) (((W)w), 0) 1690# define ABSPRI(w) (((W)w), 0)
1619#else 1739#else
1620 1740
1621#include <float.h> 1741#include <float.h>
1622 1742
1623/* a floor() replacement function, should be independent of ev_tstamp type */ 1743/* a floor() replacement function, should be independent of ev_tstamp type */
1624noinline 1744ecb_noinline
1625static ev_tstamp 1745static ev_tstamp
1626ev_floor (ev_tstamp v) 1746ev_floor (ev_tstamp v)
1627{ 1747{
1628 /* the choice of shift factor is not terribly important */ 1748 /* the choice of shift factor is not terribly important */
1629#if FLT_RADIX != 2 /* assume FLT_RADIX == 10 */ 1749#if FLT_RADIX != 2 /* assume FLT_RADIX == 10 */
1630 const ev_tstamp shift = sizeof (unsigned long) >= 8 ? 10000000000000000000. : 1000000000.; 1750 const ev_tstamp shift = sizeof (unsigned long) >= 8 ? 10000000000000000000. : 1000000000.;
1631#else 1751#else
1632 const ev_tstamp shift = sizeof (unsigned long) >= 8 ? 18446744073709551616. : 4294967296.; 1752 const ev_tstamp shift = sizeof (unsigned long) >= 8 ? 18446744073709551616. : 4294967296.;
1633#endif 1753#endif
1634 1754
1755 /* special treatment for negative arguments */
1756 if (ecb_expect_false (v < 0.))
1757 {
1758 ev_tstamp f = -ev_floor (-v);
1759
1760 return f - (f == v ? 0 : 1);
1761 }
1762
1635 /* argument too large for an unsigned long? */ 1763 /* argument too large for an unsigned long? then reduce it */
1636 if (expect_false (v >= shift)) 1764 if (ecb_expect_false (v >= shift))
1637 { 1765 {
1638 ev_tstamp f; 1766 ev_tstamp f;
1639 1767
1640 if (v == v - 1.) 1768 if (v == v - 1.)
1641 return v; /* very large number */ 1769 return v; /* very large numbers are assumed to be integer */
1642 1770
1643 f = shift * ev_floor (v * (1. / shift)); 1771 f = shift * ev_floor (v * (1. / shift));
1644 return f + ev_floor (v - f); 1772 return f + ev_floor (v - f);
1645 } 1773 }
1646 1774
1647 /* special treatment for negative args? */
1648 if (expect_false (v < 0.))
1649 {
1650 ev_tstamp f = -ev_floor (-v);
1651
1652 return f - (f == v ? 0 : 1);
1653 }
1654
1655 /* fits into an unsigned long */ 1775 /* fits into an unsigned long */
1656 return (unsigned long)v; 1776 return (unsigned long)v;
1657} 1777}
1658 1778
1659#endif 1779#endif
1662 1782
1663#ifdef __linux 1783#ifdef __linux
1664# include <sys/utsname.h> 1784# include <sys/utsname.h>
1665#endif 1785#endif
1666 1786
1667noinline ecb_cold 1787ecb_noinline ecb_cold
1668static unsigned int 1788static unsigned int
1669ev_linux_version (void) 1789ev_linux_version (void)
1670{ 1790{
1671#ifdef __linux 1791#ifdef __linux
1672 unsigned int v = 0; 1792 unsigned int v = 0;
1702} 1822}
1703 1823
1704/*****************************************************************************/ 1824/*****************************************************************************/
1705 1825
1706#if EV_AVOID_STDIO 1826#if EV_AVOID_STDIO
1707noinline ecb_cold 1827ecb_noinline ecb_cold
1708static void 1828static void
1709ev_printerr (const char *msg) 1829ev_printerr (const char *msg)
1710{ 1830{
1711 write (STDERR_FILENO, msg, strlen (msg)); 1831 write (STDERR_FILENO, msg, strlen (msg));
1712} 1832}
1719ev_set_syserr_cb (void (*cb)(const char *msg) EV_NOEXCEPT) EV_NOEXCEPT 1839ev_set_syserr_cb (void (*cb)(const char *msg) EV_NOEXCEPT) EV_NOEXCEPT
1720{ 1840{
1721 syserr_cb = cb; 1841 syserr_cb = cb;
1722} 1842}
1723 1843
1724noinline ecb_cold 1844ecb_noinline ecb_cold
1725static void 1845static void
1726ev_syserr (const char *msg) 1846ev_syserr (const char *msg)
1727{ 1847{
1728 if (!msg) 1848 if (!msg)
1729 msg = "(libev) system error"; 1849 msg = "(libev) system error";
1801{ 1921{
1802 WL head; 1922 WL head;
1803 unsigned char events; /* the events watched for */ 1923 unsigned char events; /* the events watched for */
1804 unsigned char reify; /* flag set when this ANFD needs reification (EV_ANFD_REIFY, EV__IOFDSET) */ 1924 unsigned char reify; /* flag set when this ANFD needs reification (EV_ANFD_REIFY, EV__IOFDSET) */
1805 unsigned char emask; /* some backends store the actual kernel mask in here */ 1925 unsigned char emask; /* some backends store the actual kernel mask in here */
1806 unsigned char unused; 1926 unsigned char eflags; /* flags field for use by backends */
1807#if EV_USE_EPOLL 1927#if EV_USE_EPOLL
1808 unsigned int egen; /* generation counter to counter epoll bugs */ 1928 unsigned int egen; /* generation counter to counter epoll bugs */
1809#endif 1929#endif
1810#if EV_SELECT_IS_WINSOCKET || EV_USE_IOCP 1930#if EV_SELECT_IS_WINSOCKET || EV_USE_IOCP
1811 SOCKET handle; 1931 SOCKET handle;
1865 static struct ev_loop default_loop_struct; 1985 static struct ev_loop default_loop_struct;
1866 EV_API_DECL struct ev_loop *ev_default_loop_ptr = 0; /* needs to be initialised to make it a definition despite extern */ 1986 EV_API_DECL struct ev_loop *ev_default_loop_ptr = 0; /* needs to be initialised to make it a definition despite extern */
1867 1987
1868#else 1988#else
1869 1989
1870 EV_API_DECL ev_tstamp ev_rt_now = 0; /* needs to be initialised to make it a definition despite extern */ 1990 EV_API_DECL ev_tstamp ev_rt_now = EV_TS_CONST (0.); /* needs to be initialised to make it a definition despite extern */
1871 #define VAR(name,decl) static decl; 1991 #define VAR(name,decl) static decl;
1872 #include "ev_vars.h" 1992 #include "ev_vars.h"
1873 #undef VAR 1993 #undef VAR
1874 1994
1875 static int ev_default_loop_ptr; 1995 static int ev_default_loop_ptr;
1876 1996
1877#endif 1997#endif
1878 1998
1879#if EV_FEATURE_API 1999#if EV_FEATURE_API
1880# define EV_RELEASE_CB if (expect_false (release_cb)) release_cb (EV_A) 2000# define EV_RELEASE_CB if (ecb_expect_false (release_cb)) release_cb (EV_A)
1881# define EV_ACQUIRE_CB if (expect_false (acquire_cb)) acquire_cb (EV_A) 2001# define EV_ACQUIRE_CB if (ecb_expect_false (acquire_cb)) acquire_cb (EV_A)
1882# define EV_INVOKE_PENDING invoke_cb (EV_A) 2002# define EV_INVOKE_PENDING invoke_cb (EV_A)
1883#else 2003#else
1884# define EV_RELEASE_CB (void)0 2004# define EV_RELEASE_CB (void)0
1885# define EV_ACQUIRE_CB (void)0 2005# define EV_ACQUIRE_CB (void)0
1886# define EV_INVOKE_PENDING ev_invoke_pending (EV_A) 2006# define EV_INVOKE_PENDING ev_invoke_pending (EV_A)
1893#ifndef EV_HAVE_EV_TIME 2013#ifndef EV_HAVE_EV_TIME
1894ev_tstamp 2014ev_tstamp
1895ev_time (void) EV_NOEXCEPT 2015ev_time (void) EV_NOEXCEPT
1896{ 2016{
1897#if EV_USE_REALTIME 2017#if EV_USE_REALTIME
1898 if (expect_true (have_realtime)) 2018 if (ecb_expect_true (have_realtime))
1899 { 2019 {
1900 struct timespec ts; 2020 struct timespec ts;
1901 clock_gettime (CLOCK_REALTIME, &ts); 2021 clock_gettime (CLOCK_REALTIME, &ts);
1902 return ts.tv_sec + ts.tv_nsec * 1e-9; 2022 return EV_TS_GET (ts);
1903 } 2023 }
1904#endif 2024#endif
1905 2025
2026 {
1906 struct timeval tv; 2027 struct timeval tv;
1907 gettimeofday (&tv, 0); 2028 gettimeofday (&tv, 0);
1908 return tv.tv_sec + tv.tv_usec * 1e-6; 2029 return EV_TV_GET (tv);
2030 }
1909} 2031}
1910#endif 2032#endif
1911 2033
1912inline_size ev_tstamp 2034inline_size ev_tstamp
1913get_clock (void) 2035get_clock (void)
1914{ 2036{
1915#if EV_USE_MONOTONIC 2037#if EV_USE_MONOTONIC
1916 if (expect_true (have_monotonic)) 2038 if (ecb_expect_true (have_monotonic))
1917 { 2039 {
1918 struct timespec ts; 2040 struct timespec ts;
1919 clock_gettime (CLOCK_MONOTONIC, &ts); 2041 clock_gettime (CLOCK_MONOTONIC, &ts);
1920 return ts.tv_sec + ts.tv_nsec * 1e-9; 2042 return EV_TS_GET (ts);
1921 } 2043 }
1922#endif 2044#endif
1923 2045
1924 return ev_time (); 2046 return ev_time ();
1925} 2047}
1933#endif 2055#endif
1934 2056
1935void 2057void
1936ev_sleep (ev_tstamp delay) EV_NOEXCEPT 2058ev_sleep (ev_tstamp delay) EV_NOEXCEPT
1937{ 2059{
1938 if (delay > 0.) 2060 if (delay > EV_TS_CONST (0.))
1939 { 2061 {
1940#if EV_USE_NANOSLEEP 2062#if EV_USE_NANOSLEEP
1941 struct timespec ts; 2063 struct timespec ts;
1942 2064
1943 EV_TS_SET (ts, delay); 2065 EV_TS_SET (ts, delay);
1944 nanosleep (&ts, 0); 2066 nanosleep (&ts, 0);
1945#elif defined _WIN32 2067#elif defined _WIN32
1946 /* maybe this should round up, as ms is very low resolution */ 2068 /* maybe this should round up, as ms is very low resolution */
1947 /* compared to select (µs) or nanosleep (ns) */ 2069 /* compared to select (µs) or nanosleep (ns) */
1948 Sleep ((unsigned long)(delay * 1e3)); 2070 Sleep ((unsigned long)(EV_TS_TO_MSEC (delay)));
1949#else 2071#else
1950 struct timeval tv; 2072 struct timeval tv;
1951 2073
1952 /* here we rely on sys/time.h + sys/types.h + unistd.h providing select */ 2074 /* here we rely on sys/time.h + sys/types.h + unistd.h providing select */
1953 /* something not guaranteed by newer posix versions, but guaranteed */ 2075 /* something not guaranteed by newer posix versions, but guaranteed */
1983 } 2105 }
1984 2106
1985 return ncur; 2107 return ncur;
1986} 2108}
1987 2109
1988noinline ecb_cold 2110ecb_noinline ecb_cold
1989static void * 2111static void *
1990array_realloc (int elem, void *base, int *cur, int cnt) 2112array_realloc (int elem, void *base, int *cur, int cnt)
1991{ 2113{
1992 *cur = array_nextsize (elem, *cur, cnt); 2114 *cur = array_nextsize (elem, *cur, cnt);
1993 return ev_realloc (base, elem * *cur); 2115 return ev_realloc (base, elem * *cur);
1997 2119
1998#define array_needsize_zerofill(base,offset,count) \ 2120#define array_needsize_zerofill(base,offset,count) \
1999 memset ((void *)(base + offset), 0, sizeof (*(base)) * (count)) 2121 memset ((void *)(base + offset), 0, sizeof (*(base)) * (count))
2000 2122
2001#define array_needsize(type,base,cur,cnt,init) \ 2123#define array_needsize(type,base,cur,cnt,init) \
2002 if (expect_false ((cnt) > (cur))) \ 2124 if (ecb_expect_false ((cnt) > (cur))) \
2003 { \ 2125 { \
2004 ecb_unused int ocur_ = (cur); \ 2126 ecb_unused int ocur_ = (cur); \
2005 (base) = (type *)array_realloc \ 2127 (base) = (type *)array_realloc \
2006 (sizeof (type), (base), &(cur), (cnt)); \ 2128 (sizeof (type), (base), &(cur), (cnt)); \
2007 init ((base), ocur_, ((cur) - ocur_)); \ 2129 init ((base), ocur_, ((cur) - ocur_)); \
2021 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; stem ## s idx = 0 2143 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; stem ## s idx = 0
2022 2144
2023/*****************************************************************************/ 2145/*****************************************************************************/
2024 2146
2025/* dummy callback for pending events */ 2147/* dummy callback for pending events */
2026noinline 2148ecb_noinline
2027static void 2149static void
2028pendingcb (EV_P_ ev_prepare *w, int revents) 2150pendingcb (EV_P_ ev_prepare *w, int revents)
2029{ 2151{
2030} 2152}
2031 2153
2032noinline 2154ecb_noinline
2033void 2155void
2034ev_feed_event (EV_P_ void *w, int revents) EV_NOEXCEPT 2156ev_feed_event (EV_P_ void *w, int revents) EV_NOEXCEPT
2035{ 2157{
2036 W w_ = (W)w; 2158 W w_ = (W)w;
2037 int pri = ABSPRI (w_); 2159 int pri = ABSPRI (w_);
2038 2160
2039 if (expect_false (w_->pending)) 2161 if (ecb_expect_false (w_->pending))
2040 pendings [pri][w_->pending - 1].events |= revents; 2162 pendings [pri][w_->pending - 1].events |= revents;
2041 else 2163 else
2042 { 2164 {
2043 w_->pending = ++pendingcnt [pri]; 2165 w_->pending = ++pendingcnt [pri];
2044 array_needsize (ANPENDING, pendings [pri], pendingmax [pri], w_->pending, array_needsize_noinit); 2166 array_needsize (ANPENDING, pendings [pri], pendingmax [pri], w_->pending, array_needsize_noinit);
2095inline_speed void 2217inline_speed void
2096fd_event (EV_P_ int fd, int revents) 2218fd_event (EV_P_ int fd, int revents)
2097{ 2219{
2098 ANFD *anfd = anfds + fd; 2220 ANFD *anfd = anfds + fd;
2099 2221
2100 if (expect_true (!anfd->reify)) 2222 if (ecb_expect_true (!anfd->reify))
2101 fd_event_nocheck (EV_A_ fd, revents); 2223 fd_event_nocheck (EV_A_ fd, revents);
2102} 2224}
2103 2225
2104void 2226void
2105ev_feed_fd_event (EV_P_ int fd, int revents) EV_NOEXCEPT 2227ev_feed_fd_event (EV_P_ int fd, int revents) EV_NOEXCEPT
2149 unsigned char o_events = anfd->events; 2271 unsigned char o_events = anfd->events;
2150 unsigned char o_reify = anfd->reify; 2272 unsigned char o_reify = anfd->reify;
2151 2273
2152 anfd->reify = 0; 2274 anfd->reify = 0;
2153 2275
2154 /*if (expect_true (o_reify & EV_ANFD_REIFY)) probably a deoptimisation */ 2276 /*if (ecb_expect_true (o_reify & EV_ANFD_REIFY)) probably a deoptimisation */
2155 { 2277 {
2156 anfd->events = 0; 2278 anfd->events = 0;
2157 2279
2158 for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next) 2280 for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next)
2159 anfd->events |= (unsigned char)w->events; 2281 anfd->events |= (unsigned char)w->events;
2175fd_change (EV_P_ int fd, int flags) 2297fd_change (EV_P_ int fd, int flags)
2176{ 2298{
2177 unsigned char reify = anfds [fd].reify; 2299 unsigned char reify = anfds [fd].reify;
2178 anfds [fd].reify |= flags; 2300 anfds [fd].reify |= flags;
2179 2301
2180 if (expect_true (!reify)) 2302 if (ecb_expect_true (!reify))
2181 { 2303 {
2182 ++fdchangecnt; 2304 ++fdchangecnt;
2183 array_needsize (int, fdchanges, fdchangemax, fdchangecnt, array_needsize_noinit); 2305 array_needsize (int, fdchanges, fdchangemax, fdchangecnt, array_needsize_noinit);
2184 fdchanges [fdchangecnt - 1] = fd; 2306 fdchanges [fdchangecnt - 1] = fd;
2185 } 2307 }
2208 return fcntl (fd, F_GETFD) != -1; 2330 return fcntl (fd, F_GETFD) != -1;
2209#endif 2331#endif
2210} 2332}
2211 2333
2212/* called on EBADF to verify fds */ 2334/* called on EBADF to verify fds */
2213noinline ecb_cold 2335ecb_noinline ecb_cold
2214static void 2336static void
2215fd_ebadf (EV_P) 2337fd_ebadf (EV_P)
2216{ 2338{
2217 int fd; 2339 int fd;
2218 2340
2221 if (!fd_valid (fd) && errno == EBADF) 2343 if (!fd_valid (fd) && errno == EBADF)
2222 fd_kill (EV_A_ fd); 2344 fd_kill (EV_A_ fd);
2223} 2345}
2224 2346
2225/* called on ENOMEM in select/poll to kill some fds and retry */ 2347/* called on ENOMEM in select/poll to kill some fds and retry */
2226noinline ecb_cold 2348ecb_noinline ecb_cold
2227static void 2349static void
2228fd_enomem (EV_P) 2350fd_enomem (EV_P)
2229{ 2351{
2230 int fd; 2352 int fd;
2231 2353
2236 break; 2358 break;
2237 } 2359 }
2238} 2360}
2239 2361
2240/* usually called after fork if backend needs to re-arm all fds from scratch */ 2362/* usually called after fork if backend needs to re-arm all fds from scratch */
2241noinline 2363ecb_noinline
2242static void 2364static void
2243fd_rearm_all (EV_P) 2365fd_rearm_all (EV_P)
2244{ 2366{
2245 int fd; 2367 int fd;
2246 2368
2300 ev_tstamp minat; 2422 ev_tstamp minat;
2301 ANHE *minpos; 2423 ANHE *minpos;
2302 ANHE *pos = heap + DHEAP * (k - HEAP0) + HEAP0 + 1; 2424 ANHE *pos = heap + DHEAP * (k - HEAP0) + HEAP0 + 1;
2303 2425
2304 /* find minimum child */ 2426 /* find minimum child */
2305 if (expect_true (pos + DHEAP - 1 < E)) 2427 if (ecb_expect_true (pos + DHEAP - 1 < E))
2306 { 2428 {
2307 /* fast path */ (minpos = pos + 0), (minat = ANHE_at (*minpos)); 2429 /* fast path */ (minpos = pos + 0), (minat = ANHE_at (*minpos));
2308 if ( ANHE_at (pos [1]) < minat) (minpos = pos + 1), (minat = ANHE_at (*minpos)); 2430 if ( minat > ANHE_at (pos [1])) (minpos = pos + 1), (minat = ANHE_at (*minpos));
2309 if ( ANHE_at (pos [2]) < minat) (minpos = pos + 2), (minat = ANHE_at (*minpos)); 2431 if ( minat > ANHE_at (pos [2])) (minpos = pos + 2), (minat = ANHE_at (*minpos));
2310 if ( ANHE_at (pos [3]) < minat) (minpos = pos + 3), (minat = ANHE_at (*minpos)); 2432 if ( minat > ANHE_at (pos [3])) (minpos = pos + 3), (minat = ANHE_at (*minpos));
2311 } 2433 }
2312 else if (pos < E) 2434 else if (pos < E)
2313 { 2435 {
2314 /* slow path */ (minpos = pos + 0), (minat = ANHE_at (*minpos)); 2436 /* slow path */ (minpos = pos + 0), (minat = ANHE_at (*minpos));
2315 if (pos + 1 < E && ANHE_at (pos [1]) < minat) (minpos = pos + 1), (minat = ANHE_at (*minpos)); 2437 if (pos + 1 < E && minat > ANHE_at (pos [1])) (minpos = pos + 1), (minat = ANHE_at (*minpos));
2316 if (pos + 2 < E && ANHE_at (pos [2]) < minat) (minpos = pos + 2), (minat = ANHE_at (*minpos)); 2438 if (pos + 2 < E && minat > ANHE_at (pos [2])) (minpos = pos + 2), (minat = ANHE_at (*minpos));
2317 if (pos + 3 < E && ANHE_at (pos [3]) < minat) (minpos = pos + 3), (minat = ANHE_at (*minpos)); 2439 if (pos + 3 < E && minat > ANHE_at (pos [3])) (minpos = pos + 3), (minat = ANHE_at (*minpos));
2318 } 2440 }
2319 else 2441 else
2320 break; 2442 break;
2321 2443
2322 if (ANHE_at (he) <= minat) 2444 if (ANHE_at (he) <= minat)
2330 2452
2331 heap [k] = he; 2453 heap [k] = he;
2332 ev_active (ANHE_w (he)) = k; 2454 ev_active (ANHE_w (he)) = k;
2333} 2455}
2334 2456
2335#else /* 4HEAP */ 2457#else /* not 4HEAP */
2336 2458
2337#define HEAP0 1 2459#define HEAP0 1
2338#define HPARENT(k) ((k) >> 1) 2460#define HPARENT(k) ((k) >> 1)
2339#define UPHEAP_DONE(p,k) (!(p)) 2461#define UPHEAP_DONE(p,k) (!(p))
2340 2462
2428 2550
2429/*****************************************************************************/ 2551/*****************************************************************************/
2430 2552
2431#if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE 2553#if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE
2432 2554
2433noinline ecb_cold 2555ecb_noinline ecb_cold
2434static void 2556static void
2435evpipe_init (EV_P) 2557evpipe_init (EV_P)
2436{ 2558{
2437 if (!ev_is_active (&pipe_w)) 2559 if (!ev_is_active (&pipe_w))
2438 { 2560 {
2479inline_speed void 2601inline_speed void
2480evpipe_write (EV_P_ EV_ATOMIC_T *flag) 2602evpipe_write (EV_P_ EV_ATOMIC_T *flag)
2481{ 2603{
2482 ECB_MEMORY_FENCE; /* push out the write before this function was called, acquire flag */ 2604 ECB_MEMORY_FENCE; /* push out the write before this function was called, acquire flag */
2483 2605
2484 if (expect_true (*flag)) 2606 if (ecb_expect_true (*flag))
2485 return; 2607 return;
2486 2608
2487 *flag = 1; 2609 *flag = 1;
2488 ECB_MEMORY_FENCE_RELEASE; /* make sure flag is visible before the wakeup */ 2610 ECB_MEMORY_FENCE_RELEASE; /* make sure flag is visible before the wakeup */
2489 2611
2566 sig_pending = 0; 2688 sig_pending = 0;
2567 2689
2568 ECB_MEMORY_FENCE; 2690 ECB_MEMORY_FENCE;
2569 2691
2570 for (i = EV_NSIG - 1; i--; ) 2692 for (i = EV_NSIG - 1; i--; )
2571 if (expect_false (signals [i].pending)) 2693 if (ecb_expect_false (signals [i].pending))
2572 ev_feed_signal_event (EV_A_ i + 1); 2694 ev_feed_signal_event (EV_A_ i + 1);
2573 } 2695 }
2574#endif 2696#endif
2575 2697
2576#if EV_ASYNC_ENABLE 2698#if EV_ASYNC_ENABLE
2617#endif 2739#endif
2618 2740
2619 ev_feed_signal (signum); 2741 ev_feed_signal (signum);
2620} 2742}
2621 2743
2622noinline 2744ecb_noinline
2623void 2745void
2624ev_feed_signal_event (EV_P_ int signum) EV_NOEXCEPT 2746ev_feed_signal_event (EV_P_ int signum) EV_NOEXCEPT
2625{ 2747{
2626 WL w; 2748 WL w;
2627 2749
2628 if (expect_false (signum <= 0 || signum >= EV_NSIG)) 2750 if (ecb_expect_false (signum <= 0 || signum >= EV_NSIG))
2629 return; 2751 return;
2630 2752
2631 --signum; 2753 --signum;
2632 2754
2633#if EV_MULTIPLICITY 2755#if EV_MULTIPLICITY
2634 /* it is permissible to try to feed a signal to the wrong loop */ 2756 /* it is permissible to try to feed a signal to the wrong loop */
2635 /* or, likely more useful, feeding a signal nobody is waiting for */ 2757 /* or, likely more useful, feeding a signal nobody is waiting for */
2636 2758
2637 if (expect_false (signals [signum].loop != EV_A)) 2759 if (ecb_expect_false (signals [signum].loop != EV_A))
2638 return; 2760 return;
2639#endif 2761#endif
2640 2762
2641 signals [signum].pending = 0; 2763 signals [signum].pending = 0;
2642 ECB_MEMORY_FENCE_RELEASE; 2764 ECB_MEMORY_FENCE_RELEASE;
2741# include "ev_epoll.c" 2863# include "ev_epoll.c"
2742#endif 2864#endif
2743#if EV_USE_LINUXAIO 2865#if EV_USE_LINUXAIO
2744# include "ev_linuxaio.c" 2866# include "ev_linuxaio.c"
2745#endif 2867#endif
2868#if EV_USE_IOURING
2869# include "ev_iouring.c"
2870#endif
2746#if EV_USE_POLL 2871#if EV_USE_POLL
2747# include "ev_poll.c" 2872# include "ev_poll.c"
2748#endif 2873#endif
2749#if EV_USE_SELECT 2874#if EV_USE_SELECT
2750# include "ev_select.c" 2875# include "ev_select.c"
2782 2907
2783 if (EV_USE_PORT ) flags |= EVBACKEND_PORT; 2908 if (EV_USE_PORT ) flags |= EVBACKEND_PORT;
2784 if (EV_USE_KQUEUE ) flags |= EVBACKEND_KQUEUE; 2909 if (EV_USE_KQUEUE ) flags |= EVBACKEND_KQUEUE;
2785 if (EV_USE_EPOLL ) flags |= EVBACKEND_EPOLL; 2910 if (EV_USE_EPOLL ) flags |= EVBACKEND_EPOLL;
2786 if (EV_USE_LINUXAIO) flags |= EVBACKEND_LINUXAIO; 2911 if (EV_USE_LINUXAIO) flags |= EVBACKEND_LINUXAIO;
2912 if (EV_USE_IOURING ) flags |= EVBACKEND_IOURING;
2787 if (EV_USE_POLL ) flags |= EVBACKEND_POLL; 2913 if (EV_USE_POLL ) flags |= EVBACKEND_POLL;
2788 if (EV_USE_SELECT ) flags |= EVBACKEND_SELECT; 2914 if (EV_USE_SELECT ) flags |= EVBACKEND_SELECT;
2789 2915
2790 return flags; 2916 return flags;
2791} 2917}
2812 2938
2813 /* TODO: linuxaio is very experimental */ 2939 /* TODO: linuxaio is very experimental */
2814#if !EV_RECOMMEND_LINUXAIO 2940#if !EV_RECOMMEND_LINUXAIO
2815 flags &= ~EVBACKEND_LINUXAIO; 2941 flags &= ~EVBACKEND_LINUXAIO;
2816#endif 2942#endif
2943 /* TODO: linuxaio is super experimental */
2944#if !EV_RECOMMEND_IOURING
2945 flags &= ~EVBACKEND_IOURING;
2946#endif
2817 2947
2818 return flags; 2948 return flags;
2819} 2949}
2820 2950
2821ecb_cold 2951ecb_cold
2826 2956
2827 /* epoll embeddability broken on all linux versions up to at least 2.6.23 */ 2957 /* epoll embeddability broken on all linux versions up to at least 2.6.23 */
2828 if (ev_linux_version () < 0x020620) /* disable it on linux < 2.6.32 */ 2958 if (ev_linux_version () < 0x020620) /* disable it on linux < 2.6.32 */
2829 flags &= ~EVBACKEND_EPOLL; 2959 flags &= ~EVBACKEND_EPOLL;
2830 2960
2961 /* EVBACKEND_LINUXAIO is theoretically embeddable, but suffers from a performance overhead */
2962
2963 /* EVBACKEND_IOURING is practically embeddable, but the current implementation is not
2964 * because our backend_fd is the epoll fd we need as fallback.
2965 * if the kernel ever is fixed, this might change...
2966 */
2967
2831 return flags; 2968 return flags;
2832} 2969}
2833 2970
2834unsigned int 2971unsigned int
2835ev_backend (EV_P) EV_NOEXCEPT 2972ev_backend (EV_P) EV_NOEXCEPT
2887 acquire_cb = acquire; 3024 acquire_cb = acquire;
2888} 3025}
2889#endif 3026#endif
2890 3027
2891/* initialise a loop structure, must be zero-initialised */ 3028/* initialise a loop structure, must be zero-initialised */
2892noinline ecb_cold 3029ecb_noinline ecb_cold
2893static void 3030static void
2894loop_init (EV_P_ unsigned int flags) EV_NOEXCEPT 3031loop_init (EV_P_ unsigned int flags) EV_NOEXCEPT
2895{ 3032{
2896 if (!backend) 3033 if (!backend)
2897 { 3034 {
2965 if (!backend && (flags & EVBACKEND_PORT )) backend = port_init (EV_A_ flags); 3102 if (!backend && (flags & EVBACKEND_PORT )) backend = port_init (EV_A_ flags);
2966#endif 3103#endif
2967#if EV_USE_KQUEUE 3104#if EV_USE_KQUEUE
2968 if (!backend && (flags & EVBACKEND_KQUEUE )) backend = kqueue_init (EV_A_ flags); 3105 if (!backend && (flags & EVBACKEND_KQUEUE )) backend = kqueue_init (EV_A_ flags);
2969#endif 3106#endif
3107#if EV_USE_IOURING
3108 if (!backend && (flags & EVBACKEND_IOURING )) backend = iouring_init (EV_A_ flags);
3109#endif
2970#if EV_USE_LINUXAIO 3110#if EV_USE_LINUXAIO
2971 if (!backend && (flags & EVBACKEND_LINUXAIO)) backend = linuxaio_init (EV_A_ flags); 3111 if (!backend && (flags & EVBACKEND_LINUXAIO)) backend = linuxaio_init (EV_A_ flags);
2972#endif 3112#endif
2973#if EV_USE_EPOLL 3113#if EV_USE_EPOLL
2974 if (!backend && (flags & EVBACKEND_EPOLL )) backend = epoll_init (EV_A_ flags); 3114 if (!backend && (flags & EVBACKEND_EPOLL )) backend = epoll_init (EV_A_ flags);
3002 return; 3142 return;
3003#endif 3143#endif
3004 3144
3005#if EV_CLEANUP_ENABLE 3145#if EV_CLEANUP_ENABLE
3006 /* queue cleanup watchers (and execute them) */ 3146 /* queue cleanup watchers (and execute them) */
3007 if (expect_false (cleanupcnt)) 3147 if (ecb_expect_false (cleanupcnt))
3008 { 3148 {
3009 queue_events (EV_A_ (W *)cleanups, cleanupcnt, EV_CLEANUP); 3149 queue_events (EV_A_ (W *)cleanups, cleanupcnt, EV_CLEANUP);
3010 EV_INVOKE_PENDING; 3150 EV_INVOKE_PENDING;
3011 } 3151 }
3012#endif 3152#endif
3047#if EV_USE_PORT 3187#if EV_USE_PORT
3048 if (backend == EVBACKEND_PORT ) port_destroy (EV_A); 3188 if (backend == EVBACKEND_PORT ) port_destroy (EV_A);
3049#endif 3189#endif
3050#if EV_USE_KQUEUE 3190#if EV_USE_KQUEUE
3051 if (backend == EVBACKEND_KQUEUE ) kqueue_destroy (EV_A); 3191 if (backend == EVBACKEND_KQUEUE ) kqueue_destroy (EV_A);
3192#endif
3193#if EV_USE_IOURING
3194 if (backend == EVBACKEND_IOURING ) iouring_destroy (EV_A);
3052#endif 3195#endif
3053#if EV_USE_LINUXAIO 3196#if EV_USE_LINUXAIO
3054 if (backend == EVBACKEND_LINUXAIO) linuxaio_destroy (EV_A); 3197 if (backend == EVBACKEND_LINUXAIO) linuxaio_destroy (EV_A);
3055#endif 3198#endif
3056#if EV_USE_EPOLL 3199#if EV_USE_EPOLL
3115 if (backend == EVBACKEND_PORT ) port_fork (EV_A); 3258 if (backend == EVBACKEND_PORT ) port_fork (EV_A);
3116#endif 3259#endif
3117#if EV_USE_KQUEUE 3260#if EV_USE_KQUEUE
3118 if (backend == EVBACKEND_KQUEUE ) kqueue_fork (EV_A); 3261 if (backend == EVBACKEND_KQUEUE ) kqueue_fork (EV_A);
3119#endif 3262#endif
3263#if EV_USE_IOURING
3264 if (backend == EVBACKEND_IOURING ) iouring_fork (EV_A);
3265#endif
3120#if EV_USE_LINUXAIO 3266#if EV_USE_LINUXAIO
3121 if (backend == EVBACKEND_LINUXAIO) linuxaio_fork (EV_A); 3267 if (backend == EVBACKEND_LINUXAIO) linuxaio_fork (EV_A);
3122#endif 3268#endif
3123#if EV_USE_EPOLL 3269#if EV_USE_EPOLL
3124 if (backend == EVBACKEND_EPOLL ) epoll_fork (EV_A); 3270 if (backend == EVBACKEND_EPOLL ) epoll_fork (EV_A);
3166} 3312}
3167 3313
3168#endif /* multiplicity */ 3314#endif /* multiplicity */
3169 3315
3170#if EV_VERIFY 3316#if EV_VERIFY
3171noinline ecb_cold 3317ecb_noinline ecb_cold
3172static void 3318static void
3173verify_watcher (EV_P_ W w) 3319verify_watcher (EV_P_ W w)
3174{ 3320{
3175 assert (("libev: watcher has invalid priority", ABSPRI (w) >= 0 && ABSPRI (w) < NUMPRI)); 3321 assert (("libev: watcher has invalid priority", ABSPRI (w) >= 0 && ABSPRI (w) < NUMPRI));
3176 3322
3177 if (w->pending) 3323 if (w->pending)
3178 assert (("libev: pending watcher not on pending queue", pendings [ABSPRI (w)][w->pending - 1].w == w)); 3324 assert (("libev: pending watcher not on pending queue", pendings [ABSPRI (w)][w->pending - 1].w == w));
3179} 3325}
3180 3326
3181noinline ecb_cold 3327ecb_noinline ecb_cold
3182static void 3328static void
3183verify_heap (EV_P_ ANHE *heap, int N) 3329verify_heap (EV_P_ ANHE *heap, int N)
3184{ 3330{
3185 int i; 3331 int i;
3186 3332
3192 3338
3193 verify_watcher (EV_A_ (W)ANHE_w (heap [i])); 3339 verify_watcher (EV_A_ (W)ANHE_w (heap [i]));
3194 } 3340 }
3195} 3341}
3196 3342
3197noinline ecb_cold 3343ecb_noinline ecb_cold
3198static void 3344static void
3199array_verify (EV_P_ W *ws, int cnt) 3345array_verify (EV_P_ W *ws, int cnt)
3200{ 3346{
3201 while (cnt--) 3347 while (cnt--)
3202 { 3348 {
3351 count += pendingcnt [pri]; 3497 count += pendingcnt [pri];
3352 3498
3353 return count; 3499 return count;
3354} 3500}
3355 3501
3356noinline 3502ecb_noinline
3357void 3503void
3358ev_invoke_pending (EV_P) 3504ev_invoke_pending (EV_P)
3359{ 3505{
3360 pendingpri = NUMPRI; 3506 pendingpri = NUMPRI;
3361 3507
3380/* make idle watchers pending. this handles the "call-idle */ 3526/* make idle watchers pending. this handles the "call-idle */
3381/* only when higher priorities are idle" logic */ 3527/* only when higher priorities are idle" logic */
3382inline_size void 3528inline_size void
3383idle_reify (EV_P) 3529idle_reify (EV_P)
3384{ 3530{
3385 if (expect_false (idleall)) 3531 if (ecb_expect_false (idleall))
3386 { 3532 {
3387 int pri; 3533 int pri;
3388 3534
3389 for (pri = NUMPRI; pri--; ) 3535 for (pri = NUMPRI; pri--; )
3390 { 3536 {
3420 { 3566 {
3421 ev_at (w) += w->repeat; 3567 ev_at (w) += w->repeat;
3422 if (ev_at (w) < mn_now) 3568 if (ev_at (w) < mn_now)
3423 ev_at (w) = mn_now; 3569 ev_at (w) = mn_now;
3424 3570
3425 assert (("libev: negative ev_timer repeat value found while processing timers", w->repeat > 0.)); 3571 assert (("libev: negative ev_timer repeat value found while processing timers", w->repeat > EV_TS_CONST (0.)));
3426 3572
3427 ANHE_at_cache (timers [HEAP0]); 3573 ANHE_at_cache (timers [HEAP0]);
3428 downheap (timers, timercnt, HEAP0); 3574 downheap (timers, timercnt, HEAP0);
3429 } 3575 }
3430 else 3576 else
3439 } 3585 }
3440} 3586}
3441 3587
3442#if EV_PERIODIC_ENABLE 3588#if EV_PERIODIC_ENABLE
3443 3589
3444noinline 3590ecb_noinline
3445static void 3591static void
3446periodic_recalc (EV_P_ ev_periodic *w) 3592periodic_recalc (EV_P_ ev_periodic *w)
3447{ 3593{
3448 ev_tstamp interval = w->interval > MIN_INTERVAL ? w->interval : MIN_INTERVAL; 3594 ev_tstamp interval = w->interval > MIN_INTERVAL ? w->interval : MIN_INTERVAL;
3449 ev_tstamp at = w->offset + interval * ev_floor ((ev_rt_now - w->offset) / interval); 3595 ev_tstamp at = w->offset + interval * ev_floor ((ev_rt_now - w->offset) / interval);
3452 while (at <= ev_rt_now) 3598 while (at <= ev_rt_now)
3453 { 3599 {
3454 ev_tstamp nat = at + w->interval; 3600 ev_tstamp nat = at + w->interval;
3455 3601
3456 /* when resolution fails us, we use ev_rt_now */ 3602 /* when resolution fails us, we use ev_rt_now */
3457 if (expect_false (nat == at)) 3603 if (ecb_expect_false (nat == at))
3458 { 3604 {
3459 at = ev_rt_now; 3605 at = ev_rt_now;
3460 break; 3606 break;
3461 } 3607 }
3462 3608
3508 } 3654 }
3509} 3655}
3510 3656
3511/* simply recalculate all periodics */ 3657/* simply recalculate all periodics */
3512/* TODO: maybe ensure that at least one event happens when jumping forward? */ 3658/* TODO: maybe ensure that at least one event happens when jumping forward? */
3513noinline ecb_cold 3659ecb_noinline ecb_cold
3514static void 3660static void
3515periodics_reschedule (EV_P) 3661periodics_reschedule (EV_P)
3516{ 3662{
3517 int i; 3663 int i;
3518 3664
3532 reheap (periodics, periodiccnt); 3678 reheap (periodics, periodiccnt);
3533} 3679}
3534#endif 3680#endif
3535 3681
3536/* adjust all timers by a given offset */ 3682/* adjust all timers by a given offset */
3537noinline ecb_cold 3683ecb_noinline ecb_cold
3538static void 3684static void
3539timers_reschedule (EV_P_ ev_tstamp adjust) 3685timers_reschedule (EV_P_ ev_tstamp adjust)
3540{ 3686{
3541 int i; 3687 int i;
3542 3688
3552/* also detect if there was a timejump, and act accordingly */ 3698/* also detect if there was a timejump, and act accordingly */
3553inline_speed void 3699inline_speed void
3554time_update (EV_P_ ev_tstamp max_block) 3700time_update (EV_P_ ev_tstamp max_block)
3555{ 3701{
3556#if EV_USE_MONOTONIC 3702#if EV_USE_MONOTONIC
3557 if (expect_true (have_monotonic)) 3703 if (ecb_expect_true (have_monotonic))
3558 { 3704 {
3559 int i; 3705 int i;
3560 ev_tstamp odiff = rtmn_diff; 3706 ev_tstamp odiff = rtmn_diff;
3561 3707
3562 mn_now = get_clock (); 3708 mn_now = get_clock ();
3563 3709
3564 /* only fetch the realtime clock every 0.5*MIN_TIMEJUMP seconds */ 3710 /* only fetch the realtime clock every 0.5*MIN_TIMEJUMP seconds */
3565 /* interpolate in the meantime */ 3711 /* interpolate in the meantime */
3566 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) 3712 if (ecb_expect_true (mn_now - now_floor < EV_TS_CONST (MIN_TIMEJUMP * .5)))
3567 { 3713 {
3568 ev_rt_now = rtmn_diff + mn_now; 3714 ev_rt_now = rtmn_diff + mn_now;
3569 return; 3715 return;
3570 } 3716 }
3571 3717
3585 ev_tstamp diff; 3731 ev_tstamp diff;
3586 rtmn_diff = ev_rt_now - mn_now; 3732 rtmn_diff = ev_rt_now - mn_now;
3587 3733
3588 diff = odiff - rtmn_diff; 3734 diff = odiff - rtmn_diff;
3589 3735
3590 if (expect_true ((diff < 0. ? -diff : diff) < MIN_TIMEJUMP)) 3736 if (ecb_expect_true ((diff < EV_TS_CONST (0.) ? -diff : diff) < EV_TS_CONST (MIN_TIMEJUMP)))
3591 return; /* all is well */ 3737 return; /* all is well */
3592 3738
3593 ev_rt_now = ev_time (); 3739 ev_rt_now = ev_time ();
3594 mn_now = get_clock (); 3740 mn_now = get_clock ();
3595 now_floor = mn_now; 3741 now_floor = mn_now;
3604 else 3750 else
3605#endif 3751#endif
3606 { 3752 {
3607 ev_rt_now = ev_time (); 3753 ev_rt_now = ev_time ();
3608 3754
3609 if (expect_false (mn_now > ev_rt_now || ev_rt_now > mn_now + max_block + MIN_TIMEJUMP)) 3755 if (ecb_expect_false (mn_now > ev_rt_now || ev_rt_now > mn_now + max_block + EV_TS_CONST (MIN_TIMEJUMP)))
3610 { 3756 {
3611 /* adjust timers. this is easy, as the offset is the same for all of them */ 3757 /* adjust timers. this is easy, as the offset is the same for all of them */
3612 timers_reschedule (EV_A_ ev_rt_now - mn_now); 3758 timers_reschedule (EV_A_ ev_rt_now - mn_now);
3613#if EV_PERIODIC_ENABLE 3759#if EV_PERIODIC_ENABLE
3614 periodics_reschedule (EV_A); 3760 periodics_reschedule (EV_A);
3637#if EV_VERIFY >= 2 3783#if EV_VERIFY >= 2
3638 ev_verify (EV_A); 3784 ev_verify (EV_A);
3639#endif 3785#endif
3640 3786
3641#ifndef _WIN32 3787#ifndef _WIN32
3642 if (expect_false (curpid)) /* penalise the forking check even more */ 3788 if (ecb_expect_false (curpid)) /* penalise the forking check even more */
3643 if (expect_false (getpid () != curpid)) 3789 if (ecb_expect_false (getpid () != curpid))
3644 { 3790 {
3645 curpid = getpid (); 3791 curpid = getpid ();
3646 postfork = 1; 3792 postfork = 1;
3647 } 3793 }
3648#endif 3794#endif
3649 3795
3650#if EV_FORK_ENABLE 3796#if EV_FORK_ENABLE
3651 /* we might have forked, so queue fork handlers */ 3797 /* we might have forked, so queue fork handlers */
3652 if (expect_false (postfork)) 3798 if (ecb_expect_false (postfork))
3653 if (forkcnt) 3799 if (forkcnt)
3654 { 3800 {
3655 queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK); 3801 queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK);
3656 EV_INVOKE_PENDING; 3802 EV_INVOKE_PENDING;
3657 } 3803 }
3658#endif 3804#endif
3659 3805
3660#if EV_PREPARE_ENABLE 3806#if EV_PREPARE_ENABLE
3661 /* queue prepare watchers (and execute them) */ 3807 /* queue prepare watchers (and execute them) */
3662 if (expect_false (preparecnt)) 3808 if (ecb_expect_false (preparecnt))
3663 { 3809 {
3664 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); 3810 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
3665 EV_INVOKE_PENDING; 3811 EV_INVOKE_PENDING;
3666 } 3812 }
3667#endif 3813#endif
3668 3814
3669 if (expect_false (loop_done)) 3815 if (ecb_expect_false (loop_done))
3670 break; 3816 break;
3671 3817
3672 /* we might have forked, so reify kernel state if necessary */ 3818 /* we might have forked, so reify kernel state if necessary */
3673 if (expect_false (postfork)) 3819 if (ecb_expect_false (postfork))
3674 loop_fork (EV_A); 3820 loop_fork (EV_A);
3675 3821
3676 /* update fd-related kernel structures */ 3822 /* update fd-related kernel structures */
3677 fd_reify (EV_A); 3823 fd_reify (EV_A);
3678 3824
3683 3829
3684 /* remember old timestamp for io_blocktime calculation */ 3830 /* remember old timestamp for io_blocktime calculation */
3685 ev_tstamp prev_mn_now = mn_now; 3831 ev_tstamp prev_mn_now = mn_now;
3686 3832
3687 /* update time to cancel out callback processing overhead */ 3833 /* update time to cancel out callback processing overhead */
3688 time_update (EV_A_ 1e100); 3834 time_update (EV_A_ EV_TS_CONST (EV_TSTAMP_HUGE));
3689 3835
3690 /* from now on, we want a pipe-wake-up */ 3836 /* from now on, we want a pipe-wake-up */
3691 pipe_write_wanted = 1; 3837 pipe_write_wanted = 1;
3692 3838
3693 ECB_MEMORY_FENCE; /* make sure pipe_write_wanted is visible before we check for potential skips */ 3839 ECB_MEMORY_FENCE; /* make sure pipe_write_wanted is visible before we check for potential skips */
3694 3840
3695 if (expect_true (!(flags & EVRUN_NOWAIT || idleall || !activecnt || pipe_write_skipped))) 3841 if (ecb_expect_true (!(flags & EVRUN_NOWAIT || idleall || !activecnt || pipe_write_skipped)))
3696 { 3842 {
3697 waittime = MAX_BLOCKTIME; 3843 waittime = EV_TS_CONST (MAX_BLOCKTIME);
3698 3844
3699 if (timercnt) 3845 if (timercnt)
3700 { 3846 {
3701 ev_tstamp to = ANHE_at (timers [HEAP0]) - mn_now; 3847 ev_tstamp to = ANHE_at (timers [HEAP0]) - mn_now;
3702 if (waittime > to) waittime = to; 3848 if (waittime > to) waittime = to;
3709 if (waittime > to) waittime = to; 3855 if (waittime > to) waittime = to;
3710 } 3856 }
3711#endif 3857#endif
3712 3858
3713 /* don't let timeouts decrease the waittime below timeout_blocktime */ 3859 /* don't let timeouts decrease the waittime below timeout_blocktime */
3714 if (expect_false (waittime < timeout_blocktime)) 3860 if (ecb_expect_false (waittime < timeout_blocktime))
3715 waittime = timeout_blocktime; 3861 waittime = timeout_blocktime;
3716 3862
3717 /* at this point, we NEED to wait, so we have to ensure */ 3863 /* at this point, we NEED to wait, so we have to ensure */
3718 /* to pass a minimum nonzero value to the backend */ 3864 /* to pass a minimum nonzero value to the backend */
3719 if (expect_false (waittime < backend_mintime)) 3865 if (ecb_expect_false (waittime < backend_mintime))
3720 waittime = backend_mintime; 3866 waittime = backend_mintime;
3721 3867
3722 /* extra check because io_blocktime is commonly 0 */ 3868 /* extra check because io_blocktime is commonly 0 */
3723 if (expect_false (io_blocktime)) 3869 if (ecb_expect_false (io_blocktime))
3724 { 3870 {
3725 sleeptime = io_blocktime - (mn_now - prev_mn_now); 3871 sleeptime = io_blocktime - (mn_now - prev_mn_now);
3726 3872
3727 if (sleeptime > waittime - backend_mintime) 3873 if (sleeptime > waittime - backend_mintime)
3728 sleeptime = waittime - backend_mintime; 3874 sleeptime = waittime - backend_mintime;
3729 3875
3730 if (expect_true (sleeptime > 0.)) 3876 if (ecb_expect_true (sleeptime > EV_TS_CONST (0.)))
3731 { 3877 {
3732 ev_sleep (sleeptime); 3878 ev_sleep (sleeptime);
3733 waittime -= sleeptime; 3879 waittime -= sleeptime;
3734 } 3880 }
3735 } 3881 }
3749 { 3895 {
3750 assert (("libev: pipe_w not active, but pipe not written", ev_is_active (&pipe_w))); 3896 assert (("libev: pipe_w not active, but pipe not written", ev_is_active (&pipe_w)));
3751 ev_feed_event (EV_A_ &pipe_w, EV_CUSTOM); 3897 ev_feed_event (EV_A_ &pipe_w, EV_CUSTOM);
3752 } 3898 }
3753 3899
3754
3755 /* update ev_rt_now, do magic */ 3900 /* update ev_rt_now, do magic */
3756 time_update (EV_A_ waittime + sleeptime); 3901 time_update (EV_A_ waittime + sleeptime);
3757 } 3902 }
3758 3903
3759 /* queue pending timers and reschedule them */ 3904 /* queue pending timers and reschedule them */
3767 idle_reify (EV_A); 3912 idle_reify (EV_A);
3768#endif 3913#endif
3769 3914
3770#if EV_CHECK_ENABLE 3915#if EV_CHECK_ENABLE
3771 /* queue check watchers, to be executed first */ 3916 /* queue check watchers, to be executed first */
3772 if (expect_false (checkcnt)) 3917 if (ecb_expect_false (checkcnt))
3773 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); 3918 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
3774#endif 3919#endif
3775 3920
3776 EV_INVOKE_PENDING; 3921 EV_INVOKE_PENDING;
3777 } 3922 }
3778 while (expect_true ( 3923 while (ecb_expect_true (
3779 activecnt 3924 activecnt
3780 && !loop_done 3925 && !loop_done
3781 && !(flags & (EVRUN_ONCE | EVRUN_NOWAIT)) 3926 && !(flags & (EVRUN_ONCE | EVRUN_NOWAIT))
3782 )); 3927 ));
3783 3928
3810} 3955}
3811 3956
3812void 3957void
3813ev_now_update (EV_P) EV_NOEXCEPT 3958ev_now_update (EV_P) EV_NOEXCEPT
3814{ 3959{
3815 time_update (EV_A_ 1e100); 3960 time_update (EV_A_ EV_TSTAMP_HUGE);
3816} 3961}
3817 3962
3818void 3963void
3819ev_suspend (EV_P) EV_NOEXCEPT 3964ev_suspend (EV_P) EV_NOEXCEPT
3820{ 3965{
3847inline_size void 3992inline_size void
3848wlist_del (WL *head, WL elem) 3993wlist_del (WL *head, WL elem)
3849{ 3994{
3850 while (*head) 3995 while (*head)
3851 { 3996 {
3852 if (expect_true (*head == elem)) 3997 if (ecb_expect_true (*head == elem))
3853 { 3998 {
3854 *head = elem->next; 3999 *head = elem->next;
3855 break; 4000 break;
3856 } 4001 }
3857 4002
3874ev_clear_pending (EV_P_ void *w) EV_NOEXCEPT 4019ev_clear_pending (EV_P_ void *w) EV_NOEXCEPT
3875{ 4020{
3876 W w_ = (W)w; 4021 W w_ = (W)w;
3877 int pending = w_->pending; 4022 int pending = w_->pending;
3878 4023
3879 if (expect_true (pending)) 4024 if (ecb_expect_true (pending))
3880 { 4025 {
3881 ANPENDING *p = pendings [ABSPRI (w_)] + pending - 1; 4026 ANPENDING *p = pendings [ABSPRI (w_)] + pending - 1;
3882 p->w = (W)&pending_w; 4027 p->w = (W)&pending_w;
3883 w_->pending = 0; 4028 w_->pending = 0;
3884 return p->events; 4029 return p->events;
3911 w->active = 0; 4056 w->active = 0;
3912} 4057}
3913 4058
3914/*****************************************************************************/ 4059/*****************************************************************************/
3915 4060
3916noinline 4061ecb_noinline
3917void 4062void
3918ev_io_start (EV_P_ ev_io *w) EV_NOEXCEPT 4063ev_io_start (EV_P_ ev_io *w) EV_NOEXCEPT
3919{ 4064{
3920 int fd = w->fd; 4065 int fd = w->fd;
3921 4066
3922 if (expect_false (ev_is_active (w))) 4067 if (ecb_expect_false (ev_is_active (w)))
3923 return; 4068 return;
3924 4069
3925 assert (("libev: ev_io_start called with negative fd", fd >= 0)); 4070 assert (("libev: ev_io_start called with negative fd", fd >= 0));
3926 assert (("libev: ev_io_start called with illegal event mask", !(w->events & ~(EV__IOFDSET | EV_READ | EV_WRITE)))); 4071 assert (("libev: ev_io_start called with illegal event mask", !(w->events & ~(EV__IOFDSET | EV_READ | EV_WRITE))));
3927 4072
3941 w->events &= ~EV__IOFDSET; 4086 w->events &= ~EV__IOFDSET;
3942 4087
3943 EV_FREQUENT_CHECK; 4088 EV_FREQUENT_CHECK;
3944} 4089}
3945 4090
3946noinline 4091ecb_noinline
3947void 4092void
3948ev_io_stop (EV_P_ ev_io *w) EV_NOEXCEPT 4093ev_io_stop (EV_P_ ev_io *w) EV_NOEXCEPT
3949{ 4094{
3950 clear_pending (EV_A_ (W)w); 4095 clear_pending (EV_A_ (W)w);
3951 if (expect_false (!ev_is_active (w))) 4096 if (ecb_expect_false (!ev_is_active (w)))
3952 return; 4097 return;
3953 4098
3954 assert (("libev: ev_io_stop called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax)); 4099 assert (("libev: ev_io_stop called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax));
3955 4100
3956#if EV_VERIFY >= 2 4101#if EV_VERIFY >= 2
3964 fd_change (EV_A_ w->fd, EV_ANFD_REIFY); 4109 fd_change (EV_A_ w->fd, EV_ANFD_REIFY);
3965 4110
3966 EV_FREQUENT_CHECK; 4111 EV_FREQUENT_CHECK;
3967} 4112}
3968 4113
3969noinline 4114ecb_noinline
3970void 4115void
3971ev_timer_start (EV_P_ ev_timer *w) EV_NOEXCEPT 4116ev_timer_start (EV_P_ ev_timer *w) EV_NOEXCEPT
3972{ 4117{
3973 if (expect_false (ev_is_active (w))) 4118 if (ecb_expect_false (ev_is_active (w)))
3974 return; 4119 return;
3975 4120
3976 ev_at (w) += mn_now; 4121 ev_at (w) += mn_now;
3977 4122
3978 assert (("libev: ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); 4123 assert (("libev: ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
3989 EV_FREQUENT_CHECK; 4134 EV_FREQUENT_CHECK;
3990 4135
3991 /*assert (("libev: internal timer heap corruption", timers [ev_active (w)] == (WT)w));*/ 4136 /*assert (("libev: internal timer heap corruption", timers [ev_active (w)] == (WT)w));*/
3992} 4137}
3993 4138
3994noinline 4139ecb_noinline
3995void 4140void
3996ev_timer_stop (EV_P_ ev_timer *w) EV_NOEXCEPT 4141ev_timer_stop (EV_P_ ev_timer *w) EV_NOEXCEPT
3997{ 4142{
3998 clear_pending (EV_A_ (W)w); 4143 clear_pending (EV_A_ (W)w);
3999 if (expect_false (!ev_is_active (w))) 4144 if (ecb_expect_false (!ev_is_active (w)))
4000 return; 4145 return;
4001 4146
4002 EV_FREQUENT_CHECK; 4147 EV_FREQUENT_CHECK;
4003 4148
4004 { 4149 {
4006 4151
4007 assert (("libev: internal timer heap corruption", ANHE_w (timers [active]) == (WT)w)); 4152 assert (("libev: internal timer heap corruption", ANHE_w (timers [active]) == (WT)w));
4008 4153
4009 --timercnt; 4154 --timercnt;
4010 4155
4011 if (expect_true (active < timercnt + HEAP0)) 4156 if (ecb_expect_true (active < timercnt + HEAP0))
4012 { 4157 {
4013 timers [active] = timers [timercnt + HEAP0]; 4158 timers [active] = timers [timercnt + HEAP0];
4014 adjustheap (timers, timercnt, active); 4159 adjustheap (timers, timercnt, active);
4015 } 4160 }
4016 } 4161 }
4020 ev_stop (EV_A_ (W)w); 4165 ev_stop (EV_A_ (W)w);
4021 4166
4022 EV_FREQUENT_CHECK; 4167 EV_FREQUENT_CHECK;
4023} 4168}
4024 4169
4025noinline 4170ecb_noinline
4026void 4171void
4027ev_timer_again (EV_P_ ev_timer *w) EV_NOEXCEPT 4172ev_timer_again (EV_P_ ev_timer *w) EV_NOEXCEPT
4028{ 4173{
4029 EV_FREQUENT_CHECK; 4174 EV_FREQUENT_CHECK;
4030 4175
4051} 4196}
4052 4197
4053ev_tstamp 4198ev_tstamp
4054ev_timer_remaining (EV_P_ ev_timer *w) EV_NOEXCEPT 4199ev_timer_remaining (EV_P_ ev_timer *w) EV_NOEXCEPT
4055{ 4200{
4056 return ev_at (w) - (ev_is_active (w) ? mn_now : 0.); 4201 return ev_at (w) - (ev_is_active (w) ? mn_now : EV_TS_CONST (0.));
4057} 4202}
4058 4203
4059#if EV_PERIODIC_ENABLE 4204#if EV_PERIODIC_ENABLE
4060noinline 4205ecb_noinline
4061void 4206void
4062ev_periodic_start (EV_P_ ev_periodic *w) EV_NOEXCEPT 4207ev_periodic_start (EV_P_ ev_periodic *w) EV_NOEXCEPT
4063{ 4208{
4064 if (expect_false (ev_is_active (w))) 4209 if (ecb_expect_false (ev_is_active (w)))
4065 return; 4210 return;
4066 4211
4067 if (w->reschedule_cb) 4212 if (w->reschedule_cb)
4068 ev_at (w) = w->reschedule_cb (w, ev_rt_now); 4213 ev_at (w) = w->reschedule_cb (w, ev_rt_now);
4069 else if (w->interval) 4214 else if (w->interval)
4086 EV_FREQUENT_CHECK; 4231 EV_FREQUENT_CHECK;
4087 4232
4088 /*assert (("libev: internal periodic heap corruption", ANHE_w (periodics [ev_active (w)]) == (WT)w));*/ 4233 /*assert (("libev: internal periodic heap corruption", ANHE_w (periodics [ev_active (w)]) == (WT)w));*/
4089} 4234}
4090 4235
4091noinline 4236ecb_noinline
4092void 4237void
4093ev_periodic_stop (EV_P_ ev_periodic *w) EV_NOEXCEPT 4238ev_periodic_stop (EV_P_ ev_periodic *w) EV_NOEXCEPT
4094{ 4239{
4095 clear_pending (EV_A_ (W)w); 4240 clear_pending (EV_A_ (W)w);
4096 if (expect_false (!ev_is_active (w))) 4241 if (ecb_expect_false (!ev_is_active (w)))
4097 return; 4242 return;
4098 4243
4099 EV_FREQUENT_CHECK; 4244 EV_FREQUENT_CHECK;
4100 4245
4101 { 4246 {
4103 4248
4104 assert (("libev: internal periodic heap corruption", ANHE_w (periodics [active]) == (WT)w)); 4249 assert (("libev: internal periodic heap corruption", ANHE_w (periodics [active]) == (WT)w));
4105 4250
4106 --periodiccnt; 4251 --periodiccnt;
4107 4252
4108 if (expect_true (active < periodiccnt + HEAP0)) 4253 if (ecb_expect_true (active < periodiccnt + HEAP0))
4109 { 4254 {
4110 periodics [active] = periodics [periodiccnt + HEAP0]; 4255 periodics [active] = periodics [periodiccnt + HEAP0];
4111 adjustheap (periodics, periodiccnt, active); 4256 adjustheap (periodics, periodiccnt, active);
4112 } 4257 }
4113 } 4258 }
4115 ev_stop (EV_A_ (W)w); 4260 ev_stop (EV_A_ (W)w);
4116 4261
4117 EV_FREQUENT_CHECK; 4262 EV_FREQUENT_CHECK;
4118} 4263}
4119 4264
4120noinline 4265ecb_noinline
4121void 4266void
4122ev_periodic_again (EV_P_ ev_periodic *w) EV_NOEXCEPT 4267ev_periodic_again (EV_P_ ev_periodic *w) EV_NOEXCEPT
4123{ 4268{
4124 /* TODO: use adjustheap and recalculation */ 4269 /* TODO: use adjustheap and recalculation */
4125 ev_periodic_stop (EV_A_ w); 4270 ev_periodic_stop (EV_A_ w);
4131# define SA_RESTART 0 4276# define SA_RESTART 0
4132#endif 4277#endif
4133 4278
4134#if EV_SIGNAL_ENABLE 4279#if EV_SIGNAL_ENABLE
4135 4280
4136noinline 4281ecb_noinline
4137void 4282void
4138ev_signal_start (EV_P_ ev_signal *w) EV_NOEXCEPT 4283ev_signal_start (EV_P_ ev_signal *w) EV_NOEXCEPT
4139{ 4284{
4140 if (expect_false (ev_is_active (w))) 4285 if (ecb_expect_false (ev_is_active (w)))
4141 return; 4286 return;
4142 4287
4143 assert (("libev: ev_signal_start called with illegal signal number", w->signum > 0 && w->signum < EV_NSIG)); 4288 assert (("libev: ev_signal_start called with illegal signal number", w->signum > 0 && w->signum < EV_NSIG));
4144 4289
4145#if EV_MULTIPLICITY 4290#if EV_MULTIPLICITY
4214 } 4359 }
4215 4360
4216 EV_FREQUENT_CHECK; 4361 EV_FREQUENT_CHECK;
4217} 4362}
4218 4363
4219noinline 4364ecb_noinline
4220void 4365void
4221ev_signal_stop (EV_P_ ev_signal *w) EV_NOEXCEPT 4366ev_signal_stop (EV_P_ ev_signal *w) EV_NOEXCEPT
4222{ 4367{
4223 clear_pending (EV_A_ (W)w); 4368 clear_pending (EV_A_ (W)w);
4224 if (expect_false (!ev_is_active (w))) 4369 if (ecb_expect_false (!ev_is_active (w)))
4225 return; 4370 return;
4226 4371
4227 EV_FREQUENT_CHECK; 4372 EV_FREQUENT_CHECK;
4228 4373
4229 wlist_del (&signals [w->signum - 1].head, (WL)w); 4374 wlist_del (&signals [w->signum - 1].head, (WL)w);
4262ev_child_start (EV_P_ ev_child *w) EV_NOEXCEPT 4407ev_child_start (EV_P_ ev_child *w) EV_NOEXCEPT
4263{ 4408{
4264#if EV_MULTIPLICITY 4409#if EV_MULTIPLICITY
4265 assert (("libev: child watchers are only supported in the default loop", loop == ev_default_loop_ptr)); 4410 assert (("libev: child watchers are only supported in the default loop", loop == ev_default_loop_ptr));
4266#endif 4411#endif
4267 if (expect_false (ev_is_active (w))) 4412 if (ecb_expect_false (ev_is_active (w)))
4268 return; 4413 return;
4269 4414
4270 EV_FREQUENT_CHECK; 4415 EV_FREQUENT_CHECK;
4271 4416
4272 ev_start (EV_A_ (W)w, 1); 4417 ev_start (EV_A_ (W)w, 1);
4277 4422
4278void 4423void
4279ev_child_stop (EV_P_ ev_child *w) EV_NOEXCEPT 4424ev_child_stop (EV_P_ ev_child *w) EV_NOEXCEPT
4280{ 4425{
4281 clear_pending (EV_A_ (W)w); 4426 clear_pending (EV_A_ (W)w);
4282 if (expect_false (!ev_is_active (w))) 4427 if (ecb_expect_false (!ev_is_active (w)))
4283 return; 4428 return;
4284 4429
4285 EV_FREQUENT_CHECK; 4430 EV_FREQUENT_CHECK;
4286 4431
4287 wlist_del (&childs [w->pid & ((EV_PID_HASHSIZE) - 1)], (WL)w); 4432 wlist_del (&childs [w->pid & ((EV_PID_HASHSIZE) - 1)], (WL)w);
4301 4446
4302#define DEF_STAT_INTERVAL 5.0074891 4447#define DEF_STAT_INTERVAL 5.0074891
4303#define NFS_STAT_INTERVAL 30.1074891 /* for filesystems potentially failing inotify */ 4448#define NFS_STAT_INTERVAL 30.1074891 /* for filesystems potentially failing inotify */
4304#define MIN_STAT_INTERVAL 0.1074891 4449#define MIN_STAT_INTERVAL 0.1074891
4305 4450
4306noinline static void stat_timer_cb (EV_P_ ev_timer *w_, int revents); 4451ecb_noinline static void stat_timer_cb (EV_P_ ev_timer *w_, int revents);
4307 4452
4308#if EV_USE_INOTIFY 4453#if EV_USE_INOTIFY
4309 4454
4310/* the * 2 is to allow for alignment padding, which for some reason is >> 8 */ 4455/* the * 2 is to allow for alignment padding, which for some reason is >> 8 */
4311# define EV_INOTIFY_BUFSIZE (sizeof (struct inotify_event) * 2 + NAME_MAX) 4456# define EV_INOTIFY_BUFSIZE (sizeof (struct inotify_event) * 2 + NAME_MAX)
4312 4457
4313noinline 4458ecb_noinline
4314static void 4459static void
4315infy_add (EV_P_ ev_stat *w) 4460infy_add (EV_P_ ev_stat *w)
4316{ 4461{
4317 w->wd = inotify_add_watch (fs_fd, w->path, 4462 w->wd = inotify_add_watch (fs_fd, w->path,
4318 IN_ATTRIB | IN_DELETE_SELF | IN_MOVE_SELF | IN_MODIFY 4463 IN_ATTRIB | IN_DELETE_SELF | IN_MOVE_SELF | IN_MODIFY
4383 if (ev_is_active (&w->timer)) ev_ref (EV_A); 4528 if (ev_is_active (&w->timer)) ev_ref (EV_A);
4384 ev_timer_again (EV_A_ &w->timer); 4529 ev_timer_again (EV_A_ &w->timer);
4385 if (ev_is_active (&w->timer)) ev_unref (EV_A); 4530 if (ev_is_active (&w->timer)) ev_unref (EV_A);
4386} 4531}
4387 4532
4388noinline 4533ecb_noinline
4389static void 4534static void
4390infy_del (EV_P_ ev_stat *w) 4535infy_del (EV_P_ ev_stat *w)
4391{ 4536{
4392 int slot; 4537 int slot;
4393 int wd = w->wd; 4538 int wd = w->wd;
4401 4546
4402 /* remove this watcher, if others are watching it, they will rearm */ 4547 /* remove this watcher, if others are watching it, they will rearm */
4403 inotify_rm_watch (fs_fd, wd); 4548 inotify_rm_watch (fs_fd, wd);
4404} 4549}
4405 4550
4406noinline 4551ecb_noinline
4407static void 4552static void
4408infy_wd (EV_P_ int slot, int wd, struct inotify_event *ev) 4553infy_wd (EV_P_ int slot, int wd, struct inotify_event *ev)
4409{ 4554{
4410 if (slot < 0) 4555 if (slot < 0)
4411 /* overflow, need to check for all hash slots */ 4556 /* overflow, need to check for all hash slots */
4557 w->attr.st_nlink = 0; 4702 w->attr.st_nlink = 0;
4558 else if (!w->attr.st_nlink) 4703 else if (!w->attr.st_nlink)
4559 w->attr.st_nlink = 1; 4704 w->attr.st_nlink = 1;
4560} 4705}
4561 4706
4562noinline 4707ecb_noinline
4563static void 4708static void
4564stat_timer_cb (EV_P_ ev_timer *w_, int revents) 4709stat_timer_cb (EV_P_ ev_timer *w_, int revents)
4565{ 4710{
4566 ev_stat *w = (ev_stat *)(((char *)w_) - offsetof (ev_stat, timer)); 4711 ev_stat *w = (ev_stat *)(((char *)w_) - offsetof (ev_stat, timer));
4567 4712
4601} 4746}
4602 4747
4603void 4748void
4604ev_stat_start (EV_P_ ev_stat *w) EV_NOEXCEPT 4749ev_stat_start (EV_P_ ev_stat *w) EV_NOEXCEPT
4605{ 4750{
4606 if (expect_false (ev_is_active (w))) 4751 if (ecb_expect_false (ev_is_active (w)))
4607 return; 4752 return;
4608 4753
4609 ev_stat_stat (EV_A_ w); 4754 ev_stat_stat (EV_A_ w);
4610 4755
4611 if (w->interval < MIN_STAT_INTERVAL && w->interval) 4756 if (w->interval < MIN_STAT_INTERVAL && w->interval)
4633 4778
4634void 4779void
4635ev_stat_stop (EV_P_ ev_stat *w) EV_NOEXCEPT 4780ev_stat_stop (EV_P_ ev_stat *w) EV_NOEXCEPT
4636{ 4781{
4637 clear_pending (EV_A_ (W)w); 4782 clear_pending (EV_A_ (W)w);
4638 if (expect_false (!ev_is_active (w))) 4783 if (ecb_expect_false (!ev_is_active (w)))
4639 return; 4784 return;
4640 4785
4641 EV_FREQUENT_CHECK; 4786 EV_FREQUENT_CHECK;
4642 4787
4643#if EV_USE_INOTIFY 4788#if EV_USE_INOTIFY
4658 4803
4659#if EV_IDLE_ENABLE 4804#if EV_IDLE_ENABLE
4660void 4805void
4661ev_idle_start (EV_P_ ev_idle *w) EV_NOEXCEPT 4806ev_idle_start (EV_P_ ev_idle *w) EV_NOEXCEPT
4662{ 4807{
4663 if (expect_false (ev_is_active (w))) 4808 if (ecb_expect_false (ev_is_active (w)))
4664 return; 4809 return;
4665 4810
4666 pri_adjust (EV_A_ (W)w); 4811 pri_adjust (EV_A_ (W)w);
4667 4812
4668 EV_FREQUENT_CHECK; 4813 EV_FREQUENT_CHECK;
4682 4827
4683void 4828void
4684ev_idle_stop (EV_P_ ev_idle *w) EV_NOEXCEPT 4829ev_idle_stop (EV_P_ ev_idle *w) EV_NOEXCEPT
4685{ 4830{
4686 clear_pending (EV_A_ (W)w); 4831 clear_pending (EV_A_ (W)w);
4687 if (expect_false (!ev_is_active (w))) 4832 if (ecb_expect_false (!ev_is_active (w)))
4688 return; 4833 return;
4689 4834
4690 EV_FREQUENT_CHECK; 4835 EV_FREQUENT_CHECK;
4691 4836
4692 { 4837 {
4705 4850
4706#if EV_PREPARE_ENABLE 4851#if EV_PREPARE_ENABLE
4707void 4852void
4708ev_prepare_start (EV_P_ ev_prepare *w) EV_NOEXCEPT 4853ev_prepare_start (EV_P_ ev_prepare *w) EV_NOEXCEPT
4709{ 4854{
4710 if (expect_false (ev_is_active (w))) 4855 if (ecb_expect_false (ev_is_active (w)))
4711 return; 4856 return;
4712 4857
4713 EV_FREQUENT_CHECK; 4858 EV_FREQUENT_CHECK;
4714 4859
4715 ev_start (EV_A_ (W)w, ++preparecnt); 4860 ev_start (EV_A_ (W)w, ++preparecnt);
4721 4866
4722void 4867void
4723ev_prepare_stop (EV_P_ ev_prepare *w) EV_NOEXCEPT 4868ev_prepare_stop (EV_P_ ev_prepare *w) EV_NOEXCEPT
4724{ 4869{
4725 clear_pending (EV_A_ (W)w); 4870 clear_pending (EV_A_ (W)w);
4726 if (expect_false (!ev_is_active (w))) 4871 if (ecb_expect_false (!ev_is_active (w)))
4727 return; 4872 return;
4728 4873
4729 EV_FREQUENT_CHECK; 4874 EV_FREQUENT_CHECK;
4730 4875
4731 { 4876 {
4743 4888
4744#if EV_CHECK_ENABLE 4889#if EV_CHECK_ENABLE
4745void 4890void
4746ev_check_start (EV_P_ ev_check *w) EV_NOEXCEPT 4891ev_check_start (EV_P_ ev_check *w) EV_NOEXCEPT
4747{ 4892{
4748 if (expect_false (ev_is_active (w))) 4893 if (ecb_expect_false (ev_is_active (w)))
4749 return; 4894 return;
4750 4895
4751 EV_FREQUENT_CHECK; 4896 EV_FREQUENT_CHECK;
4752 4897
4753 ev_start (EV_A_ (W)w, ++checkcnt); 4898 ev_start (EV_A_ (W)w, ++checkcnt);
4759 4904
4760void 4905void
4761ev_check_stop (EV_P_ ev_check *w) EV_NOEXCEPT 4906ev_check_stop (EV_P_ ev_check *w) EV_NOEXCEPT
4762{ 4907{
4763 clear_pending (EV_A_ (W)w); 4908 clear_pending (EV_A_ (W)w);
4764 if (expect_false (!ev_is_active (w))) 4909 if (ecb_expect_false (!ev_is_active (w)))
4765 return; 4910 return;
4766 4911
4767 EV_FREQUENT_CHECK; 4912 EV_FREQUENT_CHECK;
4768 4913
4769 { 4914 {
4778 EV_FREQUENT_CHECK; 4923 EV_FREQUENT_CHECK;
4779} 4924}
4780#endif 4925#endif
4781 4926
4782#if EV_EMBED_ENABLE 4927#if EV_EMBED_ENABLE
4783noinline 4928ecb_noinline
4784void 4929void
4785ev_embed_sweep (EV_P_ ev_embed *w) EV_NOEXCEPT 4930ev_embed_sweep (EV_P_ ev_embed *w) EV_NOEXCEPT
4786{ 4931{
4787 ev_run (w->other, EVRUN_NOWAIT); 4932 ev_run (w->other, EVRUN_NOWAIT);
4788} 4933}
4840#endif 4985#endif
4841 4986
4842void 4987void
4843ev_embed_start (EV_P_ ev_embed *w) EV_NOEXCEPT 4988ev_embed_start (EV_P_ ev_embed *w) EV_NOEXCEPT
4844{ 4989{
4845 if (expect_false (ev_is_active (w))) 4990 if (ecb_expect_false (ev_is_active (w)))
4846 return; 4991 return;
4847 4992
4848 { 4993 {
4849 EV_P = w->other; 4994 EV_P = w->other;
4850 assert (("libev: loop to be embedded is not embeddable", backend & ev_embeddable_backends ())); 4995 assert (("libev: loop to be embedded is not embeddable", backend & ev_embeddable_backends ()));
4872 5017
4873void 5018void
4874ev_embed_stop (EV_P_ ev_embed *w) EV_NOEXCEPT 5019ev_embed_stop (EV_P_ ev_embed *w) EV_NOEXCEPT
4875{ 5020{
4876 clear_pending (EV_A_ (W)w); 5021 clear_pending (EV_A_ (W)w);
4877 if (expect_false (!ev_is_active (w))) 5022 if (ecb_expect_false (!ev_is_active (w)))
4878 return; 5023 return;
4879 5024
4880 EV_FREQUENT_CHECK; 5025 EV_FREQUENT_CHECK;
4881 5026
4882 ev_io_stop (EV_A_ &w->io); 5027 ev_io_stop (EV_A_ &w->io);
4891 5036
4892#if EV_FORK_ENABLE 5037#if EV_FORK_ENABLE
4893void 5038void
4894ev_fork_start (EV_P_ ev_fork *w) EV_NOEXCEPT 5039ev_fork_start (EV_P_ ev_fork *w) EV_NOEXCEPT
4895{ 5040{
4896 if (expect_false (ev_is_active (w))) 5041 if (ecb_expect_false (ev_is_active (w)))
4897 return; 5042 return;
4898 5043
4899 EV_FREQUENT_CHECK; 5044 EV_FREQUENT_CHECK;
4900 5045
4901 ev_start (EV_A_ (W)w, ++forkcnt); 5046 ev_start (EV_A_ (W)w, ++forkcnt);
4907 5052
4908void 5053void
4909ev_fork_stop (EV_P_ ev_fork *w) EV_NOEXCEPT 5054ev_fork_stop (EV_P_ ev_fork *w) EV_NOEXCEPT
4910{ 5055{
4911 clear_pending (EV_A_ (W)w); 5056 clear_pending (EV_A_ (W)w);
4912 if (expect_false (!ev_is_active (w))) 5057 if (ecb_expect_false (!ev_is_active (w)))
4913 return; 5058 return;
4914 5059
4915 EV_FREQUENT_CHECK; 5060 EV_FREQUENT_CHECK;
4916 5061
4917 { 5062 {
4929 5074
4930#if EV_CLEANUP_ENABLE 5075#if EV_CLEANUP_ENABLE
4931void 5076void
4932ev_cleanup_start (EV_P_ ev_cleanup *w) EV_NOEXCEPT 5077ev_cleanup_start (EV_P_ ev_cleanup *w) EV_NOEXCEPT
4933{ 5078{
4934 if (expect_false (ev_is_active (w))) 5079 if (ecb_expect_false (ev_is_active (w)))
4935 return; 5080 return;
4936 5081
4937 EV_FREQUENT_CHECK; 5082 EV_FREQUENT_CHECK;
4938 5083
4939 ev_start (EV_A_ (W)w, ++cleanupcnt); 5084 ev_start (EV_A_ (W)w, ++cleanupcnt);
4947 5092
4948void 5093void
4949ev_cleanup_stop (EV_P_ ev_cleanup *w) EV_NOEXCEPT 5094ev_cleanup_stop (EV_P_ ev_cleanup *w) EV_NOEXCEPT
4950{ 5095{
4951 clear_pending (EV_A_ (W)w); 5096 clear_pending (EV_A_ (W)w);
4952 if (expect_false (!ev_is_active (w))) 5097 if (ecb_expect_false (!ev_is_active (w)))
4953 return; 5098 return;
4954 5099
4955 EV_FREQUENT_CHECK; 5100 EV_FREQUENT_CHECK;
4956 ev_ref (EV_A); 5101 ev_ref (EV_A);
4957 5102
4970 5115
4971#if EV_ASYNC_ENABLE 5116#if EV_ASYNC_ENABLE
4972void 5117void
4973ev_async_start (EV_P_ ev_async *w) EV_NOEXCEPT 5118ev_async_start (EV_P_ ev_async *w) EV_NOEXCEPT
4974{ 5119{
4975 if (expect_false (ev_is_active (w))) 5120 if (ecb_expect_false (ev_is_active (w)))
4976 return; 5121 return;
4977 5122
4978 w->sent = 0; 5123 w->sent = 0;
4979 5124
4980 evpipe_init (EV_A); 5125 evpipe_init (EV_A);
4990 5135
4991void 5136void
4992ev_async_stop (EV_P_ ev_async *w) EV_NOEXCEPT 5137ev_async_stop (EV_P_ ev_async *w) EV_NOEXCEPT
4993{ 5138{
4994 clear_pending (EV_A_ (W)w); 5139 clear_pending (EV_A_ (W)w);
4995 if (expect_false (!ev_is_active (w))) 5140 if (ecb_expect_false (!ev_is_active (w)))
4996 return; 5141 return;
4997 5142
4998 EV_FREQUENT_CHECK; 5143 EV_FREQUENT_CHECK;
4999 5144
5000 { 5145 {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines