Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
1bd6e119eb6717548443ad861ed737a0410ed0da
[simgrid.git] / include / simgrid / s4u / ConditionVariable.hpp
1 /* Copyright (c) 2006-2018. 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/chrono.hpp>
20 #include <simgrid/s4u/Mutex.hpp>
21 #include <simgrid/simix.h>
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 class XBT_PUBLIC ConditionVariable {
34 private:
35   friend s_smx_cond_t;
36   smx_cond_t cond_;
37   explicit ConditionVariable(smx_cond_t cond) : cond_(cond) {}
38 public:
39   ConditionVariable(ConditionVariable const&) = delete;
40   ConditionVariable& operator=(ConditionVariable const&) = delete;
41
42   friend XBT_PUBLIC void intrusive_ptr_add_ref(ConditionVariable * cond);
43   friend XBT_PUBLIC void intrusive_ptr_release(ConditionVariable * cond);
44   using Ptr = boost::intrusive_ptr<ConditionVariable>;
45
46   static Ptr createConditionVariable();
47
48   //  Wait functions without time:
49
50   void wait(MutexPtr lock);
51   void wait(std::unique_lock<Mutex> & lock);
52   template <class P> void wait(std::unique_lock<Mutex> & lock, P pred)
53   {
54     while (not pred())
55       wait(lock);
56   }
57
58   // Wait function taking a plain double as time:
59
60   std::cv_status wait_until(std::unique_lock<Mutex> & lock, double timeout_time);
61   std::cv_status wait_for(std::unique_lock<Mutex> & lock, double duration);
62   template <class P> bool wait_until(std::unique_lock<Mutex> & lock, double timeout_time, P pred)
63   {
64     while (not pred())
65       if (this->wait_until(lock, timeout_time) == std::cv_status::timeout)
66         return pred();
67     return true;
68   }
69   template <class P> bool wait_for(std::unique_lock<Mutex> & lock, double duration, P pred)
70   {
71     return this->wait_until(lock, SIMIX_get_clock() + duration, std::move(pred));
72   }
73
74   // Wait function taking a C++ style time:
75
76   template <class Rep, class Period, class P>
77   bool wait_for(std::unique_lock<Mutex> & lock, std::chrono::duration<Rep, Period> duration, P pred)
78   {
79     auto seconds = std::chrono::duration_cast<SimulationClockDuration>(duration);
80     return this->wait_for(lock, seconds.count(), pred);
81   }
82   template <class Rep, class Period>
83   std::cv_status wait_for(std::unique_lock<Mutex> & lock, std::chrono::duration<Rep, Period> duration)
84   {
85     auto seconds = std::chrono::duration_cast<SimulationClockDuration>(duration);
86     return this->wait_for(lock, seconds.count());
87   }
88   template <class Duration>
89   std::cv_status wait_until(std::unique_lock<Mutex> & lock, const SimulationTimePoint<Duration>& timeout_time)
90   {
91     auto timeout_native = std::chrono::time_point_cast<SimulationClockDuration>(timeout_time);
92     return this->wait_until(lock, timeout_native.time_since_epoch().count());
93   }
94   template <class Duration, class P>
95   bool wait_until(std::unique_lock<Mutex> & lock, const SimulationTimePoint<Duration>& timeout_time, P pred)
96   {
97     auto timeout_native = std::chrono::time_point_cast<SimulationClockDuration>(timeout_time);
98     return this->wait_until(lock, timeout_native.time_since_epoch().count(), std::move(pred));
99   }
100
101   // Notify functions
102
103   void notify_one();
104   void notify_all();
105 };
106
107 using ConditionVariablePtr = ConditionVariable::Ptr;
108 }
109 } // namespace simgrid::s4u
110
111 #endif