add wait-if for waiter

This commit is contained in:
mutouyun 2019-02-12 13:22:49 +08:00
parent f7f06ab052
commit 52302cc007
3 changed files with 17 additions and 2 deletions

View File

@ -10,7 +10,7 @@ public:
explicit waiter(char const * name); explicit waiter(char const * name);
waiter(waiter&& rhs); waiter(waiter&& rhs);
~waiter(); virtual ~waiter();
void swap(waiter& rhs); void swap(waiter& rhs);
waiter& operator=(waiter rhs); waiter& operator=(waiter rhs);
@ -22,9 +22,14 @@ public:
void close(); void close();
bool wait(); bool wait();
bool wait_if_pred();
bool notify(); bool notify();
bool broadcast(); bool broadcast();
protected:
virtual bool pred();
private: private:
class waiter_; class waiter_;
waiter_* p_; waiter_* p_;

View File

@ -199,7 +199,9 @@ public:
void broadcast(handle_t h) { void broadcast(handle_t h) {
if (h == invalid()) return; if (h == invalid()) return;
sem_.post([this] { return counter_.load(std::memory_order_relaxed); }); sem_.post([this] {
return counter_.load(std::memory_order_relaxed);
});
} }
}; };

View File

@ -67,6 +67,14 @@ bool waiter::wait() {
return impl(p_)->w_.wait_if([] { return true; }); return impl(p_)->w_.wait_if([] { return true; });
} }
bool waiter::pred() {
return true;
}
bool waiter::wait_if_pred() {
return impl(p_)->w_.wait_if([this] { return pred(); });
}
bool waiter::notify() { bool waiter::notify() {
return impl(p_)->w_.notify(); return impl(p_)->w_.notify();
} }