diff options
author | Chris Robinson <[email protected]> | 2018-11-13 20:26:32 -0800 |
---|---|---|
committer | Chris Robinson <[email protected]> | 2018-11-13 20:26:32 -0800 |
commit | a5f68c21214e6f85ade835cd29045026797ac2a4 (patch) | |
tree | 3b995975906303cfc42b8dc3054f3bbed632dbbb /common | |
parent | 5867c7b8c213aa47659e7c6e6cafddc643d9ea76 (diff) |
Avoid using ATOMIC_FLAG
Although it cant potentially be better than a regular atomic, it presents
compatibility issues when non-C11 atomics are mixed with C++
Diffstat (limited to 'common')
-rw-r--r-- | common/atomic.h | 30 | ||||
-rw-r--r-- | common/rwlock.c | 10 | ||||
-rw-r--r-- | common/rwlock.h | 9 |
3 files changed, 10 insertions, 39 deletions
diff --git a/common/atomic.h b/common/atomic.h index 17e616bb..2c81f62f 100644 --- a/common/atomic.h +++ b/common/atomic.h @@ -43,8 +43,6 @@ using std::memory_order_release; using std::memory_order_acq_rel; using std::memory_order_seq_cst; -using std::atomic_flag; - using std::atomic_init; using std::atomic_load_explicit; using std::atomic_store_explicit; @@ -53,8 +51,6 @@ using std::atomic_fetch_sub_explicit; using std::atomic_exchange_explicit; using std::atomic_compare_exchange_strong_explicit; using std::atomic_compare_exchange_weak_explicit; -using std::atomic_flag_test_and_set_explicit; -using std::atomic_flag_clear_explicit; using std::atomic_thread_fence; #else @@ -79,11 +75,9 @@ extern "C" { #define almemory_order_seq_cst memory_order_seq_cst #define ATOMIC(T) _Atomic(T) -#define ATOMIC_FLAG atomic_flag #define ATOMIC_INIT atomic_init #define ATOMIC_INIT_STATIC ATOMIC_VAR_INIT -/*#define ATOMIC_FLAG_INIT ATOMIC_FLAG_INIT*/ #define ATOMIC_LOAD atomic_load_explicit #define ATOMIC_STORE atomic_store_explicit @@ -95,9 +89,6 @@ extern "C" { #define ATOMIC_COMPARE_EXCHANGE_STRONG atomic_compare_exchange_strong_explicit #define ATOMIC_COMPARE_EXCHANGE_WEAK atomic_compare_exchange_weak_explicit -#define ATOMIC_FLAG_TEST_AND_SET atomic_flag_test_and_set_explicit -#define ATOMIC_FLAG_CLEAR atomic_flag_clear_explicit - #define ATOMIC_THREAD_FENCE atomic_thread_fence /* Atomics using GCC intrinsics */ @@ -113,11 +104,9 @@ enum almemory_order { }; #define ATOMIC(T) struct { T volatile value; } -#define ATOMIC_FLAG ATOMIC(int) #define ATOMIC_INIT(_val, _newval) do { (_val)->value = (_newval); } while(0) #define ATOMIC_INIT_STATIC(_newval) {(_newval)} -#define ATOMIC_FLAG_INIT ATOMIC_INIT_STATIC(0) #define ATOMIC_LOAD(_val, _MO) __extension__({ \ __typeof((_val)->value) _r = (_val)->value; \ @@ -142,15 +131,6 @@ enum almemory_order { *(_oldval) == _o; \ }) -#define ATOMIC_FLAG_TEST_AND_SET(_val, _MO) __extension__({ \ - __asm__ __volatile__("" ::: "memory"); \ - __sync_lock_test_and_set(&(_val)->value, 1); \ -}) -#define ATOMIC_FLAG_CLEAR(_val, _MO) __extension__({ \ - __sync_lock_release(&(_val)->value); \ - __asm__ __volatile__("" ::: "memory"); \ -}) - #define ATOMIC_THREAD_FENCE(order) do { \ enum { must_be_constant = (order) }; \ @@ -421,16 +401,6 @@ void *_al_invalid_atomic_ptr_size(); /* not defined */ #define ATOMIC_COMPARE_EXCHANGE_PTR_WEAK ATOMIC_COMPARE_EXCHANGE_PTR_STRONG #endif -/* If no ATOMIC_FLAG is defined, simulate one with an atomic int using exchange - * and store ops. - */ -#ifndef ATOMIC_FLAG -#define ATOMIC_FLAG ATOMIC(int) -#define ATOMIC_FLAG_INIT ATOMIC_INIT_STATIC(0) -#define ATOMIC_FLAG_TEST_AND_SET(_val, _MO) ATOMIC_EXCHANGE(_val, 1, _MO) -#define ATOMIC_FLAG_CLEAR(_val, _MO) ATOMIC_STORE(_val, 0, _MO) -#endif - #define ATOMIC_LOAD_SEQ(_val) ATOMIC_LOAD(_val, almemory_order_seq_cst) #define ATOMIC_STORE_SEQ(_val, _newval) ATOMIC_STORE(_val, _newval, almemory_order_seq_cst) diff --git a/common/rwlock.c b/common/rwlock.c index 67cf3acf..44237282 100644 --- a/common/rwlock.c +++ b/common/rwlock.c @@ -11,19 +11,19 @@ /* A simple spinlock. Yield the thread while the given integer is set by * another. Could probably be improved... */ #define LOCK(l) do { \ - while(ATOMIC_FLAG_TEST_AND_SET(&(l), almemory_order_acq_rel) == true) \ + while(ATOMIC_EXCHANGE(&(l), 1, almemory_order_acq_rel) == true) \ althrd_yield(); \ } while(0) -#define UNLOCK(l) ATOMIC_FLAG_CLEAR(&(l), almemory_order_release) +#define UNLOCK(l) ATOMIC_STORE(&(l), 0, almemory_order_release) void RWLockInit(RWLock *lock) { InitRef(&lock->read_count, 0); InitRef(&lock->write_count, 0); - ATOMIC_FLAG_CLEAR(&lock->read_lock, almemory_order_relaxed); - ATOMIC_FLAG_CLEAR(&lock->read_entry_lock, almemory_order_relaxed); - ATOMIC_FLAG_CLEAR(&lock->write_lock, almemory_order_relaxed); + ATOMIC_INIT(&lock->read_lock, 0); + ATOMIC_INIT(&lock->read_entry_lock, 0); + ATOMIC_INIT(&lock->write_lock, 0); } void ReadLock(RWLock *lock) diff --git a/common/rwlock.h b/common/rwlock.h index 8e36fa1a..fee1b070 100644 --- a/common/rwlock.h +++ b/common/rwlock.h @@ -11,12 +11,13 @@ extern "C" { typedef struct { RefCount read_count; RefCount write_count; - ATOMIC_FLAG read_lock; - ATOMIC_FLAG read_entry_lock; - ATOMIC_FLAG write_lock; + ATOMIC(int) read_lock; + ATOMIC(int) read_entry_lock; + ATOMIC(int) write_lock; } RWLock; #define RWLOCK_STATIC_INITIALIZE { ATOMIC_INIT_STATIC(0), ATOMIC_INIT_STATIC(0), \ - ATOMIC_FLAG_INIT, ATOMIC_FLAG_INIT, ATOMIC_FLAG_INIT } + ATOMIC_INIT_STATIC(0), ATOMIC_INIT_STATIC(0), \ + ATOMIC_INIT_STATIC(0) } void RWLockInit(RWLock *lock); void ReadLock(RWLock *lock); |