Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Linux futex based Read-Write Lock implementation
#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);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment