From e665a0f9952a7852c5506bf334452f8156991ad2 Mon Sep 17 00:00:00 2001 From: Samuel Susla Date: Thu, 16 Feb 2023 06:25:26 -0800 Subject: [PATCH] Use std::shared_mutex instead of folly::shared_mutex Summary: changelog: [internal] C++17 has implementation of shared_mutex in standard library. Let's use it instead of folly. Reviewed By: cipolleschi Differential Revision: D43275493 fbshipit-source-id: d766251226aa230110011aca94b4e697fe0d31a1 --- .../Mounting/RCTComponentViewFactory.mm | 10 ++-- React/Fabric/RCTSurfacePresenter.mm | 11 +++-- React/Fabric/RCTSurfaceRegistry.mm | 11 ++--- .../src/main/jni/react/fabric/Binding.cpp | 16 +++--- .../src/main/jni/react/fabric/Binding.h | 6 +-- .../react/fabric/SurfaceHandlerBinding.cpp | 4 +- .../jni/react/fabric/SurfaceHandlerBinding.h | 4 +- ReactCommon/butter/mutex.h | 20 -------- .../ComponentDescriptorProviderRegistry.cpp | 8 +-- .../ComponentDescriptorProviderRegistry.h | 4 +- .../ComponentDescriptorRegistry.cpp | 10 ++-- .../ComponentDescriptorRegistry.h | 4 +- .../react/renderer/core/EventListener.cpp | 6 +-- .../react/renderer/core/EventListener.h | 5 +- .../react/renderer/core/ShadowNodeFamily.cpp | 6 +-- .../react/renderer/core/ShadowNodeFamily.h | 4 +- .../react/renderer/mounting/ShadowTree.cpp | 10 ++-- .../react/renderer/mounting/ShadowTree.h | 3 +- .../renderer/mounting/ShadowTreeRegistry.cpp | 8 +-- .../renderer/mounting/ShadowTreeRegistry.h | 5 +- .../renderer/scheduler/SurfaceHandler.cpp | 49 +++++++++---------- .../react/renderer/scheduler/SurfaceHandler.h | 5 +- .../renderer/scheduler/SurfaceManager.cpp | 6 +-- .../react/renderer/scheduler/SurfaceManager.h | 3 +- .../react/renderer/uimanager/UIManager.cpp | 6 +-- .../react/renderer/uimanager/UIManager.h | 3 +- ReactCommon/react/utils/ContextContainer.h | 14 +++--- ReactCommon/react/utils/SharedFunction.h | 10 ++-- 28 files changed, 115 insertions(+), 136 deletions(-) delete mode 100644 ReactCommon/butter/mutex.h diff --git a/React/Fabric/Mounting/RCTComponentViewFactory.mm b/React/Fabric/Mounting/RCTComponentViewFactory.mm index 0d8e15cebc2a7b..f174ba1dfe3a90 100644 --- a/React/Fabric/Mounting/RCTComponentViewFactory.mm +++ b/React/Fabric/Mounting/RCTComponentViewFactory.mm @@ -11,8 +11,8 @@ #import #import -#import #import +#import #import #import @@ -61,7 +61,7 @@ @implementation RCTComponentViewFactory { butter::map _componentViewClasses; butter::set _registeredComponentsNames; ComponentDescriptorProviderRegistry _providerRegistry; - butter::shared_mutex _mutex; + std::shared_mutex _mutex; } + (RCTComponentViewFactory *)currentComponentViewFactory @@ -155,7 +155,7 @@ - (BOOL)registerComponentIfPossible:(std::string const &)name - (void)registerComponentViewClass:(Class)componentViewClass { RCTAssert(componentViewClass, @"RCTComponentViewFactory: Provided `componentViewClass` is `nil`."); - std::unique_lock lock(_mutex); + std::unique_lock lock(_mutex); auto componentDescriptorProvider = [componentViewClass componentDescriptorProvider]; _componentViewClasses[componentDescriptorProvider.handle] = @@ -177,7 +177,7 @@ - (void)_addDescriptorToProviderRegistry:(ComponentDescriptorProvider const &)pr - (RCTComponentViewDescriptor)createComponentViewWithComponentHandle:(facebook::react::ComponentHandle)componentHandle { RCTAssertMainQueue(); - std::shared_lock lock(_mutex); + std::shared_lock lock(_mutex); auto iterator = _componentViewClasses.find(componentHandle); RCTAssert( @@ -198,7 +198,7 @@ - (RCTComponentViewDescriptor)createComponentViewWithComponentHandle:(facebook:: - (facebook::react::ComponentDescriptorRegistry::Shared)createComponentDescriptorRegistryWithParameters: (facebook::react::ComponentDescriptorParameters)parameters { - std::shared_lock lock(_mutex); + std::shared_lock lock(_mutex); return _providerRegistry.createComponentDescriptorRegistry(parameters); } diff --git a/React/Fabric/RCTSurfacePresenter.mm b/React/Fabric/RCTSurfacePresenter.mm index 61bec9c7148da2..eadaf626d53022 100644 --- a/React/Fabric/RCTSurfacePresenter.mm +++ b/React/Fabric/RCTSurfacePresenter.mm @@ -8,6 +8,7 @@ #import "RCTSurfacePresenter.h" #import +#import #import #import @@ -83,7 +84,7 @@ @implementation RCTSurfacePresenter { RuntimeExecutor _runtimeExecutor; // Protected by `_schedulerLifeCycleMutex`. std::optional _bridgelessBindingsExecutor; // Only used for installing bindings. - butter::shared_mutex _observerListMutex; + std::shared_mutex _observerListMutex; std::vector<__weak id> _observers; // Protected by `_observerListMutex`. } @@ -390,13 +391,13 @@ - (void)schedulerDidSetIsJSResponder:(BOOL)isJSResponder - (void)addObserver:(id)observer { - std::unique_lock lock(_observerListMutex); + std::unique_lock lock(_observerListMutex); _observers.push_back(observer); } - (void)removeObserver:(id)observer { - std::unique_lock lock(_observerListMutex); + std::unique_lock lock(_observerListMutex); std::vector<__weak id>::const_iterator it = std::find(_observers.begin(), _observers.end(), observer); if (it != _observers.end()) { @@ -412,7 +413,7 @@ - (void)mountingManager:(RCTMountingManager *)mountingManager willMountComponent NSArray> *observersCopy; { - std::shared_lock lock(_observerListMutex); + std::shared_lock lock(_observerListMutex); observersCopy = [self _getObservers]; } @@ -429,7 +430,7 @@ - (void)mountingManager:(RCTMountingManager *)mountingManager didMountComponents NSArray> *observersCopy; { - std::shared_lock lock(_observerListMutex); + std::shared_lock lock(_observerListMutex); observersCopy = [self _getObservers]; } diff --git a/React/Fabric/RCTSurfaceRegistry.mm b/React/Fabric/RCTSurfaceRegistry.mm index 50c08f49752eac..5d0e4130fe35a2 100644 --- a/React/Fabric/RCTSurfaceRegistry.mm +++ b/React/Fabric/RCTSurfaceRegistry.mm @@ -7,7 +7,6 @@ #import "RCTSurfaceRegistry.h" -#import #import #import @@ -16,7 +15,7 @@ using namespace facebook; @implementation RCTSurfaceRegistry { - butter::shared_mutex _mutex; + std::shared_mutex _mutex; NSMapTable *_registry; } @@ -32,13 +31,13 @@ - (instancetype)init - (void)enumerateWithBlock:(RCTSurfaceEnumeratorBlock)block { - std::shared_lock lock(_mutex); + std::shared_lock lock(_mutex); block([_registry objectEnumerator]); } - (void)registerSurface:(RCTFabricSurface *)surface { - std::unique_lock lock(_mutex); + std::unique_lock lock(_mutex); ReactTag rootTag = surface.rootViewTag.integerValue; [_registry setObject:surface forKey:(__bridge id)(void *)rootTag]; @@ -46,7 +45,7 @@ - (void)registerSurface:(RCTFabricSurface *)surface - (void)unregisterSurface:(RCTFabricSurface *)surface { - std::unique_lock lock(_mutex); + std::unique_lock lock(_mutex); ReactTag rootTag = surface.rootViewTag.integerValue; [_registry removeObjectForKey:(__bridge id)(void *)rootTag]; @@ -54,7 +53,7 @@ - (void)unregisterSurface:(RCTFabricSurface *)surface - (RCTFabricSurface *)surfaceForRootTag:(ReactTag)rootTag { - std::shared_lock lock(_mutex); + std::shared_lock lock(_mutex); return [_registry objectForKey:(__bridge id)(void *)rootTag]; } diff --git a/ReactAndroid/src/main/jni/react/fabric/Binding.cpp b/ReactAndroid/src/main/jni/react/fabric/Binding.cpp index f5a0aa68229ced..a9b6dc7c3c223a 100644 --- a/ReactAndroid/src/main/jni/react/fabric/Binding.cpp +++ b/ReactAndroid/src/main/jni/react/fabric/Binding.cpp @@ -51,7 +51,7 @@ jni::local_ref Binding::initHybrid( // Thread-safe getter std::shared_ptr Binding::getScheduler() { - std::shared_lock lock(installMutex_); + std::shared_lock lock(installMutex_); return scheduler_; } @@ -132,7 +132,7 @@ void Binding::startSurface( { SystraceSection s2("FabricUIManagerBinding::startSurface::surfaceId::lock"); - std::unique_lock lock(surfaceHandlerRegistryMutex_); + std::unique_lock lock(surfaceHandlerRegistryMutex_); SystraceSection s3("FabricUIManagerBinding::startSurface::surfaceId"); surfaceHandlerRegistry_.emplace(surfaceId, std::move(surfaceHandler)); } @@ -202,7 +202,7 @@ void Binding::startSurfaceWithConstraints( { SystraceSection s2( "FabricUIManagerBinding::startSurfaceWithConstraints::surfaceId::lock"); - std::unique_lock lock(surfaceHandlerRegistryMutex_); + std::unique_lock lock(surfaceHandlerRegistryMutex_); SystraceSection s3( "FabricUIManagerBinding::startSurfaceWithConstraints::surfaceId"); surfaceHandlerRegistry_.emplace(surfaceId, std::move(surfaceHandler)); @@ -246,7 +246,7 @@ void Binding::stopSurface(jint surfaceId) { } { - std::unique_lock lock(surfaceHandlerRegistryMutex_); + std::unique_lock lock(surfaceHandlerRegistryMutex_); auto iterator = surfaceHandlerRegistry_.find(surfaceId); @@ -338,7 +338,7 @@ void Binding::setConstraints( isRTL ? LayoutDirection::RightToLeft : LayoutDirection::LeftToRight; { - std::shared_lock lock(surfaceHandlerRegistryMutex_); + std::shared_lock lock(surfaceHandlerRegistryMutex_); auto iterator = surfaceHandlerRegistry_.find(surfaceId); @@ -377,7 +377,7 @@ void Binding::installFabricUIManager( // Use std::lock and std::adopt_lock to prevent deadlocks by locking mutexes // at the same time - std::unique_lock lock(installMutex_); + std::unique_lock lock(installMutex_); auto globalJavaUiManager = make_global(javaUIManager); mountingManager_ = @@ -467,7 +467,7 @@ void Binding::uninstallFabricUIManager() { << this << ")."; } - std::unique_lock lock(installMutex_); + std::unique_lock lock(installMutex_); animationDriver_ = nullptr; scheduler_ = nullptr; mountingManager_ = nullptr; @@ -476,7 +476,7 @@ void Binding::uninstallFabricUIManager() { std::shared_ptr Binding::verifyMountingManager( std::string const &hint) { - std::shared_lock lock(installMutex_); + std::shared_lock lock(installMutex_); if (!mountingManager_) { LOG(ERROR) << hint << " mounting manager disappeared."; } diff --git a/ReactAndroid/src/main/jni/react/fabric/Binding.h b/ReactAndroid/src/main/jni/react/fabric/Binding.h index 355e616c20fe8c..318fa1e27c5a08 100644 --- a/ReactAndroid/src/main/jni/react/fabric/Binding.h +++ b/ReactAndroid/src/main/jni/react/fabric/Binding.h @@ -10,7 +10,7 @@ #include "FabricMountingManager.h" #include -#include +#include #include #include @@ -123,7 +123,7 @@ class Binding : public jni::HybridClass, void uninstallFabricUIManager(); // Private member variables - butter::shared_mutex installMutex_; + std::shared_mutex installMutex_; std::shared_ptr mountingManager_; std::shared_ptr scheduler_; @@ -139,7 +139,7 @@ class Binding : public jni::HybridClass, BackgroundExecutor backgroundExecutor_; butter::map surfaceHandlerRegistry_{}; - butter::shared_mutex + std::shared_mutex surfaceHandlerRegistryMutex_; // Protects `surfaceHandlerRegistry_`. float pointScaleFactor_ = 1; diff --git a/ReactAndroid/src/main/jni/react/fabric/SurfaceHandlerBinding.cpp b/ReactAndroid/src/main/jni/react/fabric/SurfaceHandlerBinding.cpp index 5eaf0a3634848d..63559fe3bb929a 100644 --- a/ReactAndroid/src/main/jni/react/fabric/SurfaceHandlerBinding.cpp +++ b/ReactAndroid/src/main/jni/react/fabric/SurfaceHandlerBinding.cpp @@ -21,7 +21,7 @@ void SurfaceHandlerBinding::setDisplayMode(jint mode) { } void SurfaceHandlerBinding::start() { - std::unique_lock lock(lifecycleMutex_); + std::unique_lock lock(lifecycleMutex_); if (surfaceHandler_.getStatus() != SurfaceHandler::Status::Running) { surfaceHandler_.start(); @@ -29,7 +29,7 @@ void SurfaceHandlerBinding::start() { } void SurfaceHandlerBinding::stop() { - std::unique_lock lock(lifecycleMutex_); + std::unique_lock lock(lifecycleMutex_); if (surfaceHandler_.getStatus() == SurfaceHandler::Status::Running) { surfaceHandler_.stop(); diff --git a/ReactAndroid/src/main/jni/react/fabric/SurfaceHandlerBinding.h b/ReactAndroid/src/main/jni/react/fabric/SurfaceHandlerBinding.h index 7fdc4919d380e6..e067810a31a4c6 100644 --- a/ReactAndroid/src/main/jni/react/fabric/SurfaceHandlerBinding.h +++ b/ReactAndroid/src/main/jni/react/fabric/SurfaceHandlerBinding.h @@ -7,6 +7,8 @@ #pragma once +#include + #include #include #include @@ -50,7 +52,7 @@ class SurfaceHandlerBinding : public jni::HybridClass { SurfaceHandler const &getSurfaceHandler(); private: - mutable butter::shared_mutex lifecycleMutex_; + mutable std::shared_mutex lifecycleMutex_; const SurfaceHandler surfaceHandler_; jni::alias_ref jhybridobject_; diff --git a/ReactCommon/butter/mutex.h b/ReactCommon/butter/mutex.h deleted file mode 100644 index 9071356eba1699..00000000000000 --- a/ReactCommon/butter/mutex.h +++ /dev/null @@ -1,20 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -#pragma once - -#include -#include -#include - -namespace facebook { -namespace butter { - -using shared_mutex = folly::SharedMutex; - -} // namespace butter -} // namespace facebook diff --git a/ReactCommon/react/renderer/componentregistry/ComponentDescriptorProviderRegistry.cpp b/ReactCommon/react/renderer/componentregistry/ComponentDescriptorProviderRegistry.cpp index 1869439cbc77a4..627b19e06e22ae 100644 --- a/ReactCommon/react/renderer/componentregistry/ComponentDescriptorProviderRegistry.cpp +++ b/ReactCommon/react/renderer/componentregistry/ComponentDescriptorProviderRegistry.cpp @@ -11,7 +11,7 @@ namespace facebook::react { void ComponentDescriptorProviderRegistry::add( const ComponentDescriptorProvider &provider) const { - std::unique_lock lock(mutex_); + std::unique_lock lock(mutex_); /* // TODO: T57583139 @@ -44,7 +44,7 @@ void ComponentDescriptorProviderRegistry::add( void ComponentDescriptorProviderRegistry::setComponentDescriptorProviderRequest( ComponentDescriptorProviderRequest componentDescriptorProviderRequest) const { - std::shared_lock lock(mutex_); + std::shared_lock lock(mutex_); componentDescriptorProviderRequest_ = std::move(componentDescriptorProviderRequest); } @@ -54,7 +54,7 @@ void ComponentDescriptorProviderRegistry::request( ComponentDescriptorProviderRequest componentDescriptorProviderRequest; { - std::shared_lock lock(mutex_); + std::shared_lock lock(mutex_); componentDescriptorProviderRequest = componentDescriptorProviderRequest_; } @@ -66,7 +66,7 @@ void ComponentDescriptorProviderRegistry::request( ComponentDescriptorRegistry::Shared ComponentDescriptorProviderRegistry::createComponentDescriptorRegistry( ComponentDescriptorParameters const ¶meters) const { - std::shared_lock lock(mutex_); + std::shared_lock lock(mutex_); auto registry = std::make_shared( parameters, *this, parameters.contextContainer); diff --git a/ReactCommon/react/renderer/componentregistry/ComponentDescriptorProviderRegistry.h b/ReactCommon/react/renderer/componentregistry/ComponentDescriptorProviderRegistry.h index 049cdd4212c43f..9ef8068f2698ac 100644 --- a/ReactCommon/react/renderer/componentregistry/ComponentDescriptorProviderRegistry.h +++ b/ReactCommon/react/renderer/componentregistry/ComponentDescriptorProviderRegistry.h @@ -7,7 +7,7 @@ #pragma once -#include +#include #include #include @@ -58,7 +58,7 @@ class ComponentDescriptorProviderRegistry final { void request(ComponentName componentName) const; - mutable butter::shared_mutex mutex_; + mutable std::shared_mutex mutex_; mutable std::vector> componentDescriptorRegistries_; mutable butter::map diff --git a/ReactCommon/react/renderer/componentregistry/ComponentDescriptorRegistry.cpp b/ReactCommon/react/renderer/componentregistry/ComponentDescriptorRegistry.cpp index 23e0330a86016b..2d85ec352734ba 100644 --- a/ReactCommon/react/renderer/componentregistry/ComponentDescriptorRegistry.cpp +++ b/ReactCommon/react/renderer/componentregistry/ComponentDescriptorRegistry.cpp @@ -28,7 +28,7 @@ ComponentDescriptorRegistry::ComponentDescriptorRegistry( void ComponentDescriptorRegistry::add( ComponentDescriptorProvider componentDescriptorProvider) const { - std::unique_lock lock(mutex_); + std::unique_lock lock(mutex_); auto componentDescriptor = componentDescriptorProvider.constructor( {parameters_.eventDispatcher, @@ -59,7 +59,7 @@ void ComponentDescriptorRegistry::registerComponentDescriptor( ComponentDescriptor const &ComponentDescriptorRegistry::at( std::string const &componentName) const { - std::shared_lock lock(mutex_); + std::shared_lock lock(mutex_); auto unifiedComponentName = componentNameByReactViewName(componentName); @@ -96,7 +96,7 @@ ComponentDescriptor const &ComponentDescriptorRegistry::at( ComponentDescriptor const *ComponentDescriptorRegistry:: findComponentDescriptorByHandle_DO_NOT_USE_THIS_IS_BROKEN( ComponentHandle componentHandle) const { - std::shared_lock lock(mutex_); + std::shared_lock lock(mutex_); auto iterator = _registryByHandle.find(componentHandle); if (iterator == _registryByHandle.end()) { @@ -108,14 +108,14 @@ ComponentDescriptor const *ComponentDescriptorRegistry:: ComponentDescriptor const &ComponentDescriptorRegistry::at( ComponentHandle componentHandle) const { - std::shared_lock lock(mutex_); + std::shared_lock lock(mutex_); return *_registryByHandle.at(componentHandle); } bool ComponentDescriptorRegistry::hasComponentDescriptorAt( ComponentHandle componentHandle) const { - std::shared_lock lock(mutex_); + std::shared_lock lock(mutex_); auto iterator = _registryByHandle.find(componentHandle); return iterator != _registryByHandle.end(); diff --git a/ReactCommon/react/renderer/componentregistry/ComponentDescriptorRegistry.h b/ReactCommon/react/renderer/componentregistry/ComponentDescriptorRegistry.h index d2eeceaf60f996..4a83a4efb89475 100644 --- a/ReactCommon/react/renderer/componentregistry/ComponentDescriptorRegistry.h +++ b/ReactCommon/react/renderer/componentregistry/ComponentDescriptorRegistry.h @@ -8,9 +8,9 @@ #pragma once #include +#include #include -#include #include #include @@ -81,7 +81,7 @@ class ComponentDescriptorRegistry { */ void add(ComponentDescriptorProvider componentDescriptorProvider) const; - mutable butter::shared_mutex mutex_; + mutable std::shared_mutex mutex_; mutable butter::map _registryByHandle; mutable butter::map _registryByName; diff --git a/ReactCommon/react/renderer/core/EventListener.cpp b/ReactCommon/react/renderer/core/EventListener.cpp index 48f5793292dc7b..caef820b11b833 100644 --- a/ReactCommon/react/renderer/core/EventListener.cpp +++ b/ReactCommon/react/renderer/core/EventListener.cpp @@ -10,7 +10,7 @@ namespace facebook::react { bool EventListenerContainer::willDispatchEvent(const RawEvent &event) { - std::shared_lock lock(mutex_); + std::shared_lock lock(mutex_); bool handled = false; for (auto const &listener : eventListeners_) { @@ -21,14 +21,14 @@ bool EventListenerContainer::willDispatchEvent(const RawEvent &event) { void EventListenerContainer::addListener( const std::shared_ptr &listener) { - std::unique_lock lock(mutex_); + std::unique_lock lock(mutex_); eventListeners_.push_back(listener); } void EventListenerContainer::removeListener( const std::shared_ptr &listener) { - std::unique_lock lock(mutex_); + std::unique_lock lock(mutex_); auto it = std::find(eventListeners_.begin(), eventListeners_.end(), listener); if (it != eventListeners_.end()) { diff --git a/ReactCommon/react/renderer/core/EventListener.h b/ReactCommon/react/renderer/core/EventListener.h index f9176b514cbfa0..f72a41614c80d1 100644 --- a/ReactCommon/react/renderer/core/EventListener.h +++ b/ReactCommon/react/renderer/core/EventListener.h @@ -7,12 +7,11 @@ #pragma once +#include #include #include -#include - namespace facebook { namespace react { @@ -36,7 +35,7 @@ class EventListenerContainer { void removeListener(const std::shared_ptr &listener); private: - butter::shared_mutex mutex_; + std::shared_mutex mutex_; std::vector> eventListeners_; }; diff --git a/ReactCommon/react/renderer/core/ShadowNodeFamily.cpp b/ReactCommon/react/renderer/core/ShadowNodeFamily.cpp index 361a51b75ed850..2c86a2555e6c50 100644 --- a/ReactCommon/react/renderer/core/ShadowNodeFamily.cpp +++ b/ReactCommon/react/renderer/core/ShadowNodeFamily.cpp @@ -97,12 +97,12 @@ AncestorList ShadowNodeFamily::getAncestors( } State::Shared ShadowNodeFamily::getMostRecentState() const { - std::unique_lock lock(mutex_); + std::unique_lock lock(mutex_); return mostRecentState_; } void ShadowNodeFamily::setMostRecentState(State::Shared const &state) const { - std::unique_lock lock(mutex_); + std::unique_lock lock(mutex_); /* * Checking and setting `isObsolete_` prevents old states to be recommitted @@ -123,7 +123,7 @@ void ShadowNodeFamily::setMostRecentState(State::Shared const &state) const { std::shared_ptr ShadowNodeFamily::getMostRecentStateIfObsolete( State const &state) const { - std::unique_lock lock(mutex_); + std::unique_lock lock(mutex_); if (!state.isObsolete_) { return {}; } diff --git a/ReactCommon/react/renderer/core/ShadowNodeFamily.h b/ReactCommon/react/renderer/core/ShadowNodeFamily.h index f907e1913b3dd0..54016e46f4d3ad 100644 --- a/ReactCommon/react/renderer/core/ShadowNodeFamily.h +++ b/ReactCommon/react/renderer/core/ShadowNodeFamily.h @@ -8,8 +8,8 @@ #pragma once #include +#include -#include #include #include @@ -108,7 +108,7 @@ class ShadowNodeFamily final { EventDispatcher::Weak eventDispatcher_; mutable std::shared_ptr mostRecentState_; - mutable butter::shared_mutex mutex_; + mutable std::shared_mutex mutex_; /* * Deprecated. diff --git a/ReactCommon/react/renderer/mounting/ShadowTree.cpp b/ReactCommon/react/renderer/mounting/ShadowTree.cpp index 318834d2494a5a..c1b66df770eed6 100644 --- a/ReactCommon/react/renderer/mounting/ShadowTree.cpp +++ b/ReactCommon/react/renderer/mounting/ShadowTree.cpp @@ -269,7 +269,7 @@ void ShadowTree::setCommitMode(CommitMode commitMode) const { auto revision = ShadowTreeRevision{}; { - std::unique_lock lock(commitMutex_); + std::unique_lock lock(commitMutex_); if (commitMode_ == commitMode) { return; } @@ -286,7 +286,7 @@ void ShadowTree::setCommitMode(CommitMode commitMode) const { } CommitMode ShadowTree::getCommitMode() const { - std::shared_lock lock(commitMutex_); + std::shared_lock lock(commitMutex_); return commitMode_; } @@ -329,7 +329,7 @@ CommitStatus ShadowTree::tryCommit( { // Reading `currentRevision_` in shared manner. - std::shared_lock lock(commitMutex_); + std::shared_lock lock(commitMutex_); commitMode = commitMode_; oldRevision = currentRevision_; } @@ -366,7 +366,7 @@ CommitStatus ShadowTree::tryCommit( { // Updating `currentRevision_` in unique manner if it hasn't changed. - std::unique_lock lock(commitMutex_); + std::unique_lock lock(commitMutex_); if (currentRevision_.number != oldRevision.number) { return CommitStatus::Failed; @@ -409,7 +409,7 @@ CommitStatus ShadowTree::tryCommit( } ShadowTreeRevision ShadowTree::getCurrentRevision() const { - std::shared_lock lock(commitMutex_); + std::shared_lock lock(commitMutex_); return currentRevision_; } diff --git a/ReactCommon/react/renderer/mounting/ShadowTree.h b/ReactCommon/react/renderer/mounting/ShadowTree.h index 7235fa8c61cab9..b750a1a2fcf0d1 100644 --- a/ReactCommon/react/renderer/mounting/ShadowTree.h +++ b/ReactCommon/react/renderer/mounting/ShadowTree.h @@ -7,7 +7,6 @@ #pragma once -#include #include #include @@ -142,7 +141,7 @@ class ShadowTree final { SurfaceId const surfaceId_; ShadowTreeDelegate const &delegate_; - mutable butter::shared_mutex commitMutex_; + mutable std::shared_mutex commitMutex_; mutable CommitMode commitMode_{ CommitMode::Normal}; // Protected by `commitMutex_`. mutable ShadowTreeRevision currentRevision_; // Protected by `commitMutex_`. diff --git a/ReactCommon/react/renderer/mounting/ShadowTreeRegistry.cpp b/ReactCommon/react/renderer/mounting/ShadowTreeRegistry.cpp index 082f7ef58d2e56..365f0fb94bd879 100644 --- a/ReactCommon/react/renderer/mounting/ShadowTreeRegistry.cpp +++ b/ReactCommon/react/renderer/mounting/ShadowTreeRegistry.cpp @@ -17,14 +17,14 @@ ShadowTreeRegistry::~ShadowTreeRegistry() { } void ShadowTreeRegistry::add(std::unique_ptr &&shadowTree) const { - std::unique_lock lock(mutex_); + std::unique_lock lock(mutex_); registry_.emplace(shadowTree->getSurfaceId(), std::move(shadowTree)); } std::unique_ptr ShadowTreeRegistry::remove( SurfaceId surfaceId) const { - std::unique_lock lock(mutex_); + std::unique_lock lock(mutex_); auto iterator = registry_.find(surfaceId); if (iterator == registry_.end()) { @@ -39,7 +39,7 @@ std::unique_ptr ShadowTreeRegistry::remove( bool ShadowTreeRegistry::visit( SurfaceId surfaceId, std::function const &callback) const { - std::shared_lock lock(mutex_); + std::shared_lock lock(mutex_); auto iterator = registry_.find(surfaceId); @@ -54,7 +54,7 @@ bool ShadowTreeRegistry::visit( void ShadowTreeRegistry::enumerate( std::function const &callback) const { - std::shared_lock lock(mutex_); + std::shared_lock lock(mutex_); auto stop = false; for (auto const &pair : registry_) { callback(*pair.second, stop); diff --git a/ReactCommon/react/renderer/mounting/ShadowTreeRegistry.h b/ReactCommon/react/renderer/mounting/ShadowTreeRegistry.h index 5eab8e760afbd2..60e4295568fd4b 100644 --- a/ReactCommon/react/renderer/mounting/ShadowTreeRegistry.h +++ b/ReactCommon/react/renderer/mounting/ShadowTreeRegistry.h @@ -7,8 +7,9 @@ #pragma once +#include + #include -#include #include #include @@ -62,7 +63,7 @@ class ShadowTreeRegistry final { &callback) const; private: - mutable butter::shared_mutex mutex_; + mutable std::shared_mutex mutex_; mutable butter::map> registry_; // Protected by `mutex_`. }; diff --git a/ReactCommon/react/renderer/scheduler/SurfaceHandler.cpp b/ReactCommon/react/renderer/scheduler/SurfaceHandler.cpp index ae795699423027..b1c4426a59b1bb 100644 --- a/ReactCommon/react/renderer/scheduler/SurfaceHandler.cpp +++ b/ReactCommon/react/renderer/scheduler/SurfaceHandler.cpp @@ -27,13 +27,10 @@ SurfaceHandler::SurfaceHandler(SurfaceHandler &&other) noexcept { } SurfaceHandler &SurfaceHandler::operator=(SurfaceHandler &&other) noexcept { - std::unique_lock lock1(linkMutex_, std::defer_lock); - std::unique_lock lock2( - parametersMutex_, std::defer_lock); - std::unique_lock lock3( - other.linkMutex_, std::defer_lock); - std::unique_lock lock4( - other.parametersMutex_, std::defer_lock); + std::unique_lock lock1(linkMutex_, std::defer_lock); + std::unique_lock lock2(parametersMutex_, std::defer_lock); + std::unique_lock lock3(other.linkMutex_, std::defer_lock); + std::unique_lock lock4(other.parametersMutex_, std::defer_lock); std::lock(lock1, lock2, lock3, lock4); link_ = other.link_; @@ -53,13 +50,13 @@ void SurfaceHandler::setContextContainer( } Status SurfaceHandler::getStatus() const noexcept { - std::shared_lock lock(linkMutex_); + std::shared_lock lock(linkMutex_); return link_.status; } void SurfaceHandler::start() const noexcept { SystraceSection s("SurfaceHandler::start"); - std::unique_lock lock(linkMutex_); + std::unique_lock lock(linkMutex_); react_native_assert( link_.status == Status::Registered && "Surface must be registered."); react_native_assert( @@ -71,7 +68,7 @@ void SurfaceHandler::start() const noexcept { auto parameters = Parameters{}; { SystraceSection s2("SurfaceHandler::start::paramsLock"); - std::shared_lock parametersLock(parametersMutex_); + std::shared_lock parametersLock(parametersMutex_); parameters = parameters_; } @@ -98,7 +95,7 @@ void SurfaceHandler::start() const noexcept { void SurfaceHandler::stop() const noexcept { auto shadowTree = ShadowTree::Unique{}; { - std::unique_lock lock(linkMutex_); + std::unique_lock lock(linkMutex_); react_native_assert( link_.status == Status::Running && "Surface must be running."); @@ -116,7 +113,7 @@ void SurfaceHandler::stop() const noexcept { void SurfaceHandler::setDisplayMode(DisplayMode displayMode) const noexcept { { - std::unique_lock lock(parametersMutex_); + std::unique_lock lock(parametersMutex_); if (parameters_.displayMode == displayMode) { return; } @@ -125,7 +122,7 @@ void SurfaceHandler::setDisplayMode(DisplayMode displayMode) const noexcept { } { - std::shared_lock lock(linkMutex_); + std::shared_lock lock(linkMutex_); if (link_.status != Status::Running) { return; @@ -142,41 +139,41 @@ void SurfaceHandler::setDisplayMode(DisplayMode displayMode) const noexcept { } DisplayMode SurfaceHandler::getDisplayMode() const noexcept { - std::shared_lock lock(parametersMutex_); + std::shared_lock lock(parametersMutex_); return parameters_.displayMode; } #pragma mark - Accessors SurfaceId SurfaceHandler::getSurfaceId() const noexcept { - std::shared_lock lock(parametersMutex_); + std::shared_lock lock(parametersMutex_); return parameters_.surfaceId; } void SurfaceHandler::setSurfaceId(SurfaceId surfaceId) const noexcept { - std::unique_lock lock(parametersMutex_); + std::unique_lock lock(parametersMutex_); parameters_.surfaceId = surfaceId; } std::string SurfaceHandler::getModuleName() const noexcept { - std::shared_lock lock(parametersMutex_); + std::shared_lock lock(parametersMutex_); return parameters_.moduleName; } void SurfaceHandler::setProps(folly::dynamic const &props) const noexcept { SystraceSection s("SurfaceHandler::setProps"); - std::unique_lock lock(parametersMutex_); + std::unique_lock lock(parametersMutex_); parameters_.props = props; } folly::dynamic SurfaceHandler::getProps() const noexcept { - std::shared_lock lock(parametersMutex_); + std::shared_lock lock(parametersMutex_); return parameters_.props; } std::shared_ptr SurfaceHandler::getMountingCoordinator() const noexcept { - std::shared_lock lock(linkMutex_); + std::shared_lock lock(linkMutex_); react_native_assert( link_.status != Status::Unregistered && "Surface must be registered."); react_native_assert( @@ -189,7 +186,7 @@ SurfaceHandler::getMountingCoordinator() const noexcept { Size SurfaceHandler::measure( LayoutConstraints const &layoutConstraints, LayoutContext const &layoutContext) const noexcept { - std::shared_lock lock(linkMutex_); + std::shared_lock lock(linkMutex_); if (link_.status != Status::Running) { return layoutConstraints.clamp({0, 0}); @@ -215,7 +212,7 @@ void SurfaceHandler::constraintLayout( LayoutContext const &layoutContext) const noexcept { SystraceSection s("SurfaceHandler::constraintLayout"); { - std::unique_lock lock(parametersMutex_); + std::unique_lock lock(parametersMutex_); if (parameters_.layoutConstraints == layoutConstraints && parameters_.layoutContext == layoutContext) { @@ -227,7 +224,7 @@ void SurfaceHandler::constraintLayout( } { - std::shared_lock lock(linkMutex_); + std::shared_lock lock(linkMutex_); if (link_.status != Status::Running) { return; @@ -248,12 +245,12 @@ void SurfaceHandler::constraintLayout( } LayoutConstraints SurfaceHandler::getLayoutConstraints() const noexcept { - std::shared_lock lock(parametersMutex_); + std::shared_lock lock(parametersMutex_); return parameters_.layoutConstraints; } LayoutContext SurfaceHandler::getLayoutContext() const noexcept { - std::shared_lock lock(parametersMutex_); + std::shared_lock lock(parametersMutex_); return parameters_.layoutContext; } @@ -294,7 +291,7 @@ void SurfaceHandler::applyDisplayMode(DisplayMode displayMode) const noexcept { } void SurfaceHandler::setUIManager(UIManager const *uiManager) const noexcept { - std::unique_lock lock(linkMutex_); + std::unique_lock lock(linkMutex_); react_native_assert( link_.status != Status::Running && "Surface must not be running."); diff --git a/ReactCommon/react/renderer/scheduler/SurfaceHandler.h b/ReactCommon/react/renderer/scheduler/SurfaceHandler.h index f550e709f59f08..2736b4effba05e 100644 --- a/ReactCommon/react/renderer/scheduler/SurfaceHandler.h +++ b/ReactCommon/react/renderer/scheduler/SurfaceHandler.h @@ -8,6 +8,7 @@ #pragma once #include +#include #include #include @@ -195,13 +196,13 @@ class SurfaceHandler { /* * `link_` and `linkMutex_` pair. */ - mutable butter::shared_mutex linkMutex_; + mutable std::shared_mutex linkMutex_; mutable Link link_; /* * `parameters_` and `parametersMutex_` pair. */ - mutable butter::shared_mutex parametersMutex_; + mutable std::shared_mutex parametersMutex_; mutable Parameters parameters_; }; diff --git a/ReactCommon/react/renderer/scheduler/SurfaceManager.cpp b/ReactCommon/react/renderer/scheduler/SurfaceManager.cpp index e663b68f4c1ea1..15512adca83f79 100644 --- a/ReactCommon/react/renderer/scheduler/SurfaceManager.cpp +++ b/ReactCommon/react/renderer/scheduler/SurfaceManager.cpp @@ -21,7 +21,7 @@ void SurfaceManager::startSurface( LayoutConstraints const &layoutConstraints, LayoutContext const &layoutContext) const noexcept { { - std::unique_lock lock(mutex_); + std::unique_lock lock(mutex_); auto surfaceHandler = SurfaceHandler{moduleName, surfaceId}; surfaceHandler.setContextContainer(scheduler_.getContextContainer()); registry_.emplace(surfaceId, std::move(surfaceHandler)); @@ -44,7 +44,7 @@ void SurfaceManager::stopSurface(SurfaceId surfaceId) const noexcept { }); { - std::unique_lock lock(mutex_); + std::unique_lock lock(mutex_); auto iterator = registry_.find(surfaceId); registry_.erase(iterator); @@ -88,7 +88,7 @@ void SurfaceManager::visit( SurfaceId surfaceId, std::function const &callback) const noexcept { - std::shared_lock lock(mutex_); + std::shared_lock lock(mutex_); auto iterator = registry_.find(surfaceId); diff --git a/ReactCommon/react/renderer/scheduler/SurfaceManager.h b/ReactCommon/react/renderer/scheduler/SurfaceManager.h index 76dd2bf8f93a41..15a68eb81c1e10 100644 --- a/ReactCommon/react/renderer/scheduler/SurfaceManager.h +++ b/ReactCommon/react/renderer/scheduler/SurfaceManager.h @@ -9,6 +9,7 @@ #include #include +#include #include #include @@ -59,7 +60,7 @@ class SurfaceManager final { const noexcept; Scheduler const &scheduler_; - mutable butter::shared_mutex mutex_; // Protects `registry_`. + mutable std::shared_mutex mutex_; // Protects `registry_`. mutable butter::map registry_{}; }; diff --git a/ReactCommon/react/renderer/uimanager/UIManager.cpp b/ReactCommon/react/renderer/uimanager/UIManager.cpp index 795c9eea18f40e..78d7bc912d579c 100644 --- a/ReactCommon/react/renderer/uimanager/UIManager.cpp +++ b/ReactCommon/react/renderer/uimanager/UIManager.cpp @@ -496,7 +496,7 @@ ShadowTreeRegistry const &UIManager::getShadowTreeRegistry() const { void UIManager::registerCommitHook( UIManagerCommitHook const &commitHook) const { - std::unique_lock lock(commitHookMutex_); + std::unique_lock lock(commitHookMutex_); react_native_assert( std::find(commitHooks_.begin(), commitHooks_.end(), &commitHook) == commitHooks_.end()); @@ -506,7 +506,7 @@ void UIManager::registerCommitHook( void UIManager::unregisterCommitHook( UIManagerCommitHook const &commitHook) const { - std::unique_lock lock(commitHookMutex_); + std::unique_lock lock(commitHookMutex_); auto iterator = std::find(commitHooks_.begin(), commitHooks_.end(), &commitHook); react_native_assert(iterator != commitHooks_.end()); @@ -520,7 +520,7 @@ RootShadowNode::Unshared UIManager::shadowTreeWillCommit( ShadowTree const &shadowTree, RootShadowNode::Shared const &oldRootShadowNode, RootShadowNode::Unshared const &newRootShadowNode) const { - std::shared_lock lock(commitHookMutex_); + std::shared_lock lock(commitHookMutex_); auto resultRootShadowNode = newRootShadowNode; for (auto const *commitHook : commitHooks_) { diff --git a/ReactCommon/react/renderer/uimanager/UIManager.h b/ReactCommon/react/renderer/uimanager/UIManager.h index 0d4700d9313236..5aaed2dddf4222 100644 --- a/ReactCommon/react/renderer/uimanager/UIManager.h +++ b/ReactCommon/react/renderer/uimanager/UIManager.h @@ -11,6 +11,7 @@ #include #include +#include #include #include @@ -203,7 +204,7 @@ class UIManager final : public ShadowTreeDelegate { BackgroundExecutor const backgroundExecutor_{}; ContextContainer::Shared contextContainer_; - mutable butter::shared_mutex commitHookMutex_; + mutable std::shared_mutex commitHookMutex_; mutable std::vector commitHooks_; std::unique_ptr leakChecker_; diff --git a/ReactCommon/react/utils/ContextContainer.h b/ReactCommon/react/utils/ContextContainer.h index 022492f43b98ca..dd3f8391554a6d 100644 --- a/ReactCommon/react/utils/ContextContainer.h +++ b/ReactCommon/react/utils/ContextContainer.h @@ -10,10 +10,10 @@ #include #include #include +#include #include #include -#include #include #include @@ -42,7 +42,7 @@ class ContextContainer final { */ template void insert(std::string const &key, T const &instance) const { - std::unique_lock lock(mutex_); + std::unique_lock lock(mutex_); instances_.insert({key, std::make_shared(instance)}); } @@ -52,7 +52,7 @@ class ContextContainer final { * Does nothing if the instance was not found. */ void erase(std::string const &key) const { - std::unique_lock lock(mutex_); + std::unique_lock lock(mutex_); instances_.erase(key); } @@ -63,7 +63,7 @@ class ContextContainer final { * values from the given container. */ void update(ContextContainer const &contextContainer) const { - std::unique_lock lock(mutex_); + std::unique_lock lock(mutex_); for (auto const &pair : contextContainer.instances_) { instances_.erase(pair.first); @@ -78,7 +78,7 @@ class ContextContainer final { */ template T at(std::string const &key) const { - std::shared_lock lock(mutex_); + std::shared_lock lock(mutex_); react_native_assert( instances_.find(key) != instances_.end() && @@ -93,7 +93,7 @@ class ContextContainer final { */ template std::optional find(std::string const &key) const { - std::shared_lock lock(mutex_); + std::shared_lock lock(mutex_); auto iterator = instances_.find(key); if (iterator == instances_.end()) { @@ -104,7 +104,7 @@ class ContextContainer final { } private: - mutable butter::shared_mutex mutex_; + mutable std::shared_mutex mutex_; // Protected by mutex_`. mutable butter::map> instances_; }; diff --git a/ReactCommon/react/utils/SharedFunction.h b/ReactCommon/react/utils/SharedFunction.h index a4a958798bef82..e834f0cf018ee5 100644 --- a/ReactCommon/react/utils/SharedFunction.h +++ b/ReactCommon/react/utils/SharedFunction.h @@ -7,9 +7,7 @@ #include #include -#include - -#include +#include namespace facebook { namespace react { @@ -31,7 +29,7 @@ class SharedFunction { struct Pair { Pair(std::function &&function) : function(std::move(function)) {} std::function function; - butter::shared_mutex mutex{}; + std::shared_mutex mutex{}; }; public: @@ -45,12 +43,12 @@ class SharedFunction { SharedFunction &operator=(SharedFunction &&other) noexcept = default; void assign(std::function function) const { - std::unique_lock lock(pair_->mutex); + std::unique_lock lock(pair_->mutex); pair_->function = function; } ReturnT operator()(ArgumentT... args) const { - std::shared_lock lock(pair_->mutex); + std::shared_lock lock(pair_->mutex); return pair_->function(args...); }