Skip to content

Commit

Permalink
Add lock for surface presenter observers (#24052)
Browse files Browse the repository at this point in the history
Summary:
Add lock for observers when do enumeration.
cc. shergin .

[iOS] [Fixed] - Add lock for surface presenter observers
Pull Request resolved: #24052

Differential Revision: D14619029

Pulled By: shergin

fbshipit-source-id: b843ac0e4b106a572de75663840f2e5e5f126f31
  • Loading branch information
zhongwuzw authored and facebook-github-bot committed Mar 26, 2019
1 parent 5e4a589 commit 946f1a6
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions React/Fabric/RCTSurfacePresenter.mm
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ @implementation RCTSurfacePresenter {
RCTBridge *_bridge; // Unsafe. We are moving away from Bridge.
RCTBridge *_batchedBridge;
std::shared_ptr<const ReactNativeConfig> _reactNativeConfig;
std::mutex _observerListMutex;
better::shared_mutex _observerListMutex;
NSMutableArray<id<RCTSurfacePresenterObserver>> *_observers;
}

Expand Down Expand Up @@ -319,13 +319,13 @@ - (void)schedulerOptimisticallyCreateComponentViewWithComponentHandle:(Component

- (void)addObserver:(id<RCTSurfacePresenterObserver>)observer
{
std::lock_guard<std::mutex> lock(_observerListMutex);
std::unique_lock<better::shared_mutex> lock(_observerListMutex);
[self->_observers addObject:observer];
}

- (void)removeObserver:(id<RCTSurfacePresenterObserver>)observer
{
std::lock_guard<std::mutex> lock(_observerListMutex);
std::unique_lock<better::shared_mutex> lock(_observerListMutex);
[self->_observers removeObject:observer];
}

Expand All @@ -335,6 +335,7 @@ - (void)mountingManager:(RCTMountingManager *)mountingManager willMountComponent
{
RCTAssertMainQueue();

std::shared_lock<better::shared_mutex> lock(_observerListMutex);
for (id<RCTSurfacePresenterObserver> observer in _observers) {
if ([observer respondsToSelector:@selector(willMountComponentsWithRootTag:)]) {
[observer willMountComponentsWithRootTag:rootTag];
Expand All @@ -355,6 +356,8 @@ - (void)mountingManager:(RCTMountingManager *)mountingManager didMountComponents
surface.view.rootView = (RCTSurfaceRootView *)rootComponentView;
}
}

std::shared_lock<better::shared_mutex> lock(_observerListMutex);
for (id<RCTSurfacePresenterObserver> observer in _observers) {
if ([observer respondsToSelector:@selector(didMountComponentsWithRootTag:)]) {
[observer didMountComponentsWithRootTag:rootTag];
Expand Down

0 comments on commit 946f1a6

Please sign in to comment.