diff options
author | Chris Robinson <[email protected]> | 2014-07-22 18:57:51 -0700 |
---|---|---|
committer | Chris Robinson <[email protected]> | 2014-07-22 18:57:51 -0700 |
commit | e4b779c492e9ffbfce806ac49acae66ab264a7da (patch) | |
tree | 0ccf89e9157fe9dfa6410192d3ff4897ed1ce4bc /common | |
parent | a3b1d4a5e20fade26bdfe2b964d8d363aac2acc3 (diff) |
Use generic atomics in more places
Diffstat (limited to 'common')
-rw-r--r-- | common/rwlock.c | 41 |
1 files changed, 18 insertions, 23 deletions
diff --git a/common/rwlock.c b/common/rwlock.c index ff7aa418..0b185c9b 100644 --- a/common/rwlock.c +++ b/common/rwlock.c @@ -10,53 +10,48 @@ /* A simple spinlock. Yield the thread while the given integer is set by * another. Could probably be improved... */ -static void Lock(volatile int *l) -{ - while(ExchangeInt(l, true) == true) - althrd_yield(); -} - -static void Unlock(volatile int *l) -{ - ExchangeInt(l, false); -} +#define LOCK(l) do { \ + while(ATOMIC_EXCHANGE(int, (l), true) == true) \ + althrd_yield(); \ +} while(0) +#define UNLOCK(l) ATOMIC_STORE(l, false) void RWLockInit(RWLock *lock) { InitRef(&lock->read_count, 0); InitRef(&lock->write_count, 0); - lock->read_lock = false; - lock->read_entry_lock = false; - lock->write_lock = false; + ATOMIC_STORE_UNSAFE(lock->read_lock, false); + ATOMIC_STORE_UNSAFE(lock->read_entry_lock, false); + ATOMIC_STORE_UNSAFE(lock->write_lock, false); } void ReadLock(RWLock *lock) { - Lock(&lock->read_entry_lock); - Lock(&lock->read_lock); + LOCK(lock->read_entry_lock); + LOCK(lock->read_lock); if(IncrementRef(&lock->read_count) == 1) - Lock(&lock->write_lock); - Unlock(&lock->read_lock); - Unlock(&lock->read_entry_lock); + LOCK(lock->write_lock); + UNLOCK(lock->read_lock); + UNLOCK(lock->read_entry_lock); } void ReadUnlock(RWLock *lock) { if(DecrementRef(&lock->read_count) == 0) - Unlock(&lock->write_lock); + UNLOCK(lock->write_lock); } void WriteLock(RWLock *lock) { if(IncrementRef(&lock->write_count) == 1) - Lock(&lock->read_lock); - Lock(&lock->write_lock); + LOCK(lock->read_lock); + LOCK(lock->write_lock); } void WriteUnlock(RWLock *lock) { - Unlock(&lock->write_lock); + UNLOCK(lock->write_lock); if(DecrementRef(&lock->write_count) == 0) - Unlock(&lock->read_lock); + UNLOCK(lock->read_lock); } |