1 /* Copyright (c) 2006-2016. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
6 #ifndef SIMGRID_S4U_COND_VARIABLE_HPP
7 #define SIMGRID_S4U_COND_VARIABLE_HPP
10 #include <condition_variable>
13 #include <utility> // std::swap
15 #include <boost/intrusive_ptr.hpp>
19 #include <simgrid/simix.h>
20 #include <simgrid/chrono.hpp>
21 #include <simgrid/s4u/mutex.hpp>
28 /** A condition variable
30 * This is based on std::condition_variable and should respect the same
31 * semantic. But we currently use (only) double for both durations and
34 XBT_PUBLIC_CLASS ConditionVariable {
38 ConditionVariable(smx_cond_t cond) : cond_(cond) {}
41 ConditionVariable(ConditionVariable const&) = delete;
42 ConditionVariable& operator=(ConditionVariable const&) = delete;
44 friend XBT_PUBLIC(void) intrusive_ptr_add_ref(ConditionVariable* cond);
45 friend XBT_PUBLIC(void) intrusive_ptr_release(ConditionVariable* cond);
46 using Ptr = boost::intrusive_ptr<ConditionVariable>;
48 static Ptr createConditionVariable();
50 // Wait functions without time:
52 void wait(std::unique_lock<Mutex>& lock);
54 void wait(std::unique_lock<Mutex>& lock, P pred)
60 // Wait function taking a plain double as time:
62 std::cv_status wait_until(std::unique_lock<Mutex>& lock, double timeout_time);
63 std::cv_status wait_for(std::unique_lock<Mutex>& lock, double duration);
65 bool wait_until(std::unique_lock<Mutex>& lock, double timeout_time, P pred)
68 if (this->wait_until(lock, timeout_time) == std::cv_status::timeout)
73 bool wait_for(std::unique_lock<Mutex>& lock, double duration, P pred)
75 return this->wait_until(lock, SIMIX_get_clock() + duration, std::move(pred));
78 // Wait function taking a C++ style time:
80 template<class Rep, class Period, class P>
82 std::unique_lock<Mutex>& lock, std::chrono::duration<Rep, Period> duration,
85 auto seconds = std::chrono::duration_cast<SimulationClockDuration>(duration);
86 return this->wait_for(lock, seconds.count(), pred);
88 template<class Rep, class Period>
89 std::cv_status wait_for(
90 std::unique_lock<Mutex>& lock, std::chrono::duration<Rep, Period> duration)
92 auto seconds = std::chrono::duration_cast<SimulationClockDuration>(duration);
93 return this->wait_for(lock, seconds.count());
95 template<class Duration>
96 std::cv_status wait_until(std::unique_lock<Mutex>& lock,
97 const SimulationTimePoint<Duration>& timeout_time)
99 auto timeout_native = std::chrono::time_point_cast<SimulationClockDuration>(timeout_time);
100 return this->wait_until(lock, timeout_native.time_since_epoch().count());
102 template<class Duration, class P>
103 bool wait_until(std::unique_lock<Mutex>& lock,
104 const SimulationTimePoint<Duration>& timeout_time, P pred)
106 auto timeout_native = std::chrono::time_point_cast<SimulationClockDuration>(timeout_time);
107 return this->wait_until(lock, timeout_native.time_since_epoch().count(),
116 XBT_ATTRIB_DEPRECATED("Use notify_one() instead")
117 void notify() { notify_one(); }
120 using ConditionVariablePtr = ConditionVariable::Ptr;
122 }} // namespace simgrid::s4u
124 #endif /* SIMGRID_S4U_COND_VARIABLE_HPP */