#include "condition.hpp" void Condition::notify_all() { notify(/* justone = */ false); } void Condition::notify_one() { notify(/* justone = */ true); } void Condition::enlisten(ConditionSetPtr cset) { std::unique_lock lock; dependents.push_back(cset); } void Condition::notify(bool justone) { std::unique_lock lock(mutex); for(;;) { if (dependents.empty()) break; std::shared_ptr cset = dependents.front().lock(); dependents.pop_front(); if (cset) { cset->notify(); if (justone) break; } } } void ConditionSet::include(ConditionPtr condition) { std::unique_lock lock(mutex); conditions.push_back(condition); } void ConditionSet::notify() { notified.notify_all(); } void ConditionSet::wait(std::unique_lock& lock_of_caller) { { std::unique_lock my_lock(mutex); if (conditions.empty()) return; for (auto condition: conditions) { condition->enlisten(shared_from_this()); } } notified.wait(lock_of_caller); }