diff options
author | Chris Robinson <[email protected]> | 2018-11-17 05:31:29 -0800 |
---|---|---|
committer | Chris Robinson <[email protected]> | 2018-11-17 05:31:29 -0800 |
commit | 1fae8c16a8c0634ffa44b4a2e25f3be4899ea7e2 (patch) | |
tree | 622a682c96ec30cb08a11e6b4e6969daaaeeacf1 /common/rwlock.c | |
parent | ccdaca80c910047e16f710d44f640a6d6f86a195 (diff) |
Convert threads.c to C++
Also vastly simplify and remove related code.
Diffstat (limited to 'common/rwlock.c')
-rw-r--r-- | common/rwlock.c | 59 |
1 files changed, 0 insertions, 59 deletions
diff --git a/common/rwlock.c b/common/rwlock.c deleted file mode 100644 index 44237282..00000000 --- a/common/rwlock.c +++ /dev/null @@ -1,59 +0,0 @@ - -#include "config.h" - -#include "rwlock.h" - -#include "bool.h" -#include "atomic.h" -#include "threads.h" - - -/* 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(&(l), 1, almemory_order_acq_rel) == true) \ - althrd_yield(); \ -} while(0) -#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_INIT(&lock->read_lock, 0); - ATOMIC_INIT(&lock->read_entry_lock, 0); - ATOMIC_INIT(&lock->write_lock, 0); -} - -void ReadLock(RWLock *lock) -{ - LOCK(lock->read_entry_lock); - LOCK(lock->read_lock); - /* 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); -} - -void ReadUnlock(RWLock *lock) -{ - /* 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(ATOMIC_ADD(&lock->write_count, 1, almemory_order_acq_rel) == 0) - LOCK(lock->read_lock); - LOCK(lock->write_lock); -} - -void WriteUnlock(RWLock *lock) -{ - UNLOCK(lock->write_lock); - if(ATOMIC_SUB(&lock->write_count, 1, almemory_order_acq_rel) == 1) - UNLOCK(lock->read_lock); -} |