You will find my implementation at https://gist.github.com/smokku/653c469d695d60be4fe8170630ba8205
The idea is that there can be only one thread taking the lock for write (futex value 0
), lock can be open (futex value 1
) or there can be many reading threads (futex values greater than 1
). So values below 1
(there is only one) block both readers and writers on futex, and values above 1
block only writers. Unlocking thread wakes one of waiting threads, but you need to be careful not to consume a readers only wake by a writer thread.
#define cpu_relax() __builtin_ia32_pause()
#define cmpxchg(P, O, N) __sync_val_compare_and_swap((P), (O), (N))
static unsigned _lock = 1; // read-write lock futex
const static unsigned _lock_open = 1;
const static unsigned _lock_wlocked = 0;
static void _unlock()
{
unsigned current, wanted;
do {
current = _lock;
if (current == _lock_open) return;
if (current == _lock_wlocked) {
wanted = _lock_open;
} else {
wanted = current - 1;
}
} while (cmpxchg(&_lock, current, wanted) != current);
syscall(SYS_futex, &_lock, FUTEX_WAKE_PRIVATE, 1, NULL, NULL, 0);
}
static void _rlock()
{
unsigned current;
while ((current = _lock) == _lock_wlocked || cmpxchg(&_lock, current, current + 1) != current) {
while (syscall(SYS_futex, &_lock, FUTEX_WAIT_PRIVATE, current, NULL, NULL, 0) != 0) {
cpu_relax();
if (_lock >= _lock_open) break;
}
// will be able to acquire rlock no matter what unlock woke us
}
}
static void _wlock()
{
unsigned current;
while ((current = cmpxchg(&_lock, _lock_open, _lock_wlocked)) != _lock_open) {
while (syscall(SYS_futex, &_lock, FUTEX_WAIT_PRIVATE, current, NULL, NULL, 0) != 0) {
cpu_relax();
if (_lock == _lock_open) break;
}
if (_lock != _lock_open) {
// in rlock - won't be able to acquire lock - wake someone else
syscall(SYS_futex, &_lock, FUTEX_WAKE_PRIVATE, 1, NULL, NULL, 0);
}
}
}