[threading] Simplify and test Fence

Remove atomic boolean in fence. Variable signaled_ is already protected
by mutex.
Remove wait loop with single predicate wait protected with mutex.

Add Fence Signal and Wait tests
Test signaling without waiting.
Test signaling before waiting.
Test signaling twice before waiting.
Test synchronizing threads with fence.

Few REQUIRES were used to test as there are no return codes.
A failing test may hang indefinitely or cause a segfault which would still
register as a fail.
This commit is contained in:
Sandy Carter
2018-03-11 16:22:53 -04:00
committed by Rick Gibbed
parent b5ea686475
commit 4280a6451d
2 changed files with 54 additions and 9 deletions

View File

@@ -32,21 +32,19 @@ class Fence {
Fence() : signaled_(false) {}
void Signal() {
std::unique_lock<std::mutex> lock(mutex_);
signaled_.store(true);
signaled_ = true;
cond_.notify_all();
}
void Wait() {
std::unique_lock<std::mutex> lock(mutex_);
while (!signaled_.load()) {
cond_.wait(lock);
}
signaled_.store(false);
cond_.wait(lock, [this] { return signaled_; });
signaled_ = false;
}
private:
std::mutex mutex_;
std::condition_variable cond_;
std::atomic<bool> signaled_;
bool signaled_;
};
// Returns the total number of logical processors in the host system.