… | |
… | |
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) |
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) |
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 | |
… | |
… | |
845 | #endif |
857 | #endif |
846 | |
858 | |
847 | #endif |
859 | #endif |
848 | |
860 | |
849 | /* ECB.H END */ |
861 | /* ECB.H END */ |
|
|
862 | |
|
|
863 | #if ECB_MEMORY_FENCE_NEEDS_PTHREADS |
|
|
864 | /* if your architetcure doesn't need memory fences, e.g. because it is |
|
|
865 | * single-cpu/core, or if you use libev in a project that doesn't use libev |
|
|
866 | * from multiple threads, then you can define ECB_AVOID_PTHREADS when compiling |
|
|
867 | * libev, in which casess the memory fences become nops. |
|
|
868 | * alternatively, you can remove this #error and link against libpthread, |
|
|
869 | * which will then provide the memory fences. |
|
|
870 | */ |
|
|
871 | # error "memory fences not defined for your architecture, please report" |
|
|
872 | #endif |
|
|
873 | |
|
|
874 | #ifndef ECB_MEMORY_FENCE |
|
|
875 | # define ECB_MEMORY_FENCE do { } while (0) |
|
|
876 | # define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE |
|
|
877 | # define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE |
|
|
878 | #endif |
850 | |
879 | |
851 | #define expect_false(cond) ecb_expect_false (cond) |
880 | #define expect_false(cond) ecb_expect_false (cond) |
852 | #define expect_true(cond) ecb_expect_true (cond) |
881 | #define expect_true(cond) ecb_expect_true (cond) |
853 | #define noinline ecb_noinline |
882 | #define noinline ecb_noinline |
854 | |
883 | |
… | |
… | |
1754 | |
1783 | |
1755 | if (pipe_write_wanted) |
1784 | if (pipe_write_wanted) |
1756 | { |
1785 | { |
1757 | int old_errno; |
1786 | int old_errno; |
1758 | |
1787 | |
1759 | pipe_write_skipped = 0; /* just an optimsiation, no fence needed */ |
1788 | pipe_write_skipped = 0; /* just an optimisation, no fence needed */ |
1760 | |
1789 | |
1761 | old_errno = errno; /* save errno because write will clobber it */ |
1790 | old_errno = errno; /* save errno because write will clobber it */ |
1762 | |
1791 | |
1763 | #if EV_USE_EVENTFD |
1792 | #if EV_USE_EVENTFD |
1764 | if (evfd >= 0) |
1793 | if (evfd >= 0) |