Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge remote-tracking branch 'adfaure/master'
[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 class Mutex;
27
28 /** A condition variable
29  *
30  *  This is based on std::condition_variable and should respect the same
31  *  semantic. But we currently use (only) double for both durations and
32  *  timestamp timeouts.
33  */
34 XBT_PUBLIC_CLASS ConditionVariable {
35 private:
36   friend s_smx_cond;
37   smx_cond_t cond_;
38   ConditionVariable(smx_cond_t cond) : cond_(cond) {}
39 public:
40
41   ConditionVariable(ConditionVariable const&) = delete;
42   ConditionVariable& operator=(ConditionVariable const&) = delete;
43
44   friend XBT_PUBLIC(void) intrusive_ptr_add_ref(ConditionVariable* cond);
45   friend XBT_PUBLIC(void) intrusive_ptr_release(ConditionVariable* cond);
46   using Ptr = boost::intrusive_ptr<ConditionVariable>;
47
48   static Ptr createConditionVariable();
49
50   //  Wait functions without time:
51
52   void wait(std::unique_lock<Mutex>& lock);
53   template<class P>
54   void wait(std::unique_lock<Mutex>& lock, P pred)
55   {
56     while (!pred())
57       wait(lock);
58   }
59
60   // Wait function taking a plain double as time:
61
62   std::cv_status wait_until(std::unique_lock<Mutex>& lock, double timeout_time);
63   std::cv_status wait_for(std::unique_lock<Mutex>& lock, double duration);
64   template<class P>
65   bool wait_until(std::unique_lock<Mutex>& lock, double timeout_time, P pred)
66   {
67     while (!pred())
68       if (this->wait_until(lock, timeout_time) == std::cv_status::timeout)
69         return pred();
70     return true;
71   }
72   template<class P>
73   bool wait_for(std::unique_lock<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   template<class Rep, class Period, class P>
81   bool wait_for(
82     std::unique_lock<Mutex>& lock, std::chrono::duration<Rep, Period> duration,
83     P pred)
84   {
85     auto seconds = std::chrono::duration_cast<SimulationClockDuration>(duration);
86     return this->wait_for(lock, seconds.count(), pred);
87   }
88   template<class Rep, class Period>
89   std::cv_status wait_for(
90     std::unique_lock<Mutex>& lock, std::chrono::duration<Rep, Period> duration)
91   {
92     auto seconds = std::chrono::duration_cast<SimulationClockDuration>(duration);
93     return this->wait_for(lock, seconds.count());
94   }
95   template<class Duration>
96   std::cv_status wait_until(std::unique_lock<Mutex>& lock,
97     const SimulationTimePoint<Duration>& timeout_time)
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());
101   }
102   template<class Duration, class P>
103   bool wait_until(std::unique_lock<Mutex>& lock,
104     const SimulationTimePoint<Duration>& timeout_time, P pred)
105   {
106     auto timeout_native = std::chrono::time_point_cast<SimulationClockDuration>(timeout_time);
107     return this->wait_until(lock, timeout_native.time_since_epoch().count(),
108       std::move(pred));
109   }
110
111   // Notify functions
112
113   void notify_one();
114   void notify_all();
115
116   XBT_ATTRIB_DEPRECATED("Use notify_one() instead")
117   void notify() { notify_one(); }
118 };
119
120 using ConditionVariablePtr = ConditionVariable::Ptr;
121
122 }} // namespace simgrid::s4u
123
124 #endif /* SIMGRID_S4U_COND_VARIABLE_HPP */