aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTest_User <hax@andrewyu.org>2024-06-20 05:49:57 -0400
committerTest_User <hax@andrewyu.org>2024-06-20 05:49:57 -0400
commit3378539884a8cec2ba7b0c0d9237390842cd30e1 (patch)
tree4cd1188ec7259127fef6bc85452dfd075a97a64d
parentea4d0f1360c2c23764decea94efcc38a8e47cafb (diff)
downloadhaxircd-3378539884a8cec2ba7b0c0d9237390842cd30e1.tar.gz
haxircd-3378539884a8cec2ba7b0c0d9237390842cd30e1.zip
Only syscall to wake if there's one to be woken
-rw-r--r--mutex.h12
1 files changed, 7 insertions, 5 deletions
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) {