From 88864140133e5eb50f17fbe94ae4336f9ab9ae10 Mon Sep 17 00:00:00 2001 From: Nekotekina Date: Sat, 27 Jul 2019 17:08:45 +0300 Subject: [PATCH] Use atomic wait in shared_mutex and semaphore --- Utilities/mutex.cpp | 27 ++++++++++++++++----------- Utilities/sema.cpp | 20 +++----------------- 2 files changed, 19 insertions(+), 28 deletions(-) diff --git a/Utilities/mutex.cpp b/Utilities/mutex.cpp index ae0436c188..b8d199ea56 100644 --- a/Utilities/mutex.cpp +++ b/Utilities/mutex.cpp @@ -1,7 +1,4 @@ #include "mutex.h" -#include "sync.h" - -#include void shared_mutex::imp_lock_shared(u32 val) { @@ -122,24 +119,32 @@ void shared_mutex::imp_unlock_vip(u32 old) void shared_mutex::imp_wait() { - while (!balanced_wait_until(m_value, -1, [&](u32& value, auto...) + while (true) { - if (value >= c_sig) + const auto [old, ok] = m_value.fetch_op([](u32& value) { - value -= c_sig; - return true; + if (value >= c_sig) + { + value -= c_sig; + return true; + } + + return false; + }); + + if (ok) + { + break; } - return false; - })) - { + m_value.wait(old); } } void shared_mutex::imp_signal() { m_value += c_sig; - balanced_awaken(m_value, 1); + m_value.notify_one(); } void shared_mutex::imp_lock(u32 val) diff --git a/Utilities/sema.cpp b/Utilities/sema.cpp index 7e2a4ca834..e4e3e7345f 100644 --- a/Utilities/sema.cpp +++ b/Utilities/sema.cpp @@ -1,5 +1,4 @@ #include "sema.h" -#include "sync.h" void semaphore_base::imp_wait() { @@ -15,14 +14,6 @@ void semaphore_base::imp_wait() } } -#ifdef _WIN32 - const s32 value = m_value.fetch_sub(1); - - if (value <= 0) - { - NtWaitForKeyedEvent(nullptr, &m_value, false, nullptr); - } -#else while (true) { // Try hard way @@ -51,24 +42,19 @@ void semaphore_base::imp_wait() if (value >= 0) { // Signal other waiter to wake up or to restore sign bit - futex(&m_value, FUTEX_WAKE_PRIVATE, 1); + m_value.notify_one(); return; } - futex(&m_value, FUTEX_WAIT_PRIVATE, value); + m_value.wait(value); } -#endif } void semaphore_base::imp_post(s32 _old) { verify("semaphore_base: overflow" HERE), _old < 0; -#ifdef _WIN32 - NtReleaseKeyedEvent(nullptr, &m_value, false, nullptr); -#else - futex(&m_value, FUTEX_WAKE_PRIVATE, 1); -#endif + m_value.notify_one(); } bool semaphore_base::try_post(s32 _max)