Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines.
[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/Mutex.hpp>
13
14 #include <future>
15
16 namespace simgrid {
17 namespace s4u {
18
19 /**
20  * @rst
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`.
24  * @endrst
25  */
26 class XBT_PUBLIC ConditionVariable {
27 private:
28   friend kernel::activity::ConditionVariableImpl;
29   kernel::activity::ConditionVariableImpl* const cond_;
30
31 public:
32 #ifndef DOXYGEN
33   explicit ConditionVariable(kernel::activity::ConditionVariableImpl* cond) : cond_(cond) {}
34
35   ConditionVariable(ConditionVariable const&) = delete;
36   ConditionVariable& operator=(ConditionVariable const&) = delete;
37
38   friend XBT_PUBLIC void intrusive_ptr_add_ref(const ConditionVariable* cond);
39   friend XBT_PUBLIC void intrusive_ptr_release(const ConditionVariable* cond);
40 #endif
41
42   /** Create a new condition variable and return a smart pointer
43    *
44    * @rst
45    * You should only manipulate :cpp:type:`simgrid::s4u::ConditionVariablePtr`, as created by this function (see also :ref:`s4u_raii`).
46    * @endrst
47    */
48   static ConditionVariablePtr create();
49
50   ///  Wait until notification, with no timeout
51   void wait(s4u::MutexPtr lock);
52   ///  Wait until notification, with no timeout
53   void wait(const std::unique_lock<s4u::Mutex>& lock);
54   template <class P> void wait(const std::unique_lock<Mutex>& lock, P pred)
55   {
56     while (not pred())
57       wait(lock);
58   }
59
60   /// Wait until the given instant (specified as a plain double)
61   std::cv_status wait_until(const std::unique_lock<s4u::Mutex>& lock, double timeout_time);
62   /// Wait for the given amount of seconds (specified as a plain double)
63   std::cv_status wait_for(const std::unique_lock<s4u::Mutex>& lock, double duration);
64   /// Wait until predicate is true, or the given instant (specified as a plain double)
65   template <class P> bool wait_until(const std::unique_lock<s4u::Mutex>& lock, double timeout_time, P pred)
66   {
67     while (not pred())
68       if (this->wait_until(lock, timeout_time) == std::cv_status::timeout)
69         return pred();
70     return true;
71   }
72   /// As long as the predicate is false, wait for the given amount of seconds (specified as a plain double)
73   template <class P> bool wait_for(const std::unique_lock<s4u::Mutex>& lock, double duration, P pred)
74   {
75     return this->wait_until(lock, SIMIX_get_clock() + duration, std::move(pred));
76   }
77
78   // Wait function taking a C++ style time:
79
80   /// As long as the predicate is false, wait for the given amount of seconds (specified in C++ style)
81   template <class Rep, class Period, class P>
82   bool wait_for(const std::unique_lock<s4u::Mutex>& lock, std::chrono::duration<Rep, Period> duration, P pred)
83   {
84     auto seconds = std::chrono::duration_cast<SimulationClockDuration>(duration);
85     return this->wait_for(lock, seconds.count(), pred);
86   }
87   /// Wait for the given amount of seconds (specified in C++ style)
88   template <class Rep, class Period>
89   std::cv_status wait_for(const std::unique_lock<s4u::Mutex>& lock, std::chrono::duration<Rep, Period> duration)
90   {
91     auto seconds = std::chrono::duration_cast<SimulationClockDuration>(duration);
92     return this->wait_for(lock, seconds.count());
93   }
94   /** Wait until the given instant (specified in C++ style) */
95   template <class Duration>
96   std::cv_status wait_until(const std::unique_lock<s4u::Mutex>& lock, const SimulationTimePoint<Duration>& timeout_time)
97   {
98     auto timeout_native = std::chrono::time_point_cast<SimulationClockDuration>(timeout_time);
99     return this->wait_until(lock, timeout_native.time_since_epoch().count());
100   }
101   /** Wait until predicate is true, or the given instant (specified in C++ style) */
102   template <class Duration, class P>
103   bool wait_until(const std::unique_lock<s4u::Mutex>& lock, const SimulationTimePoint<Duration>& timeout_time, P pred)
104   {
105     auto timeout_native = std::chrono::time_point_cast<SimulationClockDuration>(timeout_time);
106     return this->wait_until(lock, timeout_native.time_since_epoch().count(), std::move(pred));
107   }
108
109   /** Unblock one actor blocked on that condition variable. If none was blocked, nothing happens. */
110   void notify_one();
111   /** Unblock all actors blocked on that condition variable. If none was blocked, nothing happens. */
112   void notify_all();
113 };
114
115 } // namespace s4u
116 } // namespace simgrid
117
118 #endif