Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[sonar] bunch of const
[simgrid.git] / include / simgrid / s4u / ConditionVariable.hpp
1 /* Copyright (c) 2006-2021. The SimGrid Team. All rights reserved.          */
2
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. */
5
6 #ifndef SIMGRID_S4U_COND_VARIABLE_HPP
7 #define SIMGRID_S4U_COND_VARIABLE_HPP
8
9 #include <simgrid/forward.h>
10
11 #include <simgrid/chrono.hpp>
12 #include <simgrid/s4u/Engine.hpp>
13 #include <simgrid/s4u/Mutex.hpp>
14
15 #include <future>
16
17 namespace simgrid {
18 namespace s4u {
19
20 /**
21  * @beginrst
22  * SimGrid's condition variables are meant to be drop-in replacements of ``std::condition_variable``.
23  * Please refer to the `documentation of standard C++ <https://en.cppreference.com/w/cpp/thread/condition_variable>`_
24  * for more information on condition variables. A SimGrid example is available in Section :ref:`s4u_ex_IPC`.
25  * @endrst
26  */
27 class XBT_PUBLIC ConditionVariable {
28 private:
29 #ifndef DOXYGEN
30   friend kernel::activity::ConditionVariableImpl;
31   friend void kernel::activity::intrusive_ptr_release(kernel::activity::ConditionVariableImpl* cond);
32 #endif
33
34   kernel::activity::ConditionVariableImpl* const pimpl_;
35
36   explicit ConditionVariable(kernel::activity::ConditionVariableImpl* cond) : pimpl_(cond) {}
37   ~ConditionVariable() = default;
38 #ifndef DOXYGEN
39   ConditionVariable(ConditionVariable const&) = delete;
40   ConditionVariable& operator=(ConditionVariable const&) = delete;
41
42   friend XBT_PUBLIC void intrusive_ptr_add_ref(const ConditionVariable* cond);
43   friend XBT_PUBLIC void intrusive_ptr_release(const ConditionVariable* cond);
44 #endif
45
46 public:
47   /** Create a new condition variable and return a smart pointer
48    *
49    * @beginrst
50    * You should only manipulate :cpp:type:`simgrid::s4u::ConditionVariablePtr`, as created by this function (see also :ref:`s4u_raii`).
51    * @endrst
52    */
53   static ConditionVariablePtr create();
54
55   ///  Wait until notification, with no timeout
56   void wait(s4u::MutexPtr lock);
57   ///  Wait until notification, with no timeout
58   void wait(const std::unique_lock<s4u::Mutex>& lock);
59   template <class P> void wait(const std::unique_lock<Mutex>& lock, P pred)
60   {
61     while (not pred())
62       wait(lock);
63   }
64
65   /// Wait until the given instant (specified as a plain double)
66   std::cv_status wait_until(const std::unique_lock<s4u::Mutex>& lock, double timeout_time);
67   /// Wait for the given amount of seconds (specified as a plain double)
68   std::cv_status wait_for(const std::unique_lock<s4u::Mutex>& lock, double duration);
69   /// Wait until predicate is true, or the given instant (specified as a plain double)
70   template <class P> bool wait_until(const std::unique_lock<s4u::Mutex>& lock, double timeout_time, P pred)
71   {
72     while (not pred())
73       if (this->wait_until(lock, timeout_time) == std::cv_status::timeout)
74         return pred();
75     return true;
76   }
77   /// As long as the predicate is false, wait for the given amount of seconds (specified as a plain double)
78   template <class P> bool wait_for(const std::unique_lock<s4u::Mutex>& lock, double duration, P pred)
79   {
80     return this->wait_until(lock, Engine::get_clock() + duration, std::move(pred));
81   }
82
83   // Wait function taking a C++ style time:
84
85   /// As long as the predicate is false, wait for the given amount of seconds (specified in C++ style)
86   template <class Rep, class Period, class P>
87   bool wait_for(const std::unique_lock<s4u::Mutex>& lock, std::chrono::duration<Rep, Period> duration, P pred)
88   {
89     auto seconds = std::chrono::duration_cast<SimulationClockDuration>(duration);
90     return this->wait_for(lock, seconds.count(), pred);
91   }
92   /// Wait for the given amount of seconds (specified in C++ style)
93   template <class Rep, class Period>
94   std::cv_status wait_for(const std::unique_lock<s4u::Mutex>& lock, std::chrono::duration<Rep, Period> duration)
95   {
96     auto seconds = std::chrono::duration_cast<SimulationClockDuration>(duration);
97     return this->wait_for(lock, seconds.count());
98   }
99   /** Wait until the given instant (specified in C++ style) */
100   template <class Duration>
101   std::cv_status wait_until(const std::unique_lock<s4u::Mutex>& lock, const SimulationTimePoint<Duration>& timeout_time)
102   {
103     auto timeout_native = std::chrono::time_point_cast<SimulationClockDuration>(timeout_time);
104     return this->wait_until(lock, timeout_native.time_since_epoch().count());
105   }
106   /** Wait until predicate is true, or the given instant (specified in C++ style) */
107   template <class Duration, class P>
108   bool wait_until(const std::unique_lock<s4u::Mutex>& lock, const SimulationTimePoint<Duration>& timeout_time, P pred)
109   {
110     auto timeout_native = std::chrono::time_point_cast<SimulationClockDuration>(timeout_time);
111     return this->wait_until(lock, timeout_native.time_since_epoch().count(), std::move(pred));
112   }
113
114   /** Unblock one actor blocked on that condition variable. If none was blocked, nothing happens. */
115   void notify_one();
116   /** Unblock all actors blocked on that condition variable. If none was blocked, nothing happens. */
117   void notify_all();
118 };
119
120 } // namespace s4u
121 } // namespace simgrid
122
123 #endif