X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/340307c06ecc6e3e02f73820da97f0c6479c62c5..4fd0a2b6cf055692f936e29294496d14a6091ff1:/include/simgrid/s4u/conditionVariable.hpp diff --git a/include/simgrid/s4u/conditionVariable.hpp b/include/simgrid/s4u/conditionVariable.hpp index ec9823d4ca..0c34c9284f 100644 --- a/include/simgrid/s4u/conditionVariable.hpp +++ b/include/simgrid/s4u/conditionVariable.hpp @@ -6,14 +6,24 @@ #ifndef SIMGRID_S4U_COND_VARIABLE_HPP #define SIMGRID_S4U_COND_VARIABLE_HPP -#include +#include +#include +#include // std::swap +#include +#include namespace simgrid { namespace s4u { class Mutex; +/** A condition variable + * + * This is based on std::condition_variable and should respect the same + * semantic. But we currently use (only) double for both durations and + * timestamp timeouts. + */ XBT_PUBLIC_CLASS ConditionVariable { public: @@ -44,15 +54,36 @@ public: return cond_ != nullptr; } - /** - * Wait functions - */ - void wait(Mutex *mutex); - void wait_for(Mutex *mutex, double time); - - /** - * Notify functions - */ + // Wait functions: + + void wait(std::unique_lock& lock); + std::cv_status wait_until(std::unique_lock& lock, double timeout_time); + std::cv_status wait_for(std::unique_lock& lock, double duration); + + // Variants which takes a predicate: + + template + void wait(std::unique_lock& lock, P pred) + { + while (!pred()) + wait(lock); + } + template + bool wait_until(std::unique_lock& lock, double timeout_time, P pred) + { + while (!pred()) + if (this->wait_until(lock, timeout_time) == std::cv_status::timeout) + return pred(); + return true; + } + template + bool wait_for(std::unique_lock& lock, double duration, P pred) + { + return this->wait_until(lock, SIMIX_get_clock() + duration, std::move(pred)); + } + + // Notify functions + void notify(); void notify_all();