From cb56461473306ef1da8804300361a7c23631d401 Mon Sep 17 00:00:00 2001 From: Pieter De Baets Date: Fri, 22 May 2026 02:26:40 -0700 Subject: [PATCH] Use dispatch_semaphore for eager-main-queue module setup gate MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: Replace the `std::mutex` + `std::condition_variable` + `std::shared_ptr` trio that gates the JS thread on main-queue module construction with a single `dispatch_semaphore_t`. The wait block is invoked exactly once (from `_loadScriptFromSource:`'s `beforeLoad` lambda), so single-shot semaphore semantics fit the contract directly — no condition predicate, no shared state captured by three independent `shared_ptr`s. Changelog: [Internal] Reviewed By: sammy-SC Differential Revision: D105953979 --- .../runtime/platform/ios/ReactCommon/RCTInstance.mm | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/packages/react-native/ReactCommon/react/runtime/platform/ios/ReactCommon/RCTInstance.mm b/packages/react-native/ReactCommon/react/runtime/platform/ios/ReactCommon/RCTInstance.mm index d065b1b7943d..0cdbb3183f5b 100644 --- a/packages/react-native/ReactCommon/react/runtime/platform/ios/ReactCommon/RCTInstance.mm +++ b/packages/react-native/ReactCommon/react/runtime/platform/ios/ReactCommon/RCTInstance.mm @@ -379,13 +379,10 @@ - (void)_start */ NSArray *modulesRequiringMainQueueSetup = [_delegate unstableModulesRequiringMainQueueSetup]; - std::shared_ptr mutex = std::make_shared(); - std::shared_ptr cv = std::make_shared(); - std::shared_ptr isReady = std::make_shared(false); + dispatch_semaphore_t moduleSetupComplete = dispatch_semaphore_create(0); _waitUntilModuleSetupComplete = ^{ - std::unique_lock lock(*mutex); - cv->wait(lock, [isReady] { return *isReady; }); + dispatch_semaphore_wait(moduleSetupComplete, DISPATCH_TIME_FOREVER); }; // TODO(T218039767): Integrate perf logging into main queue module init @@ -398,9 +395,7 @@ - (void)_start RCTScreenScale(); RCTSwitchSize(); - std::lock_guard lock(*mutex); - *isReady = true; - cv->notify_all(); + dispatch_semaphore_signal(moduleSetupComplete); }); }