Last active
December 16, 2023 23:45
-
-
Save smokku/653c469d695d60be4fe8170630ba8205 to your computer and use it in GitHub Desktop.
Linux futex based Read-Write Lock implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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