From ff62ad1c043d3e332e35aed7451cc8413d30bde7 Mon Sep 17 00:00:00 2001 From: Ramanpreet Nara Date: Tue, 20 May 2025 14:18:57 -0700 Subject: [PATCH] Refactor RCTUnsafeExecute{On,Once}MainQueueSync utils (#51428) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/51428 Just refactoring the control flow in these functions (in a separate diff). So, that the logic in subsequent diffs is easier to read: D74769326. Changelog: [Internal] Reviewed By: yungsters Differential Revision: D74940681 fbshipit-source-id: 7aabc722948666a13993a1feff7eeca8ef1403cf --- packages/react-native/React/Base/RCTUtils.mm | 54 ++++++++++---------- 1 file changed, 26 insertions(+), 28 deletions(-) diff --git a/packages/react-native/React/Base/RCTUtils.mm b/packages/react-native/React/Base/RCTUtils.mm index cf06fe18712..16a08ac2bcb 100644 --- a/packages/react-native/React/Base/RCTUtils.mm +++ b/packages/react-native/React/Base/RCTUtils.mm @@ -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 @@ -320,14 +311,16 @@ 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) @@ -335,19 +328,24 @@ static void RCTUnsafeExecuteOnMainQueueOnceSync(dispatch_once_t *onceToken, disp // 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)