Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Classes | Public Member Functions | List of all members
fml::SyncSwitch Class Reference

#include <sync_switch.h>

Classes

struct  Handlers
 Represents the 2 code paths available when calling |SyncSwitchExecute|. More...
 
class  Observer
 Observes changes to the SyncSwitch. More...
 

Public Member Functions

 SyncSwitch (bool value=false)
 
void Execute (const Handlers &handlers) const
 
void SetSwitch (bool value)
 
void AddObserver (Observer *observer) const
 Threadsafe.
 
void RemoveObserver (Observer *observer) const
 Threadsafe.
 

Detailed Description

A threadsafe structure that allows you to switch between 2 different execution paths.

Execution and setting the switch is exclusive, i.e. only one will happen at a time.

Definition at line 22 of file sync_switch.h.

Constructor & Destructor Documentation

◆ SyncSwitch()

fml::SyncSwitch::SyncSwitch ( bool  value = false)
explicit

Create a |SyncSwitch| with the specified value.

Parameters
[in]valueDefault value for the |SyncSwitch|.

Definition at line 21 of file sync_switch.cc.

22 : mutex_(std::unique_ptr<fml::SharedMutex>(fml::SharedMutex::Create())),
23 value_(value) {}
static SharedMutex * Create()

Member Function Documentation

◆ AddObserver()

void fml::SyncSwitch::AddObserver ( Observer observer) const

Threadsafe.

Definition at line 44 of file sync_switch.cc.

44 {
45 fml::UniqueLock lock(*mutex_);
46 if (std::find(observers_.begin(), observers_.end(), observer) ==
47 observers_.end()) {
48 observers_.push_back(observer);
49 }
50}

◆ Execute()

void fml::SyncSwitch::Execute ( const Handlers handlers) const

Diverge execution between true and false values of the SyncSwitch.

This can be called on any thread. Note that attempting to call |SetSwitch| inside of the handlers will result in a self deadlock.

Parameters
[in]handlersCalled for the correct value of the |SyncSwitch|.

Definition at line 25 of file sync_switch.cc.

25 {
26 fml::SharedLock lock(*mutex_);
27 if (value_) {
28 handlers.true_handler();
29 } else {
30 handlers.false_handler();
31 }
32}

◆ RemoveObserver()

void fml::SyncSwitch::RemoveObserver ( Observer observer) const

Threadsafe.

Definition at line 52 of file sync_switch.cc.

52 {
53 fml::UniqueLock lock(*mutex_);
54 observers_.erase(std::remove(observers_.begin(), observers_.end(), observer),
55 observers_.end());
56}

◆ SetSwitch()

void fml::SyncSwitch::SetSwitch ( bool  value)

Set the value of the SyncSwitch.

This can be called on any thread.

Parameters
[in]valueNew value for the |SyncSwitch|.

Definition at line 34 of file sync_switch.cc.

34 {
35 {
36 fml::UniqueLock lock(*mutex_);
37 value_ = value;
38 }
39 for (Observer* observer : observers_) {
40 observer->OnSyncSwitchUpdate(value);
41 }
42}
uint8_t value

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