Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add new entry in Release_Notes.
[simgrid.git] / include / simgrid / s4u / ConditionVariable.hpp
1 /* Copyright (c) 2006-2023. 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/forward.h>
10
11 #include <simgrid/chrono.hpp>
12 #include <simgrid/s4u/Engine.hpp>
13 #include <simgrid/s4u/Mutex.hpp>
14
15 #include <future>
16
17 namespace simgrid::s4u {
18
19 /**
20  * @beginrst
21  * SimGrid's condition variables are meant to be drop-in replacements of ``std::condition_variable``.
22  * Please refer to the `documentation of standard C++ <https://en.cppreference.com/w/cpp/thread/condition_variable>`_
23  * for more information on condition variables. A SimGrid example is available in Section :ref:`s4u_ex_IPC`.
24  * @endrst
25  */
26 class XBT_PUBLIC ConditionVariable {
27 private:
28 #ifndef DOXYGEN
29   friend kernel::activity::ConditionVariableImpl;
30   friend XBT_PUBLIC void kernel::activity::intrusive_ptr_release(kernel::activity::ConditionVariableImpl* cond);
31 #endif
32
33   kernel::activity::ConditionVariableImpl* const pimpl_;
34
35   explicit ConditionVariable(kernel::activity::ConditionVariableImpl* cond) : pimpl_(cond) {}
36   ~ConditionVariable() = default;
37 #ifndef DOXYGEN
38   ConditionVariable(ConditionVariable const&) = delete;
39   ConditionVariable& operator=(ConditionVariable const&) = delete;
40
41   friend XBT_PUBLIC void intrusive_ptr_add_ref(const ConditionVariable* cond);
42   friend XBT_PUBLIC void intrusive_ptr_release(const ConditionVariable* cond);
43 #endif
44
45 public:
46   /** \static Create a new condition variable and return a smart pointer
47    *
48    * @beginrst
49    * You should only manipulate :cpp:type:`simgrid::s4u::ConditionVariablePtr`, as created by this function (see also :ref:`s4u_raii`).
50    * @endrst
51    */
52   static ConditionVariablePtr create();
53
54   ///  Wait until notification, with no timeout
55   void wait(s4u::MutexPtr lock);
56   ///  Wait until notification, with no timeout
57   void wait(const std::unique_lock<s4u::Mutex>& lock);
58   template <class P> void wait(const std::unique_lock<Mutex>& lock, P pred)
59   {
60     while (not pred())
61       wait(lock);
62   }
63
64   /// Wait until the given instant (specified as a plain double)
65   std::cv_status wait_until(const std::unique_lock<s4u::Mutex>& lock, double timeout_time);
66   /// Wait for the given amount of seconds (specified as a plain double)
67   std::cv_status wait_for(const std::unique_lock<s4u::Mutex>& lock, double duration);
68
69   // Wait function taking a C++ style time:
70
71   /// Wait for the given amount of seconds (specified in C++ style)
72   template <class Rep, class Period>
73   std::cv_status wait_for(const std::unique_lock<s4u::Mutex>& lock, std::chrono::duration<Rep, Period> duration)
74   {
75     auto seconds = std::chrono::duration_cast<SimulationClockDuration>(duration);
76     return this->wait_for(lock, seconds.count());
77   }
78   /** Wait until the given instant (specified in C++ style) */
79   template <class Duration>
80   std::cv_status wait_until(const std::unique_lock<s4u::Mutex>& lock, const SimulationTimePoint<Duration>& timeout_time)
81   {
82     auto timeout_native = std::chrono::time_point_cast<SimulationClockDuration>(timeout_time);
83     return this->wait_until(lock, timeout_native.time_since_epoch().count());
84   }
85
86   /** Unblock one actor blocked on that condition variable. If none was blocked, nothing happens. */
87   void notify_one();
88   /** Unblock all actors blocked on that condition variable. If none was blocked, nothing happens. */
89   void notify_all();
90 };
91
92 } // namespace simgrid::s4u
93
94 #endif