Logo AND Algorithmique Numérique Distribuée

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