Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Public Types | Public Member Functions | List of all members
impeller::ConditionVariable Class Reference

A condition variable exactly similar to the one in libcxx with two major differences: More...

#include <thread.h>

Public Types

using Predicate = std::function< bool()>
 

Public Member Functions

 ConditionVariable ()=default
 
 ~ConditionVariable ()=default
 
 ConditionVariable (const ConditionVariable &)=delete
 
ConditionVariableoperator= (const ConditionVariable &)=delete
 
void NotifyOne ()
 
void NotifyAll ()
 
template<class Clock , class Duration >
bool WaitUntil (Mutex &mutex, const std::chrono::time_point< Clock, Duration > &time_point, const Predicate &should_stop_waiting) IPLR_REQUIRES(mutex)
 Atomically unlocks the mutex and waits on the condition variable up to a specified time point. Lock will be reacquired when the wait exits. Spurious wakes may happen before the time point is reached. In such cases the predicate is invoked and it must return false for the wait to continue. The predicate will be invoked with the mutex locked.
 
template<class Representation , class Period >
bool WaitFor (Mutex &mutex, const std::chrono::duration< Representation, Period > &duration, const Predicate &should_stop_waiting) IPLR_REQUIRES(mutex)
 Atomically unlocks the mutex and waits on the condition variable for a designated duration. Lock will be reacquired when the wait exits. Spurious wakes may happen before the time point is reached. In such cases the predicate is invoked and it must return false for the wait to continue. The predicate will be invoked with the mutex locked.
 
void Wait (Mutex &mutex, const Predicate &should_stop_waiting) IPLR_REQUIRES(mutex)
 Atomically unlocks the mutex and waits on the condition variable indefinitely till the predicate determines that the wait must end. Lock will be reacquired when the wait exits. Spurious wakes may happen before the time point is reached. In such cases the predicate is invoked and it must return false for the wait to continue. The predicate will be invoked with the mutex locked.
 

Detailed Description

A condition variable exactly similar to the one in libcxx with two major differences:

Definition at line 146 of file thread.h.

Member Typedef Documentation

◆ Predicate

using impeller::ConditionVariable::Predicate = std::function<bool()>

Definition at line 160 of file thread.h.

Constructor & Destructor Documentation

◆ ConditionVariable() [1/2]

impeller::ConditionVariable::ConditionVariable ( )
default

◆ ~ConditionVariable()

impeller::ConditionVariable::~ConditionVariable ( )
default

◆ ConditionVariable() [2/2]

impeller::ConditionVariable::ConditionVariable ( const ConditionVariable )
delete

Member Function Documentation

◆ NotifyAll()

void impeller::ConditionVariable::NotifyAll ( )
inline

Definition at line 158 of file thread.h.

158{ cv_.notify_all(); }

◆ NotifyOne()

void impeller::ConditionVariable::NotifyOne ( )
inline

Definition at line 156 of file thread.h.

156{ cv_.notify_one(); }

◆ operator=()

ConditionVariable & impeller::ConditionVariable::operator= ( const ConditionVariable )
delete

◆ Wait()

void impeller::ConditionVariable::Wait ( Mutex &  mutex,
const Predicate should_stop_waiting 
)
inline

Atomically unlocks the mutex and waits on the condition variable indefinitely till the predicate determines that the wait must end. Lock will be reacquired when the wait exits. Spurious wakes may happen before the time point is reached. In such cases the predicate is invoked and it must return false for the wait to continue. The predicate will be invoked with the mutex locked.

Note
Since the predicate is invoked with the mutex locked, if it accesses other guarded resources, the predicate itself must be decorated with the IPLR_REQUIRES directive. For instance,

c++ [] () IPLR_REQUIRES(mutex) { return my_guarded_resource.should_stop_waiting; }

Parameters
mutexThe mutex
[in]should_stop_waitingThe should stop waiting

Definition at line 260 of file thread.h.

261 {
262 std::unique_lock lock(mutex.mutex_, std::adopt_lock);
263 cv_.wait(lock, should_stop_waiting);
264 lock.release();
265 }

◆ WaitFor()

template<class Representation , class Period >
bool impeller::ConditionVariable::WaitFor ( Mutex &  mutex,
const std::chrono::duration< Representation, Period > &  duration,
const Predicate should_stop_waiting 
)
inline

Atomically unlocks the mutex and waits on the condition variable for a designated duration. Lock will be reacquired when the wait exits. Spurious wakes may happen before the time point is reached. In such cases the predicate is invoked and it must return false for the wait to continue. The predicate will be invoked with the mutex locked.

Note
Since the predicate is invoked with the mutex locked, if it accesses other guarded resources, the predicate itself must be decorated with the IPLR_REQUIRES directive. For instance,

c++ [] () IPLR_REQUIRES(mutex) { return my_guarded_resource.should_stop_waiting; }

Parameters
mutexThe mutex.
[in]durationThe duration to wait for.
[in]should_stop_waitingThe predicate invoked on spurious wakes. Must return false for the wait to continue.
Template Parameters
RepresentationThe duration representation type.
PeriodThe duration period type.
Returns
The value of the predicate at the end of the wait.

Definition at line 231 of file thread.h.

233 {
234 return WaitUntil(mutex, std::chrono::steady_clock::now() + duration,
235 should_stop_waiting);
236 }
bool WaitUntil(Mutex &mutex, const std::chrono::time_point< Clock, Duration > &time_point, const Predicate &should_stop_waiting) IPLR_REQUIRES(mutex)
Atomically unlocks the mutex and waits on the condition variable up to a specified time point....
Definition thread.h:192
double duration
Definition examples.cpp:30

◆ WaitUntil()

template<class Clock , class Duration >
bool impeller::ConditionVariable::WaitUntil ( Mutex &  mutex,
const std::chrono::time_point< Clock, Duration > &  time_point,
const Predicate should_stop_waiting 
)
inline

Atomically unlocks the mutex and waits on the condition variable up to a specified time point. Lock will be reacquired when the wait exits. Spurious wakes may happen before the time point is reached. In such cases the predicate is invoked and it must return false for the wait to continue. The predicate will be invoked with the mutex locked.

Note
Since the predicate is invoked with the mutex locked, if it accesses other guarded resources, the predicate itself must be decorated with the IPLR_REQUIRES directive. For instance,

c++ [] () IPLR_REQUIRES(mutex) { return my_guarded_resource.should_stop_waiting; }

Parameters
mutexThe mutex.
[in]time_pointThe time point to wait to.
[in]should_stop_waitingThe predicate invoked on spurious wakes. Must return false for the wait to continue.
Template Parameters
ClockThe clock type.
DurationThe duration type.
Returns
The value of the predicate at the end of the wait.

Definition at line 192 of file thread.h.

194 {
195 std::unique_lock lock(mutex.mutex_, std::adopt_lock);
196 const auto result = cv_.wait_until(lock, time_point, should_stop_waiting);
197 lock.release();
198 return result;
199 }
GAsyncResult * result

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