Logo AND Algorithmique Numérique Distribuée

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