Flutter Engine
fml::SyncSwitch Class Reference

#include <sync_switch.h>

Classes

struct  Handlers
 Represents the 2 code paths available when calling |SyncSwitch::Execute|. More...
 

Public Member Functions

 SyncSwitch ()
 Create a |SyncSwitch| with the false value. More...
 
 SyncSwitch (bool value)
 
void Execute (const Handlers &handlers)
 
void SetSwitch (bool value)
 

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 21 of file sync_switch.h.

Constructor & Destructor Documentation

◆ SyncSwitch() [1/2]

fml::SyncSwitch::SyncSwitch ( )

Create a |SyncSwitch| with the false value.

Definition at line 21 of file sync_switch.cc.

21 : SyncSwitch(false) {}
SyncSwitch()
Create a |SyncSwitch| with the false value.
Definition: sync_switch.cc:21

◆ SyncSwitch() [2/2]

fml::SyncSwitch::SyncSwitch ( bool  value)

Create a |SyncSwitch| with the specified value.

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

Definition at line 23 of file sync_switch.cc.

23 : value_(value) {}
uint8_t value

Member Function Documentation

◆ Execute()

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

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.

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

Referenced by TEST().

25  {
26  std::scoped_lock guard(mutex_);
27  if (value_) {
28  handlers.true_handler();
29  } else {
30  handlers.false_handler();
31  }
32 }

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

References value.

Referenced by TEST().

34  {
35  std::scoped_lock guard(mutex_);
36  value_ = value;
37 }
uint8_t value

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