Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
document ConditionVariables in the new way
[simgrid.git] / include / simgrid / s4u / ConditionVariable.hpp
1 /* Copyright (c) 2006-2020. 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
22  * `std::condition_variable <https://en.cppreference.com/w/cpp/thread/condition_variable>`_
23  * and should respect the same semantic.
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 functions without time:
51
52   void wait(MutexPtr lock);
53   void wait(const std::unique_lock<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 function taking a plain double as time:
61
62   std::cv_status wait_until(const std::unique_lock<Mutex>& lock, double timeout_time);
63   std::cv_status wait_for(const std::unique_lock<Mutex>& lock, double duration);
64   template <class P> bool wait_until(const std::unique_lock<Mutex>& lock, double timeout_time, P pred)
65   {
66     while (not pred())
67       if (this->wait_until(lock, timeout_time) == std::cv_status::timeout)
68         return pred();
69     return true;
70   }
71   template <class P> bool wait_for(const std::unique_lock<Mutex>& lock, double duration, P pred)
72   {
73     return this->wait_until(lock, SIMIX_get_clock() + duration, std::move(pred));
74   }
75
76   // Wait function taking a C++ style time:
77
78   template <class Rep, class Period, class P>
79   bool wait_for(const std::unique_lock<Mutex>& lock, std::chrono::duration<Rep, Period> duration, P pred)
80   {
81     auto seconds = std::chrono::duration_cast<SimulationClockDuration>(duration);
82     return this->wait_for(lock, seconds.count(), pred);
83   }
84   template <class Rep, class Period>
85   std::cv_status wait_for(const std::unique_lock<Mutex>& lock, std::chrono::duration<Rep, Period> duration)
86   {
87     auto seconds = std::chrono::duration_cast<SimulationClockDuration>(duration);
88     return this->wait_for(lock, seconds.count());
89   }
90   template <class Duration>
91   std::cv_status wait_until(const std::unique_lock<Mutex>& lock, const SimulationTimePoint<Duration>& timeout_time)
92   {
93     auto timeout_native = std::chrono::time_point_cast<SimulationClockDuration>(timeout_time);
94     return this->wait_until(lock, timeout_native.time_since_epoch().count());
95   }
96   template <class Duration, class P>
97   bool wait_until(const std::unique_lock<Mutex>& lock, const SimulationTimePoint<Duration>& timeout_time, P pred)
98   {
99     auto timeout_native = std::chrono::time_point_cast<SimulationClockDuration>(timeout_time);
100     return this->wait_until(lock, timeout_native.time_since_epoch().count(), std::move(pred));
101   }
102
103   // Notify functions
104
105   void notify_one();
106   void notify_all();
107 };
108
109 } // namespace s4u
110 } // namespace simgrid
111
112 #endif