From 38c8b6dd4280b57b42845a344f8991933ba26b4a Mon Sep 17 00:00:00 2001 From: Valentin Shergin Date: Mon, 18 Sep 2017 13:34:24 -0700 Subject: [PATCH] Using mutex inside RCTUIManagerObserverCoordinator instead of GCD Reviewed By: javache Differential Revision: D5843832 fbshipit-source-id: f4051755a5de83431f8bed1b765b5d8118b5ab88 --- .../Modules/RCTUIManagerObserverCoordinator.h | 6 ++++-- ...r.m => RCTUIManagerObserverCoordinator.mm} | 19 +++++++++++++------ React/React.xcodeproj/project.pbxproj | 12 ++++++------ 3 files changed, 23 insertions(+), 14 deletions(-) rename React/Modules/{RCTUIManagerObserverCoordinator.m => RCTUIManagerObserverCoordinator.mm} (82%) diff --git a/React/Modules/RCTUIManagerObserverCoordinator.h b/React/Modules/RCTUIManagerObserverCoordinator.h index c39cea2ec..3d17b7c7b 100644 --- a/React/Modules/RCTUIManagerObserverCoordinator.h +++ b/React/Modules/RCTUIManagerObserverCoordinator.h @@ -12,9 +12,11 @@ #import /** - * Allows to hook into UIManager internals. This can be used to execute code at + * Allows hooking into UIManager internals. This can be used to execute code at * specific points during the view updating process. - * All observer handler is called on UIManager queue. + * New observers must not be added inside observer handlers. + * The particular order of handler invocation is not guaranteed. + * All observer handlers are called on UIManager queue. */ @protocol RCTUIManagerObserver diff --git a/React/Modules/RCTUIManagerObserverCoordinator.m b/React/Modules/RCTUIManagerObserverCoordinator.mm similarity index 82% rename from React/Modules/RCTUIManagerObserverCoordinator.m rename to React/Modules/RCTUIManagerObserverCoordinator.mm index f16acaa4e..00725111e 100644 --- a/React/Modules/RCTUIManagerObserverCoordinator.m +++ b/React/Modules/RCTUIManagerObserverCoordinator.mm @@ -9,10 +9,13 @@ #import "RCTUIManagerObserverCoordinator.h" +#import + #import "RCTUIManager.h" @implementation RCTUIManagerObserverCoordinator { NSHashTable> *_observers; + std::mutex _mutex; } - (instancetype)init @@ -26,22 +29,22 @@ - (void)addObserver:(id)observer { - dispatch_async(RCTGetUIManagerQueue(), ^{ - [self->_observers addObject:observer]; - }); + std::lock_guard lock(_mutex); + [self->_observers addObject:observer]; } - (void)removeObserver:(id)observer { - dispatch_async(RCTGetUIManagerQueue(), ^{ - [self->_observers removeObject:observer]; - }); + std::lock_guard lock(_mutex); + [self->_observers removeObject:observer]; } #pragma mark - RCTUIManagerObserver - (void)uiManagerWillPerformLayout:(RCTUIManager *)manager { + std::lock_guard lock(_mutex); + for (id observer in _observers) { if ([observer respondsToSelector:@selector(uiManagerWillPerformLayout:)]) { [observer uiManagerWillPerformLayout:manager]; @@ -51,6 +54,8 @@ - (void)uiManagerDidPerformLayout:(RCTUIManager *)manager { + std::lock_guard lock(_mutex); + for (id observer in _observers) { if ([observer respondsToSelector:@selector(uiManagerDidPerformLayout:)]) { [observer uiManagerDidPerformLayout:manager]; @@ -60,6 +65,8 @@ - (void)uiManagerWillFlushUIBlocks:(RCTUIManager *)manager { + std::lock_guard lock(_mutex); + for (id observer in _observers) { if ([observer respondsToSelector:@selector(uiManagerWillFlushUIBlocks:)]) { [observer uiManagerWillFlushUIBlocks:manager]; diff --git a/React/React.xcodeproj/project.pbxproj b/React/React.xcodeproj/project.pbxproj index 42fec289f..3e8faf15a 100644 --- a/React/React.xcodeproj/project.pbxproj +++ b/React/React.xcodeproj/project.pbxproj @@ -1021,8 +1021,8 @@ 59B1EBCA1EBD47520047B19B /* RCTShadowView+Layout.h in Copy Headers */ = {isa = PBXBuildFile; fileRef = 590D7BFB1EBD458B00D8A370 /* RCTShadowView+Layout.h */; }; 59EB6DBB1EBD6FC90072A5E7 /* RCTUIManagerObserverCoordinator.h in Headers */ = {isa = PBXBuildFile; fileRef = 59EB6DB91EBD6FC90072A5E7 /* RCTUIManagerObserverCoordinator.h */; }; 59EB6DBC1EBD6FC90072A5E7 /* RCTUIManagerObserverCoordinator.h in Headers */ = {isa = PBXBuildFile; fileRef = 59EB6DB91EBD6FC90072A5E7 /* RCTUIManagerObserverCoordinator.h */; }; - 59EB6DBD1EBD6FC90072A5E7 /* RCTUIManagerObserverCoordinator.m in Sources */ = {isa = PBXBuildFile; fileRef = 59EB6DBA1EBD6FC90072A5E7 /* RCTUIManagerObserverCoordinator.m */; }; - 59EB6DBE1EBD6FC90072A5E7 /* RCTUIManagerObserverCoordinator.m in Sources */ = {isa = PBXBuildFile; fileRef = 59EB6DBA1EBD6FC90072A5E7 /* RCTUIManagerObserverCoordinator.m */; }; + 59EB6DBD1EBD6FC90072A5E7 /* RCTUIManagerObserverCoordinator.mm in Sources */ = {isa = PBXBuildFile; fileRef = 59EB6DBA1EBD6FC90072A5E7 /* RCTUIManagerObserverCoordinator.mm */; }; + 59EB6DBE1EBD6FC90072A5E7 /* RCTUIManagerObserverCoordinator.mm in Sources */ = {isa = PBXBuildFile; fileRef = 59EB6DBA1EBD6FC90072A5E7 /* RCTUIManagerObserverCoordinator.mm */; }; 59EB6DBF1EBD6FFC0072A5E7 /* RCTUIManagerObserverCoordinator.h in Copy Headers */ = {isa = PBXBuildFile; fileRef = 59EB6DB91EBD6FC90072A5E7 /* RCTUIManagerObserverCoordinator.h */; }; 59EB6DC01EBD70130072A5E7 /* RCTUIManagerObserverCoordinator.h in Copy Headers */ = {isa = PBXBuildFile; fileRef = 59EB6DB91EBD6FC90072A5E7 /* RCTUIManagerObserverCoordinator.h */; }; 59FBEFB01E46D91C0095D885 /* RCTScrollContentShadowView.h in Headers */ = {isa = PBXBuildFile; fileRef = 59FBEFAC1E46D91C0095D885 /* RCTScrollContentShadowView.h */; }; @@ -1967,7 +1967,7 @@ 59A7B9FB1E577DBF0068EDBF /* RCTRootContentView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RCTRootContentView.h; sourceTree = ""; }; 59A7B9FC1E577DBF0068EDBF /* RCTRootContentView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTRootContentView.m; sourceTree = ""; }; 59EB6DB91EBD6FC90072A5E7 /* RCTUIManagerObserverCoordinator.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RCTUIManagerObserverCoordinator.h; sourceTree = ""; }; - 59EB6DBA1EBD6FC90072A5E7 /* RCTUIManagerObserverCoordinator.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTUIManagerObserverCoordinator.m; sourceTree = ""; }; + 59EB6DBA1EBD6FC90072A5E7 /* RCTUIManagerObserverCoordinator.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = RCTUIManagerObserverCoordinator.mm; sourceTree = ""; }; 59FBEFAC1E46D91C0095D885 /* RCTScrollContentShadowView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RCTScrollContentShadowView.h; sourceTree = ""; }; 59FBEFAD1E46D91C0095D885 /* RCTScrollContentShadowView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTScrollContentShadowView.m; sourceTree = ""; }; 59FBEFAE1E46D91C0095D885 /* RCTScrollContentViewManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RCTScrollContentViewManager.h; sourceTree = ""; }; @@ -2255,7 +2255,7 @@ 13E067481A70F434002CDEE1 /* RCTUIManager.h */, 13E067491A70F434002CDEE1 /* RCTUIManager.m */, 59EB6DB91EBD6FC90072A5E7 /* RCTUIManagerObserverCoordinator.h */, - 59EB6DBA1EBD6FC90072A5E7 /* RCTUIManagerObserverCoordinator.m */, + 59EB6DBA1EBD6FC90072A5E7 /* RCTUIManagerObserverCoordinator.mm */, ); path = Modules; sourceTree = ""; @@ -3625,7 +3625,7 @@ 2D3B5ED51D9B098000451313 /* RCTModalHostViewController.m in Sources */, 2D3B5EBC1D9B092600451313 /* RCTKeyboardObserver.m in Sources */, 657734931EE8356100A0E9EA /* RCTInspector.mm in Sources */, - 59EB6DBE1EBD6FC90072A5E7 /* RCTUIManagerObserverCoordinator.m in Sources */, + 59EB6DBE1EBD6FC90072A5E7 /* RCTUIManagerObserverCoordinator.mm in Sources */, 2D3B5E971D9B089000451313 /* RCTBridge.m in Sources */, 2D3B5E9B1D9B08A000451313 /* RCTFrameUpdate.m in Sources */, 2D3B5EE41D9B09BB00451313 /* RCTSegmentedControlManager.m in Sources */, @@ -3884,7 +3884,7 @@ 13CC8A821B17642100940AE7 /* RCTBorderDrawing.m in Sources */, C60128AD1F3D1258009DF9FF /* RCTCxxConvert.m in Sources */, 83CBBA511A601E3B00E9B192 /* RCTAssert.m in Sources */, - 59EB6DBD1EBD6FC90072A5E7 /* RCTUIManagerObserverCoordinator.m in Sources */, + 59EB6DBD1EBD6FC90072A5E7 /* RCTUIManagerObserverCoordinator.mm in Sources */, 13AF20451AE707F9005F5298 /* RCTSlider.m in Sources */, 130443A21E3FEAA900D93A67 /* RCTFollyConvert.mm in Sources */, 58114A501AAE93D500E7D092 /* RCTAsyncLocalStorage.m in Sources */,