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: https://github.com/facebook/react-native/pull/24075

Differential Revision: D14559213

Pulled By: shergin

fbshipit-source-id: 934c7cd7db9ce60031d6b007faeebb353860268f
This commit is contained in:
zhongwuzw
2019-03-21 00:13:39 -07:00
committed by Facebook Github Bot
parent b7c2c82c89
commit 25a58d7bbb
6 changed files with 21 additions and 24 deletions

View File

@@ -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);
@@ -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()) {
@@ -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_;
}();
@@ -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);
@@ -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_;
}();

View File

@@ -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 {
@@ -84,7 +83,7 @@ class ImageResponseObserverCoordinator {
/*
* Observer and data mutex.
*/
mutable folly::SharedMutex mutex_;
mutable better::shared_mutex mutex_;
};
} // namespace react

View File

@@ -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_;
}
@@ -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;

View File

@@ -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>
@@ -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_;

View File

@@ -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());
@@ -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);

View File

@@ -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>
@@ -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_`.
};