Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
8e926fba92e8ae189be6ec3ec12a35c609e5d9a1
[simgrid.git] / include / simgrid / s4u / ConditionVariable.hpp
1 /* Copyright (c) 2006-2019. 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/chrono.hpp>
10 #include <simgrid/s4u/Mutex.hpp>
11
12 #include <future>
13
14 namespace simgrid {
15 namespace s4u {
16
17 /** @brief A condition variable
18  *  @ingroup s4u_api
19  *
20  *  This is a drop-in replacement of `std::condition_variable` and should respect the same
21  *  semantic. But we currently use (only) double for both durations and
22  *  timestamp timeouts.
23  */
24 class XBT_PUBLIC ConditionVariable {
25 private:
26   friend kernel::activity::ConditionVariableImpl;
27   kernel::activity::ConditionVariableImpl* const cond_;
28
29   explicit ConditionVariable(kernel::activity::ConditionVariableImpl* cond) : cond_(cond) {}
30
31 public:
32   ConditionVariable(ConditionVariable const&) = delete;
33   ConditionVariable& operator=(ConditionVariable const&) = delete;
34
35   friend XBT_PUBLIC void intrusive_ptr_add_ref(ConditionVariable * cond);
36   friend XBT_PUBLIC void intrusive_ptr_release(ConditionVariable * cond);
37
38   static ConditionVariablePtr create();
39
40   //  Wait functions without time:
41
42   void wait(MutexPtr lock);
43   void wait(std::unique_lock<Mutex> & lock);
44   template <class P> void wait(std::unique_lock<Mutex> & lock, P pred)
45   {
46     while (not pred())
47       wait(lock);
48   }
49
50   // Wait function taking a plain double as time:
51
52   std::cv_status wait_until(std::unique_lock<Mutex> & lock, double timeout_time);
53   std::cv_status wait_for(std::unique_lock<Mutex> & lock, double duration);
54   template <class P> bool wait_until(std::unique_lock<Mutex> & lock, double timeout_time, P pred)
55   {
56     while (not pred())
57       if (this->wait_until(lock, timeout_time) == std::cv_status::timeout)
58         return pred();
59     return true;
60   }
61   template <class P> bool wait_for(std::unique_lock<Mutex> & lock, double duration, P pred)
62   {
63     return this->wait_until(lock, SIMIX_get_clock() + duration, std::move(pred));
64   }
65
66   // Wait function taking a C++ style time:
67
68   template <class Rep, class Period, class P>
69   bool wait_for(std::unique_lock<Mutex> & lock, std::chrono::duration<Rep, Period> duration, P pred)
70   {
71     auto seconds = std::chrono::duration_cast<SimulationClockDuration>(duration);
72     return this->wait_for(lock, seconds.count(), pred);
73   }
74   template <class Rep, class Period>
75   std::cv_status wait_for(std::unique_lock<Mutex> & lock, std::chrono::duration<Rep, Period> duration)
76   {
77     auto seconds = std::chrono::duration_cast<SimulationClockDuration>(duration);
78     return this->wait_for(lock, seconds.count());
79   }
80   template <class Duration>
81   std::cv_status wait_until(std::unique_lock<Mutex> & lock, const SimulationTimePoint<Duration>& timeout_time)
82   {
83     auto timeout_native = std::chrono::time_point_cast<SimulationClockDuration>(timeout_time);
84     return this->wait_until(lock, timeout_native.time_since_epoch().count());
85   }
86   template <class Duration, class P>
87   bool wait_until(std::unique_lock<Mutex> & lock, const SimulationTimePoint<Duration>& timeout_time, P pred)
88   {
89     auto timeout_native = std::chrono::time_point_cast<SimulationClockDuration>(timeout_time);
90     return this->wait_until(lock, timeout_native.time_since_epoch().count(), std::move(pred));
91   }
92
93   // Notify functions
94
95   void notify_one();
96   void notify_all();
97 };
98
99 } // namespace s4u
100 } // namespace simgrid
101
102 #endif