Replaced isMainThread checks with a proper test for main queue

Summary:
As per https://twitter.com/olebegemann/status/738656134731599872, our use of "main thread" to mean "main queue" seems to be unsafe.

This diff replaces the `NSThread.isMainQueue` checks with dispatch_get_specific(), which is the recommended approach.

I've also replaced all use of "MainThread" terminology with "MainQueue", and taken the opportunity to deprecate the "sync" param of `RCTExecuteOnMainThread()`, which, while we do still use it in a few places, is incredibly unsafe and shouldn't be encouraged.

Reviewed By: javache

Differential Revision: D3384910

fbshipit-source-id: ea7c216013372267b82eb25a38db5eb4cd46a089
This commit is contained in:
Nick Lockwood
2016-06-06 07:57:55 -07:00
committed by Facebook Github Bot 6
parent 1048e5d344
commit 72b363d7fc
19 changed files with 121 additions and 83 deletions

View File

@@ -48,7 +48,7 @@ RCT_EXPORT_MODULE()
@interface RCTTestCustomInitModule : NSObject <RCTBridgeModule>
@property (nonatomic, assign) BOOL initializedOnMainThread;
@property (nonatomic, assign) BOOL initializedOnMainQueue;
@end
@@ -62,7 +62,7 @@ RCT_EXPORT_MODULE()
- (id)init
{
if ((self = [super init])) {
_initializedOnMainThread = [NSThread isMainThread];
_initializedOnMainQueue = RCTIsMainQueue();
}
return self;
}
@@ -72,7 +72,7 @@ RCT_EXPORT_MODULE()
@interface RCTTestCustomSetBridgeModule : NSObject <RCTBridgeModule>
@property (nonatomic, assign) BOOL setBridgeOnMainThread;
@property (nonatomic, assign) BOOL setBridgeOnMainQueue;
@end
@@ -86,7 +86,7 @@ RCT_EXPORT_MODULE()
- (void)setBridge:(RCTBridge *)bridge
{
_bridge = bridge;
_setBridgeOnMainThread = [NSThread isMainThread];
_setBridgeOnMainQueue = RCTIsMainQueue();
}
@end
@@ -95,7 +95,7 @@ RCT_EXPORT_MODULE()
@interface RCTTestExportConstantsModule : NSObject <RCTBridgeModule>
@property (nonatomic, assign) BOOL exportedConstants;
@property (nonatomic, assign) BOOL exportedConstantsOnMainThread;
@property (nonatomic, assign) BOOL exportedConstantsOnMainQueue;
@end
@@ -109,7 +109,7 @@ RCT_EXPORT_MODULE()
- (NSDictionary<NSString *, id> *)constantsToExport
{
_exportedConstants = YES;
_exportedConstantsOnMainThread = [NSThread isMainThread];
_exportedConstantsOnMainQueue = RCTIsMainQueue();
return @{ @"foo": @"bar" };
}
@@ -137,7 +137,7 @@ RCT_EXPORT_MODULE()
BOOL _customSetBridgeModuleNotificationSent;
BOOL _exportConstantsModuleNotificationSent;
BOOL _lazyInitModuleNotificationSent;
BOOL _lazyInitModuleNotificationSentOnMainThread;
BOOL _lazyInitModuleNotificationSentOnMainQueue;
BOOL _viewManagerModuleNotificationSent;
RCTTestInjectedModule *_injectedModule;
}
@@ -196,7 +196,7 @@ RCT_EXPORT_MODULE()
_exportConstantsModuleNotificationSent = YES;
} else if ([module isKindOfClass:[RCTLazyInitModule class]]) {
_lazyInitModuleNotificationSent = YES;
_lazyInitModuleNotificationSentOnMainThread = [NSThread isMainThread];
_lazyInitModuleNotificationSentOnMainQueue = RCTIsMainQueue();
}
}
@@ -214,7 +214,7 @@ RCT_EXPORT_MODULE()
RUN_RUNLOOP_WHILE(!_customInitModuleNotificationSent);
XCTAssertTrue(_customInitModuleNotificationSent);
RCTTestCustomInitModule *module = [_bridge moduleForClass:[RCTTestCustomInitModule class]];
XCTAssertTrue(module.initializedOnMainThread);
XCTAssertTrue(module.initializedOnMainQueue);
XCTAssertEqual(module.bridge, _bridge.batchedBridge);
XCTAssertNotNil(module.methodQueue);
}
@@ -230,7 +230,7 @@ RCT_EXPORT_MODULE()
RUN_RUNLOOP_WHILE(!module);
XCTAssertTrue(_customSetBridgeModuleNotificationSent);
XCTAssertFalse(module.setBridgeOnMainThread);
XCTAssertFalse(module.setBridgeOnMainQueue);
XCTAssertEqual(module.bridge, _bridge.batchedBridge);
XCTAssertNotNil(module.methodQueue);
}
@@ -242,7 +242,7 @@ RCT_EXPORT_MODULE()
RCTTestExportConstantsModule *module = [_bridge moduleForClass:[RCTTestExportConstantsModule class]];
RUN_RUNLOOP_WHILE(!module.exportedConstants);
XCTAssertTrue(module.exportedConstants);
XCTAssertTrue(module.exportedConstantsOnMainThread);
XCTAssertTrue(module.exportedConstantsOnMainQueue);
XCTAssertEqual(module.bridge, _bridge.batchedBridge);
XCTAssertNotNil(module.methodQueue);
}
@@ -258,7 +258,7 @@ RCT_EXPORT_MODULE()
RUN_RUNLOOP_WHILE(!module);
XCTAssertTrue(_lazyInitModuleNotificationSent);
XCTAssertFalse(_lazyInitModuleNotificationSentOnMainThread);
XCTAssertFalse(_lazyInitModuleNotificationSentOnMainQueue);
XCTAssertNotNil(module);
XCTAssertEqual(module.bridge, _bridge.batchedBridge);
XCTAssertNotNil(module.methodQueue);