1 /* Copyright (c) 2006-2023. 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
9 #include <simgrid/forward.h>
11 #include <simgrid/chrono.hpp>
12 #include <simgrid/s4u/Engine.hpp>
13 #include <simgrid/s4u/Mutex.hpp>
17 namespace simgrid::s4u {
21 * SimGrid's condition variables are meant to be drop-in replacements of ``std::condition_variable``.
22 * Please refer to the `documentation of standard C++ <https://en.cppreference.com/w/cpp/thread/condition_variable>`_
23 * for more information on condition variables. A SimGrid example is available in Section :ref:`s4u_ex_IPC`.
26 class XBT_PUBLIC ConditionVariable {
29 friend kernel::activity::ConditionVariableImpl;
30 friend XBT_PUBLIC void kernel::activity::intrusive_ptr_release(kernel::activity::ConditionVariableImpl* cond);
33 kernel::activity::ConditionVariableImpl* const pimpl_;
35 explicit ConditionVariable(kernel::activity::ConditionVariableImpl* cond) : pimpl_(cond) {}
36 ~ConditionVariable() = default;
38 ConditionVariable(ConditionVariable const&) = delete;
39 ConditionVariable& operator=(ConditionVariable const&) = delete;
41 friend XBT_PUBLIC void intrusive_ptr_add_ref(const ConditionVariable* cond);
42 friend XBT_PUBLIC void intrusive_ptr_release(const ConditionVariable* cond);
46 /** \static Create a new condition variable and return a smart pointer
49 * You should only manipulate :cpp:type:`simgrid::s4u::ConditionVariablePtr`, as created by this function (see also :ref:`s4u_raii`).
52 static ConditionVariablePtr create();
54 /// Wait until notification, with no timeout
55 void wait(s4u::MutexPtr lock);
56 /// Wait until notification, with no timeout
57 void wait(const std::unique_lock<s4u::Mutex>& lock);
58 template <class P> void wait(const std::unique_lock<Mutex>& lock, P pred)
64 /// Wait until the given instant (specified as a plain double)
65 std::cv_status wait_until(const std::unique_lock<s4u::Mutex>& lock, double timeout_time);
66 /// Wait for the given amount of seconds (specified as a plain double)
67 std::cv_status wait_for(const std::unique_lock<s4u::Mutex>& lock, double duration);
68 /// Wait until predicate is true, or the given instant (specified as a plain double)
69 template <class P> bool wait_until(const std::unique_lock<s4u::Mutex>& lock, double timeout_time, P pred)
72 if (this->wait_until(lock, timeout_time) == std::cv_status::timeout)
76 /// As long as the predicate is false, wait for the given amount of seconds (specified as a plain double)
77 template <class P> bool wait_for(const std::unique_lock<s4u::Mutex>& lock, double duration, P pred)
79 return this->wait_until(lock, Engine::get_clock() + duration, std::move(pred));
82 // Wait function taking a C++ style time:
84 /// As long as the predicate is false, wait for the given amount of seconds (specified in C++ style)
85 template <class Rep, class Period, class P>
86 bool wait_for(const std::unique_lock<s4u::Mutex>& lock, std::chrono::duration<Rep, Period> duration, P pred)
88 auto seconds = std::chrono::duration_cast<SimulationClockDuration>(duration);
89 return this->wait_for(lock, seconds.count(), pred);
91 /// Wait for the given amount of seconds (specified in C++ style)
92 template <class Rep, class Period>
93 std::cv_status wait_for(const std::unique_lock<s4u::Mutex>& lock, std::chrono::duration<Rep, Period> duration)
95 auto seconds = std::chrono::duration_cast<SimulationClockDuration>(duration);
96 return this->wait_for(lock, seconds.count());
98 /** Wait until the given instant (specified in C++ style) */
99 template <class Duration>
100 std::cv_status wait_until(const std::unique_lock<s4u::Mutex>& lock, const SimulationTimePoint<Duration>& timeout_time)
102 auto timeout_native = std::chrono::time_point_cast<SimulationClockDuration>(timeout_time);
103 return this->wait_until(lock, timeout_native.time_since_epoch().count());
105 /** Wait until predicate is true, or the given instant (specified in C++ style) */
106 template <class Duration, class P>
107 bool wait_until(const std::unique_lock<s4u::Mutex>& lock, const SimulationTimePoint<Duration>& timeout_time, P pred)
109 auto timeout_native = std::chrono::time_point_cast<SimulationClockDuration>(timeout_time);
110 return this->wait_until(lock, timeout_native.time_since_epoch().count(), std::move(pred));
113 /** Unblock one actor blocked on that condition variable. If none was blocked, nothing happens. */
115 /** Unblock all actors blocked on that condition variable. If none was blocked, nothing happens. */
119 } // namespace simgrid::s4u