[Base] Fix semaphore test
- Was using `Sleep()` - Replaced with atomic value and bool flag
This commit is contained in:
parent
2d9462f02b
commit
4d0d3f3ad4
|
@ -406,9 +406,13 @@ TEST_CASE("Wait on Semaphore", "Semaphore") {
|
||||||
sem = Semaphore::Create(5, 5);
|
sem = Semaphore::Create(5, 5);
|
||||||
Sleep(10ms);
|
Sleep(10ms);
|
||||||
// Occupy the semaphore with 5 threads
|
// Occupy the semaphore with 5 threads
|
||||||
auto func = [&sem] {
|
std::atomic<int> wait_count(0);
|
||||||
|
volatile bool threads_terminate(false);
|
||||||
|
auto func = [&sem, &wait_count, &threads_terminate] {
|
||||||
auto res = Wait(sem.get(), false, 100ms);
|
auto res = Wait(sem.get(), false, 100ms);
|
||||||
Sleep(500ms);
|
wait_count++;
|
||||||
|
while (!threads_terminate) {
|
||||||
|
}
|
||||||
if (res == WaitResult::kSuccess) {
|
if (res == WaitResult::kSuccess) {
|
||||||
sem->Release(1, nullptr);
|
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), std::thread(func),
|
||||||
std::thread(func), std::thread(func),
|
std::thread(func), std::thread(func),
|
||||||
};
|
};
|
||||||
// Give threads time to acquire semaphore
|
// Wait for threads to finish semaphore calls
|
||||||
Sleep(10ms);
|
while (wait_count != 5) {
|
||||||
|
}
|
||||||
// Attempt to acquire full semaphore with current (6th) thread
|
// Attempt to acquire full semaphore with current (6th) thread
|
||||||
result = Wait(sem.get(), false, 20ms);
|
result = Wait(sem.get(), false, 20ms);
|
||||||
REQUIRE(result == WaitResult::kTimeout);
|
REQUIRE(result == WaitResult::kTimeout);
|
||||||
// Give threads time to release semaphore
|
// Give threads time to release semaphore
|
||||||
|
threads_terminate = true;
|
||||||
for (auto& t : threads) {
|
for (auto& t : threads) {
|
||||||
t.join();
|
t.join();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue