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 | #include "flutter/fml/synchronization/sync_switch.h" |
6 | |
7 | namespace fml { |
8 | |
9 | SyncSwitch::Handlers& SyncSwitch::Handlers::SetIfTrue( |
10 | const std::function<void()>& handler) { |
11 | true_handler = std::move(handler); |
12 | return *this; |
13 | } |
14 | |
15 | SyncSwitch::Handlers& SyncSwitch::Handlers::SetIfFalse( |
16 | const std::function<void()>& handler) { |
17 | false_handler = std::move(handler); |
18 | return *this; |
19 | } |
20 | |
21 | SyncSwitch::SyncSwitch() : SyncSwitch(false) {} |
22 | |
23 | SyncSwitch::SyncSwitch(bool value) : value_(value) {} |
24 | |
25 | void SyncSwitch::Execute(const SyncSwitch::Handlers& handlers) { |
26 | std::scoped_lock guard(mutex_); |
27 | if (value_) { |
28 | handlers.true_handler(); |
29 | } else { |
30 | handlers.false_handler(); |
31 | } |
32 | } |
33 | |
34 | void SyncSwitch::SetSwitch(bool value) { |
35 | std::scoped_lock guard(mutex_); |
36 | value_ = value; |
37 | } |
38 | |
39 | } // namespace fml |
40 |