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

Comparing libev/ev.c (file contents):
Revision 1.418 by root, Mon Apr 2 23:14:41 2012 UTC vs.
Revision 1.451 by root, Tue Jan 22 05:18:28 2013 UTC

201# include <sys/wait.h> 201# include <sys/wait.h>
202# include <unistd.h> 202# include <unistd.h>
203#else 203#else
204# include <io.h> 204# include <io.h>
205# define WIN32_LEAN_AND_MEAN 205# define WIN32_LEAN_AND_MEAN
206# include <winsock2.h>
206# include <windows.h> 207# include <windows.h>
207# ifndef EV_SELECT_IS_WINSOCKET 208# ifndef EV_SELECT_IS_WINSOCKET
208# define EV_SELECT_IS_WINSOCKET 1 209# define EV_SELECT_IS_WINSOCKET 1
209# endif 210# endif
210# undef EV_AVOID_STDIO 211# undef EV_AVOID_STDIO
359#endif 360#endif
360 361
361/* on linux, we can use a (slow) syscall to avoid a dependency on pthread, */ 362/* on linux, we can use a (slow) syscall to avoid a dependency on pthread, */
362/* which makes programs even slower. might work on other unices, too. */ 363/* which makes programs even slower. might work on other unices, too. */
363#if EV_USE_CLOCK_SYSCALL 364#if EV_USE_CLOCK_SYSCALL
364# include <syscall.h> 365# include <sys/syscall.h>
365# ifdef SYS_clock_gettime 366# ifdef SYS_clock_gettime
366# define clock_gettime(id, ts) syscall (SYS_clock_gettime, (id), (ts)) 367# define clock_gettime(id, ts) syscall (SYS_clock_gettime, (id), (ts))
367# undef EV_USE_MONOTONIC 368# undef EV_USE_MONOTONIC
368# define EV_USE_MONOTONIC 1 369# define EV_USE_MONOTONIC 1
369# else 370# else
408/* some very old inotify.h headers don't have IN_DONT_FOLLOW */ 409/* some very old inotify.h headers don't have IN_DONT_FOLLOW */
409# ifndef IN_DONT_FOLLOW 410# ifndef IN_DONT_FOLLOW
410# undef EV_USE_INOTIFY 411# undef EV_USE_INOTIFY
411# define EV_USE_INOTIFY 0 412# define EV_USE_INOTIFY 0
412# endif 413# endif
413#endif
414
415#if EV_SELECT_IS_WINSOCKET
416# include <winsock.h>
417#endif 414#endif
418 415
419#if EV_USE_EVENTFD 416#if EV_USE_EVENTFD
420/* our minimum requirement is glibc 2.7 which has the stub, but not the header */ 417/* our minimum requirement is glibc 2.7 which has the stub, but not the header */
421# include <stdint.h> 418# include <stdint.h>
507 */ 504 */
508 505
509#ifndef ECB_H 506#ifndef ECB_H
510#define ECB_H 507#define ECB_H
511 508
509/* 16 bits major, 16 bits minor */
510#define ECB_VERSION 0x00010002
511
512#ifdef _WIN32 512#ifdef _WIN32
513 typedef signed char int8_t; 513 typedef signed char int8_t;
514 typedef unsigned char uint8_t; 514 typedef unsigned char uint8_t;
515 typedef signed short int16_t; 515 typedef signed short int16_t;
516 typedef unsigned short uint16_t; 516 typedef unsigned short uint16_t;
521 typedef unsigned long long uint64_t; 521 typedef unsigned long long uint64_t;
522 #else /* _MSC_VER || __BORLANDC__ */ 522 #else /* _MSC_VER || __BORLANDC__ */
523 typedef signed __int64 int64_t; 523 typedef signed __int64 int64_t;
524 typedef unsigned __int64 uint64_t; 524 typedef unsigned __int64 uint64_t;
525 #endif 525 #endif
526 #ifdef _WIN64
527 #define ECB_PTRSIZE 8
528 typedef uint64_t uintptr_t;
529 typedef int64_t intptr_t;
530 #else
531 #define ECB_PTRSIZE 4
532 typedef uint32_t uintptr_t;
533 typedef int32_t intptr_t;
534 #endif
526#else 535#else
527 #include <inttypes.h> 536 #include <inttypes.h>
537 #if UINTMAX_MAX > 0xffffffffU
538 #define ECB_PTRSIZE 8
539 #else
540 #define ECB_PTRSIZE 4
541 #endif
528#endif 542#endif
529 543
530/* many compilers define _GNUC_ to some versions but then only implement 544/* many compilers define _GNUC_ to some versions but then only implement
531 * what their idiot authors think are the "more important" extensions, 545 * what their idiot authors think are the "more important" extensions,
532 * causing enormous grief in return for some better fake benchmark numbers. 546 * causing enormous grief in return for some better fake benchmark numbers.
540 #else 554 #else
541 #define ECB_GCC_VERSION(major,minor) (__GNUC__ > (major) || (__GNUC__ == (major) && __GNUC_MINOR__ >= (minor))) 555 #define ECB_GCC_VERSION(major,minor) (__GNUC__ > (major) || (__GNUC__ == (major) && __GNUC_MINOR__ >= (minor)))
542 #endif 556 #endif
543#endif 557#endif
544 558
559#define ECB_C (__STDC__+0) /* this assumes that __STDC__ is either empty or a number */
560#define ECB_C99 (__STDC_VERSION__ >= 199901L)
561#define ECB_C11 (__STDC_VERSION__ >= 201112L)
562#define ECB_CPP (__cplusplus+0)
563#define ECB_CPP11 (__cplusplus >= 201103L)
564
565#if ECB_CPP
566 #define ECB_EXTERN_C extern "C"
567 #define ECB_EXTERN_C_BEG ECB_EXTERN_C {
568 #define ECB_EXTERN_C_END }
569#else
570 #define ECB_EXTERN_C extern
571 #define ECB_EXTERN_C_BEG
572 #define ECB_EXTERN_C_END
573#endif
574
545/*****************************************************************************/ 575/*****************************************************************************/
546 576
547/* ECB_NO_THREADS - ecb is not used by multiple threads, ever */ 577/* ECB_NO_THREADS - ecb is not used by multiple threads, ever */
548/* ECB_NO_SMP - ecb might be used in multiple threads, but only on a single cpu */ 578/* ECB_NO_SMP - ecb might be used in multiple threads, but only on a single cpu */
549 579
550#if ECB_NO_THREADS 580#if ECB_NO_THREADS
551# define ECB_NO_SMP 1 581 #define ECB_NO_SMP 1
552#endif 582#endif
553 583
554#if ECB_NO_THREADS || ECB_NO_SMP 584#if ECB_NO_SMP
555 #define ECB_MEMORY_FENCE do { } while (0) 585 #define ECB_MEMORY_FENCE do { } while (0)
556#endif 586#endif
557 587
558#ifndef ECB_MEMORY_FENCE 588#ifndef ECB_MEMORY_FENCE
559 #if ECB_GCC_VERSION(2,5) || defined __INTEL_COMPILER || (__llvm__ && __GNUC__) || __SUNPRO_C >= 0x5110 || __SUNPRO_CC >= 0x5110 589 #if ECB_GCC_VERSION(2,5) || defined __INTEL_COMPILER || (__llvm__ && __GNUC__) || __SUNPRO_C >= 0x5110 || __SUNPRO_CC >= 0x5110
560 #if __i386 || __i386__ 590 #if __i386 || __i386__
561 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("lock; orb $0, -1(%%esp)" : : : "memory") 591 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("lock; orb $0, -1(%%esp)" : : : "memory")
562 #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE /* non-lock xchg might be enough */ 592 #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("" : : : "memory")
563 #define ECB_MEMORY_FENCE_RELEASE do { } while (0) /* unlikely to change in future cpus */ 593 #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("")
564 #elif __amd64 || __amd64__ || __x86_64 || __x86_64__ 594 #elif __amd64 || __amd64__ || __x86_64 || __x86_64__
565 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mfence" : : : "memory") 595 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mfence" : : : "memory")
566 #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("lfence" : : : "memory") 596 #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("" : : : "memory")
567 #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("sfence") /* play safe - not needed in any current cpu */ 597 #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("")
568 #elif __powerpc__ || __ppc__ || __powerpc64__ || __ppc64__ 598 #elif __powerpc__ || __ppc__ || __powerpc64__ || __ppc64__
569 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("sync" : : : "memory") 599 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("sync" : : : "memory")
570 #elif defined __ARM_ARCH_6__ || defined __ARM_ARCH_6J__ \ 600 #elif defined __ARM_ARCH_6__ || defined __ARM_ARCH_6J__ \
571 || defined __ARM_ARCH_6K__ || defined __ARM_ARCH_6ZK__ 601 || defined __ARM_ARCH_6K__ || defined __ARM_ARCH_6ZK__
572 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mcr p15,0,%0,c7,c10,5" : : "r" (0) : "memory") 602 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mcr p15,0,%0,c7,c10,5" : : "r" (0) : "memory")
573 #elif defined __ARM_ARCH_7__ || defined __ARM_ARCH_7A__ \ 603 #elif defined __ARM_ARCH_7__ || defined __ARM_ARCH_7A__ \
574 || defined __ARM_ARCH_7M__ || defined __ARM_ARCH_7R__ 604 || defined __ARM_ARCH_7M__ || defined __ARM_ARCH_7R__
575 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("dmb" : : : "memory") 605 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("dmb" : : : "memory")
576 #elif __sparc || __sparc__ 606 #elif __sparc || __sparc__
577 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("membar #LoadStore | #LoadLoad | #StoreStore | #StoreLoad | " : : : "memory") 607 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("membar #LoadStore | #LoadLoad | #StoreStore | #StoreLoad" : : : "memory")
578 #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("membar #LoadStore | #LoadLoad" : : : "memory") 608 #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("membar #LoadStore | #LoadLoad" : : : "memory")
579 #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("membar #LoadStore | #StoreStore") 609 #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("membar #LoadStore | #StoreStore")
580 #elif defined __s390__ || defined __s390x__ 610 #elif defined __s390__ || defined __s390x__
581 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("bcr 15,0" : : : "memory") 611 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("bcr 15,0" : : : "memory")
582 #elif defined __mips__ 612 #elif defined __mips__
583 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("sync" : : : "memory") 613 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("sync" : : : "memory")
614 #elif defined __alpha__
615 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mb" : : : "memory")
616 #elif defined __hppa__
617 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("" : : : "memory")
618 #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("")
619 #elif defined __ia64__
620 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mf" : : : "memory")
584 #endif 621 #endif
585 #endif 622 #endif
586#endif 623#endif
587 624
588#ifndef ECB_MEMORY_FENCE 625#ifndef ECB_MEMORY_FENCE
626 #if ECB_GCC_VERSION(4,7)
627 /* see comment below (stdatomic.h) about the C11 memory model. */
628 #define ECB_MEMORY_FENCE __atomic_thread_fence (__ATOMIC_SEQ_CST)
629
630 /* The __has_feature syntax from clang is so misdesigned that we cannot use it
631 * without risking compile time errors with other compilers. We *could*
632 * define our own ecb_clang_has_feature, but I just can't be bothered to work
633 * around this shit time and again.
634 * #elif defined __clang && __has_feature (cxx_atomic)
635 * // see comment below (stdatomic.h) about the C11 memory model.
636 * #define ECB_MEMORY_FENCE __c11_atomic_thread_fence (__ATOMIC_SEQ_CST)
637 */
638
589 #if ECB_GCC_VERSION(4,4) || defined __INTEL_COMPILER || defined __clang__ 639 #elif ECB_GCC_VERSION(4,4) || defined __INTEL_COMPILER || defined __clang__
590 #define ECB_MEMORY_FENCE __sync_synchronize () 640 #define ECB_MEMORY_FENCE __sync_synchronize ()
591 /*#define ECB_MEMORY_FENCE_ACQUIRE ({ char dummy = 0; __sync_lock_test_and_set (&dummy, 1); }) */
592 /*#define ECB_MEMORY_FENCE_RELEASE ({ char dummy = 1; __sync_lock_release (&dummy ); }) */
593 #elif _MSC_VER >= 1400 /* VC++ 2005 */ 641 #elif _MSC_VER >= 1400 /* VC++ 2005 */
594 #pragma intrinsic(_ReadBarrier,_WriteBarrier,_ReadWriteBarrier) 642 #pragma intrinsic(_ReadBarrier,_WriteBarrier,_ReadWriteBarrier)
595 #define ECB_MEMORY_FENCE _ReadWriteBarrier () 643 #define ECB_MEMORY_FENCE _ReadWriteBarrier ()
596 #define ECB_MEMORY_FENCE_ACQUIRE _ReadWriteBarrier () /* according to msdn, _ReadBarrier is not a load fence */ 644 #define ECB_MEMORY_FENCE_ACQUIRE _ReadWriteBarrier () /* according to msdn, _ReadBarrier is not a load fence */
597 #define ECB_MEMORY_FENCE_RELEASE _WriteBarrier () 645 #define ECB_MEMORY_FENCE_RELEASE _WriteBarrier ()
607 #define ECB_MEMORY_FENCE __sync () 655 #define ECB_MEMORY_FENCE __sync ()
608 #endif 656 #endif
609#endif 657#endif
610 658
611#ifndef ECB_MEMORY_FENCE 659#ifndef ECB_MEMORY_FENCE
660 #if ECB_C11 && !defined __STDC_NO_ATOMICS__
661 /* we assume that these memory fences work on all variables/all memory accesses, */
662 /* not just C11 atomics and atomic accesses */
663 #include <stdatomic.h>
664 /* Unfortunately, neither gcc 4.7 nor clang 3.1 generate any instructions for */
665 /* any fence other than seq_cst, which isn't very efficient for us. */
666 /* Why that is, we don't know - either the C11 memory model is quite useless */
667 /* for most usages, or gcc and clang have a bug */
668 /* I *currently* lean towards the latter, and inefficiently implement */
669 /* all three of ecb's fences as a seq_cst fence */
670 #define ECB_MEMORY_FENCE atomic_thread_fence (memory_order_seq_cst)
671 #endif
672#endif
673
674#ifndef ECB_MEMORY_FENCE
612 #if !ECB_AVOID_PTHREADS 675 #if !ECB_AVOID_PTHREADS
613 /* 676 /*
614 * if you get undefined symbol references to pthread_mutex_lock, 677 * if you get undefined symbol references to pthread_mutex_lock,
615 * or failure to find pthread.h, then you should implement 678 * or failure to find pthread.h, then you should implement
616 * the ECB_MEMORY_FENCE operations for your cpu/compiler 679 * the ECB_MEMORY_FENCE operations for your cpu/compiler
634 #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE 697 #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE
635#endif 698#endif
636 699
637/*****************************************************************************/ 700/*****************************************************************************/
638 701
639#define ECB_C99 (__STDC_VERSION__ >= 199901L)
640
641#if __cplusplus 702#if __cplusplus
642 #define ecb_inline static inline 703 #define ecb_inline static inline
643#elif ECB_GCC_VERSION(2,5) 704#elif ECB_GCC_VERSION(2,5)
644 #define ecb_inline static __inline__ 705 #define ecb_inline static __inline__
645#elif ECB_C99 706#elif ECB_C99
683#elif ECB_GCC_VERSION(3,0) 744#elif ECB_GCC_VERSION(3,0)
684 #define ecb_decltype(x) __typeof(x) 745 #define ecb_decltype(x) __typeof(x)
685#endif 746#endif
686 747
687#define ecb_noinline ecb_attribute ((__noinline__)) 748#define ecb_noinline ecb_attribute ((__noinline__))
688#define ecb_noreturn ecb_attribute ((__noreturn__))
689#define ecb_unused ecb_attribute ((__unused__)) 749#define ecb_unused ecb_attribute ((__unused__))
690#define ecb_const ecb_attribute ((__const__)) 750#define ecb_const ecb_attribute ((__const__))
691#define ecb_pure ecb_attribute ((__pure__)) 751#define ecb_pure ecb_attribute ((__pure__))
752
753#if ECB_C11
754 #define ecb_noreturn _Noreturn
755#else
756 #define ecb_noreturn ecb_attribute ((__noreturn__))
757#endif
692 758
693#if ECB_GCC_VERSION(4,3) 759#if ECB_GCC_VERSION(4,3)
694 #define ecb_artificial ecb_attribute ((__artificial__)) 760 #define ecb_artificial ecb_attribute ((__artificial__))
695 #define ecb_hot ecb_attribute ((__hot__)) 761 #define ecb_hot ecb_attribute ((__hot__))
696 #define ecb_cold ecb_attribute ((__cold__)) 762 #define ecb_cold ecb_attribute ((__cold__))
787 853
788 return r + ecb_ld32 (x); 854 return r + ecb_ld32 (x);
789 } 855 }
790#endif 856#endif
791 857
858ecb_function_ ecb_bool ecb_is_pot32 (uint32_t x) ecb_const;
859ecb_function_ ecb_bool ecb_is_pot32 (uint32_t x) { return !(x & (x - 1)); }
860ecb_function_ ecb_bool ecb_is_pot64 (uint64_t x) ecb_const;
861ecb_function_ ecb_bool ecb_is_pot64 (uint64_t x) { return !(x & (x - 1)); }
862
792ecb_function_ uint8_t ecb_bitrev8 (uint8_t x) ecb_const; 863ecb_function_ uint8_t ecb_bitrev8 (uint8_t x) ecb_const;
793ecb_function_ uint8_t ecb_bitrev8 (uint8_t x) 864ecb_function_ uint8_t ecb_bitrev8 (uint8_t x)
794{ 865{
795 return ( (x * 0x0802U & 0x22110U) 866 return ( (x * 0x0802U & 0x22110U)
796 | (x * 0x8020U & 0x88440U)) * 0x10101U >> 16; 867 | (x * 0x8020U & 0x88440U)) * 0x10101U >> 16;
880 ecb_inline void ecb_unreachable (void) ecb_noreturn; 951 ecb_inline void ecb_unreachable (void) ecb_noreturn;
881 ecb_inline void ecb_unreachable (void) { } 952 ecb_inline void ecb_unreachable (void) { }
882#endif 953#endif
883 954
884/* try to tell the compiler that some condition is definitely true */ 955/* try to tell the compiler that some condition is definitely true */
885#define ecb_assume(cond) do { if (!(cond)) ecb_unreachable (); } while (0) 956#define ecb_assume(cond) if (!(cond)) ecb_unreachable (); else 0
886 957
887ecb_inline unsigned char ecb_byteorder_helper (void) ecb_const; 958ecb_inline unsigned char ecb_byteorder_helper (void) ecb_const;
888ecb_inline unsigned char 959ecb_inline unsigned char
889ecb_byteorder_helper (void) 960ecb_byteorder_helper (void)
890{ 961{
891 const uint32_t u = 0x11223344; 962 /* the union code still generates code under pressure in gcc, */
892 return *(unsigned char *)&u; 963 /* but less than using pointers, and always seems to */
964 /* successfully return a constant. */
965 /* the reason why we have this horrible preprocessor mess */
966 /* is to avoid it in all cases, at least on common architectures */
967 /* or when using a recent enough gcc version (>= 4.6) */
968#if __i386 || __i386__ || _M_X86 || __amd64 || __amd64__ || _M_X64
969 return 0x44;
970#elif __BYTE_ORDER__ && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
971 return 0x44;
972#elif __BYTE_ORDER__ && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
973 return 0x11;
974#else
975 union
976 {
977 uint32_t i;
978 uint8_t c;
979 } u = { 0x11223344 };
980 return u.c;
981#endif
893} 982}
894 983
895ecb_inline ecb_bool ecb_big_endian (void) ecb_const; 984ecb_inline ecb_bool ecb_big_endian (void) ecb_const;
896ecb_inline ecb_bool ecb_big_endian (void) { return ecb_byteorder_helper () == 0x11; } 985ecb_inline ecb_bool ecb_big_endian (void) { return ecb_byteorder_helper () == 0x11; }
897ecb_inline ecb_bool ecb_little_endian (void) ecb_const; 986ecb_inline ecb_bool ecb_little_endian (void) ecb_const;
928 } 1017 }
929#else 1018#else
930 #define ecb_array_length(name) (sizeof (name) / sizeof (name [0])) 1019 #define ecb_array_length(name) (sizeof (name) / sizeof (name [0]))
931#endif 1020#endif
932 1021
1022/*******************************************************************************/
1023/* floating point stuff, can be disabled by defining ECB_NO_LIBM */
1024
1025/* basically, everything uses "ieee pure-endian" floating point numbers */
1026/* the only noteworthy exception is ancient armle, which uses order 43218765 */
1027#if 0 \
1028 || __i386 || __i386__ \
1029 || __amd64 || __amd64__ || __x86_64 || __x86_64__ \
1030 || __powerpc__ || __ppc__ || __powerpc64__ || __ppc64__ \
1031 || defined __arm__ && defined __ARM_EABI__ \
1032 || defined __s390__ || defined __s390x__ \
1033 || defined __mips__ \
1034 || defined __alpha__ \
1035 || defined __hppa__ \
1036 || defined __ia64__ \
1037 || defined _M_IX86 || defined _M_AMD64 || defined _M_IA64
1038 #define ECB_STDFP 1
1039 #include <string.h> /* for memcpy */
1040#else
1041 #define ECB_STDFP 0
1042 #include <math.h> /* for frexp*, ldexp* */
1043#endif
1044
1045#ifndef ECB_NO_LIBM
1046
1047 /* convert a float to ieee single/binary32 */
1048 ecb_function_ uint32_t ecb_float_to_binary32 (float x) ecb_const;
1049 ecb_function_ uint32_t
1050 ecb_float_to_binary32 (float x)
1051 {
1052 uint32_t r;
1053
1054 #if ECB_STDFP
1055 memcpy (&r, &x, 4);
1056 #else
1057 /* slow emulation, works for anything but -0 */
1058 uint32_t m;
1059 int e;
1060
1061 if (x == 0e0f ) return 0x00000000U;
1062 if (x > +3.40282346638528860e+38f) return 0x7f800000U;
1063 if (x < -3.40282346638528860e+38f) return 0xff800000U;
1064 if (x != x ) return 0x7fbfffffU;
1065
1066 m = frexpf (x, &e) * 0x1000000U;
1067
1068 r = m & 0x80000000U;
1069
1070 if (r)
1071 m = -m;
1072
1073 if (e <= -126)
1074 {
1075 m &= 0xffffffU;
1076 m >>= (-125 - e);
1077 e = -126;
1078 }
1079
1080 r |= (e + 126) << 23;
1081 r |= m & 0x7fffffU;
1082 #endif
1083
1084 return r;
1085 }
1086
1087 /* converts an ieee single/binary32 to a float */
1088 ecb_function_ float ecb_binary32_to_float (uint32_t x) ecb_const;
1089 ecb_function_ float
1090 ecb_binary32_to_float (uint32_t x)
1091 {
1092 float r;
1093
1094 #if ECB_STDFP
1095 memcpy (&r, &x, 4);
1096 #else
1097 /* emulation, only works for normals and subnormals and +0 */
1098 int neg = x >> 31;
1099 int e = (x >> 23) & 0xffU;
1100
1101 x &= 0x7fffffU;
1102
1103 if (e)
1104 x |= 0x800000U;
1105 else
1106 e = 1;
1107
1108 /* we distrust ldexpf a bit and do the 2**-24 scaling by an extra multiply */
1109 r = ldexpf (x * (0.5f / 0x800000U), e - 126);
1110
1111 r = neg ? -r : r;
1112 #endif
1113
1114 return r;
1115 }
1116
1117 /* convert a double to ieee double/binary64 */
1118 ecb_function_ uint64_t ecb_double_to_binary64 (double x) ecb_const;
1119 ecb_function_ uint64_t
1120 ecb_double_to_binary64 (double x)
1121 {
1122 uint64_t r;
1123
1124 #if ECB_STDFP
1125 memcpy (&r, &x, 8);
1126 #else
1127 /* slow emulation, works for anything but -0 */
1128 uint64_t m;
1129 int e;
1130
1131 if (x == 0e0 ) return 0x0000000000000000U;
1132 if (x > +1.79769313486231470e+308) return 0x7ff0000000000000U;
1133 if (x < -1.79769313486231470e+308) return 0xfff0000000000000U;
1134 if (x != x ) return 0X7ff7ffffffffffffU;
1135
1136 m = frexp (x, &e) * 0x20000000000000U;
1137
1138 r = m & 0x8000000000000000;;
1139
1140 if (r)
1141 m = -m;
1142
1143 if (e <= -1022)
1144 {
1145 m &= 0x1fffffffffffffU;
1146 m >>= (-1021 - e);
1147 e = -1022;
1148 }
1149
1150 r |= ((uint64_t)(e + 1022)) << 52;
1151 r |= m & 0xfffffffffffffU;
1152 #endif
1153
1154 return r;
1155 }
1156
1157 /* converts an ieee double/binary64 to a double */
1158 ecb_function_ double ecb_binary64_to_double (uint64_t x) ecb_const;
1159 ecb_function_ double
1160 ecb_binary64_to_double (uint64_t x)
1161 {
1162 double r;
1163
1164 #if ECB_STDFP
1165 memcpy (&r, &x, 8);
1166 #else
1167 /* emulation, only works for normals and subnormals and +0 */
1168 int neg = x >> 63;
1169 int e = (x >> 52) & 0x7ffU;
1170
1171 x &= 0xfffffffffffffU;
1172
1173 if (e)
1174 x |= 0x10000000000000U;
1175 else
1176 e = 1;
1177
1178 /* we distrust ldexp a bit and do the 2**-53 scaling by an extra multiply */
1179 r = ldexp (x * (0.5 / 0x10000000000000U), e - 1022);
1180
1181 r = neg ? -r : r;
1182 #endif
1183
1184 return r;
1185 }
1186
1187#endif
1188
933#endif 1189#endif
934 1190
935/* ECB.H END */ 1191/* ECB.H END */
936 1192
937#if ECB_MEMORY_FENCE_NEEDS_PTHREADS 1193#if ECB_MEMORY_FENCE_NEEDS_PTHREADS
1103{ 1359{
1104 write (STDERR_FILENO, msg, strlen (msg)); 1360 write (STDERR_FILENO, msg, strlen (msg));
1105} 1361}
1106#endif 1362#endif
1107 1363
1108static void (*syserr_cb)(const char *msg); 1364static void (*syserr_cb)(const char *msg) EV_THROW;
1109 1365
1110void ecb_cold 1366void ecb_cold
1111ev_set_syserr_cb (void (*cb)(const char *msg)) 1367ev_set_syserr_cb (void (*cb)(const char *msg) EV_THROW) EV_THROW
1112{ 1368{
1113 syserr_cb = cb; 1369 syserr_cb = cb;
1114} 1370}
1115 1371
1116static void noinline ecb_cold 1372static void noinline ecb_cold
1134 abort (); 1390 abort ();
1135 } 1391 }
1136} 1392}
1137 1393
1138static void * 1394static void *
1139ev_realloc_emul (void *ptr, long size) 1395ev_realloc_emul (void *ptr, long size) EV_THROW
1140{ 1396{
1141#if __GLIBC__
1142 return realloc (ptr, size);
1143#else
1144 /* some systems, notably openbsd and darwin, fail to properly 1397 /* some systems, notably openbsd and darwin, fail to properly
1145 * implement realloc (x, 0) (as required by both ansi c-89 and 1398 * implement realloc (x, 0) (as required by both ansi c-89 and
1146 * the single unix specification, so work around them here. 1399 * the single unix specification, so work around them here.
1400 * recently, also (at least) fedora and debian started breaking it,
1401 * despite documenting it otherwise.
1147 */ 1402 */
1148 1403
1149 if (size) 1404 if (size)
1150 return realloc (ptr, size); 1405 return realloc (ptr, size);
1151 1406
1152 free (ptr); 1407 free (ptr);
1153 return 0; 1408 return 0;
1154#endif
1155} 1409}
1156 1410
1157static void *(*alloc)(void *ptr, long size) = ev_realloc_emul; 1411static void *(*alloc)(void *ptr, long size) EV_THROW = ev_realloc_emul;
1158 1412
1159void ecb_cold 1413void ecb_cold
1160ev_set_allocator (void *(*cb)(void *ptr, long size)) 1414ev_set_allocator (void *(*cb)(void *ptr, long size) EV_THROW) EV_THROW
1161{ 1415{
1162 alloc = cb; 1416 alloc = cb;
1163} 1417}
1164 1418
1165inline_speed void * 1419inline_speed void *
1282 1536
1283/*****************************************************************************/ 1537/*****************************************************************************/
1284 1538
1285#ifndef EV_HAVE_EV_TIME 1539#ifndef EV_HAVE_EV_TIME
1286ev_tstamp 1540ev_tstamp
1287ev_time (void) 1541ev_time (void) EV_THROW
1288{ 1542{
1289#if EV_USE_REALTIME 1543#if EV_USE_REALTIME
1290 if (expect_true (have_realtime)) 1544 if (expect_true (have_realtime))
1291 { 1545 {
1292 struct timespec ts; 1546 struct timespec ts;
1316 return ev_time (); 1570 return ev_time ();
1317} 1571}
1318 1572
1319#if EV_MULTIPLICITY 1573#if EV_MULTIPLICITY
1320ev_tstamp 1574ev_tstamp
1321ev_now (EV_P) 1575ev_now (EV_P) EV_THROW
1322{ 1576{
1323 return ev_rt_now; 1577 return ev_rt_now;
1324} 1578}
1325#endif 1579#endif
1326 1580
1327void 1581void
1328ev_sleep (ev_tstamp delay) 1582ev_sleep (ev_tstamp delay) EV_THROW
1329{ 1583{
1330 if (delay > 0.) 1584 if (delay > 0.)
1331 { 1585 {
1332#if EV_USE_NANOSLEEP 1586#if EV_USE_NANOSLEEP
1333 struct timespec ts; 1587 struct timespec ts;
1414pendingcb (EV_P_ ev_prepare *w, int revents) 1668pendingcb (EV_P_ ev_prepare *w, int revents)
1415{ 1669{
1416} 1670}
1417 1671
1418void noinline 1672void noinline
1419ev_feed_event (EV_P_ void *w, int revents) 1673ev_feed_event (EV_P_ void *w, int revents) EV_THROW
1420{ 1674{
1421 W w_ = (W)w; 1675 W w_ = (W)w;
1422 int pri = ABSPRI (w_); 1676 int pri = ABSPRI (w_);
1423 1677
1424 if (expect_false (w_->pending)) 1678 if (expect_false (w_->pending))
1428 w_->pending = ++pendingcnt [pri]; 1682 w_->pending = ++pendingcnt [pri];
1429 array_needsize (ANPENDING, pendings [pri], pendingmax [pri], w_->pending, EMPTY2); 1683 array_needsize (ANPENDING, pendings [pri], pendingmax [pri], w_->pending, EMPTY2);
1430 pendings [pri][w_->pending - 1].w = w_; 1684 pendings [pri][w_->pending - 1].w = w_;
1431 pendings [pri][w_->pending - 1].events = revents; 1685 pendings [pri][w_->pending - 1].events = revents;
1432 } 1686 }
1687
1688 pendingpri = NUMPRI - 1;
1433} 1689}
1434 1690
1435inline_speed void 1691inline_speed void
1436feed_reverse (EV_P_ W w) 1692feed_reverse (EV_P_ W w)
1437{ 1693{
1483 if (expect_true (!anfd->reify)) 1739 if (expect_true (!anfd->reify))
1484 fd_event_nocheck (EV_A_ fd, revents); 1740 fd_event_nocheck (EV_A_ fd, revents);
1485} 1741}
1486 1742
1487void 1743void
1488ev_feed_fd_event (EV_P_ int fd, int revents) 1744ev_feed_fd_event (EV_P_ int fd, int revents) EV_THROW
1489{ 1745{
1490 if (fd >= 0 && fd < anfdmax) 1746 if (fd >= 0 && fd < anfdmax)
1491 fd_event_nocheck (EV_A_ fd, revents); 1747 fd_event_nocheck (EV_A_ fd, revents);
1492} 1748}
1493 1749
1812static void noinline ecb_cold 2068static void noinline ecb_cold
1813evpipe_init (EV_P) 2069evpipe_init (EV_P)
1814{ 2070{
1815 if (!ev_is_active (&pipe_w)) 2071 if (!ev_is_active (&pipe_w))
1816 { 2072 {
2073 int fds [2];
2074
1817# if EV_USE_EVENTFD 2075# if EV_USE_EVENTFD
2076 fds [0] = -1;
1818 evfd = eventfd (0, EFD_NONBLOCK | EFD_CLOEXEC); 2077 fds [1] = eventfd (0, EFD_NONBLOCK | EFD_CLOEXEC);
1819 if (evfd < 0 && errno == EINVAL) 2078 if (fds [1] < 0 && errno == EINVAL)
1820 evfd = eventfd (0, 0); 2079 fds [1] = eventfd (0, 0);
1821 2080
1822 if (evfd >= 0) 2081 if (fds [1] < 0)
2082# endif
1823 { 2083 {
2084 while (pipe (fds))
2085 ev_syserr ("(libev) error creating signal/async pipe");
2086
2087 fd_intern (fds [0]);
2088 }
2089
2090 fd_intern (fds [1]);
2091
1824 evpipe [0] = -1; 2092 evpipe [0] = fds [0];
1825 fd_intern (evfd); /* doing it twice doesn't hurt */ 2093
1826 ev_io_set (&pipe_w, evfd, EV_READ); 2094 if (evpipe [1] < 0)
2095 evpipe [1] = fds [1]; /* first call, set write fd */
2096 else
2097 {
2098 /* on subsequent calls, do not change evpipe [1] */
2099 /* so that evpipe_write can always rely on its value. */
2100 /* this branch does not do anything sensible on windows, */
2101 /* so must not be executed on windows */
2102
2103 dup2 (fds [1], evpipe [1]);
2104 close (fds [1]);
2105 }
2106
2107 ev_io_set (&pipe_w, evpipe [0] < 0 ? evpipe [1] : evpipe [0], EV_READ);
2108 ev_io_start (EV_A_ &pipe_w);
2109 ev_unref (EV_A); /* watcher should not keep loop alive */
2110 }
2111}
2112
2113inline_speed void
2114evpipe_write (EV_P_ EV_ATOMIC_T *flag)
2115{
2116 ECB_MEMORY_FENCE; /* push out the write before this function was called, acquire flag */
2117
2118 if (expect_true (*flag))
2119 return;
2120
2121 *flag = 1;
2122 ECB_MEMORY_FENCE_RELEASE; /* make sure flag is visible before the wakeup */
2123
2124 pipe_write_skipped = 1;
2125
2126 ECB_MEMORY_FENCE; /* make sure pipe_write_skipped is visible before we check pipe_write_wanted */
2127
2128 if (pipe_write_wanted)
2129 {
2130 int old_errno;
2131
2132 pipe_write_skipped = 0;
2133 ECB_MEMORY_FENCE_RELEASE;
2134
2135 old_errno = errno; /* save errno because write will clobber it */
2136
2137#if EV_USE_EVENTFD
2138 if (evpipe [0] < 0)
2139 {
2140 uint64_t counter = 1;
2141 write (evpipe [1], &counter, sizeof (uint64_t));
1827 } 2142 }
1828 else 2143 else
1829# endif 2144#endif
1830 { 2145 {
1831 while (pipe (evpipe)) 2146#ifdef _WIN32
1832 ev_syserr ("(libev) error creating signal/async pipe"); 2147 WSABUF buf;
1833 2148 DWORD sent;
1834 fd_intern (evpipe [0]); 2149 buf.buf = &buf;
1835 fd_intern (evpipe [1]); 2150 buf.len = 1;
1836 ev_io_set (&pipe_w, evpipe [0], EV_READ); 2151 WSASend (EV_FD_TO_WIN32_HANDLE (evpipe [1]), &buf, 1, &sent, 0, 0, 0);
1837 } 2152#else
1838
1839 ev_io_start (EV_A_ &pipe_w);
1840 ev_unref (EV_A); /* watcher should not keep loop alive */
1841 }
1842}
1843
1844inline_speed void
1845evpipe_write (EV_P_ EV_ATOMIC_T *flag)
1846{
1847 if (expect_true (*flag))
1848 return;
1849
1850 *flag = 1;
1851
1852 ECB_MEMORY_FENCE_RELEASE; /* make sure flag is visible before the wakeup */
1853
1854 pipe_write_skipped = 1;
1855
1856 ECB_MEMORY_FENCE; /* make sure pipe_write_skipped is visible before we check pipe_write_wanted */
1857
1858 if (pipe_write_wanted)
1859 {
1860 int old_errno;
1861
1862 pipe_write_skipped = 0; /* just an optimisation, no fence needed */
1863
1864 old_errno = errno; /* save errno because write will clobber it */
1865
1866#if EV_USE_EVENTFD
1867 if (evfd >= 0)
1868 {
1869 uint64_t counter = 1;
1870 write (evfd, &counter, sizeof (uint64_t));
1871 }
1872 else
1873#endif
1874 {
1875 /* win32 people keep sending patches that change this write() to send() */
1876 /* and then run away. but send() is wrong, it wants a socket handle on win32 */
1877 /* so when you think this write should be a send instead, please find out */
1878 /* where your send() is from - it's definitely not the microsoft send, and */
1879 /* tell me. thank you. */
1880 /* it might be that your problem is that your environment needs EV_USE_WSASOCKET */
1881 /* check the ev documentation on how to use this flag */
1882 write (evpipe [1], &(evpipe [1]), 1); 2153 write (evpipe [1], &(evpipe [1]), 1);
2154#endif
1883 } 2155 }
1884 2156
1885 errno = old_errno; 2157 errno = old_errno;
1886 } 2158 }
1887} 2159}
1894 int i; 2166 int i;
1895 2167
1896 if (revents & EV_READ) 2168 if (revents & EV_READ)
1897 { 2169 {
1898#if EV_USE_EVENTFD 2170#if EV_USE_EVENTFD
1899 if (evfd >= 0) 2171 if (evpipe [0] < 0)
1900 { 2172 {
1901 uint64_t counter; 2173 uint64_t counter;
1902 read (evfd, &counter, sizeof (uint64_t)); 2174 read (evpipe [1], &counter, sizeof (uint64_t));
1903 } 2175 }
1904 else 2176 else
1905#endif 2177#endif
1906 { 2178 {
1907 char dummy; 2179 char dummy[4];
1908 /* see discussion in evpipe_write when you think this read should be recv in win32 */ 2180#ifdef _WIN32
2181 WSABUF buf;
2182 DWORD recvd;
2183 DWORD flags = 0;
2184 buf.buf = dummy;
2185 buf.len = sizeof (dummy);
2186 WSARecv (EV_FD_TO_WIN32_HANDLE (evpipe [0]), &buf, 1, &recvd, &flags, 0, 0);
2187#else
1909 read (evpipe [0], &dummy, 1); 2188 read (evpipe [0], &dummy, sizeof (dummy));
2189#endif
1910 } 2190 }
1911 } 2191 }
1912 2192
1913 pipe_write_skipped = 0; 2193 pipe_write_skipped = 0;
2194
2195 ECB_MEMORY_FENCE; /* push out skipped, acquire flags */
1914 2196
1915#if EV_SIGNAL_ENABLE 2197#if EV_SIGNAL_ENABLE
1916 if (sig_pending) 2198 if (sig_pending)
1917 { 2199 {
1918 sig_pending = 0; 2200 sig_pending = 0;
2201
2202 ECB_MEMORY_FENCE;
1919 2203
1920 for (i = EV_NSIG - 1; i--; ) 2204 for (i = EV_NSIG - 1; i--; )
1921 if (expect_false (signals [i].pending)) 2205 if (expect_false (signals [i].pending))
1922 ev_feed_signal_event (EV_A_ i + 1); 2206 ev_feed_signal_event (EV_A_ i + 1);
1923 } 2207 }
1925 2209
1926#if EV_ASYNC_ENABLE 2210#if EV_ASYNC_ENABLE
1927 if (async_pending) 2211 if (async_pending)
1928 { 2212 {
1929 async_pending = 0; 2213 async_pending = 0;
2214
2215 ECB_MEMORY_FENCE;
1930 2216
1931 for (i = asynccnt; i--; ) 2217 for (i = asynccnt; i--; )
1932 if (asyncs [i]->sent) 2218 if (asyncs [i]->sent)
1933 { 2219 {
1934 asyncs [i]->sent = 0; 2220 asyncs [i]->sent = 0;
2221 ECB_MEMORY_FENCE_RELEASE;
1935 ev_feed_event (EV_A_ asyncs [i], EV_ASYNC); 2222 ev_feed_event (EV_A_ asyncs [i], EV_ASYNC);
1936 } 2223 }
1937 } 2224 }
1938#endif 2225#endif
1939} 2226}
1940 2227
1941/*****************************************************************************/ 2228/*****************************************************************************/
1942 2229
1943void 2230void
1944ev_feed_signal (int signum) 2231ev_feed_signal (int signum) EV_THROW
1945{ 2232{
1946#if EV_MULTIPLICITY 2233#if EV_MULTIPLICITY
2234 ECB_MEMORY_FENCE_ACQUIRE;
1947 EV_P = signals [signum - 1].loop; 2235 EV_P = signals [signum - 1].loop;
1948 2236
1949 if (!EV_A) 2237 if (!EV_A)
1950 return; 2238 return;
1951#endif 2239#endif
1952 2240
1953 if (!ev_active (&pipe_w))
1954 return;
1955
1956 signals [signum - 1].pending = 1; 2241 signals [signum - 1].pending = 1;
1957 evpipe_write (EV_A_ &sig_pending); 2242 evpipe_write (EV_A_ &sig_pending);
1958} 2243}
1959 2244
1960static void 2245static void
1966 2251
1967 ev_feed_signal (signum); 2252 ev_feed_signal (signum);
1968} 2253}
1969 2254
1970void noinline 2255void noinline
1971ev_feed_signal_event (EV_P_ int signum) 2256ev_feed_signal_event (EV_P_ int signum) EV_THROW
1972{ 2257{
1973 WL w; 2258 WL w;
1974 2259
1975 if (expect_false (signum <= 0 || signum > EV_NSIG)) 2260 if (expect_false (signum <= 0 || signum >= EV_NSIG))
1976 return; 2261 return;
1977 2262
1978 --signum; 2263 --signum;
1979 2264
1980#if EV_MULTIPLICITY 2265#if EV_MULTIPLICITY
1984 if (expect_false (signals [signum].loop != EV_A)) 2269 if (expect_false (signals [signum].loop != EV_A))
1985 return; 2270 return;
1986#endif 2271#endif
1987 2272
1988 signals [signum].pending = 0; 2273 signals [signum].pending = 0;
2274 ECB_MEMORY_FENCE_RELEASE;
1989 2275
1990 for (w = signals [signum].head; w; w = w->next) 2276 for (w = signals [signum].head; w; w = w->next)
1991 ev_feed_event (EV_A_ (W)w, EV_SIGNAL); 2277 ev_feed_event (EV_A_ (W)w, EV_SIGNAL);
1992} 2278}
1993 2279
2092#if EV_USE_SELECT 2378#if EV_USE_SELECT
2093# include "ev_select.c" 2379# include "ev_select.c"
2094#endif 2380#endif
2095 2381
2096int ecb_cold 2382int ecb_cold
2097ev_version_major (void) 2383ev_version_major (void) EV_THROW
2098{ 2384{
2099 return EV_VERSION_MAJOR; 2385 return EV_VERSION_MAJOR;
2100} 2386}
2101 2387
2102int ecb_cold 2388int ecb_cold
2103ev_version_minor (void) 2389ev_version_minor (void) EV_THROW
2104{ 2390{
2105 return EV_VERSION_MINOR; 2391 return EV_VERSION_MINOR;
2106} 2392}
2107 2393
2108/* return true if we are running with elevated privileges and should ignore env variables */ 2394/* return true if we are running with elevated privileges and should ignore env variables */
2116 || getgid () != getegid (); 2402 || getgid () != getegid ();
2117#endif 2403#endif
2118} 2404}
2119 2405
2120unsigned int ecb_cold 2406unsigned int ecb_cold
2121ev_supported_backends (void) 2407ev_supported_backends (void) EV_THROW
2122{ 2408{
2123 unsigned int flags = 0; 2409 unsigned int flags = 0;
2124 2410
2125 if (EV_USE_PORT ) flags |= EVBACKEND_PORT; 2411 if (EV_USE_PORT ) flags |= EVBACKEND_PORT;
2126 if (EV_USE_KQUEUE) flags |= EVBACKEND_KQUEUE; 2412 if (EV_USE_KQUEUE) flags |= EVBACKEND_KQUEUE;
2130 2416
2131 return flags; 2417 return flags;
2132} 2418}
2133 2419
2134unsigned int ecb_cold 2420unsigned int ecb_cold
2135ev_recommended_backends (void) 2421ev_recommended_backends (void) EV_THROW
2136{ 2422{
2137 unsigned int flags = ev_supported_backends (); 2423 unsigned int flags = ev_supported_backends ();
2138 2424
2139#ifndef __NetBSD__ 2425#ifndef __NetBSD__
2140 /* kqueue is borked on everything but netbsd apparently */ 2426 /* kqueue is borked on everything but netbsd apparently */
2152 2438
2153 return flags; 2439 return flags;
2154} 2440}
2155 2441
2156unsigned int ecb_cold 2442unsigned int ecb_cold
2157ev_embeddable_backends (void) 2443ev_embeddable_backends (void) EV_THROW
2158{ 2444{
2159 int flags = EVBACKEND_EPOLL | EVBACKEND_KQUEUE | EVBACKEND_PORT; 2445 int flags = EVBACKEND_EPOLL | EVBACKEND_KQUEUE | EVBACKEND_PORT;
2160 2446
2161 /* epoll embeddability broken on all linux versions up to at least 2.6.23 */ 2447 /* epoll embeddability broken on all linux versions up to at least 2.6.23 */
2162 if (ev_linux_version () < 0x020620) /* disable it on linux < 2.6.32 */ 2448 if (ev_linux_version () < 0x020620) /* disable it on linux < 2.6.32 */
2164 2450
2165 return flags; 2451 return flags;
2166} 2452}
2167 2453
2168unsigned int 2454unsigned int
2169ev_backend (EV_P) 2455ev_backend (EV_P) EV_THROW
2170{ 2456{
2171 return backend; 2457 return backend;
2172} 2458}
2173 2459
2174#if EV_FEATURE_API 2460#if EV_FEATURE_API
2175unsigned int 2461unsigned int
2176ev_iteration (EV_P) 2462ev_iteration (EV_P) EV_THROW
2177{ 2463{
2178 return loop_count; 2464 return loop_count;
2179} 2465}
2180 2466
2181unsigned int 2467unsigned int
2182ev_depth (EV_P) 2468ev_depth (EV_P) EV_THROW
2183{ 2469{
2184 return loop_depth; 2470 return loop_depth;
2185} 2471}
2186 2472
2187void 2473void
2188ev_set_io_collect_interval (EV_P_ ev_tstamp interval) 2474ev_set_io_collect_interval (EV_P_ ev_tstamp interval) EV_THROW
2189{ 2475{
2190 io_blocktime = interval; 2476 io_blocktime = interval;
2191} 2477}
2192 2478
2193void 2479void
2194ev_set_timeout_collect_interval (EV_P_ ev_tstamp interval) 2480ev_set_timeout_collect_interval (EV_P_ ev_tstamp interval) EV_THROW
2195{ 2481{
2196 timeout_blocktime = interval; 2482 timeout_blocktime = interval;
2197} 2483}
2198 2484
2199void 2485void
2200ev_set_userdata (EV_P_ void *data) 2486ev_set_userdata (EV_P_ void *data) EV_THROW
2201{ 2487{
2202 userdata = data; 2488 userdata = data;
2203} 2489}
2204 2490
2205void * 2491void *
2206ev_userdata (EV_P) 2492ev_userdata (EV_P) EV_THROW
2207{ 2493{
2208 return userdata; 2494 return userdata;
2209} 2495}
2210 2496
2211void 2497void
2212ev_set_invoke_pending_cb (EV_P_ void (*invoke_pending_cb)(EV_P)) 2498ev_set_invoke_pending_cb (EV_P_ void (*invoke_pending_cb)(EV_P)) EV_THROW
2213{ 2499{
2214 invoke_cb = invoke_pending_cb; 2500 invoke_cb = invoke_pending_cb;
2215} 2501}
2216 2502
2217void 2503void
2218ev_set_loop_release_cb (EV_P_ void (*release)(EV_P), void (*acquire)(EV_P)) 2504ev_set_loop_release_cb (EV_P_ void (*release)(EV_P) EV_THROW, void (*acquire)(EV_P) EV_THROW) EV_THROW
2219{ 2505{
2220 release_cb = release; 2506 release_cb = release;
2221 acquire_cb = acquire; 2507 acquire_cb = acquire;
2222} 2508}
2223#endif 2509#endif
2224 2510
2225/* initialise a loop structure, must be zero-initialised */ 2511/* initialise a loop structure, must be zero-initialised */
2226static void noinline ecb_cold 2512static void noinline ecb_cold
2227loop_init (EV_P_ unsigned int flags) 2513loop_init (EV_P_ unsigned int flags) EV_THROW
2228{ 2514{
2229 if (!backend) 2515 if (!backend)
2230 { 2516 {
2231 origflags = flags; 2517 origflags = flags;
2232 2518
2277#if EV_ASYNC_ENABLE 2563#if EV_ASYNC_ENABLE
2278 async_pending = 0; 2564 async_pending = 0;
2279#endif 2565#endif
2280 pipe_write_skipped = 0; 2566 pipe_write_skipped = 0;
2281 pipe_write_wanted = 0; 2567 pipe_write_wanted = 0;
2568 evpipe [0] = -1;
2569 evpipe [1] = -1;
2282#if EV_USE_INOTIFY 2570#if EV_USE_INOTIFY
2283 fs_fd = flags & EVFLAG_NOINOTIFY ? -1 : -2; 2571 fs_fd = flags & EVFLAG_NOINOTIFY ? -1 : -2;
2284#endif 2572#endif
2285#if EV_USE_SIGNALFD 2573#if EV_USE_SIGNALFD
2286 sigfd = flags & EVFLAG_SIGNALFD ? -2 : -1; 2574 sigfd = flags & EVFLAG_SIGNALFD ? -2 : -1;
2337 EV_INVOKE_PENDING; 2625 EV_INVOKE_PENDING;
2338 } 2626 }
2339#endif 2627#endif
2340 2628
2341#if EV_CHILD_ENABLE 2629#if EV_CHILD_ENABLE
2342 if (ev_is_active (&childev)) 2630 if (ev_is_default_loop (EV_A) && ev_is_active (&childev))
2343 { 2631 {
2344 ev_ref (EV_A); /* child watcher */ 2632 ev_ref (EV_A); /* child watcher */
2345 ev_signal_stop (EV_A_ &childev); 2633 ev_signal_stop (EV_A_ &childev);
2346 } 2634 }
2347#endif 2635#endif
2349 if (ev_is_active (&pipe_w)) 2637 if (ev_is_active (&pipe_w))
2350 { 2638 {
2351 /*ev_ref (EV_A);*/ 2639 /*ev_ref (EV_A);*/
2352 /*ev_io_stop (EV_A_ &pipe_w);*/ 2640 /*ev_io_stop (EV_A_ &pipe_w);*/
2353 2641
2354#if EV_USE_EVENTFD
2355 if (evfd >= 0)
2356 close (evfd);
2357#endif
2358
2359 if (evpipe [0] >= 0)
2360 {
2361 EV_WIN32_CLOSE_FD (evpipe [0]); 2642 if (evpipe [0] >= 0) EV_WIN32_CLOSE_FD (evpipe [0]);
2362 EV_WIN32_CLOSE_FD (evpipe [1]); 2643 if (evpipe [1] >= 0) EV_WIN32_CLOSE_FD (evpipe [1]);
2363 }
2364 } 2644 }
2365 2645
2366#if EV_USE_SIGNALFD 2646#if EV_USE_SIGNALFD
2367 if (ev_is_active (&sigfd_w)) 2647 if (ev_is_active (&sigfd_w))
2368 close (sigfd); 2648 close (sigfd);
2454#endif 2734#endif
2455#if EV_USE_INOTIFY 2735#if EV_USE_INOTIFY
2456 infy_fork (EV_A); 2736 infy_fork (EV_A);
2457#endif 2737#endif
2458 2738
2739#if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE
2459 if (ev_is_active (&pipe_w)) 2740 if (ev_is_active (&pipe_w))
2460 { 2741 {
2461 /* pipe_write_wanted must be false now, so modifying fd vars should be safe */ 2742 /* pipe_write_wanted must be false now, so modifying fd vars should be safe */
2462 2743
2463 ev_ref (EV_A); 2744 ev_ref (EV_A);
2464 ev_io_stop (EV_A_ &pipe_w); 2745 ev_io_stop (EV_A_ &pipe_w);
2465 2746
2466#if EV_USE_EVENTFD
2467 if (evfd >= 0)
2468 close (evfd);
2469#endif
2470
2471 if (evpipe [0] >= 0) 2747 if (evpipe [0] >= 0)
2472 {
2473 EV_WIN32_CLOSE_FD (evpipe [0]); 2748 EV_WIN32_CLOSE_FD (evpipe [0]);
2474 EV_WIN32_CLOSE_FD (evpipe [1]);
2475 }
2476 2749
2477#if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE
2478 evpipe_init (EV_A); 2750 evpipe_init (EV_A);
2479 /* now iterate over everything, in case we missed something */ 2751 /* iterate over everything, in case we missed something before */
2480 pipecb (EV_A_ &pipe_w, EV_READ); 2752 ev_feed_event (EV_A_ &pipe_w, EV_CUSTOM);
2481#endif
2482 } 2753 }
2754#endif
2483 2755
2484 postfork = 0; 2756 postfork = 0;
2485} 2757}
2486 2758
2487#if EV_MULTIPLICITY 2759#if EV_MULTIPLICITY
2488 2760
2489struct ev_loop * ecb_cold 2761struct ev_loop * ecb_cold
2490ev_loop_new (unsigned int flags) 2762ev_loop_new (unsigned int flags) EV_THROW
2491{ 2763{
2492 EV_P = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop)); 2764 EV_P = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop));
2493 2765
2494 memset (EV_A, 0, sizeof (struct ev_loop)); 2766 memset (EV_A, 0, sizeof (struct ev_loop));
2495 loop_init (EV_A_ flags); 2767 loop_init (EV_A_ flags);
2539} 2811}
2540#endif 2812#endif
2541 2813
2542#if EV_FEATURE_API 2814#if EV_FEATURE_API
2543void ecb_cold 2815void ecb_cold
2544ev_verify (EV_P) 2816ev_verify (EV_P) EV_THROW
2545{ 2817{
2546#if EV_VERIFY 2818#if EV_VERIFY
2547 int i; 2819 int i;
2548 WL w; 2820 WL w, w2;
2549 2821
2550 assert (activecnt >= -1); 2822 assert (activecnt >= -1);
2551 2823
2552 assert (fdchangemax >= fdchangecnt); 2824 assert (fdchangemax >= fdchangecnt);
2553 for (i = 0; i < fdchangecnt; ++i) 2825 for (i = 0; i < fdchangecnt; ++i)
2554 assert (("libev: negative fd in fdchanges", fdchanges [i] >= 0)); 2826 assert (("libev: negative fd in fdchanges", fdchanges [i] >= 0));
2555 2827
2556 assert (anfdmax >= 0); 2828 assert (anfdmax >= 0);
2557 for (i = 0; i < anfdmax; ++i) 2829 for (i = 0; i < anfdmax; ++i)
2830 {
2831 int j = 0;
2832
2558 for (w = anfds [i].head; w; w = w->next) 2833 for (w = w2 = anfds [i].head; w; w = w->next)
2559 { 2834 {
2560 verify_watcher (EV_A_ (W)w); 2835 verify_watcher (EV_A_ (W)w);
2836
2837 if (j++ & 1)
2838 {
2839 assert (("libev: io watcher list contains a loop", w != w2));
2840 w2 = w2->next;
2841 }
2842
2561 assert (("libev: inactive fd watcher on anfd list", ev_active (w) == 1)); 2843 assert (("libev: inactive fd watcher on anfd list", ev_active (w) == 1));
2562 assert (("libev: fd mismatch between watcher and anfd", ((ev_io *)w)->fd == i)); 2844 assert (("libev: fd mismatch between watcher and anfd", ((ev_io *)w)->fd == i));
2563 } 2845 }
2846 }
2564 2847
2565 assert (timermax >= timercnt); 2848 assert (timermax >= timercnt);
2566 verify_heap (EV_A_ timers, timercnt); 2849 verify_heap (EV_A_ timers, timercnt);
2567 2850
2568#if EV_PERIODIC_ENABLE 2851#if EV_PERIODIC_ENABLE
2618#if EV_MULTIPLICITY 2901#if EV_MULTIPLICITY
2619struct ev_loop * ecb_cold 2902struct ev_loop * ecb_cold
2620#else 2903#else
2621int 2904int
2622#endif 2905#endif
2623ev_default_loop (unsigned int flags) 2906ev_default_loop (unsigned int flags) EV_THROW
2624{ 2907{
2625 if (!ev_default_loop_ptr) 2908 if (!ev_default_loop_ptr)
2626 { 2909 {
2627#if EV_MULTIPLICITY 2910#if EV_MULTIPLICITY
2628 EV_P = ev_default_loop_ptr = &default_loop_struct; 2911 EV_P = ev_default_loop_ptr = &default_loop_struct;
2647 2930
2648 return ev_default_loop_ptr; 2931 return ev_default_loop_ptr;
2649} 2932}
2650 2933
2651void 2934void
2652ev_loop_fork (EV_P) 2935ev_loop_fork (EV_P) EV_THROW
2653{ 2936{
2654 postfork = 1; /* must be in line with ev_default_fork */ 2937 postfork = 1;
2655} 2938}
2656 2939
2657/*****************************************************************************/ 2940/*****************************************************************************/
2658 2941
2659void 2942void
2661{ 2944{
2662 EV_CB_INVOKE ((W)w, revents); 2945 EV_CB_INVOKE ((W)w, revents);
2663} 2946}
2664 2947
2665unsigned int 2948unsigned int
2666ev_pending_count (EV_P) 2949ev_pending_count (EV_P) EV_THROW
2667{ 2950{
2668 int pri; 2951 int pri;
2669 unsigned int count = 0; 2952 unsigned int count = 0;
2670 2953
2671 for (pri = NUMPRI; pri--; ) 2954 for (pri = NUMPRI; pri--; )
2675} 2958}
2676 2959
2677void noinline 2960void noinline
2678ev_invoke_pending (EV_P) 2961ev_invoke_pending (EV_P)
2679{ 2962{
2680 int pri; 2963 pendingpri = NUMPRI;
2681 2964
2682 for (pri = NUMPRI; pri--; ) 2965 while (pendingpri) /* pendingpri possibly gets modified in the inner loop */
2966 {
2967 --pendingpri;
2968
2683 while (pendingcnt [pri]) 2969 while (pendingcnt [pendingpri])
2684 { 2970 {
2685 ANPENDING *p = pendings [pri] + --pendingcnt [pri]; 2971 ANPENDING *p = pendings [pendingpri] + --pendingcnt [pendingpri];
2686 2972
2687 p->w->pending = 0; 2973 p->w->pending = 0;
2688 EV_CB_INVOKE (p->w, p->events); 2974 EV_CB_INVOKE (p->w, p->events);
2689 EV_FREQUENT_CHECK; 2975 EV_FREQUENT_CHECK;
2690 } 2976 }
2977 }
2691} 2978}
2692 2979
2693#if EV_IDLE_ENABLE 2980#if EV_IDLE_ENABLE
2694/* make idle watchers pending. this handles the "call-idle */ 2981/* make idle watchers pending. this handles the "call-idle */
2695/* only when higher priorities are idle" logic */ 2982/* only when higher priorities are idle" logic */
2785{ 3072{
2786 EV_FREQUENT_CHECK; 3073 EV_FREQUENT_CHECK;
2787 3074
2788 while (periodiccnt && ANHE_at (periodics [HEAP0]) < ev_rt_now) 3075 while (periodiccnt && ANHE_at (periodics [HEAP0]) < ev_rt_now)
2789 { 3076 {
2790 int feed_count = 0;
2791
2792 do 3077 do
2793 { 3078 {
2794 ev_periodic *w = (ev_periodic *)ANHE_w (periodics [HEAP0]); 3079 ev_periodic *w = (ev_periodic *)ANHE_w (periodics [HEAP0]);
2795 3080
2796 /*assert (("libev: inactive timer on periodic heap detected", ev_is_active (w)));*/ 3081 /*assert (("libev: inactive timer on periodic heap detected", ev_is_active (w)));*/
3055 backend_poll (EV_A_ waittime); 3340 backend_poll (EV_A_ waittime);
3056 assert ((loop_done = EVBREAK_CANCEL, 1)); /* assert for side effect */ 3341 assert ((loop_done = EVBREAK_CANCEL, 1)); /* assert for side effect */
3057 3342
3058 pipe_write_wanted = 0; /* just an optimisation, no fence needed */ 3343 pipe_write_wanted = 0; /* just an optimisation, no fence needed */
3059 3344
3345 ECB_MEMORY_FENCE_ACQUIRE;
3060 if (pipe_write_skipped) 3346 if (pipe_write_skipped)
3061 { 3347 {
3062 assert (("libev: pipe_w not active, but pipe not written", ev_is_active (&pipe_w))); 3348 assert (("libev: pipe_w not active, but pipe not written", ev_is_active (&pipe_w)));
3063 ev_feed_event (EV_A_ &pipe_w, EV_CUSTOM); 3349 ev_feed_event (EV_A_ &pipe_w, EV_CUSTOM);
3064 } 3350 }
3102 3388
3103 return activecnt; 3389 return activecnt;
3104} 3390}
3105 3391
3106void 3392void
3107ev_break (EV_P_ int how) 3393ev_break (EV_P_ int how) EV_THROW
3108{ 3394{
3109 loop_done = how; 3395 loop_done = how;
3110} 3396}
3111 3397
3112void 3398void
3113ev_ref (EV_P) 3399ev_ref (EV_P) EV_THROW
3114{ 3400{
3115 ++activecnt; 3401 ++activecnt;
3116} 3402}
3117 3403
3118void 3404void
3119ev_unref (EV_P) 3405ev_unref (EV_P) EV_THROW
3120{ 3406{
3121 --activecnt; 3407 --activecnt;
3122} 3408}
3123 3409
3124void 3410void
3125ev_now_update (EV_P) 3411ev_now_update (EV_P) EV_THROW
3126{ 3412{
3127 time_update (EV_A_ 1e100); 3413 time_update (EV_A_ 1e100);
3128} 3414}
3129 3415
3130void 3416void
3131ev_suspend (EV_P) 3417ev_suspend (EV_P) EV_THROW
3132{ 3418{
3133 ev_now_update (EV_A); 3419 ev_now_update (EV_A);
3134} 3420}
3135 3421
3136void 3422void
3137ev_resume (EV_P) 3423ev_resume (EV_P) EV_THROW
3138{ 3424{
3139 ev_tstamp mn_prev = mn_now; 3425 ev_tstamp mn_prev = mn_now;
3140 3426
3141 ev_now_update (EV_A); 3427 ev_now_update (EV_A);
3142 timers_reschedule (EV_A_ mn_now - mn_prev); 3428 timers_reschedule (EV_A_ mn_now - mn_prev);
3181 w->pending = 0; 3467 w->pending = 0;
3182 } 3468 }
3183} 3469}
3184 3470
3185int 3471int
3186ev_clear_pending (EV_P_ void *w) 3472ev_clear_pending (EV_P_ void *w) EV_THROW
3187{ 3473{
3188 W w_ = (W)w; 3474 W w_ = (W)w;
3189 int pending = w_->pending; 3475 int pending = w_->pending;
3190 3476
3191 if (expect_true (pending)) 3477 if (expect_true (pending))
3224} 3510}
3225 3511
3226/*****************************************************************************/ 3512/*****************************************************************************/
3227 3513
3228void noinline 3514void noinline
3229ev_io_start (EV_P_ ev_io *w) 3515ev_io_start (EV_P_ ev_io *w) EV_THROW
3230{ 3516{
3231 int fd = w->fd; 3517 int fd = w->fd;
3232 3518
3233 if (expect_false (ev_is_active (w))) 3519 if (expect_false (ev_is_active (w)))
3234 return; 3520 return;
3240 3526
3241 ev_start (EV_A_ (W)w, 1); 3527 ev_start (EV_A_ (W)w, 1);
3242 array_needsize (ANFD, anfds, anfdmax, fd + 1, array_init_zero); 3528 array_needsize (ANFD, anfds, anfdmax, fd + 1, array_init_zero);
3243 wlist_add (&anfds[fd].head, (WL)w); 3529 wlist_add (&anfds[fd].head, (WL)w);
3244 3530
3531 /* common bug, apparently */
3532 assert (("libev: ev_io_start called with corrupted watcher", ((WL)w)->next != (WL)w));
3533
3245 fd_change (EV_A_ fd, w->events & EV__IOFDSET | EV_ANFD_REIFY); 3534 fd_change (EV_A_ fd, w->events & EV__IOFDSET | EV_ANFD_REIFY);
3246 w->events &= ~EV__IOFDSET; 3535 w->events &= ~EV__IOFDSET;
3247 3536
3248 EV_FREQUENT_CHECK; 3537 EV_FREQUENT_CHECK;
3249} 3538}
3250 3539
3251void noinline 3540void noinline
3252ev_io_stop (EV_P_ ev_io *w) 3541ev_io_stop (EV_P_ ev_io *w) EV_THROW
3253{ 3542{
3254 clear_pending (EV_A_ (W)w); 3543 clear_pending (EV_A_ (W)w);
3255 if (expect_false (!ev_is_active (w))) 3544 if (expect_false (!ev_is_active (w)))
3256 return; 3545 return;
3257 3546
3266 3555
3267 EV_FREQUENT_CHECK; 3556 EV_FREQUENT_CHECK;
3268} 3557}
3269 3558
3270void noinline 3559void noinline
3271ev_timer_start (EV_P_ ev_timer *w) 3560ev_timer_start (EV_P_ ev_timer *w) EV_THROW
3272{ 3561{
3273 if (expect_false (ev_is_active (w))) 3562 if (expect_false (ev_is_active (w)))
3274 return; 3563 return;
3275 3564
3276 ev_at (w) += mn_now; 3565 ev_at (w) += mn_now;
3290 3579
3291 /*assert (("libev: internal timer heap corruption", timers [ev_active (w)] == (WT)w));*/ 3580 /*assert (("libev: internal timer heap corruption", timers [ev_active (w)] == (WT)w));*/
3292} 3581}
3293 3582
3294void noinline 3583void noinline
3295ev_timer_stop (EV_P_ ev_timer *w) 3584ev_timer_stop (EV_P_ ev_timer *w) EV_THROW
3296{ 3585{
3297 clear_pending (EV_A_ (W)w); 3586 clear_pending (EV_A_ (W)w);
3298 if (expect_false (!ev_is_active (w))) 3587 if (expect_false (!ev_is_active (w)))
3299 return; 3588 return;
3300 3589
3320 3609
3321 EV_FREQUENT_CHECK; 3610 EV_FREQUENT_CHECK;
3322} 3611}
3323 3612
3324void noinline 3613void noinline
3325ev_timer_again (EV_P_ ev_timer *w) 3614ev_timer_again (EV_P_ ev_timer *w) EV_THROW
3326{ 3615{
3327 EV_FREQUENT_CHECK; 3616 EV_FREQUENT_CHECK;
3328 3617
3329 clear_pending (EV_A_ (W)w); 3618 clear_pending (EV_A_ (W)w);
3330 3619
3347 3636
3348 EV_FREQUENT_CHECK; 3637 EV_FREQUENT_CHECK;
3349} 3638}
3350 3639
3351ev_tstamp 3640ev_tstamp
3352ev_timer_remaining (EV_P_ ev_timer *w) 3641ev_timer_remaining (EV_P_ ev_timer *w) EV_THROW
3353{ 3642{
3354 return ev_at (w) - (ev_is_active (w) ? mn_now : 0.); 3643 return ev_at (w) - (ev_is_active (w) ? mn_now : 0.);
3355} 3644}
3356 3645
3357#if EV_PERIODIC_ENABLE 3646#if EV_PERIODIC_ENABLE
3358void noinline 3647void noinline
3359ev_periodic_start (EV_P_ ev_periodic *w) 3648ev_periodic_start (EV_P_ ev_periodic *w) EV_THROW
3360{ 3649{
3361 if (expect_false (ev_is_active (w))) 3650 if (expect_false (ev_is_active (w)))
3362 return; 3651 return;
3363 3652
3364 if (w->reschedule_cb) 3653 if (w->reschedule_cb)
3384 3673
3385 /*assert (("libev: internal periodic heap corruption", ANHE_w (periodics [ev_active (w)]) == (WT)w));*/ 3674 /*assert (("libev: internal periodic heap corruption", ANHE_w (periodics [ev_active (w)]) == (WT)w));*/
3386} 3675}
3387 3676
3388void noinline 3677void noinline
3389ev_periodic_stop (EV_P_ ev_periodic *w) 3678ev_periodic_stop (EV_P_ ev_periodic *w) EV_THROW
3390{ 3679{
3391 clear_pending (EV_A_ (W)w); 3680 clear_pending (EV_A_ (W)w);
3392 if (expect_false (!ev_is_active (w))) 3681 if (expect_false (!ev_is_active (w)))
3393 return; 3682 return;
3394 3683
3412 3701
3413 EV_FREQUENT_CHECK; 3702 EV_FREQUENT_CHECK;
3414} 3703}
3415 3704
3416void noinline 3705void noinline
3417ev_periodic_again (EV_P_ ev_periodic *w) 3706ev_periodic_again (EV_P_ ev_periodic *w) EV_THROW
3418{ 3707{
3419 /* TODO: use adjustheap and recalculation */ 3708 /* TODO: use adjustheap and recalculation */
3420 ev_periodic_stop (EV_A_ w); 3709 ev_periodic_stop (EV_A_ w);
3421 ev_periodic_start (EV_A_ w); 3710 ev_periodic_start (EV_A_ w);
3422} 3711}
3427#endif 3716#endif
3428 3717
3429#if EV_SIGNAL_ENABLE 3718#if EV_SIGNAL_ENABLE
3430 3719
3431void noinline 3720void noinline
3432ev_signal_start (EV_P_ ev_signal *w) 3721ev_signal_start (EV_P_ ev_signal *w) EV_THROW
3433{ 3722{
3434 if (expect_false (ev_is_active (w))) 3723 if (expect_false (ev_is_active (w)))
3435 return; 3724 return;
3436 3725
3437 assert (("libev: ev_signal_start called with illegal signal number", w->signum > 0 && w->signum < EV_NSIG)); 3726 assert (("libev: ev_signal_start called with illegal signal number", w->signum > 0 && w->signum < EV_NSIG));
3439#if EV_MULTIPLICITY 3728#if EV_MULTIPLICITY
3440 assert (("libev: a signal must not be attached to two different loops", 3729 assert (("libev: a signal must not be attached to two different loops",
3441 !signals [w->signum - 1].loop || signals [w->signum - 1].loop == loop)); 3730 !signals [w->signum - 1].loop || signals [w->signum - 1].loop == loop));
3442 3731
3443 signals [w->signum - 1].loop = EV_A; 3732 signals [w->signum - 1].loop = EV_A;
3733 ECB_MEMORY_FENCE_RELEASE;
3444#endif 3734#endif
3445 3735
3446 EV_FREQUENT_CHECK; 3736 EV_FREQUENT_CHECK;
3447 3737
3448#if EV_USE_SIGNALFD 3738#if EV_USE_SIGNALFD
3508 3798
3509 EV_FREQUENT_CHECK; 3799 EV_FREQUENT_CHECK;
3510} 3800}
3511 3801
3512void noinline 3802void noinline
3513ev_signal_stop (EV_P_ ev_signal *w) 3803ev_signal_stop (EV_P_ ev_signal *w) EV_THROW
3514{ 3804{
3515 clear_pending (EV_A_ (W)w); 3805 clear_pending (EV_A_ (W)w);
3516 if (expect_false (!ev_is_active (w))) 3806 if (expect_false (!ev_is_active (w)))
3517 return; 3807 return;
3518 3808
3549#endif 3839#endif
3550 3840
3551#if EV_CHILD_ENABLE 3841#if EV_CHILD_ENABLE
3552 3842
3553void 3843void
3554ev_child_start (EV_P_ ev_child *w) 3844ev_child_start (EV_P_ ev_child *w) EV_THROW
3555{ 3845{
3556#if EV_MULTIPLICITY 3846#if EV_MULTIPLICITY
3557 assert (("libev: child watchers are only supported in the default loop", loop == ev_default_loop_ptr)); 3847 assert (("libev: child watchers are only supported in the default loop", loop == ev_default_loop_ptr));
3558#endif 3848#endif
3559 if (expect_false (ev_is_active (w))) 3849 if (expect_false (ev_is_active (w)))
3566 3856
3567 EV_FREQUENT_CHECK; 3857 EV_FREQUENT_CHECK;
3568} 3858}
3569 3859
3570void 3860void
3571ev_child_stop (EV_P_ ev_child *w) 3861ev_child_stop (EV_P_ ev_child *w) EV_THROW
3572{ 3862{
3573 clear_pending (EV_A_ (W)w); 3863 clear_pending (EV_A_ (W)w);
3574 if (expect_false (!ev_is_active (w))) 3864 if (expect_false (!ev_is_active (w)))
3575 return; 3865 return;
3576 3866
3603# define EV_INOTIFY_BUFSIZE (sizeof (struct inotify_event) * 2 + NAME_MAX) 3893# define EV_INOTIFY_BUFSIZE (sizeof (struct inotify_event) * 2 + NAME_MAX)
3604 3894
3605static void noinline 3895static void noinline
3606infy_add (EV_P_ ev_stat *w) 3896infy_add (EV_P_ ev_stat *w)
3607{ 3897{
3608 w->wd = inotify_add_watch (fs_fd, w->path, IN_ATTRIB | IN_DELETE_SELF | IN_MOVE_SELF | IN_MODIFY | IN_DONT_FOLLOW | IN_MASK_ADD); 3898 w->wd = inotify_add_watch (fs_fd, w->path,
3899 IN_ATTRIB | IN_DELETE_SELF | IN_MOVE_SELF | IN_MODIFY
3900 | IN_CREATE | IN_DELETE | IN_MOVED_FROM | IN_MOVED_TO
3901 | IN_DONT_FOLLOW | IN_MASK_ADD);
3609 3902
3610 if (w->wd >= 0) 3903 if (w->wd >= 0)
3611 { 3904 {
3612 struct statfs sfs; 3905 struct statfs sfs;
3613 3906
3617 3910
3618 if (!fs_2625) 3911 if (!fs_2625)
3619 w->timer.repeat = w->interval ? w->interval : DEF_STAT_INTERVAL; 3912 w->timer.repeat = w->interval ? w->interval : DEF_STAT_INTERVAL;
3620 else if (!statfs (w->path, &sfs) 3913 else if (!statfs (w->path, &sfs)
3621 && (sfs.f_type == 0x1373 /* devfs */ 3914 && (sfs.f_type == 0x1373 /* devfs */
3915 || sfs.f_type == 0x4006 /* fat */
3916 || sfs.f_type == 0x4d44 /* msdos */
3622 || sfs.f_type == 0xEF53 /* ext2/3 */ 3917 || sfs.f_type == 0xEF53 /* ext2/3 */
3918 || sfs.f_type == 0x72b6 /* jffs2 */
3919 || sfs.f_type == 0x858458f6 /* ramfs */
3920 || sfs.f_type == 0x5346544e /* ntfs */
3623 || sfs.f_type == 0x3153464a /* jfs */ 3921 || sfs.f_type == 0x3153464a /* jfs */
3922 || sfs.f_type == 0x9123683e /* btrfs */
3624 || sfs.f_type == 0x52654973 /* reiser3 */ 3923 || sfs.f_type == 0x52654973 /* reiser3 */
3625 || sfs.f_type == 0x01021994 /* tempfs */ 3924 || sfs.f_type == 0x01021994 /* tmpfs */
3626 || sfs.f_type == 0x58465342 /* xfs */)) 3925 || sfs.f_type == 0x58465342 /* xfs */))
3627 w->timer.repeat = 0.; /* filesystem is local, kernel new enough */ 3926 w->timer.repeat = 0.; /* filesystem is local, kernel new enough */
3628 else 3927 else
3629 w->timer.repeat = w->interval ? w->interval : NFS_STAT_INTERVAL; /* remote, use reduced frequency */ 3928 w->timer.repeat = w->interval ? w->interval : NFS_STAT_INTERVAL; /* remote, use reduced frequency */
3630 } 3929 }
3828#else 4127#else
3829# define EV_LSTAT(p,b) lstat (p, b) 4128# define EV_LSTAT(p,b) lstat (p, b)
3830#endif 4129#endif
3831 4130
3832void 4131void
3833ev_stat_stat (EV_P_ ev_stat *w) 4132ev_stat_stat (EV_P_ ev_stat *w) EV_THROW
3834{ 4133{
3835 if (lstat (w->path, &w->attr) < 0) 4134 if (lstat (w->path, &w->attr) < 0)
3836 w->attr.st_nlink = 0; 4135 w->attr.st_nlink = 0;
3837 else if (!w->attr.st_nlink) 4136 else if (!w->attr.st_nlink)
3838 w->attr.st_nlink = 1; 4137 w->attr.st_nlink = 1;
3877 ev_feed_event (EV_A_ w, EV_STAT); 4176 ev_feed_event (EV_A_ w, EV_STAT);
3878 } 4177 }
3879} 4178}
3880 4179
3881void 4180void
3882ev_stat_start (EV_P_ ev_stat *w) 4181ev_stat_start (EV_P_ ev_stat *w) EV_THROW
3883{ 4182{
3884 if (expect_false (ev_is_active (w))) 4183 if (expect_false (ev_is_active (w)))
3885 return; 4184 return;
3886 4185
3887 ev_stat_stat (EV_A_ w); 4186 ev_stat_stat (EV_A_ w);
3908 4207
3909 EV_FREQUENT_CHECK; 4208 EV_FREQUENT_CHECK;
3910} 4209}
3911 4210
3912void 4211void
3913ev_stat_stop (EV_P_ ev_stat *w) 4212ev_stat_stop (EV_P_ ev_stat *w) EV_THROW
3914{ 4213{
3915 clear_pending (EV_A_ (W)w); 4214 clear_pending (EV_A_ (W)w);
3916 if (expect_false (!ev_is_active (w))) 4215 if (expect_false (!ev_is_active (w)))
3917 return; 4216 return;
3918 4217
3934} 4233}
3935#endif 4234#endif
3936 4235
3937#if EV_IDLE_ENABLE 4236#if EV_IDLE_ENABLE
3938void 4237void
3939ev_idle_start (EV_P_ ev_idle *w) 4238ev_idle_start (EV_P_ ev_idle *w) EV_THROW
3940{ 4239{
3941 if (expect_false (ev_is_active (w))) 4240 if (expect_false (ev_is_active (w)))
3942 return; 4241 return;
3943 4242
3944 pri_adjust (EV_A_ (W)w); 4243 pri_adjust (EV_A_ (W)w);
3957 4256
3958 EV_FREQUENT_CHECK; 4257 EV_FREQUENT_CHECK;
3959} 4258}
3960 4259
3961void 4260void
3962ev_idle_stop (EV_P_ ev_idle *w) 4261ev_idle_stop (EV_P_ ev_idle *w) EV_THROW
3963{ 4262{
3964 clear_pending (EV_A_ (W)w); 4263 clear_pending (EV_A_ (W)w);
3965 if (expect_false (!ev_is_active (w))) 4264 if (expect_false (!ev_is_active (w)))
3966 return; 4265 return;
3967 4266
3981} 4280}
3982#endif 4281#endif
3983 4282
3984#if EV_PREPARE_ENABLE 4283#if EV_PREPARE_ENABLE
3985void 4284void
3986ev_prepare_start (EV_P_ ev_prepare *w) 4285ev_prepare_start (EV_P_ ev_prepare *w) EV_THROW
3987{ 4286{
3988 if (expect_false (ev_is_active (w))) 4287 if (expect_false (ev_is_active (w)))
3989 return; 4288 return;
3990 4289
3991 EV_FREQUENT_CHECK; 4290 EV_FREQUENT_CHECK;
3996 4295
3997 EV_FREQUENT_CHECK; 4296 EV_FREQUENT_CHECK;
3998} 4297}
3999 4298
4000void 4299void
4001ev_prepare_stop (EV_P_ ev_prepare *w) 4300ev_prepare_stop (EV_P_ ev_prepare *w) EV_THROW
4002{ 4301{
4003 clear_pending (EV_A_ (W)w); 4302 clear_pending (EV_A_ (W)w);
4004 if (expect_false (!ev_is_active (w))) 4303 if (expect_false (!ev_is_active (w)))
4005 return; 4304 return;
4006 4305
4019} 4318}
4020#endif 4319#endif
4021 4320
4022#if EV_CHECK_ENABLE 4321#if EV_CHECK_ENABLE
4023void 4322void
4024ev_check_start (EV_P_ ev_check *w) 4323ev_check_start (EV_P_ ev_check *w) EV_THROW
4025{ 4324{
4026 if (expect_false (ev_is_active (w))) 4325 if (expect_false (ev_is_active (w)))
4027 return; 4326 return;
4028 4327
4029 EV_FREQUENT_CHECK; 4328 EV_FREQUENT_CHECK;
4034 4333
4035 EV_FREQUENT_CHECK; 4334 EV_FREQUENT_CHECK;
4036} 4335}
4037 4336
4038void 4337void
4039ev_check_stop (EV_P_ ev_check *w) 4338ev_check_stop (EV_P_ ev_check *w) EV_THROW
4040{ 4339{
4041 clear_pending (EV_A_ (W)w); 4340 clear_pending (EV_A_ (W)w);
4042 if (expect_false (!ev_is_active (w))) 4341 if (expect_false (!ev_is_active (w)))
4043 return; 4342 return;
4044 4343
4057} 4356}
4058#endif 4357#endif
4059 4358
4060#if EV_EMBED_ENABLE 4359#if EV_EMBED_ENABLE
4061void noinline 4360void noinline
4062ev_embed_sweep (EV_P_ ev_embed *w) 4361ev_embed_sweep (EV_P_ ev_embed *w) EV_THROW
4063{ 4362{
4064 ev_run (w->other, EVRUN_NOWAIT); 4363 ev_run (w->other, EVRUN_NOWAIT);
4065} 4364}
4066 4365
4067static void 4366static void
4115 ev_idle_stop (EV_A_ idle); 4414 ev_idle_stop (EV_A_ idle);
4116} 4415}
4117#endif 4416#endif
4118 4417
4119void 4418void
4120ev_embed_start (EV_P_ ev_embed *w) 4419ev_embed_start (EV_P_ ev_embed *w) EV_THROW
4121{ 4420{
4122 if (expect_false (ev_is_active (w))) 4421 if (expect_false (ev_is_active (w)))
4123 return; 4422 return;
4124 4423
4125 { 4424 {
4146 4445
4147 EV_FREQUENT_CHECK; 4446 EV_FREQUENT_CHECK;
4148} 4447}
4149 4448
4150void 4449void
4151ev_embed_stop (EV_P_ ev_embed *w) 4450ev_embed_stop (EV_P_ ev_embed *w) EV_THROW
4152{ 4451{
4153 clear_pending (EV_A_ (W)w); 4452 clear_pending (EV_A_ (W)w);
4154 if (expect_false (!ev_is_active (w))) 4453 if (expect_false (!ev_is_active (w)))
4155 return; 4454 return;
4156 4455
4166} 4465}
4167#endif 4466#endif
4168 4467
4169#if EV_FORK_ENABLE 4468#if EV_FORK_ENABLE
4170void 4469void
4171ev_fork_start (EV_P_ ev_fork *w) 4470ev_fork_start (EV_P_ ev_fork *w) EV_THROW
4172{ 4471{
4173 if (expect_false (ev_is_active (w))) 4472 if (expect_false (ev_is_active (w)))
4174 return; 4473 return;
4175 4474
4176 EV_FREQUENT_CHECK; 4475 EV_FREQUENT_CHECK;
4181 4480
4182 EV_FREQUENT_CHECK; 4481 EV_FREQUENT_CHECK;
4183} 4482}
4184 4483
4185void 4484void
4186ev_fork_stop (EV_P_ ev_fork *w) 4485ev_fork_stop (EV_P_ ev_fork *w) EV_THROW
4187{ 4486{
4188 clear_pending (EV_A_ (W)w); 4487 clear_pending (EV_A_ (W)w);
4189 if (expect_false (!ev_is_active (w))) 4488 if (expect_false (!ev_is_active (w)))
4190 return; 4489 return;
4191 4490
4204} 4503}
4205#endif 4504#endif
4206 4505
4207#if EV_CLEANUP_ENABLE 4506#if EV_CLEANUP_ENABLE
4208void 4507void
4209ev_cleanup_start (EV_P_ ev_cleanup *w) 4508ev_cleanup_start (EV_P_ ev_cleanup *w) EV_THROW
4210{ 4509{
4211 if (expect_false (ev_is_active (w))) 4510 if (expect_false (ev_is_active (w)))
4212 return; 4511 return;
4213 4512
4214 EV_FREQUENT_CHECK; 4513 EV_FREQUENT_CHECK;
4221 ev_unref (EV_A); 4520 ev_unref (EV_A);
4222 EV_FREQUENT_CHECK; 4521 EV_FREQUENT_CHECK;
4223} 4522}
4224 4523
4225void 4524void
4226ev_cleanup_stop (EV_P_ ev_cleanup *w) 4525ev_cleanup_stop (EV_P_ ev_cleanup *w) EV_THROW
4227{ 4526{
4228 clear_pending (EV_A_ (W)w); 4527 clear_pending (EV_A_ (W)w);
4229 if (expect_false (!ev_is_active (w))) 4528 if (expect_false (!ev_is_active (w)))
4230 return; 4529 return;
4231 4530
4245} 4544}
4246#endif 4545#endif
4247 4546
4248#if EV_ASYNC_ENABLE 4547#if EV_ASYNC_ENABLE
4249void 4548void
4250ev_async_start (EV_P_ ev_async *w) 4549ev_async_start (EV_P_ ev_async *w) EV_THROW
4251{ 4550{
4252 if (expect_false (ev_is_active (w))) 4551 if (expect_false (ev_is_active (w)))
4253 return; 4552 return;
4254 4553
4255 w->sent = 0; 4554 w->sent = 0;
4264 4563
4265 EV_FREQUENT_CHECK; 4564 EV_FREQUENT_CHECK;
4266} 4565}
4267 4566
4268void 4567void
4269ev_async_stop (EV_P_ ev_async *w) 4568ev_async_stop (EV_P_ ev_async *w) EV_THROW
4270{ 4569{
4271 clear_pending (EV_A_ (W)w); 4570 clear_pending (EV_A_ (W)w);
4272 if (expect_false (!ev_is_active (w))) 4571 if (expect_false (!ev_is_active (w)))
4273 return; 4572 return;
4274 4573
4285 4584
4286 EV_FREQUENT_CHECK; 4585 EV_FREQUENT_CHECK;
4287} 4586}
4288 4587
4289void 4588void
4290ev_async_send (EV_P_ ev_async *w) 4589ev_async_send (EV_P_ ev_async *w) EV_THROW
4291{ 4590{
4292 w->sent = 1; 4591 w->sent = 1;
4293 evpipe_write (EV_A_ &async_pending); 4592 evpipe_write (EV_A_ &async_pending);
4294} 4593}
4295#endif 4594#endif
4332 4631
4333 once_cb (EV_A_ once, revents | ev_clear_pending (EV_A_ &once->io)); 4632 once_cb (EV_A_ once, revents | ev_clear_pending (EV_A_ &once->io));
4334} 4633}
4335 4634
4336void 4635void
4337ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) 4636ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) EV_THROW
4338{ 4637{
4339 struct ev_once *once = (struct ev_once *)ev_malloc (sizeof (struct ev_once)); 4638 struct ev_once *once = (struct ev_once *)ev_malloc (sizeof (struct ev_once));
4340 4639
4341 if (expect_false (!once)) 4640 if (expect_false (!once))
4342 { 4641 {
4364 4663
4365/*****************************************************************************/ 4664/*****************************************************************************/
4366 4665
4367#if EV_WALK_ENABLE 4666#if EV_WALK_ENABLE
4368void ecb_cold 4667void ecb_cold
4369ev_walk (EV_P_ int types, void (*cb)(EV_P_ int type, void *w)) 4668ev_walk (EV_P_ int types, void (*cb)(EV_P_ int type, void *w)) EV_THROW
4370{ 4669{
4371 int i, j; 4670 int i, j;
4372 ev_watcher_list *wl, *wn; 4671 ev_watcher_list *wl, *wn;
4373 4672
4374 if (types & (EV_IO | EV_EMBED)) 4673 if (types & (EV_IO | EV_EMBED))

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines