From 8f7eb2d0e9fcdc1e192c2df623afff3f7e7d1178 Mon Sep 17 00:00:00 2001 From: IndecisiveTurtle <47210458+raphaelthegreat@users.noreply.github.com> Date: Tue, 24 Dec 2024 15:39:17 +0200 Subject: [PATCH] semaphore: Attempt to acquire before checking timeout * The posix specification says that if the object can be acquired immediately, timeout doesnt matter --- src/core/libraries/kernel/sync/semaphore.h | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/core/libraries/kernel/sync/semaphore.h b/src/core/libraries/kernel/sync/semaphore.h index 314e2cfa..738df37d 100644 --- a/src/core/libraries/kernel/sync/semaphore.h +++ b/src/core/libraries/kernel/sync/semaphore.h @@ -90,7 +90,7 @@ public: const auto start_time = std::chrono::high_resolution_clock::now(); auto rel_time_ms = std::chrono::ceil(rel_time); - while (rel_time_ms.count() > 0) { + do { u64 timeout_ms = static_cast(rel_time_ms.count()); u64 res = WaitForSingleObjectEx(sem, timeout_ms, true); if (res == WAIT_OBJECT_0) { @@ -101,7 +101,7 @@ public: } else { return false; } - } + } while (rel_time_ms.count() > 0); return false; #elif defined(__APPLE__) @@ -117,12 +117,9 @@ public: bool try_acquire_until(const std::chrono::time_point& abs_time) { #ifdef _WIN64 const auto start_time = Clock::now(); - if (start_time >= abs_time) { - return false; - } auto rel_time = std::chrono::ceil(abs_time - start_time); - while (rel_time.count() > 0) { + do { u64 timeout_ms = static_cast(rel_time.count()); u64 res = WaitForSingleObjectEx(sem, timeout_ms, true); if (res == WAIT_OBJECT_0) { @@ -133,7 +130,7 @@ public: } else { return false; } - } + } while (rel_time.count() > 0); return false; #elif defined(__APPLE__)