Skip to content

Commit becacc2

Browse files
RSNarafacebook-github-bot
authored andcommitted
Refactor RCTUnsafeExecute{On,Once}MainQueueSync utils
Summary: Just refactoring the control flow in these functions (in a separate diff). So, that the logic in subsequent diffs is easier to read: D74769326. Note: I made the second argument to RCTUnsafeExecuteOnMainQueueSyncWithError nullable. Changelog: [Internal] Differential Revision: D74940681
1 parent ba092bf commit becacc2

File tree

2 files changed

+30
-30
lines changed

2 files changed

+30
-30
lines changed

packages/react-native/React/Base/RCTUtils.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ RCT_EXTERN void RCTExecuteOnMainQueue(dispatch_block_t block);
4949
// Legacy function to execute the specified block on the main queue synchronously.
5050
// Please do not use this unless you know what you're doing.
5151
RCT_EXTERN void RCTUnsafeExecuteOnMainQueueSync(dispatch_block_t block);
52-
RCT_EXTERN void RCTUnsafeExecuteOnMainQueueSyncWithError(dispatch_block_t block, NSString *context);
52+
RCT_EXTERN void RCTUnsafeExecuteOnMainQueueSyncWithError(dispatch_block_t block, NSString *_Nullable context);
5353

5454
// Get screen metrics in a thread-safe way
5555
RCT_EXTERN CGFloat RCTScreenScale(void);

packages/react-native/React/Base/RCTUtils.mm

+29-29
Original file line numberDiff line numberDiff line change
@@ -299,52 +299,52 @@ void RCTExecuteOnMainQueue(dispatch_block_t block)
299299
// unless you know what you are doing.
300300
void RCTUnsafeExecuteOnMainQueueSync(dispatch_block_t block)
301301
{
302-
if (RCTIsMainQueue()) {
303-
block();
304-
} else {
305-
if (facebook::react::ReactNativeFeatureFlags::disableMainQueueSyncDispatchIOS()) {
306-
RCTLogError(@"RCTUnsafeExecuteOnMainQueueSync: Sync dispatches to the main queue can deadlock React Native.");
307-
}
308-
dispatch_sync(dispatch_get_main_queue(), ^{
309-
block();
310-
});
311-
}
302+
RCTUnsafeExecuteOnMainQueueSyncWithError(block, nil);
312303
}
313304

314305
// Please do not use this method
315306
// unless you know what you are doing.
316-
void RCTUnsafeExecuteOnMainQueueSyncWithError(dispatch_block_t block, NSString *context)
307+
void RCTUnsafeExecuteOnMainQueueSyncWithError(dispatch_block_t block, NSString *_Nullable context)
317308
{
318309
if (RCTIsMainQueue()) {
319310
block();
320-
} else {
321-
if (facebook::react::ReactNativeFeatureFlags::disableMainQueueSyncDispatchIOS()) {
322-
RCTLogError(@"RCTUnsafeExecuteOnMainQueueSync: %@", context);
323-
}
324-
dispatch_sync(dispatch_get_main_queue(), ^{
325-
block();
326-
});
311+
return;
327312
}
313+
314+
if (facebook::react::ReactNativeFeatureFlags::disableMainQueueSyncDispatchIOS()) {
315+
RCTLogError(
316+
@"RCTUnsafeExecuteOnMainQueueSync: %@",
317+
context ?: @"Sync dispatches to the main queue can deadlock React Native.");
318+
}
319+
320+
dispatch_sync(dispatch_get_main_queue(), ^{
321+
block();
322+
});
328323
}
329324

330325
static void RCTUnsafeExecuteOnMainQueueOnceSync(dispatch_once_t *onceToken, dispatch_block_t block)
331326
{
332327
// The solution was borrowed from a post by Sophie Alpert:
333328
// https://sophiebits.com/2014/04/02/dispatch-once-initialization-on-the-main-thread
334329
// See also: https://www.mikeash.com/pyblog/friday-qa-2014-06-06-secrets-of-dispatch_once.html
335-
if (RCTIsMainQueue()) {
330+
auto executeOnce = ^{
336331
dispatch_once(onceToken, block);
337-
} else {
338-
if (DISPATCH_EXPECT(*onceToken == 0L, NO)) {
339-
if (facebook::react::ReactNativeFeatureFlags::disableMainQueueSyncDispatchIOS()) {
340-
RCTLogError(
341-
@"RCTUnsafeExecuteOnMainQueueOnceSync: Sync dispatches to the main queue can deadlock React Native.");
342-
}
343-
dispatch_sync(dispatch_get_main_queue(), ^{
344-
dispatch_once(onceToken, block);
345-
});
346-
}
332+
};
333+
334+
if (RCTIsMainQueue()) {
335+
executeOnce();
336+
return;
337+
}
338+
339+
if (!DISPATCH_EXPECT(*onceToken == 0L, NO)) {
340+
return;
341+
}
342+
343+
if (facebook::react::ReactNativeFeatureFlags::disableMainQueueSyncDispatchIOS()) {
344+
RCTLogError(@"RCTUnsafeExecuteOnMainQueueOnceSync: Sync dispatches to the main queue can deadlock React Native.");
347345
}
346+
347+
dispatch_sync(dispatch_get_main_queue(), executeOnce);
348348
}
349349

350350
CGFloat RCTScreenScale(void)

0 commit comments

Comments
 (0)