Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
d3e777e6ae5a3cae64f6d6991b8724106b6daf84
[simgrid.git] / include / simgrid / s4u / conditionVariable.hpp
1 /* Copyright (c) 2006-2016. 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 <chrono>
10 #include <condition_variable>
11 #include <future>
12 #include <mutex>
13 #include <utility> // std::swap
14
15 #include <boost/intrusive_ptr.hpp>
16
17 #include <xbt/base.h>
18
19 #include <simgrid/simix.h>
20 #include <simgrid/chrono.hpp>
21 #include <simgrid/s4u/Mutex.hpp>
22
23 namespace simgrid {
24 namespace s4u {
25
26 /** @brief A condition variable
27  *  @ingroup s4u_api
28  *
29  *  This is a drop-in replacement of `std::condition_variable` and should respect the same
30  *  semantic. But we currently use (only) double for both durations and
31  *  timestamp timeouts.
32  */
33 XBT_PUBLIC_CLASS ConditionVariable {
34 private:
35   friend s_smx_cond;
36   smx_cond_t cond_;
37   ConditionVariable(smx_cond_t cond) : cond_(cond) {}
38 public:
39
40   ConditionVariable(ConditionVariable const&) = delete;
41   ConditionVariable& operator=(ConditionVariable const&) = delete;
42
43   friend XBT_PUBLIC(void) intrusive_ptr_add_ref(ConditionVariable* cond);
44   friend XBT_PUBLIC(void) intrusive_ptr_release(ConditionVariable* cond);
45   using Ptr = boost::intrusive_ptr<ConditionVariable>;
46
47   static Ptr createConditionVariable();
48
49   //  Wait functions without time:
50
51   void wait(std::unique_lock<Mutex>& lock);
52   template<class P>
53   void wait(std::unique_lock<Mutex>& lock, P pred)
54   {
55     while (!pred())
56       wait(lock);
57   }
58
59   // Wait function taking a plain double as time:
60
61   std::cv_status wait_until(std::unique_lock<Mutex>& lock, double timeout_time);
62   std::cv_status wait_for(std::unique_lock<Mutex>& lock, double duration);
63   template<class P>
64   bool wait_until(std::unique_lock<Mutex>& lock, double timeout_time, P pred)
65   {
66     while (!pred())
67       if (this->wait_until(lock, timeout_time) == std::cv_status::timeout)
68         return pred();
69     return true;
70   }
71   template<class P>
72   bool wait_for(std::unique_lock<Mutex>& lock, double duration, P pred)
73   {
74     return this->wait_until(lock, SIMIX_get_clock() + duration, std::move(pred));
75   }
76
77   // Wait function taking a C++ style time:
78
79   template<class Rep, class Period, class P>
80   bool wait_for(
81     std::unique_lock<Mutex>& lock, std::chrono::duration<Rep, Period> duration,
82     P pred)
83   {
84     auto seconds = std::chrono::duration_cast<SimulationClockDuration>(duration);
85     return this->wait_for(lock, seconds.count(), pred);
86   }
87   template<class Rep, class Period>
88   std::cv_status wait_for(
89     std::unique_lock<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   template<class Duration>
95   std::cv_status wait_until(std::unique_lock<Mutex>& lock,
96     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   template<class Duration, class P>
102   bool wait_until(std::unique_lock<Mutex>& lock,
103     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(),
107       std::move(pred));
108   }
109
110   // Notify functions
111
112   void notify_one();
113   void notify_all();
114
115   XBT_ATTRIB_DEPRECATED("Use notify_one() instead")
116   void notify() { notify_one(); }
117 };
118
119 using ConditionVariablePtr = ConditionVariable::Ptr;
120
121 }} // namespace simgrid::s4u
122
123 #endif /* SIMGRID_S4U_COND_VARIABLE_HPP */