Flutter Engine
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 <forward_list>
9 #include <functional>
10 #include <mutex>
11 
12 #include "flutter/fml/macros.h"
13 
14 namespace fml {
15 
16 /// A threadsafe structure that allows you to switch between 2 different
17 /// execution paths.
18 ///
19 /// Execution and setting the switch is exclusive, i.e. only one will happen
20 /// at a time.
21 class SyncSwitch {
22  public:
23  /// Represents the 2 code paths available when calling |SyncSwitch::Execute|.
24  struct Handlers {
25  /// Sets the handler that will be executed if the |SyncSwitch| is true.
26  Handlers& SetIfTrue(const std::function<void()>& handler);
27 
28  /// Sets the handler that will be executed if the |SyncSwitch| is false.
29  Handlers& SetIfFalse(const std::function<void()>& handler);
30 
31  std::function<void()> true_handler = [] {};
32  std::function<void()> false_handler = [] {};
33  };
34 
35  /// Create a |SyncSwitch| with the specified value.
36  ///
37  /// @param[in] value Default value for the |SyncSwitch|.
38  explicit SyncSwitch(bool value = false);
39 
40  /// Diverge execution between true and false values of the SyncSwitch.
41  ///
42  /// This can be called on any thread. Note that attempting to call
43  /// |SetSwitch| inside of the handlers will result in a self deadlock.
44  ///
45  /// @param[in] handlers Called for the correct value of the |SyncSwitch|.
46  void Execute(const Handlers& handlers) const;
47 
48  /// Set the value of the SyncSwitch.
49  ///
50  /// This can be called on any thread.
51  ///
52  /// @param[in] value New value for the |SyncSwitch|.
53  void SetSwitch(bool value);
54 
55  private:
56  mutable std::mutex mutex_;
57  bool value_;
58 
60 };
61 
62 } // namespace fml
63 
64 #endif // FLUTTER_FML_SYNCHRONIZATION_SYNC_SWITCH_H_
Handlers & SetIfTrue(const std::function< void()> &handler)
Sets the handler that will be executed if the |SyncSwitch| is true.
Definition: sync_switch.cc:9
void SetSwitch(bool value)
Definition: sync_switch.cc:32
std::function< void()> false_handler
Definition: sync_switch.h:32
Dart_NativeFunction function
Definition: fuchsia.cc:51
std::function< void()> true_handler
Definition: sync_switch.h:31
Definition: ascii_trie.cc:9
void Execute(const Handlers &handlers) const
Definition: sync_switch.cc:23
uint8_t value
SyncSwitch(bool value=false)
Definition: sync_switch.cc:21
Handlers & SetIfFalse(const std::function< void()> &handler)
Sets the handler that will be executed if the |SyncSwitch| is false.
Definition: sync_switch.cc:15
#define FML_DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition: macros.h:27
Represents the 2 code paths available when calling |SyncSwitch::Execute|.
Definition: sync_switch.h:24