Logo AND Algorithmique Numérique Distribuée

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