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>
54 CvStatus WaitFor(std::unique_lock<Mutex>& lock, std::chrono::duration<Rep, Period> timeout);
59 template <
typename Rep,
typename Period,
typename Predicate>
60 bool WaitFor(std::unique_lock<Mutex>& lock, std::chrono::duration<Rep, Period> timeout, Predicate&& predicate);
67 template <
typename Clock,
typename Duration>
68 CvStatus WaitUntil(std::unique_lock<Mutex>& lock, std::chrono::time_point<Clock, Duration> until);
75 CvStatus WaitUntil(std::unique_lock<Mutex>& lock, Deadline deadline);
80 template <
typename Clock,
typename Duration,
typename Predicate>
82 WaitUntil(std::unique_lock<Mutex>& lock, std::chrono::time_point<Clock, Duration> until, Predicate&& predicate);
87 template <
typename Predicate>
88 bool WaitUntil(std::unique_lock<Mutex>& lock, Deadline deadline, Predicate&& predicate);
97 impl::ConditionVariableAny<Mutex> impl_;
100template <
typename Predicate>
101bool ConditionVariable::
Wait(std::unique_lock<Mutex>& lock, Predicate&& predicate) {
102 return WaitUntil(lock, {}, std::forward<Predicate>(predicate));
105template <
typename Rep,
typename Period>
106CvStatus ConditionVariable::WaitFor(std::unique_lock<Mutex>& lock, std::chrono::duration<Rep, Period> timeout) {
107 return WaitUntil(lock, Deadline::FromDuration(timeout));
110template <
typename Rep,
typename Period,
typename Predicate>
111bool ConditionVariable::WaitFor(
112 std::unique_lock<Mutex>& lock,
113 std::chrono::duration<Rep, Period> timeout,
114 Predicate&& predicate
116 return WaitUntil(lock, Deadline::FromDuration(timeout), std::forward<Predicate>(predicate));
119template <
typename Clock,
typename Duration>
120CvStatus ConditionVariable::WaitUntil(std::unique_lock<Mutex>& lock, std::chrono::time_point<Clock, Duration> until) {
121 return WaitUntil(lock, Deadline::FromTimePoint(until));
124template <
typename Clock,
typename Duration,
typename Predicate>
125bool ConditionVariable::WaitUntil(
126 std::unique_lock<Mutex>& lock,
127 std::chrono::time_point<Clock, Duration> until,
128 Predicate&& predicate
130 return WaitUntil(lock, Deadline::FromTimePoint(until), std::forward<Predicate>(predicate));
133template <
typename Predicate>
134bool ConditionVariable::WaitUntil(std::unique_lock<Mutex>& lock, Deadline deadline, Predicate&& predicate) {
135 return impl_.WaitUntil(lock, deadline, std::forward<Predicate>(predicate));