aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChris Robinson <[email protected]>2016-11-22 03:40:15 -0800
committerChris Robinson <[email protected]>2016-11-22 03:40:15 -0800
commit8492c2845d805c540c115ca8c8efc552dbf2db0c (patch)
treefc821aec9f7828a753ed2094adef7c56274b97b1
parentc6189717580453b03cc70f0647a215f3f5f886fe (diff)
Avoid some unnecessary seq_cst memory ordering
-rw-r--r--common/rwlock.c14
1 files changed, 8 insertions, 6 deletions
diff --git a/common/rwlock.c b/common/rwlock.c
index cfa3aee4..f1a86fa6 100644
--- a/common/rwlock.c
+++ b/common/rwlock.c
@@ -11,10 +11,10 @@
/* A simple spinlock. Yield the thread while the given integer is set by
* another. Could probably be improved... */
#define LOCK(l) do { \
- while(ATOMIC_EXCHANGE(int, &(l), true) == true) \
+ while(ATOMIC_EXCHANGE(int, &(l), true, almemory_order_acq_rel) == true) \
althrd_yield(); \
} while(0)
-#define UNLOCK(l) ATOMIC_STORE(&(l), false)
+#define UNLOCK(l) ATOMIC_STORE(&(l), false, almemory_order_release)
void RWLockInit(RWLock *lock)
@@ -30,7 +30,8 @@ void ReadLock(RWLock *lock)
{
LOCK(lock->read_entry_lock);
LOCK(lock->read_lock);
- if(IncrementRef(&lock->read_count) == 1)
+ /* NOTE: ATOMIC_ADD returns the *old* value! */
+ if(ATOMIC_ADD(&lock->read_count, 1, almemory_order_acq_rel) == 0)
LOCK(lock->write_lock);
UNLOCK(lock->read_lock);
UNLOCK(lock->read_entry_lock);
@@ -38,13 +39,14 @@ void ReadLock(RWLock *lock)
void ReadUnlock(RWLock *lock)
{
- if(DecrementRef(&lock->read_count) == 0)
+ /* NOTE: ATOMIC_SUB returns the *old* value! */
+ if(ATOMIC_SUB(&lock->read_count, 1, almemory_order_acq_rel) == 1)
UNLOCK(lock->write_lock);
}
void WriteLock(RWLock *lock)
{
- if(IncrementRef(&lock->write_count) == 1)
+ if(ATOMIC_ADD(&lock->write_count, 1, almemory_order_acq_rel) == 0)
LOCK(lock->read_lock);
LOCK(lock->write_lock);
}
@@ -52,6 +54,6 @@ void WriteLock(RWLock *lock)
void WriteUnlock(RWLock *lock)
{
UNLOCK(lock->write_lock);
- if(DecrementRef(&lock->write_count) == 0)
+ if(ATOMIC_SUB(&lock->write_count, 1, almemory_order_acq_rel) == 1)
UNLOCK(lock->read_lock);
}