| Flutter Impeller |

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
ConditionVariable & | operator= (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:

- On the Wait, WaitFor, and WaitUntil calls, static analysis annotation are respected.
- There is no ability to wait on a condition variable and also be susceptible to spurious wakes. This is because the predicate is mandatory.

Definition at line 144 of file thread.h.

## Member Typedef Documentation

## ◆ Predicate

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

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

  | mutex | The mutex
  [in] | should_stop_waiting | The 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 }

Referenced by impeller::testing::TEST().

## ◆ 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

  | mutex | The mutex.
  [in] | duration | The duration to wait for.
  [in] | should_stop_waiting | The predicate invoked on spurious wakes. Must return false for the wait to continue.

Template Parameters

  Representation | The duration representation type.
  Period | The 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 }

References WaitUntil().

Referenced by impeller::testing::TEST().

## ◆ 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

  | mutex | The mutex.
  [in] | time_point | The time point to wait to.
  [in] | should_stop_waiting | The predicate invoked on spurious wakes. Must return false for the wait to continue.

Template Parameters

  Clock | The clock type.
  Duration | The 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 }

Referenced by impeller::testing::TEST(), and WaitFor().

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

- impeller/base/thread.h

impeller::ConditionVariable::WaitUntil
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