This repository was archived by the owner on Feb 25, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 3 files changed +37
-5
lines changed Expand file tree Collapse file tree 3 files changed +37
-5
lines changed Original file line number Diff line number Diff line change @@ -18,10 +18,12 @@ SyncSwitch::Handlers& SyncSwitch::Handlers::SetIfFalse(
18
18
return *this ;
19
19
}
20
20
21
- SyncSwitch::SyncSwitch (bool value) : value_(value) {}
21
+ SyncSwitch::SyncSwitch (bool value)
22
+ : mutex_(std::unique_ptr<fml::SharedMutex>(fml::SharedMutex::Create())),
23
+ value_ (value) {}
22
24
23
25
void SyncSwitch::Execute (const SyncSwitch::Handlers& handlers) const {
24
- std::scoped_lock guard ( mutex_);
26
+ fml::SharedLock lock (* mutex_);
25
27
if (value_) {
26
28
handlers.true_handler ();
27
29
} else {
@@ -30,7 +32,7 @@ void SyncSwitch::Execute(const SyncSwitch::Handlers& handlers) const {
30
32
}
31
33
32
34
void SyncSwitch::SetSwitch (bool value) {
33
- std::scoped_lock guard ( mutex_);
35
+ fml::UniqueLock lock (* mutex_);
34
36
value_ = value;
35
37
}
36
38
Original file line number Diff line number Diff line change 7
7
8
8
#include < forward_list>
9
9
#include < functional>
10
- #include < mutex >
10
+ #include < memory >
11
11
12
12
#include " flutter/fml/macros.h"
13
+ #include " flutter/fml/synchronization/shared_mutex.h"
13
14
14
15
namespace fml {
15
16
@@ -53,7 +54,7 @@ class SyncSwitch {
53
54
void SetSwitch (bool value);
54
55
55
56
private:
56
- mutable std::mutex mutex_;
57
+ mutable std::unique_ptr<fml::SharedMutex> mutex_;
57
58
bool value_;
58
59
59
60
FML_DISALLOW_COPY_AND_ASSIGN (SyncSwitch);
Original file line number Diff line number Diff line change 4
4
5
5
#include " flutter/fml/synchronization/sync_switch.h"
6
6
7
+ #include < thread>
8
+
7
9
#include " gtest/gtest.h"
8
10
9
11
using fml::SyncSwitch;
@@ -28,3 +30,30 @@ TEST(SyncSwitchTest, NoopIfUndefined) {
28
30
syncSwitch.Execute (SyncSwitch::Handlers ());
29
31
EXPECT_FALSE (switchValue);
30
32
}
33
+
34
+ TEST (SyncSwitchTest, SharedLock) {
35
+ SyncSwitch syncSwitch;
36
+ syncSwitch.SetSwitch (true );
37
+ bool switchValue1 = false ;
38
+ bool switchValue2 = false ;
39
+
40
+ std::thread thread1 ([&] {
41
+ syncSwitch.Execute (
42
+ SyncSwitch::Handlers ()
43
+ .SetIfTrue ([&] {
44
+ switchValue1 = true ;
45
+
46
+ std::thread thread2 ([&]() {
47
+ syncSwitch.Execute (
48
+ SyncSwitch::Handlers ()
49
+ .SetIfTrue ([&] { switchValue2 = true ; })
50
+ .SetIfFalse ([&] { switchValue2 = false ; }));
51
+ });
52
+ thread2.join ();
53
+ })
54
+ .SetIfFalse ([&] { switchValue1 = false ; }));
55
+ });
56
+ thread1.join ();
57
+ EXPECT_TRUE (switchValue1);
58
+ EXPECT_TRUE (switchValue2);
59
+ }
You can’t perform that action at this time.
0 commit comments