userver: userver/engine/condition_variable.hpp Source File
Loading...
Searching...
No Matches
condition_variable.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/engine/condition_variable.hpp
4/// @brief @copybrief engine::ConditionVariable
5
6#include <chrono>
7
8#include <userver/engine/condition_variable_status.hpp>
9#include <userver/engine/deadline.hpp>
10#include <userver/engine/impl/condition_variable_any.hpp>
11#include <userver/engine/mutex.hpp>
12
13USERVER_NAMESPACE_BEGIN
14
15namespace engine {
16
17/// @ingroup userver_concurrency
18///
19/// @brief std::condition_variable replacement for asynchronous tasks
20///
21/// ## Example usage:
22///
23/// @snippet core/src/engine/condition_variable_test.cpp Sample ConditionVariable usage
24///
25/// @see @ref scripts/docs/en/userver/synchronization.md
26class ConditionVariable final {
27public:
28 ConditionVariable();
29 ~ConditionVariable();
30
31 ConditionVariable(const ConditionVariable&) = delete;
32 ConditionVariable(ConditionVariable&&) = delete;
33 ConditionVariable& operator=(const ConditionVariable&) = delete;
34 ConditionVariable& operator=(ConditionVariable&&) = delete;
35
36 /// Suspends execution until notified or cancelled
37 /// @returns `CvStatus::kNoTimeout` if variable was notified
38 /// @returns `CvStatus::kCancelled` if current task is being cancelled
39 [[nodiscard]] CvStatus Wait(std::unique_lock<Mutex>& lock);
40
41 /// @brief Suspends execution until the predicate is `true` when notification
42 /// is received or the task is cancelled
43 /// @returns the value of the predicate
44 template <typename Predicate>
45 [[nodiscard]] bool Wait(std::unique_lock<Mutex>& lock, Predicate&& predicate);
46
47 /// @brief Suspends execution until notified or until the timeout expires or
48 /// until the task is cancelled.
49 /// @returns `CvStatus::kNoTimeout` if variable was notified
50 /// @returns `CvStatus::kTimeout` if `timeout` has expired
51 /// @returns `CvStatus::kCancelled` if current task is being cancelled
52 template <typename Rep, typename Period>
53 CvStatus WaitFor(std::unique_lock<Mutex>& lock, std::chrono::duration<Rep, Period> timeout);
54
55 /// @brief Suspends execution until the predicate is `true` when notified
56 /// or the timeout expires or the task is cancelled.
57 /// @returns the value of the predicate
58 template <typename Rep, typename Period, typename Predicate>
59 bool WaitFor(std::unique_lock<Mutex>& lock, std::chrono::duration<Rep, Period> timeout, Predicate&& predicate);
60
61 /// @brief Suspends execution until notified or the time point is reached or
62 /// the task is cancelled.
63 /// @returns `CvStatus::kNoTimeout` if variable was notified
64 /// @returns `CvStatus::kTimeout` if `until` time point was reached
65 /// @returns `CvStatus::kCancelled` if current task is being cancelled
66 template <typename Clock, typename Duration>
67 CvStatus WaitUntil(std::unique_lock<Mutex>& lock, std::chrono::time_point<Clock, Duration> until);
68
69 /// @brief Suspends execution until notified or the deadline is reached or the
70 /// task is cancelled.
71 /// @returns `CvStatus::kNoTimeout` if variable was notified
72 /// @returns `CvStatus::kTimeout` if deadline was reached
73 /// @returns `CvStatus::kCancelled` if current task is being cancelled
74 CvStatus WaitUntil(std::unique_lock<Mutex>& lock, Deadline deadline);
75
76 /// @brief Suspends execution until the predicate is `true` when notified
77 /// or the time point is reached or the task is cancelled.
78 /// @returns the value of the predicate
79 template <typename Clock, typename Duration, typename Predicate>
80 bool WaitUntil(
81 std::unique_lock<Mutex>& lock,
82 std::chrono::time_point<Clock, Duration> until,
83 Predicate&& predicate
84 );
85
86 /// @brief Suspends execution until the predicate is `true` when notified
87 /// or the deadline is reached or the task is cancelled.
88 /// @returns the value of the predicate
89 template <typename Predicate>
90 bool WaitUntil(std::unique_lock<Mutex>& lock, Deadline deadline, Predicate&& predicate);
91
92 /// Notifies one of the waiting tasks
93 void NotifyOne();
94
95 /// Notifies all waiting tasks
96 void NotifyAll();
97
98private:
99 impl::ConditionVariableAny<Mutex> impl_;
100};
101
102template <typename Predicate>
103bool ConditionVariable::Wait(std::unique_lock<Mutex>& lock, Predicate&& predicate) {
104 return WaitUntil(lock, {}, std::forward<Predicate>(predicate));
105}
106
107template <typename Rep, typename Period>
108CvStatus ConditionVariable::WaitFor(std::unique_lock<Mutex>& lock, std::chrono::duration<Rep, Period> timeout) {
109 return WaitUntil(lock, Deadline::FromDuration(timeout));
110}
111
112template <typename Rep, typename Period, typename Predicate>
113bool ConditionVariable::WaitFor(
114 std::unique_lock<Mutex>& lock,
115 std::chrono::duration<Rep, Period> timeout,
116 Predicate&& predicate
117) {
118 return WaitUntil(lock, Deadline::FromDuration(timeout), std::forward<Predicate>(predicate));
119}
120
121template <typename Clock, typename Duration>
122CvStatus ConditionVariable::WaitUntil(std::unique_lock<Mutex>& lock, std::chrono::time_point<Clock, Duration> until) {
123 return WaitUntil(lock, Deadline::FromTimePoint(until));
124}
125
126template <typename Clock, typename Duration, typename Predicate>
127bool ConditionVariable::WaitUntil(
128 std::unique_lock<Mutex>& lock,
129 std::chrono::time_point<Clock, Duration> until,
130 Predicate&& predicate
131) {
132 return WaitUntil(lock, Deadline::FromTimePoint(until), std::forward<Predicate>(predicate));
133}
134
135template <typename Predicate>
136bool ConditionVariable::WaitUntil(std::unique_lock<Mutex>& lock, Deadline deadline, Predicate&& predicate) {
137 return impl_.WaitUntil(lock, deadline, std::forward<Predicate>(predicate));
138}
139
140} // namespace engine
141
142USERVER_NAMESPACE_END