userver: engine::ConditionVariable Class Reference
Loading...
Searching...
No Matches
engine::ConditionVariable Class Referencefinal

#include <userver/engine/condition_variable.hpp>

Detailed Description

std::condition_variable replacement for asynchronous tasks

Example usage:

bool ok = false;
auto task = engine::AsyncNoSpan([&] {
std::unique_lock<engine::Mutex> lock(mutex);
EXPECT_TRUE(cv.Wait(lock, [&ok] { return ok; }));
});
EXPECT_FALSE(task.IsFinished());
ok = true;
cv.NotifyAll();
EXPECT_TRUE(task.IsFinished());
See also
Synchronization Primitives

Definition at line 27 of file condition_variable.hpp.

Public Member Functions

 ConditionVariable (const ConditionVariable &)=delete
 
 ConditionVariable (ConditionVariable &&)=delete
 
ConditionVariableoperator= (const ConditionVariable &)=delete
 
ConditionVariableoperator= (ConditionVariable &&)=delete
 
CvStatus Wait (std::unique_lock< Mutex > &lock)
 
template<typename Predicate >
bool Wait (std::unique_lock< Mutex > &lock, Predicate &&predicate)
 Suspends execution until the predicate is true when notification is received or the task is cancelled.
 
template<typename Rep , typename Period >
CvStatus WaitFor (std::unique_lock< Mutex > &lock, std::chrono::duration< Rep, Period > timeout)
 Suspends execution until notified or until the timeout expires or until the task is cancelled.
 
template<typename Rep , typename Period , typename Predicate >
bool WaitFor (std::unique_lock< Mutex > &lock, std::chrono::duration< Rep, Period > timeout, Predicate &&predicate)
 Suspends execution until the predicate is true when notified or the timeout expires or the task is cancelled.
 
template<typename Clock , typename Duration >
CvStatus WaitUntil (std::unique_lock< Mutex > &lock, std::chrono::time_point< Clock, Duration > until)
 Suspends execution until notified or the time point is reached or the task is cancelled.
 
CvStatus WaitUntil (std::unique_lock< Mutex > &lock, Deadline deadline)
 Suspends execution until notified or the deadline is reached or the task is cancelled.
 
template<typename Clock , typename Duration , typename Predicate >
bool WaitUntil (std::unique_lock< Mutex > &lock, std::chrono::time_point< Clock, Duration > until, Predicate &&predicate)
 Suspends execution until the predicate is true when notified or the time point is reached or the task is cancelled.
 
template<typename Predicate >
bool WaitUntil (std::unique_lock< Mutex > &lock, Deadline deadline, Predicate &&predicate)
 Suspends execution until the predicate is true when notified or the deadline is reached or the task is cancelled.
 
void NotifyOne ()
 Notifies one of the waiting tasks.
 
void NotifyAll ()
 Notifies all waiting tasks.
 

Member Function Documentation

◆ Wait() [1/2]

CvStatus engine::ConditionVariable::Wait ( std::unique_lock< Mutex > & lock)

Suspends execution until notified or cancelled

Returns
CvStatus::kNoTimeout if variable was notified
CvStatus::kCancelled if current task is being cancelled

◆ Wait() [2/2]

template<typename Predicate >
bool engine::ConditionVariable::Wait ( std::unique_lock< Mutex > & lock,
Predicate && predicate )

Suspends execution until the predicate is true when notification is received or the task is cancelled.

Returns
the value of the predicate

Definition at line 107 of file condition_variable.hpp.

◆ WaitFor() [1/2]

template<typename Rep , typename Period >
CvStatus engine::ConditionVariable::WaitFor ( std::unique_lock< Mutex > & lock,
std::chrono::duration< Rep, Period > timeout )

Suspends execution until notified or until the timeout expires or until the task is cancelled.

Returns
CvStatus::kNoTimeout if variable was notified
CvStatus::kTimeout if timeout has expired
CvStatus::kCancelled if current task is being cancelled

Definition at line 113 of file condition_variable.hpp.

◆ WaitFor() [2/2]

template<typename Rep , typename Period , typename Predicate >
bool engine::ConditionVariable::WaitFor ( std::unique_lock< Mutex > & lock,
std::chrono::duration< Rep, Period > timeout,
Predicate && predicate )

Suspends execution until the predicate is true when notified or the timeout expires or the task is cancelled.

Returns
the value of the predicate

Definition at line 119 of file condition_variable.hpp.

◆ WaitUntil() [1/4]

CvStatus engine::ConditionVariable::WaitUntil ( std::unique_lock< Mutex > & lock,
Deadline deadline )

Suspends execution until notified or the deadline is reached or the task is cancelled.

Returns
CvStatus::kNoTimeout if variable was notified
CvStatus::kTimeout if deadline was reached
CvStatus::kCancelled if current task is being cancelled

◆ WaitUntil() [2/4]

template<typename Predicate >
bool engine::ConditionVariable::WaitUntil ( std::unique_lock< Mutex > & lock,
Deadline deadline,
Predicate && predicate )

Suspends execution until the predicate is true when notified or the deadline is reached or the task is cancelled.

Returns
the value of the predicate

Definition at line 142 of file condition_variable.hpp.

◆ WaitUntil() [3/4]

template<typename Clock , typename Duration >
CvStatus engine::ConditionVariable::WaitUntil ( std::unique_lock< Mutex > & lock,
std::chrono::time_point< Clock, Duration > until )

Suspends execution until notified or the time point is reached or the task is cancelled.

Returns
CvStatus::kNoTimeout if variable was notified
CvStatus::kTimeout if until time point was reached
CvStatus::kCancelled if current task is being cancelled

Definition at line 127 of file condition_variable.hpp.

◆ WaitUntil() [4/4]

template<typename Clock , typename Duration , typename Predicate >
bool engine::ConditionVariable::WaitUntil ( std::unique_lock< Mutex > & lock,
std::chrono::time_point< Clock, Duration > until,
Predicate && predicate )

Suspends execution until the predicate is true when notified or the time point is reached or the task is cancelled.

Returns
the value of the predicate

Definition at line 134 of file condition_variable.hpp.


The documentation for this class was generated from the following file: