Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit e38914e

Browse files
Replace 'std::mutex' inside 'SyncSwitch' to 'fml::SharedMutex' (#32773)
1 parent 9b2c8a1 commit e38914e

File tree

3 files changed

+37
-5
lines changed

3 files changed

+37
-5
lines changed

fml/synchronization/sync_switch.cc

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@ SyncSwitch::Handlers& SyncSwitch::Handlers::SetIfFalse(
1818
return *this;
1919
}
2020

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) {}
2224

2325
void SyncSwitch::Execute(const SyncSwitch::Handlers& handlers) const {
24-
std::scoped_lock guard(mutex_);
26+
fml::SharedLock lock(*mutex_);
2527
if (value_) {
2628
handlers.true_handler();
2729
} else {
@@ -30,7 +32,7 @@ void SyncSwitch::Execute(const SyncSwitch::Handlers& handlers) const {
3032
}
3133

3234
void SyncSwitch::SetSwitch(bool value) {
33-
std::scoped_lock guard(mutex_);
35+
fml::UniqueLock lock(*mutex_);
3436
value_ = value;
3537
}
3638

fml/synchronization/sync_switch.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77

88
#include <forward_list>
99
#include <functional>
10-
#include <mutex>
10+
#include <memory>
1111

1212
#include "flutter/fml/macros.h"
13+
#include "flutter/fml/synchronization/shared_mutex.h"
1314

1415
namespace fml {
1516

@@ -53,7 +54,7 @@ class SyncSwitch {
5354
void SetSwitch(bool value);
5455

5556
private:
56-
mutable std::mutex mutex_;
57+
mutable std::unique_ptr<fml::SharedMutex> mutex_;
5758
bool value_;
5859

5960
FML_DISALLOW_COPY_AND_ASSIGN(SyncSwitch);

fml/synchronization/sync_switch_unittest.cc

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
#include "flutter/fml/synchronization/sync_switch.h"
66

7+
#include <thread>
8+
79
#include "gtest/gtest.h"
810

911
using fml::SyncSwitch;
@@ -28,3 +30,30 @@ TEST(SyncSwitchTest, NoopIfUndefined) {
2830
syncSwitch.Execute(SyncSwitch::Handlers());
2931
EXPECT_FALSE(switchValue);
3032
}
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+
}

0 commit comments

Comments
 (0)