From e0675785bda5bbcd4a5ebe31dd589f522b2f3ebc Mon Sep 17 00:00:00 2001 From: squidbus <175574877+squidbus@users.noreply.github.com> Date: Thu, 28 Nov 2024 19:12:47 -0800 Subject: [PATCH] Revert "semaphore: Use binary_semaphore instead of condvar" This reverts commit 85dc57b86875f7b0807dbc8d886cd5b1bdc407e7. --- .../libraries/kernel/threads/semaphore.cpp | 24 +++++++++---------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/src/core/libraries/kernel/threads/semaphore.cpp b/src/core/libraries/kernel/threads/semaphore.cpp index 9fcbd4356..177dc4a8c 100644 --- a/src/core/libraries/kernel/threads/semaphore.cpp +++ b/src/core/libraries/kernel/threads/semaphore.cpp @@ -49,9 +49,7 @@ public: const auto it = AddWaiter(&waiter); // Perform the wait. - lk.unlock(); - const s32 result = waiter.Wait(timeout); - lk.lock(); + const s32 result = waiter.Wait(lk, timeout); if (result == SCE_KERNEL_ERROR_ETIMEDOUT) { wait_list.erase(it); } @@ -74,7 +72,7 @@ public: } it = wait_list.erase(it); token_count -= waiter->need_count; - waiter->sema.release(); + waiter->cv.notify_one(); } return true; @@ -87,7 +85,7 @@ public: } for (auto* waiter : wait_list) { waiter->was_cancled = true; - waiter->sema.release(); + waiter->cv.notify_one(); } wait_list.clear(); token_count = set_count < 0 ? init_count : set_count; @@ -98,20 +96,20 @@ public: std::scoped_lock lk{mutex}; for (auto* waiter : wait_list) { waiter->was_deleted = true; - waiter->sema.release(); + waiter->cv.notify_one(); } wait_list.clear(); } public: struct WaitingThread { - std::binary_semaphore sema; + std::condition_variable cv; u32 priority; s32 need_count; bool was_deleted{}; bool was_cancled{}; - explicit WaitingThread(s32 need_count, bool is_fifo) : sema{0}, need_count{need_count} { + explicit WaitingThread(s32 need_count, bool is_fifo) : need_count{need_count} { // Retrieve calling thread priority for sorting into waiting threads list. if (!is_fifo) { priority = g_curthread->attr.prio; @@ -131,24 +129,24 @@ public: return SCE_OK; } - int Wait(u32* timeout) { + int Wait(std::unique_lock& lk, u32* timeout) { if (!timeout) { // Wait indefinitely until we are woken up. - sema.acquire(); + cv.wait(lk); return GetResult(false); } // Wait until timeout runs out, recording how much remaining time there was. const auto start = std::chrono::high_resolution_clock::now(); - const auto sema_timeout = !sema.try_acquire_for(std::chrono::microseconds(*timeout)); + const auto status = cv.wait_for(lk, std::chrono::microseconds(*timeout)); const auto end = std::chrono::high_resolution_clock::now(); const auto time = std::chrono::duration_cast(end - start).count(); - if (sema_timeout) { + if (status == std::cv_status::timeout) { *timeout = 0; } else { *timeout -= time; } - return GetResult(sema_timeout); + return GetResult(status == std::cv_status::timeout); } };