Skip to content

Commit 25a58d7

Browse files
zhongwuzwfacebook-github-bot
authored andcommitted
Consolidate shared_mutex with better::shared_mutex (#24075)
Summary: Replace `folly::SharedMutex` with `better::shared_mutex`, consolidate the shared_mutex. cc. shergin . [General] [Changed] - Consolidate shared_mutex with better::shared_mutex Pull Request resolved: #24075 Differential Revision: D14559213 Pulled By: shergin fbshipit-source-id: 934c7cd7db9ce60031d6b007faeebb353860268f
1 parent b7c2c82 commit 25a58d7

File tree

6 files changed

+21
-24
lines changed

6 files changed

+21
-24
lines changed

Diff for: ReactCommon/fabric/imagemanager/ImageResponseObserverCoordinator.cpp

+10-10
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,16 @@ ImageResponseObserverCoordinator::~ImageResponseObserverCoordinator() {}
2121
void ImageResponseObserverCoordinator::addObserver(
2222
ImageResponseObserver *observer) const {
2323
ImageResponse::Status status = [this] {
24-
std::shared_lock<folly::SharedMutex> read(mutex_);
24+
std::shared_lock<better::shared_mutex> read(mutex_);
2525
return status_;
2626
}();
2727

2828
if (status == ImageResponse::Status::Loading) {
29-
std::unique_lock<folly::SharedMutex> write(mutex_);
29+
std::unique_lock<better::shared_mutex> write(mutex_);
3030
observers_.push_back(observer);
3131
} else if (status == ImageResponse::Status::Completed) {
3232
ImageResponse imageResponseCopy = [this] {
33-
std::unique_lock<folly::SharedMutex> read(mutex_);
33+
std::unique_lock<better::shared_mutex> read(mutex_);
3434
return ImageResponse(imageData_);
3535
}();
3636
observer->didReceiveImage(imageResponseCopy);
@@ -41,7 +41,7 @@ void ImageResponseObserverCoordinator::addObserver(
4141

4242
void ImageResponseObserverCoordinator::removeObserver(
4343
ImageResponseObserver *observer) const {
44-
std::unique_lock<folly::SharedMutex> write(mutex_);
44+
std::unique_lock<better::shared_mutex> write(mutex_);
4545

4646
auto position = std::find(observers_.begin(), observers_.end(), observer);
4747
if (position != observers_.end()) {
@@ -52,7 +52,7 @@ void ImageResponseObserverCoordinator::removeObserver(
5252
void ImageResponseObserverCoordinator::nativeImageResponseProgress(
5353
float progress) const {
5454
std::vector<ImageResponseObserver *> observersCopy = [this] {
55-
std::shared_lock<folly::SharedMutex> read(mutex_);
55+
std::shared_lock<better::shared_mutex> read(mutex_);
5656
return observers_;
5757
}();
5858

@@ -64,19 +64,19 @@ void ImageResponseObserverCoordinator::nativeImageResponseProgress(
6464
void ImageResponseObserverCoordinator::nativeImageResponseComplete(
6565
const ImageResponse &imageResponse) const {
6666
{
67-
std::unique_lock<folly::SharedMutex> write(mutex_);
67+
std::unique_lock<better::shared_mutex> write(mutex_);
6868
imageData_ = imageResponse.getImage();
6969
status_ = ImageResponse::Status::Completed;
7070
}
7171

7272
std::vector<ImageResponseObserver *> observersCopy = [this] {
73-
std::shared_lock<folly::SharedMutex> read(mutex_);
73+
std::shared_lock<better::shared_mutex> read(mutex_);
7474
return observers_;
7575
}();
7676

7777
for (auto observer : observersCopy) {
7878
ImageResponse imageResponseCopy = [this] {
79-
std::unique_lock<folly::SharedMutex> read(mutex_);
79+
std::unique_lock<better::shared_mutex> read(mutex_);
8080
return ImageResponse(imageData_);
8181
}();
8282
observer->didReceiveImage(imageResponseCopy);
@@ -85,12 +85,12 @@ void ImageResponseObserverCoordinator::nativeImageResponseComplete(
8585

8686
void ImageResponseObserverCoordinator::nativeImageResponseFailed() const {
8787
{
88-
std::unique_lock<folly::SharedMutex> write(mutex_);
88+
std::unique_lock<better::shared_mutex> write(mutex_);
8989
status_ = ImageResponse::Status::Failed;
9090
}
9191

9292
std::vector<ImageResponseObserver *> observersCopy = [this] {
93-
std::shared_lock<folly::SharedMutex> read(mutex_);
93+
std::shared_lock<better::shared_mutex> read(mutex_);
9494
return observers_;
9595
}();
9696

Diff for: ReactCommon/fabric/imagemanager/ImageResponseObserverCoordinator.h

+2-3
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
#include <react/imagemanager/ImageResponse.h>
1111
#include <react/imagemanager/ImageResponseObserver.h>
1212

13-
#include <folly/SharedMutex.h>
14-
#include <shared_mutex>
13+
#include <better/mutex.h>
1514
#include <vector>
1615

1716
namespace facebook {
@@ -84,7 +83,7 @@ class ImageResponseObserverCoordinator {
8483
/*
8584
* Observer and data mutex.
8685
*/
87-
mutable folly::SharedMutex mutex_;
86+
mutable better::shared_mutex mutex_;
8887
};
8988

9089
} // namespace react

Diff for: ReactCommon/fabric/uimanager/ShadowTree.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ bool ShadowTree::tryCommit(
149149

150150
{
151151
// Reading `rootShadowNode_` in shared manner.
152-
std::shared_lock<folly::SharedMutex> lock(commitMutex_);
152+
std::shared_lock<better::shared_mutex> lock(commitMutex_);
153153
oldRootShadowNode = rootShadowNode_;
154154
}
155155

@@ -169,7 +169,7 @@ bool ShadowTree::tryCommit(
169169

170170
{
171171
// Updating `rootShadowNode_` in unique manner if it hasn't changed.
172-
std::unique_lock<folly::SharedMutex> lock(commitMutex_);
172+
std::unique_lock<better::shared_mutex> lock(commitMutex_);
173173

174174
if (rootShadowNode_ != oldRootShadowNode) {
175175
return false;

Diff for: ReactCommon/fabric/uimanager/ShadowTree.h

+2-3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@
55

66
#pragma once
77

8-
#include <folly/SharedMutex.h>
8+
#include <better/mutex.h>
99
#include <memory>
10-
#include <shared_mutex>
1110

1211
#include <react/components/root/RootComponentDescriptor.h>
1312
#include <react/components/root/RootShadowNode.h>
@@ -84,7 +83,7 @@ class ShadowTree final {
8483
void emitLayoutEvents(const ShadowViewMutationList &mutations) const;
8584

8685
const SurfaceId surfaceId_;
87-
mutable folly::SharedMutex commitMutex_;
86+
mutable better::shared_mutex commitMutex_;
8887
mutable SharedRootShadowNode rootShadowNode_; // Protected by `commitMutex_`.
8988
mutable int revision_{1}; // Protected by `commitMutex_`.
9089
ShadowTreeDelegate const *delegate_;

Diff for: ReactCommon/fabric/uimanager/ShadowTreeRegistry.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ namespace facebook {
99
namespace react {
1010

1111
void ShadowTreeRegistry::add(std::unique_ptr<ShadowTree> &&shadowTree) const {
12-
std::unique_lock<folly::SharedMutex> lock(mutex_);
12+
std::unique_lock<better::shared_mutex> lock(mutex_);
1313

1414
registry_.emplace(shadowTree->getSurfaceId(), std::move(shadowTree));
1515
}
1616

1717
std::unique_ptr<ShadowTree> ShadowTreeRegistry::remove(
1818
SurfaceId surfaceId) const {
19-
std::unique_lock<folly::SharedMutex> lock(mutex_);
19+
std::unique_lock<better::shared_mutex> lock(mutex_);
2020

2121
auto iterator = registry_.find(surfaceId);
2222
auto shadowTree = std::unique_ptr<ShadowTree>(iterator->second.release());
@@ -27,7 +27,7 @@ std::unique_ptr<ShadowTree> ShadowTreeRegistry::remove(
2727
bool ShadowTreeRegistry::visit(
2828
SurfaceId surfaceId,
2929
std::function<void(const ShadowTree &shadowTree)> callback) const {
30-
std::shared_lock<folly::SharedMutex> lock(mutex_);
30+
std::shared_lock<better::shared_mutex> lock(mutex_);
3131

3232
auto iterator = registry_.find(surfaceId);
3333

Diff for: ReactCommon/fabric/uimanager/ShadowTreeRegistry.h

+2-3
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55

66
#pragma once
77

8-
#include <folly/SharedMutex.h>
9-
#include <shared_mutex>
8+
#include <better/mutex.h>
109

1110
#include <react/core/ReactPrimitives.h>
1211
#include <react/uimanager/ShadowTree.h>
@@ -49,7 +48,7 @@ class ShadowTreeRegistry final {
4948
std::function<void(const ShadowTree &shadowTree)> callback) const;
5049

5150
private:
52-
mutable folly::SharedMutex mutex_;
51+
mutable better::shared_mutex mutex_;
5352
mutable std::unordered_map<SurfaceId, std::unique_ptr<ShadowTree>>
5453
registry_; // Protected by `mutex_`.
5554
};

0 commit comments

Comments
 (0)