Implement BlockingQueue::WaitUntilEmpty(). (#775)

PAIR=gaschler
master
Christoph Schütte 2017-12-20 10:22:53 +01:00 committed by Wally B. Feed
parent e0faf7094e
commit 5b5b290e9f
1 changed files with 10 additions and 4 deletions

View File

@ -67,7 +67,7 @@ class BlockingQueue {
// Pops the next value from the queue. Blocks until a value is available. // Pops the next value from the queue. Blocks until a value is available.
T Pop() { T Pop() {
MutexLocker lock(&mutex_); MutexLocker lock(&mutex_);
lock.Await([this]() REQUIRES(mutex_) { return QueueNotEmptyCondition(); }); lock.Await([this]() REQUIRES(mutex_) { return !QueueEmptyCondition(); });
T t = std::move(deque_.front()); T t = std::move(deque_.front());
deque_.pop_front(); deque_.pop_front();
@ -78,7 +78,7 @@ class BlockingQueue {
T PopWithTimeout(const common::Duration timeout) { T PopWithTimeout(const common::Duration timeout) {
MutexLocker lock(&mutex_); MutexLocker lock(&mutex_);
if (!lock.AwaitWithTimeout( if (!lock.AwaitWithTimeout(
[this]() REQUIRES(mutex_) { return QueueNotEmptyCondition(); }, [this]() REQUIRES(mutex_) { return !QueueEmptyCondition(); },
timeout)) { timeout)) {
return nullptr; return nullptr;
} }
@ -105,9 +105,15 @@ class BlockingQueue {
return deque_.size(); return deque_.size();
} }
// Blocks until the queue is empty.
void WaitUntilEmpty() {
MutexLocker lock(&mutex_);
lock.Await([this]() REQUIRES(mutex_) { return QueueEmptyCondition(); });
}
private: private:
// Returns true iff the queue is not empty. // Returns true iff the queue is empty.
bool QueueNotEmptyCondition() REQUIRES(mutex_) { return !deque_.empty(); } bool QueueEmptyCondition() REQUIRES(mutex_) { return deque_.empty(); }
// Returns true iff the queue is not full. // Returns true iff the queue is not full.
bool QueueNotFullCondition() REQUIRES(mutex_) { bool QueueNotFullCondition() REQUIRES(mutex_) {