Skip to content

Commit

Permalink
Consolidate shared_mutex with better::shared_mutex (#24075)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
zhongwuzw authored and facebook-github-bot committed Mar 21, 2019
1 parent b7c2c82 commit 25a58d7
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@ ImageResponseObserverCoordinator::~ImageResponseObserverCoordinator() {}
void ImageResponseObserverCoordinator::addObserver(
ImageResponseObserver *observer) const {
ImageResponse::Status status = [this] {
std::shared_lock<folly::SharedMutex> read(mutex_);
std::shared_lock<better::shared_mutex> read(mutex_);
return status_;
}();

if (status == ImageResponse::Status::Loading) {
std::unique_lock<folly::SharedMutex> write(mutex_);
std::unique_lock<better::shared_mutex> write(mutex_);
observers_.push_back(observer);
} else if (status == ImageResponse::Status::Completed) {
ImageResponse imageResponseCopy = [this] {
std::unique_lock<folly::SharedMutex> read(mutex_);
std::unique_lock<better::shared_mutex> read(mutex_);
return ImageResponse(imageData_);
}();
observer->didReceiveImage(imageResponseCopy);
Expand All @@ -41,7 +41,7 @@ void ImageResponseObserverCoordinator::addObserver(

void ImageResponseObserverCoordinator::removeObserver(
ImageResponseObserver *observer) const {
std::unique_lock<folly::SharedMutex> write(mutex_);
std::unique_lock<better::shared_mutex> write(mutex_);

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

Expand All @@ -64,19 +64,19 @@ void ImageResponseObserverCoordinator::nativeImageResponseProgress(
void ImageResponseObserverCoordinator::nativeImageResponseComplete(
const ImageResponse &imageResponse) const {
{
std::unique_lock<folly::SharedMutex> write(mutex_);
std::unique_lock<better::shared_mutex> write(mutex_);
imageData_ = imageResponse.getImage();
status_ = ImageResponse::Status::Completed;
}

std::vector<ImageResponseObserver *> observersCopy = [this] {
std::shared_lock<folly::SharedMutex> read(mutex_);
std::shared_lock<better::shared_mutex> read(mutex_);
return observers_;
}();

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

void ImageResponseObserverCoordinator::nativeImageResponseFailed() const {
{
std::unique_lock<folly::SharedMutex> write(mutex_);
std::unique_lock<better::shared_mutex> write(mutex_);
status_ = ImageResponse::Status::Failed;
}

std::vector<ImageResponseObserver *> observersCopy = [this] {
std::shared_lock<folly::SharedMutex> read(mutex_);
std::shared_lock<better::shared_mutex> read(mutex_);
return observers_;
}();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
#include <react/imagemanager/ImageResponse.h>
#include <react/imagemanager/ImageResponseObserver.h>

#include <folly/SharedMutex.h>
#include <shared_mutex>
#include <better/mutex.h>
#include <vector>

namespace facebook {
Expand Down Expand Up @@ -84,7 +83,7 @@ class ImageResponseObserverCoordinator {
/*
* Observer and data mutex.
*/
mutable folly::SharedMutex mutex_;
mutable better::shared_mutex mutex_;
};

} // namespace react
Expand Down
4 changes: 2 additions & 2 deletions ReactCommon/fabric/uimanager/ShadowTree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ bool ShadowTree::tryCommit(

{
// Reading `rootShadowNode_` in shared manner.
std::shared_lock<folly::SharedMutex> lock(commitMutex_);
std::shared_lock<better::shared_mutex> lock(commitMutex_);
oldRootShadowNode = rootShadowNode_;
}

Expand All @@ -169,7 +169,7 @@ bool ShadowTree::tryCommit(

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

if (rootShadowNode_ != oldRootShadowNode) {
return false;
Expand Down
5 changes: 2 additions & 3 deletions ReactCommon/fabric/uimanager/ShadowTree.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@

#pragma once

#include <folly/SharedMutex.h>
#include <better/mutex.h>
#include <memory>
#include <shared_mutex>

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

const SurfaceId surfaceId_;
mutable folly::SharedMutex commitMutex_;
mutable better::shared_mutex commitMutex_;
mutable SharedRootShadowNode rootShadowNode_; // Protected by `commitMutex_`.
mutable int revision_{1}; // Protected by `commitMutex_`.
ShadowTreeDelegate const *delegate_;
Expand Down
6 changes: 3 additions & 3 deletions ReactCommon/fabric/uimanager/ShadowTreeRegistry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ namespace facebook {
namespace react {

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

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

std::unique_ptr<ShadowTree> ShadowTreeRegistry::remove(
SurfaceId surfaceId) const {
std::unique_lock<folly::SharedMutex> lock(mutex_);
std::unique_lock<better::shared_mutex> lock(mutex_);

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

auto iterator = registry_.find(surfaceId);

Expand Down
5 changes: 2 additions & 3 deletions ReactCommon/fabric/uimanager/ShadowTreeRegistry.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@

#pragma once

#include <folly/SharedMutex.h>
#include <shared_mutex>
#include <better/mutex.h>

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

private:
mutable folly::SharedMutex mutex_;
mutable better::shared_mutex mutex_;
mutable std::unordered_map<SurfaceId, std::unique_ptr<ShadowTree>>
registry_; // Protected by `mutex_`.
};
Expand Down

0 comments on commit 25a58d7

Please sign in to comment.