diff options
-rw-r--r-- | ecb.h | 43 | ||||
-rw-r--r-- | eio.c | 36 |
2 files changed, 51 insertions, 28 deletions
@@ -63,6 +63,49 @@ #endif #endif +/*****************************************************************************/ + +#ifndef ECB_MEMORY_FENCE + #if ECB_GCC_VERSION(2,5) + #if __x86 + #define ECB_MEMORY_FENCE __asm__ __volatile__ ("lock; or.b $0, -1(%%esp)" : : : "memory") + #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE + #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE /* better be safe than sorry */ + #elif __amd64 + #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mfence" : : : "memory") + #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("lfence" : : : "memory") + #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("sfence") + #endif + #endif +#endif + +#ifndef ECB_MEMORY_FENCE + #if ECB_GCC_VERSION(4,4) + #define ECB_MEMORY_FENCE __sync_synchronize () + #define ECB_MEMORY_FENCE_ACQUIRE ({ char dummy = 0; __sync_lock_test_and_set (&dummy, 1); }) + #define ECB_MEMORY_FENCE_RELEASE ({ char dummy = 1; __sync_lock_release (&dummy ); }) + #elif _MSC_VER >= 1400 + #define ECB_MEMORY_FENCE do { } while (0) + #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE + #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE + #elif defined(_WIN32) && defined(MemoryBarrier) + #define ECB_MEMORY_FENCE MemoryBarrier () + #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE + #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE + #endif +#endif + +#ifndef ECB_MEMORY_FENCE + #include <pthread.h> + + static pthread_mutex_t ecb_mf_lock = PTHREAD_MUTEX_INITIALIZER; + #define ECB_MEMORY_FENCE do { pthread_mutex_lock (&ecb_mf_lock); pthread_mutex_unlock (&ecb_mf_lock); } while (0) + #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE + #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE +#endif + +/*****************************************************************************/ + #define ECB_C99 (__STDC_VERSION__ >= 199901L) #if __cplusplus @@ -304,7 +304,7 @@ static xcond_t reqwait; * normal read/write by using a mutex. slows down execution a lot, * but that's your problem, not mine. */ -static xmutex_t preadwritelock = X_MUTEX_INIT; +static xmutex_t preadwritelock; #endif typedef struct etp_worker @@ -320,7 +320,7 @@ typedef struct etp_worker ETP_WORKER_COMMON } etp_worker; -static etp_worker wrk_first = { &wrk_first, &wrk_first, 0 }; /* NOT etp */ +static etp_worker wrk_first; /* NOT etp */ #define ETP_WORKER_LOCK(wrk) X_LOCK (wrklock) #define ETP_WORKER_UNLOCK(wrk) X_UNLOCK (wrklock) @@ -455,21 +455,14 @@ reqq_shift (etp_reqq *q) abort (); } -static void ecb_cold -etp_thread_init (void) +static int ecb_cold +etp_init (void (*want_poll)(void), void (*done_poll)(void)) { -#if !HAVE_PREADWRITE - X_MUTEX_CREATE (preadwritelock); -#endif X_MUTEX_CREATE (wrklock); X_MUTEX_CREATE (reslock); X_MUTEX_CREATE (reqlock); X_COND_CREATE (reqwait); -} -static void ecb_cold -etp_atfork_child (void) -{ reqq_init (&req_queue); reqq_init (&res_queue); @@ -482,23 +475,6 @@ etp_atfork_child (void) nready = 0; npending = 0; - etp_thread_init (); -} - -static void ecb_cold -etp_once_init (void) -{ - etp_thread_init (); - X_THREAD_ATFORK (0, 0, etp_atfork_child); -} - -static int ecb_cold -etp_init (void (*want_poll)(void), void (*done_poll)(void)) -{ - static pthread_once_t doinit = PTHREAD_ONCE_INIT; - - pthread_once (&doinit, etp_once_init); - want_poll_cb = want_poll; done_poll_cb = done_poll; @@ -1993,6 +1969,10 @@ quit: int ecb_cold eio_init (void (*want_poll)(void), void (*done_poll)(void)) { +#if !HAVE_PREADWRITE + X_MUTEX_CREATE (preadwritelock); +#endif + return etp_init (want_poll, done_poll); } |