Fix default effect initialization
[openal-soft.git] / common / rwlock.c
blob67cf3acf06d2d9279369ceaca672c9c809b0dd15
2 #include "config.h"
4 #include "rwlock.h"
6 #include "bool.h"
7 #include "atomic.h"
8 #include "threads.h"
11 /* A simple spinlock. Yield the thread while the given integer is set by
12 * another. Could probably be improved... */
13 #define LOCK(l) do { \
14 while(ATOMIC_FLAG_TEST_AND_SET(&(l), almemory_order_acq_rel) == true) \
15 althrd_yield(); \
16 } while(0)
17 #define UNLOCK(l) ATOMIC_FLAG_CLEAR(&(l), almemory_order_release)
20 void RWLockInit(RWLock *lock)
22 InitRef(&lock->read_count, 0);
23 InitRef(&lock->write_count, 0);
24 ATOMIC_FLAG_CLEAR(&lock->read_lock, almemory_order_relaxed);
25 ATOMIC_FLAG_CLEAR(&lock->read_entry_lock, almemory_order_relaxed);
26 ATOMIC_FLAG_CLEAR(&lock->write_lock, almemory_order_relaxed);
29 void ReadLock(RWLock *lock)
31 LOCK(lock->read_entry_lock);
32 LOCK(lock->read_lock);
33 /* NOTE: ATOMIC_ADD returns the *old* value! */
34 if(ATOMIC_ADD(&lock->read_count, 1, almemory_order_acq_rel) == 0)
35 LOCK(lock->write_lock);
36 UNLOCK(lock->read_lock);
37 UNLOCK(lock->read_entry_lock);
40 void ReadUnlock(RWLock *lock)
42 /* NOTE: ATOMIC_SUB returns the *old* value! */
43 if(ATOMIC_SUB(&lock->read_count, 1, almemory_order_acq_rel) == 1)
44 UNLOCK(lock->write_lock);
47 void WriteLock(RWLock *lock)
49 if(ATOMIC_ADD(&lock->write_count, 1, almemory_order_acq_rel) == 0)
50 LOCK(lock->read_lock);
51 LOCK(lock->write_lock);
54 void WriteUnlock(RWLock *lock)
56 UNLOCK(lock->write_lock);
57 if(ATOMIC_SUB(&lock->write_count, 1, almemory_order_acq_rel) == 1)
58 UNLOCK(lock->read_lock);