summaryrefslogtreecommitdiffstats
path: root/libev/ev.c
diff options
context:
space:
mode:
Diffstat (limited to 'libev/ev.c')
-rw-r--r--libev/ev.c872
1 files changed, 735 insertions, 137 deletions
diff --git a/libev/ev.c b/libev/ev.c
index 1a8b319..fe1b6ac 100644
--- a/libev/ev.c
+++ b/libev/ev.c
@@ -6,14 +6,14 @@
*
* Redistribution and use in source and binary forms, with or without modifica-
* tion, are permitted provided that the following conditions are met:
- *
+ *
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
- *
+ *
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
- *
+ *
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER-
* CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
@@ -45,6 +45,12 @@
# include "config.h"
# endif
+#if HAVE_FLOOR
+# ifndef EV_USE_FLOOR
+# define EV_USE_FLOOR 1
+# endif
+#endif
+
# if HAVE_CLOCK_SYSCALL
# ifndef EV_USE_CLOCK_SYSCALL
# define EV_USE_CLOCK_SYSCALL 1
@@ -158,7 +164,6 @@
#endif
-#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
@@ -180,7 +185,16 @@
# include "ev.h"
#endif
-EV_CPP(extern "C" {)
+#if EV_NO_THREADS
+# undef EV_NO_SMP
+# define EV_NO_SMP 1
+# undef ECB_NO_THREADS
+# define ECB_NO_THREADS 1
+#endif
+#if EV_NO_SMP
+# undef EV_NO_SMP
+# define ECB_NO_SMP 1
+#endif
#ifndef _WIN32
# include <sys/time.h>
@@ -234,6 +248,10 @@ EV_CPP(extern "C" {)
# define EV_NSIG 65
#endif
+#ifndef EV_USE_FLOOR
+# define EV_USE_FLOOR 0
+#endif
+
#ifndef EV_USE_CLOCK_SYSCALL
# if __linux && __GLIBC__ >= 2
# define EV_USE_CLOCK_SYSCALL EV_FEATURE_OS
@@ -378,7 +396,8 @@ EV_CPP(extern "C" {)
#endif
#if !EV_USE_NANOSLEEP
-# ifndef _WIN32
+/* hp-ux has it in sys/time.h, which we unconditionally include above */
+# if !defined(_WIN32) && !defined(__hpux)
# include <sys/select.h>
# endif
#endif
@@ -444,14 +463,11 @@ struct signalfd_siginfo
#endif
/*
- * This is used to avoid floating point rounding problems.
- * It is added to ev_rt_now when scheduling periodics
- * to ensure progress, time-wise, even when rounding
- * errors are against us.
+ * This is used to work around floating point rounding problems.
* This value is good at least till the year 4000.
- * Better solutions welcome.
*/
-#define TIME_EPSILON 0.0001220703125 /* 1/8192 */
+#define MIN_INTERVAL 0.0001220703125 /* 1/2**13, good till 4000 */
+/*#define MIN_INTERVAL 0.00000095367431640625 /* 1/2**20, good till 2200 */
#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
#define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */
@@ -459,23 +475,486 @@ struct signalfd_siginfo
#define EV_TV_SET(tv,t) do { tv.tv_sec = (long)t; tv.tv_usec = (long)((t - tv.tv_sec) * 1e6); } while (0)
#define EV_TS_SET(ts,t) do { ts.tv_sec = (long)t; ts.tv_nsec = (long)((t - ts.tv_sec) * 1e9); } while (0)
-#if __GNUC__ >= 4
-# define expect(expr,value) __builtin_expect ((expr),(value))
-# define noinline __attribute__ ((noinline))
+/* the following is ecb.h embedded into libev - use update_ev_c to update from an external copy */
+/* ECB.H BEGIN */
+/*
+ * libecb - http://software.schmorp.de/pkg/libecb
+ *
+ * Copyright (©) 2009-2012 Marc Alexander Lehmann <libecb@schmorp.de>
+ * Copyright (©) 2011 Emanuele Giaquinta
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without modifica-
+ * tion, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER-
+ * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
+ * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE-
+ * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+ * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH-
+ * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef ECB_H
+#define ECB_H
+
+#ifdef _WIN32
+ typedef signed char int8_t;
+ typedef unsigned char uint8_t;
+ typedef signed short int16_t;
+ typedef unsigned short uint16_t;
+ typedef signed int int32_t;
+ typedef unsigned int uint32_t;
+ #if __GNUC__
+ typedef signed long long int64_t;
+ typedef unsigned long long uint64_t;
+ #else /* _MSC_VER || __BORLANDC__ */
+ typedef signed __int64 int64_t;
+ typedef unsigned __int64 uint64_t;
+ #endif
#else
-# define expect(expr,value) (expr)
-# define noinline
-# if __STDC_VERSION__ < 199901L && __GNUC__ < 2
-# define inline
-# endif
+ #include <inttypes.h>
+#endif
+
+/* many compilers define _GNUC_ to some versions but then only implement
+ * what their idiot authors think are the "more important" extensions,
+ * causing enormous grief in return for some better fake benchmark numbers.
+ * or so.
+ * we try to detect these and simply assume they are not gcc - if they have
+ * an issue with that they should have done it right in the first place.
+ */
+#ifndef ECB_GCC_VERSION
+ #if !defined(__GNUC_MINOR__) || defined(__INTEL_COMPILER) || defined(__SUNPRO_C) || defined(__SUNPRO_CC) || defined(__llvm__) || defined(__clang__)
+ #define ECB_GCC_VERSION(major,minor) 0
+ #else
+ #define ECB_GCC_VERSION(major,minor) (__GNUC__ > (major) || (__GNUC__ == (major) && __GNUC_MINOR__ >= (minor)))
+ #endif
+#endif
+
+/*****************************************************************************/
+
+/* ECB_NO_THREADS - ecb is not used by multiple threads, ever */
+/* ECB_NO_SMP - ecb might be used in multiple threads, but only on a single cpu */
+
+#if ECB_NO_THREADS
+# define ECB_NO_SMP 1
+#endif
+
+#if ECB_NO_THREADS || ECB_NO_SMP
+ #define ECB_MEMORY_FENCE do { } while (0)
+#endif
+
+#ifndef ECB_MEMORY_FENCE
+ #if ECB_GCC_VERSION(2,5) || defined(__INTEL_COMPILER) || (__llvm__ && __GNUC__) || __SUNPRO_C >= 0x5110 || __SUNPRO_CC >= 0x5110
+ #if __i386 || __i386__
+ #define ECB_MEMORY_FENCE __asm__ __volatile__ ("lock; orb $0, -1(%%esp)" : : : "memory")
+ #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE /* non-lock xchg might be enough */
+ #define ECB_MEMORY_FENCE_RELEASE do { } while (0) /* unlikely to change in future cpus */
+ #elif __amd64 || __amd64__ || __x86_64 || __x86_64__
+ #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mfence" : : : "memory")
+ #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("lfence" : : : "memory")
+ #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("sfence") /* play safe - not needed in any current cpu */
+ #elif __powerpc__ || __ppc__ || __powerpc64__ || __ppc64__
+ #define ECB_MEMORY_FENCE __asm__ __volatile__ ("sync" : : : "memory")
+ #elif defined(__ARM_ARCH_6__ ) || defined(__ARM_ARCH_6J__ ) \
+ || defined(__ARM_ARCH_6K__) || defined(__ARM_ARCH_6ZK__)
+ #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mcr p15,0,%0,c7,c10,5" : : "r" (0) : "memory")
+ #elif defined(__ARM_ARCH_7__ ) || defined(__ARM_ARCH_7A__ ) \
+ || defined(__ARM_ARCH_7M__) || defined(__ARM_ARCH_7R__ )
+ #define ECB_MEMORY_FENCE __asm__ __volatile__ ("dmb" : : : "memory")
+ #elif __sparc || __sparc__
+ #define ECB_MEMORY_FENCE __asm__ __volatile__ ("membar #LoadStore | #LoadLoad | #StoreStore | #StoreLoad | " : : : "memory")
+ #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("membar #LoadStore | #LoadLoad" : : : "memory")
+ #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("membar #LoadStore | #StoreStore")
+ #elif defined(__s390__) || defined(__s390x__)
+ #define ECB_MEMORY_FENCE __asm__ __volatile__ ("bcr 15,0" : : : "memory")
+ #endif
+ #endif
+#endif
+
+#ifndef ECB_MEMORY_FENCE
+ #if ECB_GCC_VERSION(4,4) || defined(__INTEL_COMPILER) || defined(__clang__)
+ #define ECB_MEMORY_FENCE __sync_synchronize ()
+ /*#define ECB_MEMORY_FENCE_ACQUIRE ({ char dummy = 0; __sync_lock_test_and_set (&dummy, 1); }) */
+ /*#define ECB_MEMORY_FENCE_RELEASE ({ char dummy = 1; __sync_lock_release (&dummy ); }) */
+ #elif _MSC_VER >= 1400 /* VC++ 2005 */
+ #pragma intrinsic(_ReadBarrier,_WriteBarrier,_ReadWriteBarrier)
+ #define ECB_MEMORY_FENCE _ReadWriteBarrier ()
+ #define ECB_MEMORY_FENCE_ACQUIRE _ReadWriteBarrier () /* according to msdn, _ReadBarrier is not a load fence */
+ #define ECB_MEMORY_FENCE_RELEASE _WriteBarrier ()
+ #elif defined(_WIN32)
+ #include <WinNT.h>
+ #define ECB_MEMORY_FENCE MemoryBarrier () /* actually just xchg on x86... scary */
+ #elif __SUNPRO_C >= 0x5110 || __SUNPRO_CC >= 0x5110
+ #include <mbarrier.h>
+ #define ECB_MEMORY_FENCE __machine_rw_barrier ()
+ #define ECB_MEMORY_FENCE_ACQUIRE __machine_r_barrier ()
+ #define ECB_MEMORY_FENCE_RELEASE __machine_w_barrier ()
+ #endif
+#endif
+
+#ifndef ECB_MEMORY_FENCE
+ #if !ECB_AVOID_PTHREADS
+ /*
+ * if you get undefined symbol references to pthread_mutex_lock,
+ * or failure to find pthread.h, then you should implement
+ * the ECB_MEMORY_FENCE operations for your cpu/compiler
+ * OR provide pthread.h and link against the posix thread library
+ * of your system.
+ */
+ #include <pthread.h>
+ #define ECB_NEEDS_PTHREADS 1
+ #define ECB_MEMORY_FENCE_NEEDS_PTHREADS 1
+
+ static pthread_mutex_t ecb_mf_lock = PTHREAD_MUTEX_INITIALIZER;
+ #define ECB_MEMORY_FENCE do { pthread_mutex_lock (&ecb_mf_lock); pthread_mutex_unlock (&ecb_mf_lock); } while (0)
+ #endif
+#endif
+
+#if !defined(ECB_MEMORY_FENCE_ACQUIRE) && defined(ECB_MEMORY_FENCE)
+ #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE
+#endif
+
+#if !defined(ECB_MEMORY_FENCE_RELEASE) && defined(ECB_MEMORY_FENCE)
+ #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE
+#endif
+
+/*****************************************************************************/
+
+#define ECB_C99 (__STDC_VERSION__ >= 199901L)
+
+#if __cplusplus
+ #define ecb_inline static inline
+#elif ECB_GCC_VERSION(2,5)
+ #define ecb_inline static __inline__
+#elif ECB_C99
+ #define ecb_inline static inline
+#else
+ #define ecb_inline static
+#endif
+
+#if ECB_GCC_VERSION(3,3)
+ #define ecb_restrict __restrict__
+#elif ECB_C99
+ #define ecb_restrict restrict
+#else
+ #define ecb_restrict
+#endif
+
+typedef int ecb_bool;
+
+#define ECB_CONCAT_(a, b) a ## b
+#define ECB_CONCAT(a, b) ECB_CONCAT_(a, b)
+#define ECB_STRINGIFY_(a) # a
+#define ECB_STRINGIFY(a) ECB_STRINGIFY_(a)
+
+#define ecb_function_ ecb_inline
+
+#if ECB_GCC_VERSION(3,1)
+ #define ecb_attribute(attrlist) __attribute__(attrlist)
+ #define ecb_is_constant(expr) __builtin_constant_p (expr)
+ #define ecb_expect(expr,value) __builtin_expect ((expr),(value))
+ #define ecb_prefetch(addr,rw,locality) __builtin_prefetch (addr, rw, locality)
+#else
+ #define ecb_attribute(attrlist)
+ #define ecb_is_constant(expr) 0
+ #define ecb_expect(expr,value) (expr)
+ #define ecb_prefetch(addr,rw,locality)
+#endif
+
+/* no emulation for ecb_decltype */
+#if ECB_GCC_VERSION(4,5)
+ #define ecb_decltype(x) __decltype(x)
+#elif ECB_GCC_VERSION(3,0)
+ #define ecb_decltype(x) __typeof(x)
+#endif
+
+#define ecb_noinline ecb_attribute ((__noinline__))
+#define ecb_noreturn ecb_attribute ((__noreturn__))
+#define ecb_unused ecb_attribute ((__unused__))
+#define ecb_const ecb_attribute ((__const__))
+#define ecb_pure ecb_attribute ((__pure__))
+
+#if ECB_GCC_VERSION(4,3)
+ #define ecb_artificial ecb_attribute ((__artificial__))
+ #define ecb_hot ecb_attribute ((__hot__))
+ #define ecb_cold ecb_attribute ((__cold__))
+#else
+ #define ecb_artificial
+ #define ecb_hot
+ #define ecb_cold
+#endif
+
+/* put around conditional expressions if you are very sure that the */
+/* expression is mostly true or mostly false. note that these return */
+/* booleans, not the expression. */
+#define ecb_expect_false(expr) ecb_expect (!!(expr), 0)
+#define ecb_expect_true(expr) ecb_expect (!!(expr), 1)
+/* for compatibility to the rest of the world */
+#define ecb_likely(expr) ecb_expect_true (expr)
+#define ecb_unlikely(expr) ecb_expect_false (expr)
+
+/* count trailing zero bits and count # of one bits */
+#if ECB_GCC_VERSION(3,4)
+ /* we assume int == 32 bit, long == 32 or 64 bit and long long == 64 bit */
+ #define ecb_ld32(x) (__builtin_clz (x) ^ 31)
+ #define ecb_ld64(x) (__builtin_clzll (x) ^ 63)
+ #define ecb_ctz32(x) __builtin_ctz (x)
+ #define ecb_ctz64(x) __builtin_ctzll (x)
+ #define ecb_popcount32(x) __builtin_popcount (x)
+ /* no popcountll */
+#else
+ ecb_function_ int ecb_ctz32 (uint32_t x) ecb_const;
+ ecb_function_ int
+ ecb_ctz32 (uint32_t x)
+ {
+ int r = 0;
+
+ x &= ~x + 1; /* this isolates the lowest bit */
+
+#if ECB_branchless_on_i386
+ r += !!(x & 0xaaaaaaaa) << 0;
+ r += !!(x & 0xcccccccc) << 1;
+ r += !!(x & 0xf0f0f0f0) << 2;
+ r += !!(x & 0xff00ff00) << 3;
+ r += !!(x & 0xffff0000) << 4;
+#else
+ if (x & 0xaaaaaaaa) r += 1;
+ if (x & 0xcccccccc) r += 2;
+ if (x & 0xf0f0f0f0) r += 4;
+ if (x & 0xff00ff00) r += 8;
+ if (x & 0xffff0000) r += 16;
+#endif
+
+ return r;
+ }
+
+ ecb_function_ int ecb_ctz64 (uint64_t x) ecb_const;
+ ecb_function_ int
+ ecb_ctz64 (uint64_t x)
+ {
+ int shift = x & 0xffffffffU ? 0 : 32;
+ return ecb_ctz32 (x >> shift) + shift;
+ }
+
+ ecb_function_ int ecb_popcount32 (uint32_t x) ecb_const;
+ ecb_function_ int
+ ecb_popcount32 (uint32_t x)
+ {
+ x -= (x >> 1) & 0x55555555;
+ x = ((x >> 2) & 0x33333333) + (x & 0x33333333);
+ x = ((x >> 4) + x) & 0x0f0f0f0f;
+ x *= 0x01010101;
+
+ return x >> 24;
+ }
+
+ ecb_function_ int ecb_ld32 (uint32_t x) ecb_const;
+ ecb_function_ int ecb_ld32 (uint32_t x)
+ {
+ int r = 0;
+
+ if (x >> 16) { x >>= 16; r += 16; }
+ if (x >> 8) { x >>= 8; r += 8; }
+ if (x >> 4) { x >>= 4; r += 4; }
+ if (x >> 2) { x >>= 2; r += 2; }
+ if (x >> 1) { r += 1; }
+
+ return r;
+ }
+
+ ecb_function_ int ecb_ld64 (uint64_t x) ecb_const;
+ ecb_function_ int ecb_ld64 (uint64_t x)
+ {
+ int r = 0;
+
+ if (x >> 32) { x >>= 32; r += 32; }
+
+ return r + ecb_ld32 (x);
+ }
#endif
-#define expect_false(expr) expect ((expr) != 0, 0)
-#define expect_true(expr) expect ((expr) != 0, 1)
-#define inline_size static inline
+ecb_function_ uint8_t ecb_bitrev8 (uint8_t x) ecb_const;
+ecb_function_ uint8_t ecb_bitrev8 (uint8_t x)
+{
+ return ( (x * 0x0802U & 0x22110U)
+ | (x * 0x8020U & 0x88440U)) * 0x10101U >> 16;
+}
+
+ecb_function_ uint16_t ecb_bitrev16 (uint16_t x) ecb_const;
+ecb_function_ uint16_t ecb_bitrev16 (uint16_t x)
+{
+ x = ((x >> 1) & 0x5555) | ((x & 0x5555) << 1);
+ x = ((x >> 2) & 0x3333) | ((x & 0x3333) << 2);
+ x = ((x >> 4) & 0x0f0f) | ((x & 0x0f0f) << 4);
+ x = ( x >> 8 ) | ( x << 8);
+
+ return x;
+}
+
+ecb_function_ uint32_t ecb_bitrev32 (uint32_t x) ecb_const;
+ecb_function_ uint32_t ecb_bitrev32 (uint32_t x)
+{
+ x = ((x >> 1) & 0x55555555) | ((x & 0x55555555) << 1);
+ x = ((x >> 2) & 0x33333333) | ((x & 0x33333333) << 2);
+ x = ((x >> 4) & 0x0f0f0f0f) | ((x & 0x0f0f0f0f) << 4);
+ x = ((x >> 8) & 0x00ff00ff) | ((x & 0x00ff00ff) << 8);
+ x = ( x >> 16 ) | ( x << 16);
+
+ return x;
+}
+
+/* popcount64 is only available on 64 bit cpus as gcc builtin */
+/* so for this version we are lazy */
+ecb_function_ int ecb_popcount64 (uint64_t x) ecb_const;
+ecb_function_ int
+ecb_popcount64 (uint64_t x)
+{
+ return ecb_popcount32 (x) + ecb_popcount32 (x >> 32);
+}
+
+ecb_inline uint8_t ecb_rotl8 (uint8_t x, unsigned int count) ecb_const;
+ecb_inline uint8_t ecb_rotr8 (uint8_t x, unsigned int count) ecb_const;
+ecb_inline uint16_t ecb_rotl16 (uint16_t x, unsigned int count) ecb_const;
+ecb_inline uint16_t ecb_rotr16 (uint16_t x, unsigned int count) ecb_const;
+ecb_inline uint32_t ecb_rotl32 (uint32_t x, unsigned int count) ecb_const;
+ecb_inline uint32_t ecb_rotr32 (uint32_t x, unsigned int count) ecb_const;
+ecb_inline uint64_t ecb_rotl64 (uint64_t x, unsigned int count) ecb_const;
+ecb_inline uint64_t ecb_rotr64 (uint64_t x, unsigned int count) ecb_const;
+
+ecb_inline uint8_t ecb_rotl8 (uint8_t x, unsigned int count) { return (x >> ( 8 - count)) | (x << count); }
+ecb_inline uint8_t ecb_rotr8 (uint8_t x, unsigned int count) { return (x << ( 8 - count)) | (x >> count); }
+ecb_inline uint16_t ecb_rotl16 (uint16_t x, unsigned int count) { return (x >> (16 - count)) | (x << count); }
+ecb_inline uint16_t ecb_rotr16 (uint16_t x, unsigned int count) { return (x << (16 - count)) | (x >> count); }
+ecb_inline uint32_t ecb_rotl32 (uint32_t x, unsigned int count) { return (x >> (32 - count)) | (x << count); }
+ecb_inline uint32_t ecb_rotr32 (uint32_t x, unsigned int count) { return (x << (32 - count)) | (x >> count); }
+ecb_inline uint64_t ecb_rotl64 (uint64_t x, unsigned int count) { return (x >> (64 - count)) | (x << count); }
+ecb_inline uint64_t ecb_rotr64 (uint64_t x, unsigned int count) { return (x << (64 - count)) | (x >> count); }
+
+#if ECB_GCC_VERSION(4,3)
+ #define ecb_bswap16(x) (__builtin_bswap32 (x) >> 16)
+ #define ecb_bswap32(x) __builtin_bswap32 (x)
+ #define ecb_bswap64(x) __builtin_bswap64 (x)
+#else
+ ecb_function_ uint16_t ecb_bswap16 (uint16_t x) ecb_const;
+ ecb_function_ uint16_t
+ ecb_bswap16 (uint16_t x)
+ {
+ return ecb_rotl16 (x, 8);
+ }
+
+ ecb_function_ uint32_t ecb_bswap32 (uint32_t x) ecb_const;
+ ecb_function_ uint32_t
+ ecb_bswap32 (uint32_t x)
+ {
+ return (((uint32_t)ecb_bswap16 (x)) << 16) | ecb_bswap16 (x >> 16);
+ }
+
+ ecb_function_ uint64_t ecb_bswap64 (uint64_t x) ecb_const;
+ ecb_function_ uint64_t
+ ecb_bswap64 (uint64_t x)
+ {
+ return (((uint64_t)ecb_bswap32 (x)) << 32) | ecb_bswap32 (x >> 32);
+ }
+#endif
+
+#if ECB_GCC_VERSION(4,5)
+ #define ecb_unreachable() __builtin_unreachable ()
+#else
+ /* this seems to work fine, but gcc always emits a warning for it :/ */
+ ecb_inline void ecb_unreachable (void) ecb_noreturn;
+ ecb_inline void ecb_unreachable (void) { }
+#endif
+
+/* try to tell the compiler that some condition is definitely true */
+#define ecb_assume(cond) do { if (!(cond)) ecb_unreachable (); } while (0)
+
+ecb_inline unsigned char ecb_byteorder_helper (void) ecb_const;
+ecb_inline unsigned char
+ecb_byteorder_helper (void)
+{
+ const uint32_t u = 0x11223344;
+ return *(unsigned char *)&u;
+}
+
+ecb_inline ecb_bool ecb_big_endian (void) ecb_const;
+ecb_inline ecb_bool ecb_big_endian (void) { return ecb_byteorder_helper () == 0x11; }
+ecb_inline ecb_bool ecb_little_endian (void) ecb_const;
+ecb_inline ecb_bool ecb_little_endian (void) { return ecb_byteorder_helper () == 0x44; }
+
+#if ECB_GCC_VERSION(3,0) || ECB_C99
+ #define ecb_mod(m,n) ((m) % (n) + ((m) % (n) < 0 ? (n) : 0))
+#else
+ #define ecb_mod(m,n) ((m) < 0 ? ((n) - 1 - ((-1 - (m)) % (n))) : ((m) % (n)))
+#endif
+
+#if __cplusplus
+ template<typename T>
+ static inline T ecb_div_rd (T val, T div)
+ {
+ return val < 0 ? - ((-val + div - 1) / div) : (val ) / div;
+ }
+ template<typename T>
+ static inline T ecb_div_ru (T val, T div)
+ {
+ return val < 0 ? - ((-val ) / div) : (val + div - 1) / div;
+ }
+#else
+ #define ecb_div_rd(val,div) ((val) < 0 ? - ((-(val) + (div) - 1) / (div)) : ((val) ) / (div))
+ #define ecb_div_ru(val,div) ((val) < 0 ? - ((-(val) ) / (div)) : ((val) + (div) - 1) / (div))
+#endif
+
+#if ecb_cplusplus_does_not_suck
+ /* does not work for local types (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2657.htm) */
+ template<typename T, int N>
+ static inline int ecb_array_length (const T (&arr)[N])
+ {
+ return N;
+ }
+#else
+ #define ecb_array_length(name) (sizeof (name) / sizeof (name [0]))
+#endif
+
+#endif
+
+/* ECB.H END */
+
+#if ECB_MEMORY_FENCE_NEEDS_PTHREADS
+/* if your architecture doesn't need memory fences, e.g. because it is
+ * single-cpu/core, or if you use libev in a project that doesn't use libev
+ * from multiple threads, then you can define ECB_AVOID_PTHREADS when compiling
+ * libev, in which cases the memory fences become nops.
+ * alternatively, you can remove this #error and link against libpthread,
+ * which will then provide the memory fences.
+ */
+# error "memory fences not defined for your architecture, please report"
+#endif
+
+#ifndef ECB_MEMORY_FENCE
+# define ECB_MEMORY_FENCE do { } while (0)
+# define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE
+# define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE
+#endif
+
+#define expect_false(cond) ecb_expect_false (cond)
+#define expect_true(cond) ecb_expect_true (cond)
+#define noinline ecb_noinline
+
+#define inline_size ecb_inline
#if EV_FEATURE_CODE
-# define inline_speed static inline
+# define inline_speed ecb_inline
#else
# define inline_speed static noinline
#endif
@@ -524,11 +1003,59 @@ static EV_ATOMIC_T have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work?
/*****************************************************************************/
+/* define a suitable floor function (only used by periodics atm) */
+
+#if EV_USE_FLOOR
+# include <math.h>
+# define ev_floor(v) floor (v)
+#else
+
+#include <float.h>
+
+/* a floor() replacement function, should be independent of ev_tstamp type */
+static ev_tstamp noinline
+ev_floor (ev_tstamp v)
+{
+ /* the choice of shift factor is not terribly important */
+#if FLT_RADIX != 2 /* assume FLT_RADIX == 10 */
+ const ev_tstamp shift = sizeof (unsigned long) >= 8 ? 10000000000000000000. : 1000000000.;
+#else
+ const ev_tstamp shift = sizeof (unsigned long) >= 8 ? 18446744073709551616. : 4294967296.;
+#endif
+
+ /* argument too large for an unsigned long? */
+ if (expect_false (v >= shift))
+ {
+ ev_tstamp f;
+
+ if (v == v - 1.)
+ return v; /* very large number */
+
+ f = shift * ev_floor (v * (1. / shift));
+ return f + ev_floor (v - f);
+ }
+
+ /* special treatment for negative args? */
+ if (expect_false (v < 0.))
+ {
+ ev_tstamp f = -ev_floor (-v);
+
+ return f - (f == v ? 0 : 1);
+ }
+
+ /* fits into an unsigned long */
+ return (unsigned long)v;
+}
+
+#endif
+
+/*****************************************************************************/
+
#ifdef __linux
# include <sys/utsname.h>
#endif
-static unsigned int noinline
+static unsigned int noinline ecb_cold
ev_linux_version (void)
{
#ifdef __linux
@@ -567,7 +1094,7 @@ ev_linux_version (void)
/*****************************************************************************/
#if EV_AVOID_STDIO
-static void noinline
+static void noinline ecb_cold
ev_printerr (const char *msg)
{
write (STDERR_FILENO, msg, strlen (msg));
@@ -576,13 +1103,13 @@ ev_printerr (const char *msg)
static void (*syserr_cb)(const char *msg);
-void
+void ecb_cold
ev_set_syserr_cb (void (*cb)(const char *msg))
{
syserr_cb = cb;
}
-static void noinline
+static void noinline ecb_cold
ev_syserr (const char *msg)
{
if (!msg)
@@ -625,7 +1152,7 @@ ev_realloc_emul (void *ptr, long size)
static void *(*alloc)(void *ptr, long size) = ev_realloc_emul;
-void
+void ecb_cold
ev_set_allocator (void *(*cb)(void *ptr, long size))
{
alloc = cb;
@@ -724,11 +1251,11 @@ typedef struct
#include "ev_wrap.h"
static struct ev_loop default_loop_struct;
- struct ev_loop *ev_default_loop_ptr;
+ EV_API_DECL struct ev_loop *ev_default_loop_ptr = 0; /* needs to be initialised to make it a definition despite extern */
#else
- ev_tstamp ev_rt_now;
+ EV_API_DECL ev_tstamp ev_rt_now = 0; /* needs to be initialised to make it a definition despite extern */
#define VAR(name,decl) static decl;
#include "ev_vars.h"
#undef VAR
@@ -832,7 +1359,7 @@ array_nextsize (int elem, int cur, int cnt)
ncur <<= 1;
while (cnt > ncur);
- /* if size is large, round to MALLOC_ROUND - 4 * longs to accomodate malloc overhead */
+ /* if size is large, round to MALLOC_ROUND - 4 * longs to accommodate malloc overhead */
if (elem * ncur > MALLOC_ROUND - sizeof (void *) * 4)
{
ncur *= elem;
@@ -844,7 +1371,7 @@ array_nextsize (int elem, int cur, int cnt)
return ncur;
}
-static noinline void *
+static void * noinline ecb_cold
array_realloc (int elem, void *base, int *cur, int cnt)
{
*cur = array_nextsize (elem, *cur, cnt);
@@ -857,7 +1384,7 @@ array_realloc (int elem, void *base, int *cur, int cnt)
#define array_needsize(type,base,cur,cnt,init) \
if (expect_false ((cnt) > (cur))) \
{ \
- int ocur_ = (cur); \
+ int ecb_unused ocur_ = (cur); \
(base) = (type *)array_realloc \
(sizeof (type), (base), &(cur), (cnt)); \
init ((base) + (ocur_), (cur) - ocur_); \
@@ -967,6 +1494,31 @@ fd_reify (EV_P)
{
int i;
+#if EV_SELECT_IS_WINSOCKET || EV_USE_IOCP
+ for (i = 0; i < fdchangecnt; ++i)
+ {
+ int fd = fdchanges [i];
+ ANFD *anfd = anfds + fd;
+
+ if (anfd->reify & EV__IOFDSET && anfd->head)
+ {
+ SOCKET handle = EV_FD_TO_WIN32_HANDLE (fd);
+
+ if (handle != anfd->handle)
+ {
+ unsigned long arg;
+
+ assert (("libev: only socket fds supported in this configuration", ioctlsocket (handle, FIONREAD, &arg) == 0));
+
+ /* handle changed, but fd didn't - we need to do it in two steps */
+ backend_modify (EV_A_ fd, anfd->events, 0);
+ anfd->events = 0;
+ anfd->handle = handle;
+ }
+ }
+ }
+#endif
+
for (i = 0; i < fdchangecnt; ++i)
{
int fd = fdchanges [i];
@@ -978,16 +1530,6 @@ fd_reify (EV_P)
anfd->reify = 0;
-#if EV_SELECT_IS_WINSOCKET || EV_USE_IOCP
- if (o_reify & EV__IOFDSET)
- {
- unsigned long arg;
- anfd->handle = EV_FD_TO_WIN32_HANDLE (fd);
- assert (("libev: only socket fds supported in this configuration", ioctlsocket (anfd->handle, FIONREAD, &arg) == 0));
- printf ("oi %d %x\n", fd, anfd->handle);//D
- }
-#endif
-
/*if (expect_true (o_reify & EV_ANFD_REIFY)) probably a deoptimisation */
{
anfd->events = 0;
@@ -1022,7 +1564,7 @@ fd_change (EV_P_ int fd, int flags)
}
/* the given fd is invalid/unusable, so make sure it doesn't hurt us anymore */
-inline_speed void
+inline_speed void ecb_cold
fd_kill (EV_P_ int fd)
{
ev_io *w;
@@ -1035,7 +1577,7 @@ fd_kill (EV_P_ int fd)
}
/* check whether the given fd is actually valid, for error recovery */
-inline_size int
+inline_size int ecb_cold
fd_valid (int fd)
{
#ifdef _WIN32
@@ -1046,7 +1588,7 @@ fd_valid (int fd)
}
/* called on EBADF to verify fds */
-static void noinline
+static void noinline ecb_cold
fd_ebadf (EV_P)
{
int fd;
@@ -1058,7 +1600,7 @@ fd_ebadf (EV_P)
}
/* called on ENOMEM in select/poll to kill some fds and retry */
-static void noinline
+static void noinline ecb_cold
fd_enomem (EV_P)
{
int fd;
@@ -1263,7 +1805,7 @@ static ANSIG signals [EV_NSIG - 1];
#if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE
-static void noinline
+static void noinline ecb_cold
evpipe_init (EV_P)
{
if (!ev_is_active (&pipe_w))
@@ -1295,15 +1837,27 @@ evpipe_init (EV_P)
}
}
-inline_size void
+inline_speed void
evpipe_write (EV_P_ EV_ATOMIC_T *flag)
{
- if (!*flag)
+ if (expect_true (*flag))
+ return;
+
+ *flag = 1;
+
+ ECB_MEMORY_FENCE_RELEASE; /* make sure flag is visible before the wakeup */
+
+ pipe_write_skipped = 1;
+
+ ECB_MEMORY_FENCE; /* make sure pipe_write_skipped is visible before we check pipe_write_wanted */
+
+ if (pipe_write_wanted)
{
- int old_errno = errno; /* save errno because write might clobber it */
- char dummy;
+ int old_errno;
- *flag = 1;
+ pipe_write_skipped = 0; /* just an optimisation, no fence needed */
+
+ old_errno = errno; /* save errno because write will clobber it */
#if EV_USE_EVENTFD
if (evfd >= 0)
@@ -1313,12 +1867,14 @@ evpipe_write (EV_P_ EV_ATOMIC_T *flag)
}
else
#endif
- /* win32 people keep sending patches that change this write() to send() */
- /* and then run away. but send() is wrong, it wants a socket handle on win32 */
- /* so when you think this write should be a send instead, please find out */
- /* where your send() is from - it's definitely not the microsoft send, and */
- /* tell me. thank you. */
- write (evpipe [1], &dummy, 1);
+ {
+ /* win32 people keep sending patches that change this write() to send() */
+ /* and then run away. but send() is wrong, it wants a socket handle on win32 */
+ /* so when you think this write should be a send instead, please find out */
+ /* where your send() is from - it's definitely not the microsoft send, and */
+ /* tell me. thank you. */
+ write (evpipe [1], &(evpipe [1]), 1);
+ }
errno = old_errno;
}
@@ -1331,28 +1887,35 @@ pipecb (EV_P_ ev_io *iow, int revents)
{
int i;
-#if EV_USE_EVENTFD
- if (evfd >= 0)
+ if (revents & EV_READ)
{
- uint64_t counter;
- read (evfd, &counter, sizeof (uint64_t));
- }
- else
+#if EV_USE_EVENTFD
+ if (evfd >= 0)
+ {
+ uint64_t counter;
+ read (evfd, &counter, sizeof (uint64_t));
+ }
+ else
#endif
- {
- char dummy;
- /* see discussion in evpipe_write when you think this read should be recv in win32 */
- read (evpipe [0], &dummy, 1);
+ {
+ char dummy;
+ /* see discussion in evpipe_write when you think this read should be recv in win32 */
+ read (evpipe [0], &dummy, 1);
+ }
}
+ pipe_write_skipped = 0;
+
+#if EV_SIGNAL_ENABLE
if (sig_pending)
- {
+ {
sig_pending = 0;
for (i = EV_NSIG - 1; i--; )
if (expect_false (signals [i].pending))
ev_feed_signal_event (EV_A_ i + 1);
}
+#endif
#if EV_ASYNC_ENABLE
if (async_pending)
@@ -1381,6 +1944,9 @@ ev_feed_signal (int signum)
return;
#endif
+ if (!ev_active (&pipe_w))
+ return;
+
signals [signum - 1].pending = 1;
evpipe_write (EV_A_ &sig_pending);
}
@@ -1521,20 +2087,20 @@ childcb (EV_P_ ev_signal *sw, int revents)
# include "ev_select.c"
#endif
-int
+int ecb_cold
ev_version_major (void)
{
return EV_VERSION_MAJOR;
}
-int
+int ecb_cold
ev_version_minor (void)
{
return EV_VERSION_MINOR;
}
/* return true if we are running with elevated privileges and should ignore env variables */
-int inline_size
+int inline_size ecb_cold
enable_secure (void)
{
#ifdef _WIN32
@@ -1545,7 +2111,7 @@ enable_secure (void)
#endif
}
-unsigned int
+unsigned int ecb_cold
ev_supported_backends (void)
{
unsigned int flags = 0;
@@ -1559,7 +2125,7 @@ ev_supported_backends (void)
return flags;
}
-unsigned int
+unsigned int ecb_cold
ev_recommended_backends (void)
{
unsigned int flags = ev_supported_backends ();
@@ -1581,7 +2147,7 @@ ev_recommended_backends (void)
return flags;
}
-unsigned int
+unsigned int ecb_cold
ev_embeddable_backends (void)
{
int flags = EVBACKEND_EPOLL | EVBACKEND_KQUEUE | EVBACKEND_PORT;
@@ -1636,12 +2202,14 @@ ev_userdata (EV_P)
return userdata;
}
-void ev_set_invoke_pending_cb (EV_P_ void (*invoke_pending_cb)(EV_P))
+void
+ev_set_invoke_pending_cb (EV_P_ void (*invoke_pending_cb)(EV_P))
{
invoke_cb = invoke_pending_cb;
}
-void ev_set_loop_release_cb (EV_P_ void (*release)(EV_P), void (*acquire)(EV_P))
+void
+ev_set_loop_release_cb (EV_P_ void (*release)(EV_P), void (*acquire)(EV_P))
{
release_cb = release;
acquire_cb = acquire;
@@ -1649,7 +2217,7 @@ void ev_set_loop_release_cb (EV_P_ void (*release)(EV_P), void (*acquire)(EV_P))
#endif
/* initialise a loop structure, must be zero-initialised */
-static void noinline
+static void noinline ecb_cold
loop_init (EV_P_ unsigned int flags)
{
if (!backend)
@@ -1687,27 +2255,29 @@ loop_init (EV_P_ unsigned int flags)
&& getenv ("LIBEV_FLAGS"))
flags = atoi (getenv ("LIBEV_FLAGS"));
- ev_rt_now = ev_time ();
- mn_now = get_clock ();
- now_floor = mn_now;
- rtmn_diff = ev_rt_now - mn_now;
+ ev_rt_now = ev_time ();
+ mn_now = get_clock ();
+ now_floor = mn_now;
+ rtmn_diff = ev_rt_now - mn_now;
#if EV_FEATURE_API
- invoke_cb = ev_invoke_pending;
+ invoke_cb = ev_invoke_pending;
#endif
- io_blocktime = 0.;
- timeout_blocktime = 0.;
- backend = 0;
- backend_fd = -1;
- sig_pending = 0;
+ io_blocktime = 0.;
+ timeout_blocktime = 0.;
+ backend = 0;
+ backend_fd = -1;
+ sig_pending = 0;
#if EV_ASYNC_ENABLE
- async_pending = 0;
+ async_pending = 0;
#endif
+ pipe_write_skipped = 0;
+ pipe_write_wanted = 0;
#if EV_USE_INOTIFY
- fs_fd = flags & EVFLAG_NOINOTIFY ? -1 : -2;
+ fs_fd = flags & EVFLAG_NOINOTIFY ? -1 : -2;
#endif
#if EV_USE_SIGNALFD
- sigfd = flags & EVFLAG_SIGNALFD ? -2 : -1;
+ sigfd = flags & EVFLAG_SIGNALFD ? -2 : -1;
#endif
if (!(flags & EVBACKEND_MASK))
@@ -1742,7 +2312,7 @@ loop_init (EV_P_ unsigned int flags)
}
/* free up a loop structure */
-void
+void ecb_cold
ev_loop_destroy (EV_P)
{
int i;
@@ -1882,12 +2452,7 @@ loop_fork (EV_P)
if (ev_is_active (&pipe_w))
{
- /* this "locks" the handlers against writing to the pipe */
- /* while we modify the fd vars */
- sig_pending = 1;
-#if EV_ASYNC_ENABLE
- async_pending = 1;
-#endif
+ /* pipe_write_wanted must be false now, so modifying fd vars should be safe */
ev_ref (EV_A);
ev_io_stop (EV_A_ &pipe_w);
@@ -1915,7 +2480,7 @@ loop_fork (EV_P)
#if EV_MULTIPLICITY
-struct ev_loop *
+struct ev_loop * ecb_cold
ev_loop_new (unsigned int flags)
{
EV_P = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop));
@@ -1933,7 +2498,7 @@ ev_loop_new (unsigned int flags)
#endif /* multiplicity */
#if EV_VERIFY
-static void noinline
+static void noinline ecb_cold
verify_watcher (EV_P_ W w)
{
assert (("libev: watcher has invalid priority", ABSPRI (w) >= 0 && ABSPRI (w) < NUMPRI));
@@ -1942,7 +2507,7 @@ verify_watcher (EV_P_ W w)
assert (("libev: pending watcher not on pending queue", pendings [ABSPRI (w)][w->pending - 1].w == w));
}
-static void noinline
+static void noinline ecb_cold
verify_heap (EV_P_ ANHE *heap, int N)
{
int i;
@@ -1957,7 +2522,7 @@ verify_heap (EV_P_ ANHE *heap, int N)
}
}
-static void noinline
+static void noinline ecb_cold
array_verify (EV_P_ W *ws, int cnt)
{
while (cnt--)
@@ -1969,7 +2534,7 @@ array_verify (EV_P_ W *ws, int cnt)
#endif
#if EV_FEATURE_API
-void
+void ecb_cold
ev_verify (EV_P)
{
#if EV_VERIFY
@@ -2045,7 +2610,7 @@ ev_verify (EV_P)
#endif
#if EV_MULTIPLICITY
-struct ev_loop *
+struct ev_loop * ecb_cold
#else
int
#endif
@@ -2183,6 +2748,31 @@ timers_reify (EV_P)
}
#if EV_PERIODIC_ENABLE
+
+static void noinline
+periodic_recalc (EV_P_ ev_periodic *w)
+{
+ ev_tstamp interval = w->interval > MIN_INTERVAL ? w->interval : MIN_INTERVAL;
+ ev_tstamp at = w->offset + interval * ev_floor ((ev_rt_now - w->offset) / interval);
+
+ /* the above almost always errs on the low side */
+ while (at <= ev_rt_now)
+ {
+ ev_tstamp nat = at + w->interval;
+
+ /* when resolution fails us, we use ev_rt_now */
+ if (expect_false (nat == at))
+ {
+ at = ev_rt_now;
+ break;
+ }
+
+ at = nat;
+ }
+
+ ev_at (w) = at;
+}
+
/* make periodics pending */
inline_size void
periodics_reify (EV_P)
@@ -2211,20 +2801,7 @@ periodics_reify (EV_P)
}
else if (w->interval)
{
- ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval;
- /* if next trigger time is not sufficiently in the future, put it there */
- /* this might happen because of floating point inexactness */
- if (ev_at (w) - ev_rt_now < TIME_EPSILON)
- {
- ev_at (w) += w->interval;
-
- /* if interval is unreasonably low we might still have a time in the past */
- /* so correct this. this will make the periodic very inexact, but the user */
- /* has effectively asked to get triggered more often than possible */
- if (ev_at (w) < ev_rt_now)
- ev_at (w) = ev_rt_now;
- }
-
+ periodic_recalc (EV_A_ w);
ANHE_at_cache (periodics [HEAP0]);
downheap (periodics, periodiccnt, HEAP0);
}
@@ -2242,7 +2819,7 @@ periodics_reify (EV_P)
/* simply recalculate all periodics */
/* TODO: maybe ensure that at least one event happens when jumping forward? */
-static void noinline
+static void noinline ecb_cold
periodics_reschedule (EV_P)
{
int i;
@@ -2255,7 +2832,7 @@ periodics_reschedule (EV_P)
if (w->reschedule_cb)
ev_at (w) = w->reschedule_cb (w, ev_rt_now);
else if (w->interval)
- ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval;
+ periodic_recalc (EV_A_ w);
ANHE_at_cache (periodics [i]);
}
@@ -2265,7 +2842,7 @@ periodics_reschedule (EV_P)
#endif
/* adjust all timers by a given offset */
-static void noinline
+static void noinline ecb_cold
timers_reschedule (EV_P_ ev_tstamp adjust)
{
int i;
@@ -2312,9 +2889,12 @@ time_update (EV_P_ ev_tstamp max_block)
*/
for (i = 4; --i; )
{
+ ev_tstamp diff;
rtmn_diff = ev_rt_now - mn_now;
- if (expect_true (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP))
+ diff = odiff - rtmn_diff;
+
+ if (expect_true ((diff < 0. ? -diff : diff) < MIN_TIMEJUMP))
return; /* all is well */
ev_rt_now = ev_time ();
@@ -2414,20 +2994,25 @@ ev_run (EV_P_ int flags)
/* update time to cancel out callback processing overhead */
time_update (EV_A_ 1e100);
- if (expect_true (!(flags & EVRUN_NOWAIT || idleall || !activecnt)))
+ /* from now on, we want a pipe-wake-up */
+ pipe_write_wanted = 1;
+
+ ECB_MEMORY_FENCE; /* make sure pipe_write_wanted is visible before we check for potential skips */
+
+ if (expect_true (!(flags & EVRUN_NOWAIT || idleall || !activecnt || pipe_write_skipped)))
{
waittime = MAX_BLOCKTIME;
if (timercnt)
{
- ev_tstamp to = ANHE_at (timers [HEAP0]) - mn_now + backend_fudge;
+ ev_tstamp to = ANHE_at (timers [HEAP0]) - mn_now;
if (waittime > to) waittime = to;
}
#if EV_PERIODIC_ENABLE
if (periodiccnt)
{
- ev_tstamp to = ANHE_at (periodics [HEAP0]) - ev_rt_now + backend_fudge;
+ ev_tstamp to = ANHE_at (periodics [HEAP0]) - ev_rt_now;
if (waittime > to) waittime = to;
}
#endif
@@ -2436,13 +3021,18 @@ ev_run (EV_P_ int flags)
if (expect_false (waittime < timeout_blocktime))
waittime = timeout_blocktime;
+ /* at this point, we NEED to wait, so we have to ensure */
+ /* to pass a minimum nonzero value to the backend */
+ if (expect_false (waittime < backend_mintime))
+ waittime = backend_mintime;
+
/* extra check because io_blocktime is commonly 0 */
if (expect_false (io_blocktime))
{
sleeptime = io_blocktime - (mn_now - prev_mn_now);
- if (sleeptime > waittime - backend_fudge)
- sleeptime = waittime - backend_fudge;
+ if (sleeptime > waittime - backend_mintime)
+ sleeptime = waittime - backend_mintime;
if (expect_true (sleeptime > 0.))
{
@@ -2459,6 +3049,15 @@ ev_run (EV_P_ int flags)
backend_poll (EV_A_ waittime);
assert ((loop_done = EVBREAK_CANCEL, 1)); /* assert for side effect */
+ pipe_write_wanted = 0; /* just an optimisation, no fence needed */
+
+ if (pipe_write_skipped)
+ {
+ assert (("libev: pipe_w not active, but pipe not written", ev_is_active (&pipe_w)));
+ ev_feed_event (EV_A_ &pipe_w, EV_CUSTOM);
+ }
+
+
/* update ev_rt_now, do magic */
time_update (EV_A_ waittime + sleeptime);
}
@@ -2719,6 +3318,8 @@ ev_timer_again (EV_P_ ev_timer *w)
{
EV_FREQUENT_CHECK;
+ clear_pending (EV_A_ (W)w);
+
if (ev_is_active (w))
{
if (w->repeat)
@@ -2757,8 +3358,7 @@ ev_periodic_start (EV_P_ ev_periodic *w)
else if (w->interval)
{
assert (("libev: ev_periodic_start called with negative interval value", w->interval >= 0.));
- /* this formula differs from the one in periodic_reify because we do not always round up */
- ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval;
+ periodic_recalc (EV_A_ w);
}
else
ev_at (w) = w->offset;
@@ -3045,7 +3645,7 @@ infy_add (EV_P_ ev_stat *w)
*pend = 0;
w->wd = inotify_add_watch (fs_fd, path, mask);
- }
+ }
while (w->wd < 0 && (errno == ENOENT || errno == EACCES));
}
}
@@ -3122,7 +3722,7 @@ infy_cb (EV_P_ ev_io *w, int revents)
}
}
-inline_size void
+inline_size void ecb_cold
ev_check_2625 (EV_P)
{
/* kernels < 2.6.25 are borked
@@ -3757,7 +4357,7 @@ ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, vo
/*****************************************************************************/
#if EV_WALK_ENABLE
-void
+void ecb_cold
ev_walk (EV_P_ int types, void (*cb)(EV_P_ int type, void *w))
{
int i, j;
@@ -3811,7 +4411,7 @@ ev_walk (EV_P_ int types, void (*cb)(EV_P_ int type, void *w))
#if EV_IDLE_ENABLE
if (types & EV_IDLE)
- for (j = NUMPRI; i--; )
+ for (j = NUMPRI; j--; )
for (i = idlecnt [j]; i--; )
cb (EV_A_ EV_IDLE, idles [j][i]);
#endif
@@ -3874,5 +4474,3 @@ ev_walk (EV_P_ int types, void (*cb)(EV_P_ int type, void *w))
#include "ev_wrap.h"
#endif
-EV_CPP(})
-