Flutter Engine
 
Loading...
Searching...
No Matches
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 24 of file sync_switch.cc.

24: value_(value) {}

References value.

Member Function Documentation

◆ AddObserver()

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

Threadsafe.

Definition at line 45 of file sync_switch.cc.

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

◆ 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 26 of file sync_switch.cc.

26 {
27 std::shared_lock lock(mutex_);
28 if (value_) {
29 handlers.true_handler();
30 } else {
31 handlers.false_handler();
32 }
33}

References fml::SyncSwitch::Handlers::false_handler, and fml::SyncSwitch::Handlers::true_handler.

Referenced by TEST(), TEST(), and TEST().

◆ RemoveObserver()

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

Threadsafe.

Definition at line 53 of file sync_switch.cc.

53 {
54 std::unique_lock lock(mutex_);
55 observers_.erase(std::remove(observers_.begin(), observers_.end(), observer),
56 observers_.end());
57}

◆ 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 35 of file sync_switch.cc.

35 {
36 {
37 std::unique_lock lock(mutex_);
38 value_ = value;
39 }
40 for (Observer* observer : observers_) {
41 observer->OnSyncSwitchUpdate(value);
42 }
43}
int32_t value

References value.

Referenced by TEST(), and TEST().


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