Logo AND Algorithmique Numérique Distribuée

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