X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/198b09ec16ca1b8fc05053bcae9e75c0ad689711..e79c652187a77f7383b81afb91a4adc8d38986ef:/include/simgrid/s4u/conditionVariable.hpp diff --git a/include/simgrid/s4u/conditionVariable.hpp b/include/simgrid/s4u/conditionVariable.hpp index f2790166a2..3b03bca7ff 100644 --- a/include/simgrid/s4u/conditionVariable.hpp +++ b/include/simgrid/s4u/conditionVariable.hpp @@ -6,9 +6,18 @@ #ifndef SIMGRID_S4U_COND_VARIABLE_HPP #define SIMGRID_S4U_COND_VARIABLE_HPP +#include +#include +#include +#include #include // std::swap +#include + +#include + #include +#include #include namespace simgrid { @@ -16,52 +25,100 @@ 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 { - +private: + friend s_smx_cond; + smx_cond_t cond_; + ConditionVariable(smx_cond_t cond) : cond_(cond) {} public: - ConditionVariable(); - ConditionVariable(ConditionVariable* cond) : cond_(SIMIX_cond_ref(cond->cond_)) {} - ~ConditionVariable(); + ConditionVariable(ConditionVariable const&) = delete; + ConditionVariable& operator=(ConditionVariable const&) = delete; + + friend XBT_PUBLIC(void) intrusive_ptr_add_ref(ConditionVariable* cond); + friend XBT_PUBLIC(void) intrusive_ptr_release(ConditionVariable* cond); + using Ptr = boost::intrusive_ptr; + + static Ptr createConditionVariable(); - // Copy+move (with the copy-and-swap idiom): - ConditionVariable(ConditionVariable const& cond) : cond_(SIMIX_cond_ref(cond.cond_)) {} - friend void swap(ConditionVariable& first, ConditionVariable& second) + // Wait functions without time: + + void wait(std::unique_lock& lock); + template + void wait(std::unique_lock& lock, P pred) { - using std::swap; - swap(first.cond_, second.cond_); + while (!pred()) + wait(lock); } - ConditionVariable& operator=(ConditionVariable cond) + + // Wait function taking a plain double as time: + + std::cv_status wait_until(std::unique_lock& lock, double timeout_time); + std::cv_status wait_for(std::unique_lock& lock, double duration); + template + bool wait_until(std::unique_lock& lock, double timeout_time, P pred) { - swap(*this, cond); - return *this; + while (!pred()) + if (this->wait_until(lock, timeout_time) == std::cv_status::timeout) + return pred(); + return true; } - ConditionVariable(ConditionVariable&& cond) : cond_(nullptr) + template + bool wait_for(std::unique_lock& lock, double duration, P pred) { - swap(*this, cond); + return this->wait_until(lock, SIMIX_get_clock() + duration, std::move(pred)); } - bool valid() const + // Wait function taking a C++ style time: + + template + bool wait_for( + std::unique_lock& lock, std::chrono::duration duration, + P pred) { - return cond_ != nullptr; + auto seconds = std::chrono::duration_cast(duration); + return this->wait_for(lock, seconds.count(), pred); + } + template + std::cv_status wait_for( + std::unique_lock& lock, std::chrono::duration duration) + { + auto seconds = std::chrono::duration_cast(duration); + return this->wait_for(lock, seconds.count()); + } + template + std::cv_status wait_until(std::unique_lock& lock, + const SimulationTimePoint& timeout_time) + { + auto timeout_native = std::chrono::time_point_cast(timeout_time); + return this->wait_until(lock, timeout_native.time_since_epoch().count()); + } + template + bool wait_until(std::unique_lock& lock, + const SimulationTimePoint& timeout_time, P pred) + { + auto timeout_native = std::chrono::time_point_cast(timeout_time); + return this->wait_until(lock, timeout_native.time_since_epoch().count(), + std::move(pred)); } - - /** - * Wait functions - */ - void wait(Mutex *mutex); - void wait_for(Mutex *mutex, double time); - - /** - * Notify functions - */ - void notify(); - void notify_all(); -private: - smx_cond_t cond_; + // Notify functions + + void notify_one(); + void notify_all(); + XBT_ATTRIB_DEPRECATED("Use notify_one() instead") + void notify() { notify_one(); } }; + +using ConditionVariablePtr = ConditionVariable::Ptr; + }} // namespace simgrid::s4u #endif /* SIMGRID_S4U_COND_VARIABLE_HPP */