From 3378539884a8cec2ba7b0c0d9237390842cd30e1 Mon Sep 17 00:00:00 2001 From: Test_User Date: Thu, 20 Jun 2024 05:49:57 -0400 Subject: Only syscall to wake if there's one to be woken --- mutex.h | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'mutex.h') diff --git a/mutex.h b/mutex.h index b0182a2..4e42dab 100644 --- a/mutex.h +++ b/mutex.h @@ -42,14 +42,16 @@ inline int mutex_init(uint32_t *futex) { } inline void mutex_lock(uint32_t *futex) { - uint32_t val; - while ((val = __sync_lock_test_and_set(futex, 1))) - syscall(SYS_futex, futex, FUTEX_PRIVATE_FLAG | FUTEX_WAIT, val, 0, 0, 0); + if (__sync_fetch_and_or(futex, 0x1) == 0) + return; + + while (__sync_fetch_and_or(futex, 0x3) != 0) + syscall(SYS_futex, futex, FUTEX_PRIVATE_FLAG | FUTEX_WAIT, 3, 0, 0, 0); } inline void mutex_unlock(uint32_t *futex) { - __sync_lock_release(futex); - syscall(SYS_futex, futex, FUTEX_PRIVATE_FLAG | FUTEX_WAKE, 1, 0, 0, 0); + if (__sync_fetch_and_and(futex, 0) & 0x2) + syscall(SYS_futex, futex, FUTEX_PRIVATE_FLAG | FUTEX_WAKE, 1, 0, 0, 0); } inline void mutex_destroy(uint32_t *futex) { -- cgit v1.2.3