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

Comparing libev/ev.c (file contents):
Revision 1.419 by root, Thu Apr 12 04:10:15 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")
584 #elif defined __alpha__ 614 #elif defined __alpha__
585 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mb" : : : "memory") 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")
586 #endif 621 #endif
587 #endif 622 #endif
588#endif 623#endif
589 624
590#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
591 #if ECB_GCC_VERSION(4,4) || defined __INTEL_COMPILER || defined __clang__ 639 #elif ECB_GCC_VERSION(4,4) || defined __INTEL_COMPILER || defined __clang__
592 #define ECB_MEMORY_FENCE __sync_synchronize () 640 #define ECB_MEMORY_FENCE __sync_synchronize ()
593 /*#define ECB_MEMORY_FENCE_ACQUIRE ({ char dummy = 0; __sync_lock_test_and_set (&dummy, 1); }) */
594 /*#define ECB_MEMORY_FENCE_RELEASE ({ char dummy = 1; __sync_lock_release (&dummy ); }) */
595 #elif _MSC_VER >= 1400 /* VC++ 2005 */ 641 #elif _MSC_VER >= 1400 /* VC++ 2005 */
596 #pragma intrinsic(_ReadBarrier,_WriteBarrier,_ReadWriteBarrier) 642 #pragma intrinsic(_ReadBarrier,_WriteBarrier,_ReadWriteBarrier)
597 #define ECB_MEMORY_FENCE _ReadWriteBarrier () 643 #define ECB_MEMORY_FENCE _ReadWriteBarrier ()
598 #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 */
599 #define ECB_MEMORY_FENCE_RELEASE _WriteBarrier () 645 #define ECB_MEMORY_FENCE_RELEASE _WriteBarrier ()
609 #define ECB_MEMORY_FENCE __sync () 655 #define ECB_MEMORY_FENCE __sync ()
610 #endif 656 #endif
611#endif 657#endif
612 658
613#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
614 #if !ECB_AVOID_PTHREADS 675 #if !ECB_AVOID_PTHREADS
615 /* 676 /*
616 * if you get undefined symbol references to pthread_mutex_lock, 677 * if you get undefined symbol references to pthread_mutex_lock,
617 * or failure to find pthread.h, then you should implement 678 * or failure to find pthread.h, then you should implement
618 * the ECB_MEMORY_FENCE operations for your cpu/compiler 679 * the ECB_MEMORY_FENCE operations for your cpu/compiler
636 #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE 697 #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE
637#endif 698#endif
638 699
639/*****************************************************************************/ 700/*****************************************************************************/
640 701
641#define ECB_C99 (__STDC_VERSION__ >= 199901L)
642
643#if __cplusplus 702#if __cplusplus
644 #define ecb_inline static inline 703 #define ecb_inline static inline
645#elif ECB_GCC_VERSION(2,5) 704#elif ECB_GCC_VERSION(2,5)
646 #define ecb_inline static __inline__ 705 #define ecb_inline static __inline__
647#elif ECB_C99 706#elif ECB_C99
685#elif ECB_GCC_VERSION(3,0) 744#elif ECB_GCC_VERSION(3,0)
686 #define ecb_decltype(x) __typeof(x) 745 #define ecb_decltype(x) __typeof(x)
687#endif 746#endif
688 747
689#define ecb_noinline ecb_attribute ((__noinline__)) 748#define ecb_noinline ecb_attribute ((__noinline__))
690#define ecb_noreturn ecb_attribute ((__noreturn__))
691#define ecb_unused ecb_attribute ((__unused__)) 749#define ecb_unused ecb_attribute ((__unused__))
692#define ecb_const ecb_attribute ((__const__)) 750#define ecb_const ecb_attribute ((__const__))
693#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
694 758
695#if ECB_GCC_VERSION(4,3) 759#if ECB_GCC_VERSION(4,3)
696 #define ecb_artificial ecb_attribute ((__artificial__)) 760 #define ecb_artificial ecb_attribute ((__artificial__))
697 #define ecb_hot ecb_attribute ((__hot__)) 761 #define ecb_hot ecb_attribute ((__hot__))
698 #define ecb_cold ecb_attribute ((__cold__)) 762 #define ecb_cold ecb_attribute ((__cold__))
789 853
790 return r + ecb_ld32 (x); 854 return r + ecb_ld32 (x);
791 } 855 }
792#endif 856#endif
793 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
794ecb_function_ uint8_t ecb_bitrev8 (uint8_t x) ecb_const; 863ecb_function_ uint8_t ecb_bitrev8 (uint8_t x) ecb_const;
795ecb_function_ uint8_t ecb_bitrev8 (uint8_t x) 864ecb_function_ uint8_t ecb_bitrev8 (uint8_t x)
796{ 865{
797 return ( (x * 0x0802U & 0x22110U) 866 return ( (x * 0x0802U & 0x22110U)
798 | (x * 0x8020U & 0x88440U)) * 0x10101U >> 16; 867 | (x * 0x8020U & 0x88440U)) * 0x10101U >> 16;
882 ecb_inline void ecb_unreachable (void) ecb_noreturn; 951 ecb_inline void ecb_unreachable (void) ecb_noreturn;
883 ecb_inline void ecb_unreachable (void) { } 952 ecb_inline void ecb_unreachable (void) { }
884#endif 953#endif
885 954
886/* try to tell the compiler that some condition is definitely true */ 955/* try to tell the compiler that some condition is definitely true */
887#define ecb_assume(cond) do { if (!(cond)) ecb_unreachable (); } while (0) 956#define ecb_assume(cond) if (!(cond)) ecb_unreachable (); else 0
888 957
889ecb_inline unsigned char ecb_byteorder_helper (void) ecb_const; 958ecb_inline unsigned char ecb_byteorder_helper (void) ecb_const;
890ecb_inline unsigned char 959ecb_inline unsigned char
891ecb_byteorder_helper (void) 960ecb_byteorder_helper (void)
892{ 961{
893 const uint32_t u = 0x11223344; 962 /* the union code still generates code under pressure in gcc, */
894 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
895} 982}
896 983
897ecb_inline ecb_bool ecb_big_endian (void) ecb_const; 984ecb_inline ecb_bool ecb_big_endian (void) ecb_const;
898ecb_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; }
899ecb_inline ecb_bool ecb_little_endian (void) ecb_const; 986ecb_inline ecb_bool ecb_little_endian (void) ecb_const;
930 } 1017 }
931#else 1018#else
932 #define ecb_array_length(name) (sizeof (name) / sizeof (name [0])) 1019 #define ecb_array_length(name) (sizeof (name) / sizeof (name [0]))
933#endif 1020#endif
934 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
935#endif 1189#endif
936 1190
937/* ECB.H END */ 1191/* ECB.H END */
938 1192
939#if ECB_MEMORY_FENCE_NEEDS_PTHREADS 1193#if ECB_MEMORY_FENCE_NEEDS_PTHREADS
1105{ 1359{
1106 write (STDERR_FILENO, msg, strlen (msg)); 1360 write (STDERR_FILENO, msg, strlen (msg));
1107} 1361}
1108#endif 1362#endif
1109 1363
1110static void (*syserr_cb)(const char *msg); 1364static void (*syserr_cb)(const char *msg) EV_THROW;
1111 1365
1112void ecb_cold 1366void ecb_cold
1113ev_set_syserr_cb (void (*cb)(const char *msg)) 1367ev_set_syserr_cb (void (*cb)(const char *msg) EV_THROW) EV_THROW
1114{ 1368{
1115 syserr_cb = cb; 1369 syserr_cb = cb;
1116} 1370}
1117 1371
1118static void noinline ecb_cold 1372static void noinline ecb_cold
1136 abort (); 1390 abort ();
1137 } 1391 }
1138} 1392}
1139 1393
1140static void * 1394static void *
1141ev_realloc_emul (void *ptr, long size) 1395ev_realloc_emul (void *ptr, long size) EV_THROW
1142{ 1396{
1143#if __GLIBC__
1144 return realloc (ptr, size);
1145#else
1146 /* some systems, notably openbsd and darwin, fail to properly 1397 /* some systems, notably openbsd and darwin, fail to properly
1147 * 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
1148 * 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.
1149 */ 1402 */
1150 1403
1151 if (size) 1404 if (size)
1152 return realloc (ptr, size); 1405 return realloc (ptr, size);
1153 1406
1154 free (ptr); 1407 free (ptr);
1155 return 0; 1408 return 0;
1156#endif
1157} 1409}
1158 1410
1159static void *(*alloc)(void *ptr, long size) = ev_realloc_emul; 1411static void *(*alloc)(void *ptr, long size) EV_THROW = ev_realloc_emul;
1160 1412
1161void ecb_cold 1413void ecb_cold
1162ev_set_allocator (void *(*cb)(void *ptr, long size)) 1414ev_set_allocator (void *(*cb)(void *ptr, long size) EV_THROW) EV_THROW
1163{ 1415{
1164 alloc = cb; 1416 alloc = cb;
1165} 1417}
1166 1418
1167inline_speed void * 1419inline_speed void *
1284 1536
1285/*****************************************************************************/ 1537/*****************************************************************************/
1286 1538
1287#ifndef EV_HAVE_EV_TIME 1539#ifndef EV_HAVE_EV_TIME
1288ev_tstamp 1540ev_tstamp
1289ev_time (void) 1541ev_time (void) EV_THROW
1290{ 1542{
1291#if EV_USE_REALTIME 1543#if EV_USE_REALTIME
1292 if (expect_true (have_realtime)) 1544 if (expect_true (have_realtime))
1293 { 1545 {
1294 struct timespec ts; 1546 struct timespec ts;
1318 return ev_time (); 1570 return ev_time ();
1319} 1571}
1320 1572
1321#if EV_MULTIPLICITY 1573#if EV_MULTIPLICITY
1322ev_tstamp 1574ev_tstamp
1323ev_now (EV_P) 1575ev_now (EV_P) EV_THROW
1324{ 1576{
1325 return ev_rt_now; 1577 return ev_rt_now;
1326} 1578}
1327#endif 1579#endif
1328 1580
1329void 1581void
1330ev_sleep (ev_tstamp delay) 1582ev_sleep (ev_tstamp delay) EV_THROW
1331{ 1583{
1332 if (delay > 0.) 1584 if (delay > 0.)
1333 { 1585 {
1334#if EV_USE_NANOSLEEP 1586#if EV_USE_NANOSLEEP
1335 struct timespec ts; 1587 struct timespec ts;
1416pendingcb (EV_P_ ev_prepare *w, int revents) 1668pendingcb (EV_P_ ev_prepare *w, int revents)
1417{ 1669{
1418} 1670}
1419 1671
1420void noinline 1672void noinline
1421ev_feed_event (EV_P_ void *w, int revents) 1673ev_feed_event (EV_P_ void *w, int revents) EV_THROW
1422{ 1674{
1423 W w_ = (W)w; 1675 W w_ = (W)w;
1424 int pri = ABSPRI (w_); 1676 int pri = ABSPRI (w_);
1425 1677
1426 if (expect_false (w_->pending)) 1678 if (expect_false (w_->pending))
1430 w_->pending = ++pendingcnt [pri]; 1682 w_->pending = ++pendingcnt [pri];
1431 array_needsize (ANPENDING, pendings [pri], pendingmax [pri], w_->pending, EMPTY2); 1683 array_needsize (ANPENDING, pendings [pri], pendingmax [pri], w_->pending, EMPTY2);
1432 pendings [pri][w_->pending - 1].w = w_; 1684 pendings [pri][w_->pending - 1].w = w_;
1433 pendings [pri][w_->pending - 1].events = revents; 1685 pendings [pri][w_->pending - 1].events = revents;
1434 } 1686 }
1687
1688 pendingpri = NUMPRI - 1;
1435} 1689}
1436 1690
1437inline_speed void 1691inline_speed void
1438feed_reverse (EV_P_ W w) 1692feed_reverse (EV_P_ W w)
1439{ 1693{
1485 if (expect_true (!anfd->reify)) 1739 if (expect_true (!anfd->reify))
1486 fd_event_nocheck (EV_A_ fd, revents); 1740 fd_event_nocheck (EV_A_ fd, revents);
1487} 1741}
1488 1742
1489void 1743void
1490ev_feed_fd_event (EV_P_ int fd, int revents) 1744ev_feed_fd_event (EV_P_ int fd, int revents) EV_THROW
1491{ 1745{
1492 if (fd >= 0 && fd < anfdmax) 1746 if (fd >= 0 && fd < anfdmax)
1493 fd_event_nocheck (EV_A_ fd, revents); 1747 fd_event_nocheck (EV_A_ fd, revents);
1494} 1748}
1495 1749
1814static void noinline ecb_cold 2068static void noinline ecb_cold
1815evpipe_init (EV_P) 2069evpipe_init (EV_P)
1816{ 2070{
1817 if (!ev_is_active (&pipe_w)) 2071 if (!ev_is_active (&pipe_w))
1818 { 2072 {
2073 int fds [2];
2074
1819# if EV_USE_EVENTFD 2075# if EV_USE_EVENTFD
2076 fds [0] = -1;
1820 evfd = eventfd (0, EFD_NONBLOCK | EFD_CLOEXEC); 2077 fds [1] = eventfd (0, EFD_NONBLOCK | EFD_CLOEXEC);
1821 if (evfd < 0 && errno == EINVAL) 2078 if (fds [1] < 0 && errno == EINVAL)
1822 evfd = eventfd (0, 0); 2079 fds [1] = eventfd (0, 0);
1823 2080
1824 if (evfd >= 0) 2081 if (fds [1] < 0)
2082# endif
1825 { 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
1826 evpipe [0] = -1; 2092 evpipe [0] = fds [0];
1827 fd_intern (evfd); /* doing it twice doesn't hurt */ 2093
1828 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));
1829 } 2142 }
1830 else 2143 else
1831# endif 2144#endif
1832 { 2145 {
1833 while (pipe (evpipe)) 2146#ifdef _WIN32
1834 ev_syserr ("(libev) error creating signal/async pipe"); 2147 WSABUF buf;
1835 2148 DWORD sent;
1836 fd_intern (evpipe [0]); 2149 buf.buf = &buf;
1837 fd_intern (evpipe [1]); 2150 buf.len = 1;
1838 ev_io_set (&pipe_w, evpipe [0], EV_READ); 2151 WSASend (EV_FD_TO_WIN32_HANDLE (evpipe [1]), &buf, 1, &sent, 0, 0, 0);
1839 } 2152#else
1840
1841 ev_io_start (EV_A_ &pipe_w);
1842 ev_unref (EV_A); /* watcher should not keep loop alive */
1843 }
1844}
1845
1846inline_speed void
1847evpipe_write (EV_P_ EV_ATOMIC_T *flag)
1848{
1849 if (expect_true (*flag))
1850 return;
1851
1852 *flag = 1;
1853
1854 ECB_MEMORY_FENCE_RELEASE; /* make sure flag is visible before the wakeup */
1855
1856 pipe_write_skipped = 1;
1857
1858 ECB_MEMORY_FENCE; /* make sure pipe_write_skipped is visible before we check pipe_write_wanted */
1859
1860 if (pipe_write_wanted)
1861 {
1862 int old_errno;
1863
1864 pipe_write_skipped = 0; /* just an optimisation, no fence needed */
1865
1866 old_errno = errno; /* save errno because write will clobber it */
1867
1868#if EV_USE_EVENTFD
1869 if (evfd >= 0)
1870 {
1871 uint64_t counter = 1;
1872 write (evfd, &counter, sizeof (uint64_t));
1873 }
1874 else
1875#endif
1876 {
1877 /* win32 people keep sending patches that change this write() to send() */
1878 /* and then run away. but send() is wrong, it wants a socket handle on win32 */
1879 /* so when you think this write should be a send instead, please find out */
1880 /* where your send() is from - it's definitely not the microsoft send, and */
1881 /* tell me. thank you. */
1882 /* it might be that your problem is that your environment needs EV_USE_WSASOCKET */
1883 /* check the ev documentation on how to use this flag */
1884 write (evpipe [1], &(evpipe [1]), 1); 2153 write (evpipe [1], &(evpipe [1]), 1);
2154#endif
1885 } 2155 }
1886 2156
1887 errno = old_errno; 2157 errno = old_errno;
1888 } 2158 }
1889} 2159}
1896 int i; 2166 int i;
1897 2167
1898 if (revents & EV_READ) 2168 if (revents & EV_READ)
1899 { 2169 {
1900#if EV_USE_EVENTFD 2170#if EV_USE_EVENTFD
1901 if (evfd >= 0) 2171 if (evpipe [0] < 0)
1902 { 2172 {
1903 uint64_t counter; 2173 uint64_t counter;
1904 read (evfd, &counter, sizeof (uint64_t)); 2174 read (evpipe [1], &counter, sizeof (uint64_t));
1905 } 2175 }
1906 else 2176 else
1907#endif 2177#endif
1908 { 2178 {
1909 char dummy; 2179 char dummy[4];
1910 /* 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
1911 read (evpipe [0], &dummy, 1); 2188 read (evpipe [0], &dummy, sizeof (dummy));
2189#endif
1912 } 2190 }
1913 } 2191 }
1914 2192
1915 pipe_write_skipped = 0; 2193 pipe_write_skipped = 0;
2194
2195 ECB_MEMORY_FENCE; /* push out skipped, acquire flags */
1916 2196
1917#if EV_SIGNAL_ENABLE 2197#if EV_SIGNAL_ENABLE
1918 if (sig_pending) 2198 if (sig_pending)
1919 { 2199 {
1920 sig_pending = 0; 2200 sig_pending = 0;
2201
2202 ECB_MEMORY_FENCE;
1921 2203
1922 for (i = EV_NSIG - 1; i--; ) 2204 for (i = EV_NSIG - 1; i--; )
1923 if (expect_false (signals [i].pending)) 2205 if (expect_false (signals [i].pending))
1924 ev_feed_signal_event (EV_A_ i + 1); 2206 ev_feed_signal_event (EV_A_ i + 1);
1925 } 2207 }
1927 2209
1928#if EV_ASYNC_ENABLE 2210#if EV_ASYNC_ENABLE
1929 if (async_pending) 2211 if (async_pending)
1930 { 2212 {
1931 async_pending = 0; 2213 async_pending = 0;
2214
2215 ECB_MEMORY_FENCE;
1932 2216
1933 for (i = asynccnt; i--; ) 2217 for (i = asynccnt; i--; )
1934 if (asyncs [i]->sent) 2218 if (asyncs [i]->sent)
1935 { 2219 {
1936 asyncs [i]->sent = 0; 2220 asyncs [i]->sent = 0;
2221 ECB_MEMORY_FENCE_RELEASE;
1937 ev_feed_event (EV_A_ asyncs [i], EV_ASYNC); 2222 ev_feed_event (EV_A_ asyncs [i], EV_ASYNC);
1938 } 2223 }
1939 } 2224 }
1940#endif 2225#endif
1941} 2226}
1942 2227
1943/*****************************************************************************/ 2228/*****************************************************************************/
1944 2229
1945void 2230void
1946ev_feed_signal (int signum) 2231ev_feed_signal (int signum) EV_THROW
1947{ 2232{
1948#if EV_MULTIPLICITY 2233#if EV_MULTIPLICITY
2234 ECB_MEMORY_FENCE_ACQUIRE;
1949 EV_P = signals [signum - 1].loop; 2235 EV_P = signals [signum - 1].loop;
1950 2236
1951 if (!EV_A) 2237 if (!EV_A)
1952 return; 2238 return;
1953#endif 2239#endif
1954 2240
1955 if (!ev_active (&pipe_w))
1956 return;
1957
1958 signals [signum - 1].pending = 1; 2241 signals [signum - 1].pending = 1;
1959 evpipe_write (EV_A_ &sig_pending); 2242 evpipe_write (EV_A_ &sig_pending);
1960} 2243}
1961 2244
1962static void 2245static void
1968 2251
1969 ev_feed_signal (signum); 2252 ev_feed_signal (signum);
1970} 2253}
1971 2254
1972void noinline 2255void noinline
1973ev_feed_signal_event (EV_P_ int signum) 2256ev_feed_signal_event (EV_P_ int signum) EV_THROW
1974{ 2257{
1975 WL w; 2258 WL w;
1976 2259
1977 if (expect_false (signum <= 0 || signum > EV_NSIG)) 2260 if (expect_false (signum <= 0 || signum >= EV_NSIG))
1978 return; 2261 return;
1979 2262
1980 --signum; 2263 --signum;
1981 2264
1982#if EV_MULTIPLICITY 2265#if EV_MULTIPLICITY
1986 if (expect_false (signals [signum].loop != EV_A)) 2269 if (expect_false (signals [signum].loop != EV_A))
1987 return; 2270 return;
1988#endif 2271#endif
1989 2272
1990 signals [signum].pending = 0; 2273 signals [signum].pending = 0;
2274 ECB_MEMORY_FENCE_RELEASE;
1991 2275
1992 for (w = signals [signum].head; w; w = w->next) 2276 for (w = signals [signum].head; w; w = w->next)
1993 ev_feed_event (EV_A_ (W)w, EV_SIGNAL); 2277 ev_feed_event (EV_A_ (W)w, EV_SIGNAL);
1994} 2278}
1995 2279
2094#if EV_USE_SELECT 2378#if EV_USE_SELECT
2095# include "ev_select.c" 2379# include "ev_select.c"
2096#endif 2380#endif
2097 2381
2098int ecb_cold 2382int ecb_cold
2099ev_version_major (void) 2383ev_version_major (void) EV_THROW
2100{ 2384{
2101 return EV_VERSION_MAJOR; 2385 return EV_VERSION_MAJOR;
2102} 2386}
2103 2387
2104int ecb_cold 2388int ecb_cold
2105ev_version_minor (void) 2389ev_version_minor (void) EV_THROW
2106{ 2390{
2107 return EV_VERSION_MINOR; 2391 return EV_VERSION_MINOR;
2108} 2392}
2109 2393
2110/* 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 */
2118 || getgid () != getegid (); 2402 || getgid () != getegid ();
2119#endif 2403#endif
2120} 2404}
2121 2405
2122unsigned int ecb_cold 2406unsigned int ecb_cold
2123ev_supported_backends (void) 2407ev_supported_backends (void) EV_THROW
2124{ 2408{
2125 unsigned int flags = 0; 2409 unsigned int flags = 0;
2126 2410
2127 if (EV_USE_PORT ) flags |= EVBACKEND_PORT; 2411 if (EV_USE_PORT ) flags |= EVBACKEND_PORT;
2128 if (EV_USE_KQUEUE) flags |= EVBACKEND_KQUEUE; 2412 if (EV_USE_KQUEUE) flags |= EVBACKEND_KQUEUE;
2132 2416
2133 return flags; 2417 return flags;
2134} 2418}
2135 2419
2136unsigned int ecb_cold 2420unsigned int ecb_cold
2137ev_recommended_backends (void) 2421ev_recommended_backends (void) EV_THROW
2138{ 2422{
2139 unsigned int flags = ev_supported_backends (); 2423 unsigned int flags = ev_supported_backends ();
2140 2424
2141#ifndef __NetBSD__ 2425#ifndef __NetBSD__
2142 /* kqueue is borked on everything but netbsd apparently */ 2426 /* kqueue is borked on everything but netbsd apparently */
2154 2438
2155 return flags; 2439 return flags;
2156} 2440}
2157 2441
2158unsigned int ecb_cold 2442unsigned int ecb_cold
2159ev_embeddable_backends (void) 2443ev_embeddable_backends (void) EV_THROW
2160{ 2444{
2161 int flags = EVBACKEND_EPOLL | EVBACKEND_KQUEUE | EVBACKEND_PORT; 2445 int flags = EVBACKEND_EPOLL | EVBACKEND_KQUEUE | EVBACKEND_PORT;
2162 2446
2163 /* 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 */
2164 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 */
2166 2450
2167 return flags; 2451 return flags;
2168} 2452}
2169 2453
2170unsigned int 2454unsigned int
2171ev_backend (EV_P) 2455ev_backend (EV_P) EV_THROW
2172{ 2456{
2173 return backend; 2457 return backend;
2174} 2458}
2175 2459
2176#if EV_FEATURE_API 2460#if EV_FEATURE_API
2177unsigned int 2461unsigned int
2178ev_iteration (EV_P) 2462ev_iteration (EV_P) EV_THROW
2179{ 2463{
2180 return loop_count; 2464 return loop_count;
2181} 2465}
2182 2466
2183unsigned int 2467unsigned int
2184ev_depth (EV_P) 2468ev_depth (EV_P) EV_THROW
2185{ 2469{
2186 return loop_depth; 2470 return loop_depth;
2187} 2471}
2188 2472
2189void 2473void
2190ev_set_io_collect_interval (EV_P_ ev_tstamp interval) 2474ev_set_io_collect_interval (EV_P_ ev_tstamp interval) EV_THROW
2191{ 2475{
2192 io_blocktime = interval; 2476 io_blocktime = interval;
2193} 2477}
2194 2478
2195void 2479void
2196ev_set_timeout_collect_interval (EV_P_ ev_tstamp interval) 2480ev_set_timeout_collect_interval (EV_P_ ev_tstamp interval) EV_THROW
2197{ 2481{
2198 timeout_blocktime = interval; 2482 timeout_blocktime = interval;
2199} 2483}
2200 2484
2201void 2485void
2202ev_set_userdata (EV_P_ void *data) 2486ev_set_userdata (EV_P_ void *data) EV_THROW
2203{ 2487{
2204 userdata = data; 2488 userdata = data;
2205} 2489}
2206 2490
2207void * 2491void *
2208ev_userdata (EV_P) 2492ev_userdata (EV_P) EV_THROW
2209{ 2493{
2210 return userdata; 2494 return userdata;
2211} 2495}
2212 2496
2213void 2497void
2214ev_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
2215{ 2499{
2216 invoke_cb = invoke_pending_cb; 2500 invoke_cb = invoke_pending_cb;
2217} 2501}
2218 2502
2219void 2503void
2220ev_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
2221{ 2505{
2222 release_cb = release; 2506 release_cb = release;
2223 acquire_cb = acquire; 2507 acquire_cb = acquire;
2224} 2508}
2225#endif 2509#endif
2226 2510
2227/* initialise a loop structure, must be zero-initialised */ 2511/* initialise a loop structure, must be zero-initialised */
2228static void noinline ecb_cold 2512static void noinline ecb_cold
2229loop_init (EV_P_ unsigned int flags) 2513loop_init (EV_P_ unsigned int flags) EV_THROW
2230{ 2514{
2231 if (!backend) 2515 if (!backend)
2232 { 2516 {
2233 origflags = flags; 2517 origflags = flags;
2234 2518
2279#if EV_ASYNC_ENABLE 2563#if EV_ASYNC_ENABLE
2280 async_pending = 0; 2564 async_pending = 0;
2281#endif 2565#endif
2282 pipe_write_skipped = 0; 2566 pipe_write_skipped = 0;
2283 pipe_write_wanted = 0; 2567 pipe_write_wanted = 0;
2568 evpipe [0] = -1;
2569 evpipe [1] = -1;
2284#if EV_USE_INOTIFY 2570#if EV_USE_INOTIFY
2285 fs_fd = flags & EVFLAG_NOINOTIFY ? -1 : -2; 2571 fs_fd = flags & EVFLAG_NOINOTIFY ? -1 : -2;
2286#endif 2572#endif
2287#if EV_USE_SIGNALFD 2573#if EV_USE_SIGNALFD
2288 sigfd = flags & EVFLAG_SIGNALFD ? -2 : -1; 2574 sigfd = flags & EVFLAG_SIGNALFD ? -2 : -1;
2339 EV_INVOKE_PENDING; 2625 EV_INVOKE_PENDING;
2340 } 2626 }
2341#endif 2627#endif
2342 2628
2343#if EV_CHILD_ENABLE 2629#if EV_CHILD_ENABLE
2344 if (ev_is_active (&childev)) 2630 if (ev_is_default_loop (EV_A) && ev_is_active (&childev))
2345 { 2631 {
2346 ev_ref (EV_A); /* child watcher */ 2632 ev_ref (EV_A); /* child watcher */
2347 ev_signal_stop (EV_A_ &childev); 2633 ev_signal_stop (EV_A_ &childev);
2348 } 2634 }
2349#endif 2635#endif
2351 if (ev_is_active (&pipe_w)) 2637 if (ev_is_active (&pipe_w))
2352 { 2638 {
2353 /*ev_ref (EV_A);*/ 2639 /*ev_ref (EV_A);*/
2354 /*ev_io_stop (EV_A_ &pipe_w);*/ 2640 /*ev_io_stop (EV_A_ &pipe_w);*/
2355 2641
2356#if EV_USE_EVENTFD
2357 if (evfd >= 0)
2358 close (evfd);
2359#endif
2360
2361 if (evpipe [0] >= 0)
2362 {
2363 EV_WIN32_CLOSE_FD (evpipe [0]); 2642 if (evpipe [0] >= 0) EV_WIN32_CLOSE_FD (evpipe [0]);
2364 EV_WIN32_CLOSE_FD (evpipe [1]); 2643 if (evpipe [1] >= 0) EV_WIN32_CLOSE_FD (evpipe [1]);
2365 }
2366 } 2644 }
2367 2645
2368#if EV_USE_SIGNALFD 2646#if EV_USE_SIGNALFD
2369 if (ev_is_active (&sigfd_w)) 2647 if (ev_is_active (&sigfd_w))
2370 close (sigfd); 2648 close (sigfd);
2456#endif 2734#endif
2457#if EV_USE_INOTIFY 2735#if EV_USE_INOTIFY
2458 infy_fork (EV_A); 2736 infy_fork (EV_A);
2459#endif 2737#endif
2460 2738
2739#if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE
2461 if (ev_is_active (&pipe_w)) 2740 if (ev_is_active (&pipe_w))
2462 { 2741 {
2463 /* 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 */
2464 2743
2465 ev_ref (EV_A); 2744 ev_ref (EV_A);
2466 ev_io_stop (EV_A_ &pipe_w); 2745 ev_io_stop (EV_A_ &pipe_w);
2467 2746
2468#if EV_USE_EVENTFD
2469 if (evfd >= 0)
2470 close (evfd);
2471#endif
2472
2473 if (evpipe [0] >= 0) 2747 if (evpipe [0] >= 0)
2474 {
2475 EV_WIN32_CLOSE_FD (evpipe [0]); 2748 EV_WIN32_CLOSE_FD (evpipe [0]);
2476 EV_WIN32_CLOSE_FD (evpipe [1]);
2477 }
2478 2749
2479#if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE
2480 evpipe_init (EV_A); 2750 evpipe_init (EV_A);
2481 /* now iterate over everything, in case we missed something */ 2751 /* iterate over everything, in case we missed something before */
2482 pipecb (EV_A_ &pipe_w, EV_READ); 2752 ev_feed_event (EV_A_ &pipe_w, EV_CUSTOM);
2483#endif
2484 } 2753 }
2754#endif
2485 2755
2486 postfork = 0; 2756 postfork = 0;
2487} 2757}
2488 2758
2489#if EV_MULTIPLICITY 2759#if EV_MULTIPLICITY
2490 2760
2491struct ev_loop * ecb_cold 2761struct ev_loop * ecb_cold
2492ev_loop_new (unsigned int flags) 2762ev_loop_new (unsigned int flags) EV_THROW
2493{ 2763{
2494 EV_P = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop)); 2764 EV_P = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop));
2495 2765
2496 memset (EV_A, 0, sizeof (struct ev_loop)); 2766 memset (EV_A, 0, sizeof (struct ev_loop));
2497 loop_init (EV_A_ flags); 2767 loop_init (EV_A_ flags);
2541} 2811}
2542#endif 2812#endif
2543 2813
2544#if EV_FEATURE_API 2814#if EV_FEATURE_API
2545void ecb_cold 2815void ecb_cold
2546ev_verify (EV_P) 2816ev_verify (EV_P) EV_THROW
2547{ 2817{
2548#if EV_VERIFY 2818#if EV_VERIFY
2549 int i; 2819 int i;
2550 WL w; 2820 WL w, w2;
2551 2821
2552 assert (activecnt >= -1); 2822 assert (activecnt >= -1);
2553 2823
2554 assert (fdchangemax >= fdchangecnt); 2824 assert (fdchangemax >= fdchangecnt);
2555 for (i = 0; i < fdchangecnt; ++i) 2825 for (i = 0; i < fdchangecnt; ++i)
2556 assert (("libev: negative fd in fdchanges", fdchanges [i] >= 0)); 2826 assert (("libev: negative fd in fdchanges", fdchanges [i] >= 0));
2557 2827
2558 assert (anfdmax >= 0); 2828 assert (anfdmax >= 0);
2559 for (i = 0; i < anfdmax; ++i) 2829 for (i = 0; i < anfdmax; ++i)
2830 {
2831 int j = 0;
2832
2560 for (w = anfds [i].head; w; w = w->next) 2833 for (w = w2 = anfds [i].head; w; w = w->next)
2561 { 2834 {
2562 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
2563 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));
2564 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));
2565 } 2845 }
2846 }
2566 2847
2567 assert (timermax >= timercnt); 2848 assert (timermax >= timercnt);
2568 verify_heap (EV_A_ timers, timercnt); 2849 verify_heap (EV_A_ timers, timercnt);
2569 2850
2570#if EV_PERIODIC_ENABLE 2851#if EV_PERIODIC_ENABLE
2620#if EV_MULTIPLICITY 2901#if EV_MULTIPLICITY
2621struct ev_loop * ecb_cold 2902struct ev_loop * ecb_cold
2622#else 2903#else
2623int 2904int
2624#endif 2905#endif
2625ev_default_loop (unsigned int flags) 2906ev_default_loop (unsigned int flags) EV_THROW
2626{ 2907{
2627 if (!ev_default_loop_ptr) 2908 if (!ev_default_loop_ptr)
2628 { 2909 {
2629#if EV_MULTIPLICITY 2910#if EV_MULTIPLICITY
2630 EV_P = ev_default_loop_ptr = &default_loop_struct; 2911 EV_P = ev_default_loop_ptr = &default_loop_struct;
2649 2930
2650 return ev_default_loop_ptr; 2931 return ev_default_loop_ptr;
2651} 2932}
2652 2933
2653void 2934void
2654ev_loop_fork (EV_P) 2935ev_loop_fork (EV_P) EV_THROW
2655{ 2936{
2656 postfork = 1; /* must be in line with ev_default_fork */ 2937 postfork = 1;
2657} 2938}
2658 2939
2659/*****************************************************************************/ 2940/*****************************************************************************/
2660 2941
2661void 2942void
2663{ 2944{
2664 EV_CB_INVOKE ((W)w, revents); 2945 EV_CB_INVOKE ((W)w, revents);
2665} 2946}
2666 2947
2667unsigned int 2948unsigned int
2668ev_pending_count (EV_P) 2949ev_pending_count (EV_P) EV_THROW
2669{ 2950{
2670 int pri; 2951 int pri;
2671 unsigned int count = 0; 2952 unsigned int count = 0;
2672 2953
2673 for (pri = NUMPRI; pri--; ) 2954 for (pri = NUMPRI; pri--; )
2677} 2958}
2678 2959
2679void noinline 2960void noinline
2680ev_invoke_pending (EV_P) 2961ev_invoke_pending (EV_P)
2681{ 2962{
2682 int pri; 2963 pendingpri = NUMPRI;
2683 2964
2684 for (pri = NUMPRI; pri--; ) 2965 while (pendingpri) /* pendingpri possibly gets modified in the inner loop */
2966 {
2967 --pendingpri;
2968
2685 while (pendingcnt [pri]) 2969 while (pendingcnt [pendingpri])
2686 { 2970 {
2687 ANPENDING *p = pendings [pri] + --pendingcnt [pri]; 2971 ANPENDING *p = pendings [pendingpri] + --pendingcnt [pendingpri];
2688 2972
2689 p->w->pending = 0; 2973 p->w->pending = 0;
2690 EV_CB_INVOKE (p->w, p->events); 2974 EV_CB_INVOKE (p->w, p->events);
2691 EV_FREQUENT_CHECK; 2975 EV_FREQUENT_CHECK;
2692 } 2976 }
2977 }
2693} 2978}
2694 2979
2695#if EV_IDLE_ENABLE 2980#if EV_IDLE_ENABLE
2696/* make idle watchers pending. this handles the "call-idle */ 2981/* make idle watchers pending. this handles the "call-idle */
2697/* only when higher priorities are idle" logic */ 2982/* only when higher priorities are idle" logic */
2787{ 3072{
2788 EV_FREQUENT_CHECK; 3073 EV_FREQUENT_CHECK;
2789 3074
2790 while (periodiccnt && ANHE_at (periodics [HEAP0]) < ev_rt_now) 3075 while (periodiccnt && ANHE_at (periodics [HEAP0]) < ev_rt_now)
2791 { 3076 {
2792 int feed_count = 0;
2793
2794 do 3077 do
2795 { 3078 {
2796 ev_periodic *w = (ev_periodic *)ANHE_w (periodics [HEAP0]); 3079 ev_periodic *w = (ev_periodic *)ANHE_w (periodics [HEAP0]);
2797 3080
2798 /*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)));*/
3057 backend_poll (EV_A_ waittime); 3340 backend_poll (EV_A_ waittime);
3058 assert ((loop_done = EVBREAK_CANCEL, 1)); /* assert for side effect */ 3341 assert ((loop_done = EVBREAK_CANCEL, 1)); /* assert for side effect */
3059 3342
3060 pipe_write_wanted = 0; /* just an optimisation, no fence needed */ 3343 pipe_write_wanted = 0; /* just an optimisation, no fence needed */
3061 3344
3345 ECB_MEMORY_FENCE_ACQUIRE;
3062 if (pipe_write_skipped) 3346 if (pipe_write_skipped)
3063 { 3347 {
3064 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)));
3065 ev_feed_event (EV_A_ &pipe_w, EV_CUSTOM); 3349 ev_feed_event (EV_A_ &pipe_w, EV_CUSTOM);
3066 } 3350 }
3104 3388
3105 return activecnt; 3389 return activecnt;
3106} 3390}
3107 3391
3108void 3392void
3109ev_break (EV_P_ int how) 3393ev_break (EV_P_ int how) EV_THROW
3110{ 3394{
3111 loop_done = how; 3395 loop_done = how;
3112} 3396}
3113 3397
3114void 3398void
3115ev_ref (EV_P) 3399ev_ref (EV_P) EV_THROW
3116{ 3400{
3117 ++activecnt; 3401 ++activecnt;
3118} 3402}
3119 3403
3120void 3404void
3121ev_unref (EV_P) 3405ev_unref (EV_P) EV_THROW
3122{ 3406{
3123 --activecnt; 3407 --activecnt;
3124} 3408}
3125 3409
3126void 3410void
3127ev_now_update (EV_P) 3411ev_now_update (EV_P) EV_THROW
3128{ 3412{
3129 time_update (EV_A_ 1e100); 3413 time_update (EV_A_ 1e100);
3130} 3414}
3131 3415
3132void 3416void
3133ev_suspend (EV_P) 3417ev_suspend (EV_P) EV_THROW
3134{ 3418{
3135 ev_now_update (EV_A); 3419 ev_now_update (EV_A);
3136} 3420}
3137 3421
3138void 3422void
3139ev_resume (EV_P) 3423ev_resume (EV_P) EV_THROW
3140{ 3424{
3141 ev_tstamp mn_prev = mn_now; 3425 ev_tstamp mn_prev = mn_now;
3142 3426
3143 ev_now_update (EV_A); 3427 ev_now_update (EV_A);
3144 timers_reschedule (EV_A_ mn_now - mn_prev); 3428 timers_reschedule (EV_A_ mn_now - mn_prev);
3183 w->pending = 0; 3467 w->pending = 0;
3184 } 3468 }
3185} 3469}
3186 3470
3187int 3471int
3188ev_clear_pending (EV_P_ void *w) 3472ev_clear_pending (EV_P_ void *w) EV_THROW
3189{ 3473{
3190 W w_ = (W)w; 3474 W w_ = (W)w;
3191 int pending = w_->pending; 3475 int pending = w_->pending;
3192 3476
3193 if (expect_true (pending)) 3477 if (expect_true (pending))
3226} 3510}
3227 3511
3228/*****************************************************************************/ 3512/*****************************************************************************/
3229 3513
3230void noinline 3514void noinline
3231ev_io_start (EV_P_ ev_io *w) 3515ev_io_start (EV_P_ ev_io *w) EV_THROW
3232{ 3516{
3233 int fd = w->fd; 3517 int fd = w->fd;
3234 3518
3235 if (expect_false (ev_is_active (w))) 3519 if (expect_false (ev_is_active (w)))
3236 return; 3520 return;
3242 3526
3243 ev_start (EV_A_ (W)w, 1); 3527 ev_start (EV_A_ (W)w, 1);
3244 array_needsize (ANFD, anfds, anfdmax, fd + 1, array_init_zero); 3528 array_needsize (ANFD, anfds, anfdmax, fd + 1, array_init_zero);
3245 wlist_add (&anfds[fd].head, (WL)w); 3529 wlist_add (&anfds[fd].head, (WL)w);
3246 3530
3531 /* common bug, apparently */
3532 assert (("libev: ev_io_start called with corrupted watcher", ((WL)w)->next != (WL)w));
3533
3247 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);
3248 w->events &= ~EV__IOFDSET; 3535 w->events &= ~EV__IOFDSET;
3249 3536
3250 EV_FREQUENT_CHECK; 3537 EV_FREQUENT_CHECK;
3251} 3538}
3252 3539
3253void noinline 3540void noinline
3254ev_io_stop (EV_P_ ev_io *w) 3541ev_io_stop (EV_P_ ev_io *w) EV_THROW
3255{ 3542{
3256 clear_pending (EV_A_ (W)w); 3543 clear_pending (EV_A_ (W)w);
3257 if (expect_false (!ev_is_active (w))) 3544 if (expect_false (!ev_is_active (w)))
3258 return; 3545 return;
3259 3546
3268 3555
3269 EV_FREQUENT_CHECK; 3556 EV_FREQUENT_CHECK;
3270} 3557}
3271 3558
3272void noinline 3559void noinline
3273ev_timer_start (EV_P_ ev_timer *w) 3560ev_timer_start (EV_P_ ev_timer *w) EV_THROW
3274{ 3561{
3275 if (expect_false (ev_is_active (w))) 3562 if (expect_false (ev_is_active (w)))
3276 return; 3563 return;
3277 3564
3278 ev_at (w) += mn_now; 3565 ev_at (w) += mn_now;
3292 3579
3293 /*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));*/
3294} 3581}
3295 3582
3296void noinline 3583void noinline
3297ev_timer_stop (EV_P_ ev_timer *w) 3584ev_timer_stop (EV_P_ ev_timer *w) EV_THROW
3298{ 3585{
3299 clear_pending (EV_A_ (W)w); 3586 clear_pending (EV_A_ (W)w);
3300 if (expect_false (!ev_is_active (w))) 3587 if (expect_false (!ev_is_active (w)))
3301 return; 3588 return;
3302 3589
3322 3609
3323 EV_FREQUENT_CHECK; 3610 EV_FREQUENT_CHECK;
3324} 3611}
3325 3612
3326void noinline 3613void noinline
3327ev_timer_again (EV_P_ ev_timer *w) 3614ev_timer_again (EV_P_ ev_timer *w) EV_THROW
3328{ 3615{
3329 EV_FREQUENT_CHECK; 3616 EV_FREQUENT_CHECK;
3330 3617
3331 clear_pending (EV_A_ (W)w); 3618 clear_pending (EV_A_ (W)w);
3332 3619
3349 3636
3350 EV_FREQUENT_CHECK; 3637 EV_FREQUENT_CHECK;
3351} 3638}
3352 3639
3353ev_tstamp 3640ev_tstamp
3354ev_timer_remaining (EV_P_ ev_timer *w) 3641ev_timer_remaining (EV_P_ ev_timer *w) EV_THROW
3355{ 3642{
3356 return ev_at (w) - (ev_is_active (w) ? mn_now : 0.); 3643 return ev_at (w) - (ev_is_active (w) ? mn_now : 0.);
3357} 3644}
3358 3645
3359#if EV_PERIODIC_ENABLE 3646#if EV_PERIODIC_ENABLE
3360void noinline 3647void noinline
3361ev_periodic_start (EV_P_ ev_periodic *w) 3648ev_periodic_start (EV_P_ ev_periodic *w) EV_THROW
3362{ 3649{
3363 if (expect_false (ev_is_active (w))) 3650 if (expect_false (ev_is_active (w)))
3364 return; 3651 return;
3365 3652
3366 if (w->reschedule_cb) 3653 if (w->reschedule_cb)
3386 3673
3387 /*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));*/
3388} 3675}
3389 3676
3390void noinline 3677void noinline
3391ev_periodic_stop (EV_P_ ev_periodic *w) 3678ev_periodic_stop (EV_P_ ev_periodic *w) EV_THROW
3392{ 3679{
3393 clear_pending (EV_A_ (W)w); 3680 clear_pending (EV_A_ (W)w);
3394 if (expect_false (!ev_is_active (w))) 3681 if (expect_false (!ev_is_active (w)))
3395 return; 3682 return;
3396 3683
3414 3701
3415 EV_FREQUENT_CHECK; 3702 EV_FREQUENT_CHECK;
3416} 3703}
3417 3704
3418void noinline 3705void noinline
3419ev_periodic_again (EV_P_ ev_periodic *w) 3706ev_periodic_again (EV_P_ ev_periodic *w) EV_THROW
3420{ 3707{
3421 /* TODO: use adjustheap and recalculation */ 3708 /* TODO: use adjustheap and recalculation */
3422 ev_periodic_stop (EV_A_ w); 3709 ev_periodic_stop (EV_A_ w);
3423 ev_periodic_start (EV_A_ w); 3710 ev_periodic_start (EV_A_ w);
3424} 3711}
3429#endif 3716#endif
3430 3717
3431#if EV_SIGNAL_ENABLE 3718#if EV_SIGNAL_ENABLE
3432 3719
3433void noinline 3720void noinline
3434ev_signal_start (EV_P_ ev_signal *w) 3721ev_signal_start (EV_P_ ev_signal *w) EV_THROW
3435{ 3722{
3436 if (expect_false (ev_is_active (w))) 3723 if (expect_false (ev_is_active (w)))
3437 return; 3724 return;
3438 3725
3439 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));
3441#if EV_MULTIPLICITY 3728#if EV_MULTIPLICITY
3442 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",
3443 !signals [w->signum - 1].loop || signals [w->signum - 1].loop == loop)); 3730 !signals [w->signum - 1].loop || signals [w->signum - 1].loop == loop));
3444 3731
3445 signals [w->signum - 1].loop = EV_A; 3732 signals [w->signum - 1].loop = EV_A;
3733 ECB_MEMORY_FENCE_RELEASE;
3446#endif 3734#endif
3447 3735
3448 EV_FREQUENT_CHECK; 3736 EV_FREQUENT_CHECK;
3449 3737
3450#if EV_USE_SIGNALFD 3738#if EV_USE_SIGNALFD
3510 3798
3511 EV_FREQUENT_CHECK; 3799 EV_FREQUENT_CHECK;
3512} 3800}
3513 3801
3514void noinline 3802void noinline
3515ev_signal_stop (EV_P_ ev_signal *w) 3803ev_signal_stop (EV_P_ ev_signal *w) EV_THROW
3516{ 3804{
3517 clear_pending (EV_A_ (W)w); 3805 clear_pending (EV_A_ (W)w);
3518 if (expect_false (!ev_is_active (w))) 3806 if (expect_false (!ev_is_active (w)))
3519 return; 3807 return;
3520 3808
3551#endif 3839#endif
3552 3840
3553#if EV_CHILD_ENABLE 3841#if EV_CHILD_ENABLE
3554 3842
3555void 3843void
3556ev_child_start (EV_P_ ev_child *w) 3844ev_child_start (EV_P_ ev_child *w) EV_THROW
3557{ 3845{
3558#if EV_MULTIPLICITY 3846#if EV_MULTIPLICITY
3559 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));
3560#endif 3848#endif
3561 if (expect_false (ev_is_active (w))) 3849 if (expect_false (ev_is_active (w)))
3568 3856
3569 EV_FREQUENT_CHECK; 3857 EV_FREQUENT_CHECK;
3570} 3858}
3571 3859
3572void 3860void
3573ev_child_stop (EV_P_ ev_child *w) 3861ev_child_stop (EV_P_ ev_child *w) EV_THROW
3574{ 3862{
3575 clear_pending (EV_A_ (W)w); 3863 clear_pending (EV_A_ (W)w);
3576 if (expect_false (!ev_is_active (w))) 3864 if (expect_false (!ev_is_active (w)))
3577 return; 3865 return;
3578 3866
3605# define EV_INOTIFY_BUFSIZE (sizeof (struct inotify_event) * 2 + NAME_MAX) 3893# define EV_INOTIFY_BUFSIZE (sizeof (struct inotify_event) * 2 + NAME_MAX)
3606 3894
3607static void noinline 3895static void noinline
3608infy_add (EV_P_ ev_stat *w) 3896infy_add (EV_P_ ev_stat *w)
3609{ 3897{
3610 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);
3611 3902
3612 if (w->wd >= 0) 3903 if (w->wd >= 0)
3613 { 3904 {
3614 struct statfs sfs; 3905 struct statfs sfs;
3615 3906
3619 3910
3620 if (!fs_2625) 3911 if (!fs_2625)
3621 w->timer.repeat = w->interval ? w->interval : DEF_STAT_INTERVAL; 3912 w->timer.repeat = w->interval ? w->interval : DEF_STAT_INTERVAL;
3622 else if (!statfs (w->path, &sfs) 3913 else if (!statfs (w->path, &sfs)
3623 && (sfs.f_type == 0x1373 /* devfs */ 3914 && (sfs.f_type == 0x1373 /* devfs */
3915 || sfs.f_type == 0x4006 /* fat */
3916 || sfs.f_type == 0x4d44 /* msdos */
3624 || 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 */
3625 || sfs.f_type == 0x3153464a /* jfs */ 3921 || sfs.f_type == 0x3153464a /* jfs */
3922 || sfs.f_type == 0x9123683e /* btrfs */
3626 || sfs.f_type == 0x52654973 /* reiser3 */ 3923 || sfs.f_type == 0x52654973 /* reiser3 */
3627 || sfs.f_type == 0x01021994 /* tempfs */ 3924 || sfs.f_type == 0x01021994 /* tmpfs */
3628 || sfs.f_type == 0x58465342 /* xfs */)) 3925 || sfs.f_type == 0x58465342 /* xfs */))
3629 w->timer.repeat = 0.; /* filesystem is local, kernel new enough */ 3926 w->timer.repeat = 0.; /* filesystem is local, kernel new enough */
3630 else 3927 else
3631 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 */
3632 } 3929 }
3830#else 4127#else
3831# define EV_LSTAT(p,b) lstat (p, b) 4128# define EV_LSTAT(p,b) lstat (p, b)
3832#endif 4129#endif
3833 4130
3834void 4131void
3835ev_stat_stat (EV_P_ ev_stat *w) 4132ev_stat_stat (EV_P_ ev_stat *w) EV_THROW
3836{ 4133{
3837 if (lstat (w->path, &w->attr) < 0) 4134 if (lstat (w->path, &w->attr) < 0)
3838 w->attr.st_nlink = 0; 4135 w->attr.st_nlink = 0;
3839 else if (!w->attr.st_nlink) 4136 else if (!w->attr.st_nlink)
3840 w->attr.st_nlink = 1; 4137 w->attr.st_nlink = 1;
3879 ev_feed_event (EV_A_ w, EV_STAT); 4176 ev_feed_event (EV_A_ w, EV_STAT);
3880 } 4177 }
3881} 4178}
3882 4179
3883void 4180void
3884ev_stat_start (EV_P_ ev_stat *w) 4181ev_stat_start (EV_P_ ev_stat *w) EV_THROW
3885{ 4182{
3886 if (expect_false (ev_is_active (w))) 4183 if (expect_false (ev_is_active (w)))
3887 return; 4184 return;
3888 4185
3889 ev_stat_stat (EV_A_ w); 4186 ev_stat_stat (EV_A_ w);
3910 4207
3911 EV_FREQUENT_CHECK; 4208 EV_FREQUENT_CHECK;
3912} 4209}
3913 4210
3914void 4211void
3915ev_stat_stop (EV_P_ ev_stat *w) 4212ev_stat_stop (EV_P_ ev_stat *w) EV_THROW
3916{ 4213{
3917 clear_pending (EV_A_ (W)w); 4214 clear_pending (EV_A_ (W)w);
3918 if (expect_false (!ev_is_active (w))) 4215 if (expect_false (!ev_is_active (w)))
3919 return; 4216 return;
3920 4217
3936} 4233}
3937#endif 4234#endif
3938 4235
3939#if EV_IDLE_ENABLE 4236#if EV_IDLE_ENABLE
3940void 4237void
3941ev_idle_start (EV_P_ ev_idle *w) 4238ev_idle_start (EV_P_ ev_idle *w) EV_THROW
3942{ 4239{
3943 if (expect_false (ev_is_active (w))) 4240 if (expect_false (ev_is_active (w)))
3944 return; 4241 return;
3945 4242
3946 pri_adjust (EV_A_ (W)w); 4243 pri_adjust (EV_A_ (W)w);
3959 4256
3960 EV_FREQUENT_CHECK; 4257 EV_FREQUENT_CHECK;
3961} 4258}
3962 4259
3963void 4260void
3964ev_idle_stop (EV_P_ ev_idle *w) 4261ev_idle_stop (EV_P_ ev_idle *w) EV_THROW
3965{ 4262{
3966 clear_pending (EV_A_ (W)w); 4263 clear_pending (EV_A_ (W)w);
3967 if (expect_false (!ev_is_active (w))) 4264 if (expect_false (!ev_is_active (w)))
3968 return; 4265 return;
3969 4266
3983} 4280}
3984#endif 4281#endif
3985 4282
3986#if EV_PREPARE_ENABLE 4283#if EV_PREPARE_ENABLE
3987void 4284void
3988ev_prepare_start (EV_P_ ev_prepare *w) 4285ev_prepare_start (EV_P_ ev_prepare *w) EV_THROW
3989{ 4286{
3990 if (expect_false (ev_is_active (w))) 4287 if (expect_false (ev_is_active (w)))
3991 return; 4288 return;
3992 4289
3993 EV_FREQUENT_CHECK; 4290 EV_FREQUENT_CHECK;
3998 4295
3999 EV_FREQUENT_CHECK; 4296 EV_FREQUENT_CHECK;
4000} 4297}
4001 4298
4002void 4299void
4003ev_prepare_stop (EV_P_ ev_prepare *w) 4300ev_prepare_stop (EV_P_ ev_prepare *w) EV_THROW
4004{ 4301{
4005 clear_pending (EV_A_ (W)w); 4302 clear_pending (EV_A_ (W)w);
4006 if (expect_false (!ev_is_active (w))) 4303 if (expect_false (!ev_is_active (w)))
4007 return; 4304 return;
4008 4305
4021} 4318}
4022#endif 4319#endif
4023 4320
4024#if EV_CHECK_ENABLE 4321#if EV_CHECK_ENABLE
4025void 4322void
4026ev_check_start (EV_P_ ev_check *w) 4323ev_check_start (EV_P_ ev_check *w) EV_THROW
4027{ 4324{
4028 if (expect_false (ev_is_active (w))) 4325 if (expect_false (ev_is_active (w)))
4029 return; 4326 return;
4030 4327
4031 EV_FREQUENT_CHECK; 4328 EV_FREQUENT_CHECK;
4036 4333
4037 EV_FREQUENT_CHECK; 4334 EV_FREQUENT_CHECK;
4038} 4335}
4039 4336
4040void 4337void
4041ev_check_stop (EV_P_ ev_check *w) 4338ev_check_stop (EV_P_ ev_check *w) EV_THROW
4042{ 4339{
4043 clear_pending (EV_A_ (W)w); 4340 clear_pending (EV_A_ (W)w);
4044 if (expect_false (!ev_is_active (w))) 4341 if (expect_false (!ev_is_active (w)))
4045 return; 4342 return;
4046 4343
4059} 4356}
4060#endif 4357#endif
4061 4358
4062#if EV_EMBED_ENABLE 4359#if EV_EMBED_ENABLE
4063void noinline 4360void noinline
4064ev_embed_sweep (EV_P_ ev_embed *w) 4361ev_embed_sweep (EV_P_ ev_embed *w) EV_THROW
4065{ 4362{
4066 ev_run (w->other, EVRUN_NOWAIT); 4363 ev_run (w->other, EVRUN_NOWAIT);
4067} 4364}
4068 4365
4069static void 4366static void
4117 ev_idle_stop (EV_A_ idle); 4414 ev_idle_stop (EV_A_ idle);
4118} 4415}
4119#endif 4416#endif
4120 4417
4121void 4418void
4122ev_embed_start (EV_P_ ev_embed *w) 4419ev_embed_start (EV_P_ ev_embed *w) EV_THROW
4123{ 4420{
4124 if (expect_false (ev_is_active (w))) 4421 if (expect_false (ev_is_active (w)))
4125 return; 4422 return;
4126 4423
4127 { 4424 {
4148 4445
4149 EV_FREQUENT_CHECK; 4446 EV_FREQUENT_CHECK;
4150} 4447}
4151 4448
4152void 4449void
4153ev_embed_stop (EV_P_ ev_embed *w) 4450ev_embed_stop (EV_P_ ev_embed *w) EV_THROW
4154{ 4451{
4155 clear_pending (EV_A_ (W)w); 4452 clear_pending (EV_A_ (W)w);
4156 if (expect_false (!ev_is_active (w))) 4453 if (expect_false (!ev_is_active (w)))
4157 return; 4454 return;
4158 4455
4168} 4465}
4169#endif 4466#endif
4170 4467
4171#if EV_FORK_ENABLE 4468#if EV_FORK_ENABLE
4172void 4469void
4173ev_fork_start (EV_P_ ev_fork *w) 4470ev_fork_start (EV_P_ ev_fork *w) EV_THROW
4174{ 4471{
4175 if (expect_false (ev_is_active (w))) 4472 if (expect_false (ev_is_active (w)))
4176 return; 4473 return;
4177 4474
4178 EV_FREQUENT_CHECK; 4475 EV_FREQUENT_CHECK;
4183 4480
4184 EV_FREQUENT_CHECK; 4481 EV_FREQUENT_CHECK;
4185} 4482}
4186 4483
4187void 4484void
4188ev_fork_stop (EV_P_ ev_fork *w) 4485ev_fork_stop (EV_P_ ev_fork *w) EV_THROW
4189{ 4486{
4190 clear_pending (EV_A_ (W)w); 4487 clear_pending (EV_A_ (W)w);
4191 if (expect_false (!ev_is_active (w))) 4488 if (expect_false (!ev_is_active (w)))
4192 return; 4489 return;
4193 4490
4206} 4503}
4207#endif 4504#endif
4208 4505
4209#if EV_CLEANUP_ENABLE 4506#if EV_CLEANUP_ENABLE
4210void 4507void
4211ev_cleanup_start (EV_P_ ev_cleanup *w) 4508ev_cleanup_start (EV_P_ ev_cleanup *w) EV_THROW
4212{ 4509{
4213 if (expect_false (ev_is_active (w))) 4510 if (expect_false (ev_is_active (w)))
4214 return; 4511 return;
4215 4512
4216 EV_FREQUENT_CHECK; 4513 EV_FREQUENT_CHECK;
4223 ev_unref (EV_A); 4520 ev_unref (EV_A);
4224 EV_FREQUENT_CHECK; 4521 EV_FREQUENT_CHECK;
4225} 4522}
4226 4523
4227void 4524void
4228ev_cleanup_stop (EV_P_ ev_cleanup *w) 4525ev_cleanup_stop (EV_P_ ev_cleanup *w) EV_THROW
4229{ 4526{
4230 clear_pending (EV_A_ (W)w); 4527 clear_pending (EV_A_ (W)w);
4231 if (expect_false (!ev_is_active (w))) 4528 if (expect_false (!ev_is_active (w)))
4232 return; 4529 return;
4233 4530
4247} 4544}
4248#endif 4545#endif
4249 4546
4250#if EV_ASYNC_ENABLE 4547#if EV_ASYNC_ENABLE
4251void 4548void
4252ev_async_start (EV_P_ ev_async *w) 4549ev_async_start (EV_P_ ev_async *w) EV_THROW
4253{ 4550{
4254 if (expect_false (ev_is_active (w))) 4551 if (expect_false (ev_is_active (w)))
4255 return; 4552 return;
4256 4553
4257 w->sent = 0; 4554 w->sent = 0;
4266 4563
4267 EV_FREQUENT_CHECK; 4564 EV_FREQUENT_CHECK;
4268} 4565}
4269 4566
4270void 4567void
4271ev_async_stop (EV_P_ ev_async *w) 4568ev_async_stop (EV_P_ ev_async *w) EV_THROW
4272{ 4569{
4273 clear_pending (EV_A_ (W)w); 4570 clear_pending (EV_A_ (W)w);
4274 if (expect_false (!ev_is_active (w))) 4571 if (expect_false (!ev_is_active (w)))
4275 return; 4572 return;
4276 4573
4287 4584
4288 EV_FREQUENT_CHECK; 4585 EV_FREQUENT_CHECK;
4289} 4586}
4290 4587
4291void 4588void
4292ev_async_send (EV_P_ ev_async *w) 4589ev_async_send (EV_P_ ev_async *w) EV_THROW
4293{ 4590{
4294 w->sent = 1; 4591 w->sent = 1;
4295 evpipe_write (EV_A_ &async_pending); 4592 evpipe_write (EV_A_ &async_pending);
4296} 4593}
4297#endif 4594#endif
4334 4631
4335 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));
4336} 4633}
4337 4634
4338void 4635void
4339ev_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
4340{ 4637{
4341 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));
4342 4639
4343 if (expect_false (!once)) 4640 if (expect_false (!once))
4344 { 4641 {
4366 4663
4367/*****************************************************************************/ 4664/*****************************************************************************/
4368 4665
4369#if EV_WALK_ENABLE 4666#if EV_WALK_ENABLE
4370void ecb_cold 4667void ecb_cold
4371ev_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
4372{ 4669{
4373 int i, j; 4670 int i, j;
4374 ev_watcher_list *wl, *wn; 4671 ev_watcher_list *wl, *wn;
4375 4672
4376 if (types & (EV_IO | EV_EMBED)) 4673 if (types & (EV_IO | EV_EMBED))

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines