Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
sync_switch.h
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef FLUTTER_FML_SYNCHRONIZATION_SYNC_SWITCH_H_
6#define FLUTTER_FML_SYNCHRONIZATION_SYNC_SWITCH_H_
7
8#include <functional>
9#include <memory>
10#include <vector>
11
12#include "flutter/fml/macros.h"
13#include "flutter/fml/synchronization/shared_mutex.h"
14
15namespace fml {
16
17/// A threadsafe structure that allows you to switch between 2 different
18/// execution paths.
19///
20/// Execution and setting the switch is exclusive, i.e. only one will happen
21/// at a time.
23 public:
24 /// Observes changes to the SyncSwitch.
25 class Observer {
26 public:
27 virtual ~Observer() = default;
28 /// `new_is_disabled` not guaranteed to be the value of the SyncSwitch
29 /// during execution, it should be checked with calls to
30 /// SyncSwitch::Execute.
31 virtual void OnSyncSwitchUpdate(bool new_is_disabled) = 0;
32 };
33
34 /// Represents the 2 code paths available when calling |SyncSwitch::Execute|.
35 struct Handlers {
36 /// Sets the handler that will be executed if the |SyncSwitch| is true.
37 Handlers& SetIfTrue(const std::function<void()>& handler);
38
39 /// Sets the handler that will be executed if the |SyncSwitch| is false.
40 Handlers& SetIfFalse(const std::function<void()>& handler);
41
42 std::function<void()> true_handler = [] {};
43 std::function<void()> false_handler = [] {};
44 };
45
46 /// Create a |SyncSwitch| with the specified value.
47 ///
48 /// @param[in] value Default value for the |SyncSwitch|.
49 explicit SyncSwitch(bool value = false);
50
51 /// Diverge execution between true and false values of the SyncSwitch.
52 ///
53 /// This can be called on any thread. Note that attempting to call
54 /// |SetSwitch| inside of the handlers will result in a self deadlock.
55 ///
56 /// @param[in] handlers Called for the correct value of the |SyncSwitch|.
57 void Execute(const Handlers& handlers) const;
58
59 /// Set the value of the SyncSwitch.
60 ///
61 /// This can be called on any thread.
62 ///
63 /// @param[in] value New value for the |SyncSwitch|.
64 void SetSwitch(bool value);
65
66 /// Threadsafe.
67 void AddObserver(Observer* observer) const;
68
69 /// Threadsafe.
70 void RemoveObserver(Observer* observer) const;
71
72 private:
73 mutable std::unique_ptr<fml::SharedMutex> mutex_;
74 mutable std::vector<Observer*> observers_;
75 bool value_;
76
78};
79
80} // namespace fml
81
82#endif // FLUTTER_FML_SYNCHRONIZATION_SYNC_SWITCH_H_
Observes changes to the SyncSwitch.
Definition sync_switch.h:25
virtual void OnSyncSwitchUpdate(bool new_is_disabled)=0
virtual ~Observer()=default
void AddObserver(Observer *observer) const
Threadsafe.
void RemoveObserver(Observer *observer) const
Threadsafe.
void Execute(const Handlers &handlers) const
void SetSwitch(bool value)
#define FML_DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition macros.h:27
Represents the 2 code paths available when calling |SyncSwitchExecute|.
Definition sync_switch.h:35
std::function< void()> false_handler
Definition sync_switch.h:43
Handlers & SetIfFalse(const std::function< void()> &handler)
Sets the handler that will be executed if the |SyncSwitch| is false.
Handlers & SetIfTrue(const std::function< void()> &handler)
Sets the handler that will be executed if the |SyncSwitch| is true.
Definition sync_switch.cc:9
std::function< void()> true_handler
Definition sync_switch.h:42