9#include <userver/engine/condition_variable_status.hpp>
10#include <userver/engine/deadline.hpp>
11#include <userver/engine/impl/condition_variable_any.hpp>
12#include <userver/engine/mutex.hpp>
14USERVER_NAMESPACE_BEGIN
27class ConditionVariable
final {
32 ConditionVariable(
const ConditionVariable&) =
delete;
33 ConditionVariable(ConditionVariable&&) =
delete;
34 ConditionVariable& operator=(
const ConditionVariable&) =
delete;
35 ConditionVariable& operator=(ConditionVariable&&) =
delete;
45 template <
typename Predicate>
46 [[nodiscard]]
bool Wait(std::unique_lock<Mutex>& lock, Predicate&& predicate);
53 template <
typename Rep,
typename Period>
55 std::chrono::duration<Rep, Period> timeout);
60 template <
typename Rep,
typename Period,
typename Predicate>
61 bool WaitFor(std::unique_lock<Mutex>& lock,
62 std::chrono::duration<Rep, Period> timeout,
63 Predicate&& predicate);
70 template <
typename Clock,
typename Duration>
72 std::chrono::time_point<Clock, Duration> until);
84 template <
typename Clock,
typename Duration,
typename Predicate>
85 bool WaitUntil(std::unique_lock<Mutex>& lock,
86 std::chrono::time_point<Clock, Duration> until,
87 Predicate&& predicate);
92 template <
typename Predicate>
93 bool WaitUntil(std::unique_lock<Mutex>& lock, Deadline deadline,
94 Predicate&& predicate);
103 impl::ConditionVariableAny<Mutex> impl_;
106template <
typename Predicate>
107bool ConditionVariable::
Wait(std::unique_lock<Mutex>& lock,
108 Predicate&& predicate) {
109 return WaitUntil(lock, {}, std::forward<Predicate>(predicate));
112template <
typename Rep,
typename Period>
114 std::unique_lock<Mutex>& lock, std::chrono::duration<Rep, Period> timeout) {
115 return WaitUntil(lock, Deadline::FromDuration(timeout));
118template <
typename Rep,
typename Period,
typename Predicate>
119bool ConditionVariable::
WaitFor(std::unique_lock<Mutex>& lock,
120 std::chrono::duration<Rep, Period> timeout,
121 Predicate&& predicate) {
122 return WaitUntil(lock, Deadline::FromDuration(timeout),
123 std::forward<Predicate>(predicate));
126template <
typename Clock,
typename Duration>
128 std::unique_lock<Mutex>& lock,
129 std::chrono::time_point<Clock, Duration> until) {
130 return WaitUntil(lock, Deadline::FromTimePoint(until));
133template <
typename Clock,
typename Duration,
typename Predicate>
135 std::unique_lock<Mutex>& lock,
136 std::chrono::time_point<Clock, Duration> until, Predicate&& predicate) {
137 return WaitUntil(lock, Deadline::FromTimePoint(until),
138 std::forward<Predicate>(predicate));
141template <
typename Predicate>
142bool ConditionVariable::
WaitUntil(std::unique_lock<Mutex>& lock,
143 Deadline deadline, Predicate&& predicate) {
144 return impl_.WaitUntil(lock, deadline, std::forward<Predicate>(predicate));