… | |
… | |
537 | |
537 | |
538 | /* ECB_NO_THREADS - ecb is not used by multiple threads, ever */ |
538 | /* ECB_NO_THREADS - ecb is not used by multiple threads, ever */ |
539 | /* ECB_NO_SMP - ecb might be used in multiple threads, but only on a single cpu */ |
539 | /* ECB_NO_SMP - ecb might be used in multiple threads, but only on a single cpu */ |
540 | |
540 | |
541 | #if ECB_NO_THREADS || ECB_NO_SMP |
541 | #if ECB_NO_THREADS || ECB_NO_SMP |
542 | #define ECB_MEMORY_FENCE do { } while (0) |
542 | #define ECB_MEMORY_FENCE do { } while (0) |
543 | #define ECB_MEMORY_FENCE_ACQUIRE do { } while (0) |
|
|
544 | #define ECB_MEMORY_FENCE_RELEASE do { } while (0) |
|
|
545 | #endif |
543 | #endif |
546 | |
544 | |
547 | #ifndef ECB_MEMORY_FENCE |
545 | #ifndef ECB_MEMORY_FENCE |
548 | #if ECB_GCC_VERSION(2,5) |
546 | #if ECB_GCC_VERSION(2,5) || defined(__INTEL_COMPILER) || defined(__clang__) |
549 | #if __x86 |
547 | #if __i386__ |
550 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("lock; orb $0, -1(%%esp)" : : : "memory") |
548 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("lock; orb $0, -1(%%esp)" : : : "memory") |
551 | #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE /* non-lock xchg might be enough */ |
549 | #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE /* non-lock xchg might be enough */ |
552 | #define ECB_MEMORY_FENCE_RELEASE do { } while (0) /* unlikely to change in future cpus */ |
550 | #define ECB_MEMORY_FENCE_RELEASE do { } while (0) /* unlikely to change in future cpus */ |
553 | #elif __amd64 |
551 | #elif __amd64 |
554 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mfence" : : : "memory") |
552 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mfence" : : : "memory") |
555 | #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("lfence" : : : "memory") |
553 | #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("lfence" : : : "memory") |
556 | #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("sfence") /* play safe - not needed in any current cpu */ |
554 | #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("sfence") /* play safe - not needed in any current cpu */ |
|
|
555 | #elif __powerpc__ || __ppc__ || __powerpc64__ || __ppc64__ |
|
|
556 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("sync" : : : "memory") |
|
|
557 | #elif defined(__ARM_ARCH_6__ ) || defined(__ARM_ARCH_6J__ ) \ |
|
|
558 | || defined(__ARM_ARCH_6K__) || defined(__ARM_ARCH_6ZK__) |
|
|
559 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mcr p15,0,%0,c7,c10,5" : : "r" (0) : "memory") |
|
|
560 | #elif defined(__ARM_ARCH_7__ ) || defined(__ARM_ARCH_7A__ ) \ |
|
|
561 | || defined(__ARM_ARCH_7M__) || defined(__ARM_ARCH_7R__ ) |
|
|
562 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("dmb" : : : "memory") |
557 | #endif |
563 | #endif |
558 | #endif |
564 | #endif |
559 | #endif |
565 | #endif |
560 | |
566 | |
561 | #ifndef ECB_MEMORY_FENCE |
567 | #ifndef ECB_MEMORY_FENCE |
562 | #if ECB_GCC_VERSION(4,4) |
568 | #if ECB_GCC_VERSION(4,4) || defined(__INTEL_COMPILER) || defined(__clang__) |
563 | #define ECB_MEMORY_FENCE __sync_synchronize () |
569 | #define ECB_MEMORY_FENCE __sync_synchronize () |
564 | #define ECB_MEMORY_FENCE_ACQUIRE ({ char dummy = 0; __sync_lock_test_and_set (&dummy, 1); }) |
570 | /*#define ECB_MEMORY_FENCE_ACQUIRE ({ char dummy = 0; __sync_lock_test_and_set (&dummy, 1); }) */ |
565 | #define ECB_MEMORY_FENCE_RELEASE ({ char dummy = 1; __sync_lock_release (&dummy ); }) |
571 | /*#define ECB_MEMORY_FENCE_RELEASE ({ char dummy = 1; __sync_lock_release (&dummy ); }) */ |
566 | #elif _MSC_VER >= 1400 /* VC++ 2005 */ |
572 | #elif _MSC_VER >= 1400 /* VC++ 2005 */ |
567 | #pragma intrinsic(_ReadBarrier,_WriteBarrier,_ReadWriteBarrier) |
573 | #pragma intrinsic(_ReadBarrier,_WriteBarrier,_ReadWriteBarrier) |
568 | #define ECB_MEMORY_FENCE _ReadWriteBarrier () |
574 | #define ECB_MEMORY_FENCE _ReadWriteBarrier () |
569 | #define ECB_MEMORY_FENCE_ACQUIRE _ReadWriteBarrier () /* according to msdn, _ReadBarrier is not a load fence */ |
575 | #define ECB_MEMORY_FENCE_ACQUIRE _ReadWriteBarrier () /* according to msdn, _ReadBarrier is not a load fence */ |
570 | #define ECB_MEMORY_FENCE_RELEASE _WriteBarrier () |
576 | #define ECB_MEMORY_FENCE_RELEASE _WriteBarrier () |
571 | #elif defined(_WIN32) |
577 | #elif defined(_WIN32) |
572 | #include <WinNT.h> |
578 | #include <WinNT.h> |
573 | #define ECB_MEMORY_FENCE MemoryBarrier () /* actually just xchg on x86... scary */ |
579 | #define ECB_MEMORY_FENCE MemoryBarrier () /* actually just xchg on x86... scary */ |
574 | #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE |
|
|
575 | #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE |
|
|
576 | #endif |
580 | #endif |
577 | #endif |
581 | #endif |
578 | |
582 | |
579 | #ifndef ECB_MEMORY_FENCE |
583 | #ifndef ECB_MEMORY_FENCE |
|
|
584 | #if !ECB_AVOID_PTHREADS |
580 | /* |
585 | /* |
581 | * if you get undefined symbol references to pthread_mutex_lock, |
586 | * if you get undefined symbol references to pthread_mutex_lock, |
582 | * or failure to find pthread.h, then you should implement |
587 | * or failure to find pthread.h, then you should implement |
583 | * the ECB_MEMORY_FENCE operations for your cpu/compiler |
588 | * the ECB_MEMORY_FENCE operations for your cpu/compiler |
584 | * OR provide pthread.h and link against the posix thread library |
589 | * OR provide pthread.h and link against the posix thread library |
585 | * of your system. |
590 | * of your system. |
586 | */ |
591 | */ |
587 | #include <pthread.h> |
592 | #include <pthread.h> |
588 | #define ECB_NEEDS_PTHREADS 1 |
593 | #define ECB_NEEDS_PTHREADS 1 |
589 | #define ECB_MEMORY_FENCE_NEEDS_PTHREADS 1 |
594 | #define ECB_MEMORY_FENCE_NEEDS_PTHREADS 1 |
590 | |
595 | |
591 | static pthread_mutex_t ecb_mf_lock = PTHREAD_MUTEX_INITIALIZER; |
596 | static pthread_mutex_t ecb_mf_lock = PTHREAD_MUTEX_INITIALIZER; |
592 | #define ECB_MEMORY_FENCE do { pthread_mutex_lock (&ecb_mf_lock); pthread_mutex_unlock (&ecb_mf_lock); } while (0) |
597 | #define ECB_MEMORY_FENCE do { pthread_mutex_lock (&ecb_mf_lock); pthread_mutex_unlock (&ecb_mf_lock); } while (0) |
|
|
598 | #endif |
|
|
599 | #endif |
|
|
600 | |
|
|
601 | #if !defined(ECB_MEMORY_FENCE_ACQUIRE) && defined(ECB_MEMORY_FENCE) |
593 | #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE |
602 | #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE |
|
|
603 | #endif |
|
|
604 | |
|
|
605 | #if !defined(ECB_MEMORY_FENCE_RELEASE) && defined(ECB_MEMORY_FENCE) |
594 | #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE |
606 | #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE |
595 | #endif |
607 | #endif |
596 | |
608 | |
597 | /*****************************************************************************/ |
609 | /*****************************************************************************/ |
598 | |
610 | |
… | |
… | |
831 | #define ecb_mod(m,n) ((m) % (n) + ((m) % (n) < 0 ? (n) : 0)) |
843 | #define ecb_mod(m,n) ((m) % (n) + ((m) % (n) < 0 ? (n) : 0)) |
832 | #else |
844 | #else |
833 | #define ecb_mod(m,n) ((m) < 0 ? ((n) - 1 - ((-1 - (m)) % (n))) : ((m) % (n))) |
845 | #define ecb_mod(m,n) ((m) < 0 ? ((n) - 1 - ((-1 - (m)) % (n))) : ((m) % (n))) |
834 | #endif |
846 | #endif |
835 | |
847 | |
|
|
848 | #if __cplusplus |
|
|
849 | template<typename T> |
|
|
850 | static inline T ecb_div_rd (T val, T div) |
|
|
851 | { |
|
|
852 | return val < 0 ? - ((-val + div - 1) / div) : (val ) / div; |
|
|
853 | } |
|
|
854 | template<typename T> |
|
|
855 | static inline T ecb_div_ru (T val, T div) |
|
|
856 | { |
|
|
857 | return val < 0 ? - ((-val ) / div) : (val + div - 1) / div; |
|
|
858 | } |
|
|
859 | #else |
|
|
860 | #define ecb_div_rd(val,div) ((val) < 0 ? - ((-(val) + (div) - 1) / (div)) : ((val) ) / (div)) |
|
|
861 | #define ecb_div_ru(val,div) ((val) < 0 ? - ((-(val) ) / (div)) : ((val) + (div) - 1) / (div)) |
|
|
862 | #endif |
|
|
863 | |
836 | #if ecb_cplusplus_does_not_suck |
864 | #if ecb_cplusplus_does_not_suck |
837 | /* does not work for local types (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2657.htm) */ |
865 | /* does not work for local types (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2657.htm) */ |
838 | template<typename T, int N> |
866 | template<typename T, int N> |
839 | static inline int ecb_array_length (const T (&arr)[N]) |
867 | static inline int ecb_array_length (const T (&arr)[N]) |
840 | { |
868 | { |
… | |
… | |
845 | #endif |
873 | #endif |
846 | |
874 | |
847 | #endif |
875 | #endif |
848 | |
876 | |
849 | /* ECB.H END */ |
877 | /* ECB.H END */ |
|
|
878 | |
|
|
879 | #if ECB_MEMORY_FENCE_NEEDS_PTHREADS |
|
|
880 | /* if your architecture doesn't need memory fences, e.g. because it is |
|
|
881 | * single-cpu/core, or if you use libev in a project that doesn't use libev |
|
|
882 | * from multiple threads, then you can define ECB_AVOID_PTHREADS when compiling |
|
|
883 | * libev, in which casess the memory fences become nops. |
|
|
884 | * alternatively, you can remove this #error and link against libpthread, |
|
|
885 | * which will then provide the memory fences. |
|
|
886 | */ |
|
|
887 | # error "memory fences not defined for your architecture, please report" |
|
|
888 | #endif |
|
|
889 | |
|
|
890 | #ifndef ECB_MEMORY_FENCE |
|
|
891 | # define ECB_MEMORY_FENCE do { } while (0) |
|
|
892 | # define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE |
|
|
893 | # define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE |
|
|
894 | #endif |
850 | |
895 | |
851 | #define expect_false(cond) ecb_expect_false (cond) |
896 | #define expect_false(cond) ecb_expect_false (cond) |
852 | #define expect_true(cond) ecb_expect_true (cond) |
897 | #define expect_true(cond) ecb_expect_true (cond) |
853 | #define noinline ecb_noinline |
898 | #define noinline ecb_noinline |
854 | |
899 | |
… | |
… | |
1754 | |
1799 | |
1755 | if (pipe_write_wanted) |
1800 | if (pipe_write_wanted) |
1756 | { |
1801 | { |
1757 | int old_errno; |
1802 | int old_errno; |
1758 | |
1803 | |
1759 | pipe_write_skipped = 0; /* just an optimsiation, no fence needed */ |
1804 | pipe_write_skipped = 0; /* just an optimisation, no fence needed */ |
1760 | |
1805 | |
1761 | old_errno = errno; /* save errno because write will clobber it */ |
1806 | old_errno = errno; /* save errno because write will clobber it */ |
1762 | |
1807 | |
1763 | #if EV_USE_EVENTFD |
1808 | #if EV_USE_EVENTFD |
1764 | if (evfd >= 0) |
1809 | if (evfd >= 0) |