Flutter Engine
The Flutter Engine
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. More...
 
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. More...
 
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. More...
 

Detailed Description

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

Definition at line 144 of file thread.h.

Member Typedef Documentation

◆ Predicate

Definition at line 158 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 156 of file thread.h.

156{ cv_.notify_all(); }

◆ NotifyOne()

void impeller::ConditionVariable::NotifyOne ( )
inline

Definition at line 154 of file thread.h.

154{ 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 258 of file thread.h.

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

◆ 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 229 of file thread.h.

231 {
232 return WaitUntil(mutex, std::chrono::steady_clock::now() + duration,
233 should_stop_waiting);
234 }
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:190
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 190 of file thread.h.

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

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