From 4d0d3f3ad4b4f35a749131a5193805028e072e6d Mon Sep 17 00:00:00 2001 From: Joel Linn Date: Tue, 18 May 2021 16:07:17 +0200 Subject: [PATCH] [Base] Fix semaphore test - Was using `Sleep()` - Replaced with atomic value and bool flag --- src/xenia/base/testing/threading_test.cc | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/xenia/base/testing/threading_test.cc b/src/xenia/base/testing/threading_test.cc index e15fcc3a2..2eec1e320 100644 --- a/src/xenia/base/testing/threading_test.cc +++ b/src/xenia/base/testing/threading_test.cc @@ -406,9 +406,13 @@ TEST_CASE("Wait on Semaphore", "Semaphore") { sem = Semaphore::Create(5, 5); Sleep(10ms); // Occupy the semaphore with 5 threads - auto func = [&sem] { + std::atomic wait_count(0); + volatile bool threads_terminate(false); + auto func = [&sem, &wait_count, &threads_terminate] { auto res = Wait(sem.get(), false, 100ms); - Sleep(500ms); + wait_count++; + while (!threads_terminate) { + } if (res == WaitResult::kSuccess) { sem->Release(1, nullptr); } @@ -417,12 +421,14 @@ TEST_CASE("Wait on Semaphore", "Semaphore") { std::thread(func), std::thread(func), std::thread(func), std::thread(func), std::thread(func), }; - // Give threads time to acquire semaphore - Sleep(10ms); + // Wait for threads to finish semaphore calls + while (wait_count != 5) { + } // Attempt to acquire full semaphore with current (6th) thread result = Wait(sem.get(), false, 20ms); REQUIRE(result == WaitResult::kTimeout); // Give threads time to release semaphore + threads_terminate = true; for (auto& t : threads) { t.join(); }