From 946f1a6c8793951c7a164b57b0e21e27d73134c7 Mon Sep 17 00:00:00 2001 From: zhongwuzw Date: Tue, 26 Mar 2019 09:52:27 -0700 Subject: [PATCH] Add lock for surface presenter observers (#24052) Summary: Add lock for observers when do enumeration. cc. shergin . [iOS] [Fixed] - Add lock for surface presenter observers Pull Request resolved: https://github.com/facebook/react-native/pull/24052 Differential Revision: D14619029 Pulled By: shergin fbshipit-source-id: b843ac0e4b106a572de75663840f2e5e5f126f31 --- React/Fabric/RCTSurfacePresenter.mm | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/React/Fabric/RCTSurfacePresenter.mm b/React/Fabric/RCTSurfacePresenter.mm index 1a2ab70abf75bb..42083028e54ae2 100644 --- a/React/Fabric/RCTSurfacePresenter.mm +++ b/React/Fabric/RCTSurfacePresenter.mm @@ -53,7 +53,7 @@ @implementation RCTSurfacePresenter { RCTBridge *_bridge; // Unsafe. We are moving away from Bridge. RCTBridge *_batchedBridge; std::shared_ptr _reactNativeConfig; - std::mutex _observerListMutex; + better::shared_mutex _observerListMutex; NSMutableArray> *_observers; } @@ -319,13 +319,13 @@ - (void)schedulerOptimisticallyCreateComponentViewWithComponentHandle:(Component - (void)addObserver:(id)observer { - std::lock_guard lock(_observerListMutex); + std::unique_lock lock(_observerListMutex); [self->_observers addObject:observer]; } - (void)removeObserver:(id)observer { - std::lock_guard lock(_observerListMutex); + std::unique_lock lock(_observerListMutex); [self->_observers removeObject:observer]; } @@ -335,6 +335,7 @@ - (void)mountingManager:(RCTMountingManager *)mountingManager willMountComponent { RCTAssertMainQueue(); + std::shared_lock lock(_observerListMutex); for (id observer in _observers) { if ([observer respondsToSelector:@selector(willMountComponentsWithRootTag:)]) { [observer willMountComponentsWithRootTag:rootTag]; @@ -355,6 +356,8 @@ - (void)mountingManager:(RCTMountingManager *)mountingManager didMountComponents surface.view.rootView = (RCTSurfaceRootView *)rootComponentView; } } + + std::shared_lock lock(_observerListMutex); for (id observer in _observers) { if ([observer respondsToSelector:@selector(didMountComponentsWithRootTag:)]) { [observer didMountComponentsWithRootTag:rootTag];