Skip to content

Refactor RCTUnsafeExecute{On,Once}MainQueueSync utils #51428

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 26 additions & 28 deletions packages/react-native/React/Base/RCTUtils.mm
Original file line number Diff line number Diff line change
Expand Up @@ -302,16 +302,7 @@ void RCTExecuteOnMainQueue(dispatch_block_t block)
// unless you know what you are doing.
void RCTUnsafeExecuteOnMainQueueSync(dispatch_block_t block)
{
if (RCTIsMainQueue()) {
block();
} else {
if (facebook::react::ReactNativeFeatureFlags::disableMainQueueSyncDispatchIOS()) {
RCTLogError(@"RCTUnsafeExecuteOnMainQueueSync: Sync dispatches to the main queue can deadlock React Native.");
}
dispatch_sync(dispatch_get_main_queue(), ^{
block();
});
}
RCTUnsafeExecuteOnMainQueueSyncWithError(block, @"Sync dispatches to the main queue can deadlock React Native.");
}

// Please do not use this method
Expand All @@ -320,34 +311,41 @@ void RCTUnsafeExecuteOnMainQueueSyncWithError(dispatch_block_t block, NSString *
{
if (RCTIsMainQueue()) {
block();
} else {
if (facebook::react::ReactNativeFeatureFlags::disableMainQueueSyncDispatchIOS()) {
RCTLogError(@"RCTUnsafeExecuteOnMainQueueSync: %@", context);
}
dispatch_sync(dispatch_get_main_queue(), ^{
block();
});
return;
}

if (facebook::react::ReactNativeFeatureFlags::disableMainQueueSyncDispatchIOS()) {
RCTLogError(@"RCTUnsafeExecuteOnMainQueueSync: %@", context);
}

dispatch_sync(dispatch_get_main_queue(), ^{
block();
});
}

static void RCTUnsafeExecuteOnMainQueueOnceSync(dispatch_once_t *onceToken, dispatch_block_t block)
{
// The solution was borrowed from a post by Sophie Alpert:
// https://sophiebits.com/2014/04/02/dispatch-once-initialization-on-the-main-thread
// See also: https://www.mikeash.com/pyblog/friday-qa-2014-06-06-secrets-of-dispatch_once.html
if (RCTIsMainQueue()) {
auto executeOnce = ^{
dispatch_once(onceToken, block);
} else {
if (DISPATCH_EXPECT(*onceToken == 0L, NO)) {
if (facebook::react::ReactNativeFeatureFlags::disableMainQueueSyncDispatchIOS()) {
RCTLogError(
@"RCTUnsafeExecuteOnMainQueueOnceSync: Sync dispatches to the main queue can deadlock React Native.");
}
dispatch_sync(dispatch_get_main_queue(), ^{
dispatch_once(onceToken, block);
});
}
};

if (RCTIsMainQueue()) {
executeOnce();
return;
}

if (!DISPATCH_EXPECT(*onceToken == 0L, NO)) {
return;
}

if (facebook::react::ReactNativeFeatureFlags::disableMainQueueSyncDispatchIOS()) {
RCTLogError(@"RCTUnsafeExecuteOnMainQueueOnceSync: Sync dispatches to the main queue can deadlock React Native.");
}

dispatch_sync(dispatch_get_main_queue(), executeOnce);
}

CGFloat RCTScreenScale(void)
Expand Down
Loading