diff --git a/packages/react-native/ReactAndroid/src/main/jni/react/runtime/jni/JReactInstance.cpp b/packages/react-native/ReactAndroid/src/main/jni/react/runtime/jni/JReactInstance.cpp index 61c48351584d82..43c10f563ecf8e 100644 --- a/packages/react-native/ReactAndroid/src/main/jni/react/runtime/jni/JReactInstance.cpp +++ b/packages/react-native/ReactAndroid/src/main/jni/react/runtime/jni/JReactInstance.cpp @@ -74,6 +74,7 @@ JReactInstance::JReactInstance( timerManager->setRuntimeExecutor(bufferedRuntimeExecutor); ReactInstance::JSRuntimeFlags options = {.isProfiling = isProfiling}; + // TODO T194671568 Consider moving runtime init to the JS thread. instance_->initializeRuntime(options, [this](jsi::Runtime& runtime) { react::Logger androidLogger = static_cast( diff --git a/packages/react-native/ReactCommon/react/runtime/JSRuntimeFactory.h b/packages/react-native/ReactCommon/react/runtime/JSRuntimeFactory.h index dca9c1ac25c7b0..c60a46ab9bc25b 100644 --- a/packages/react-native/ReactCommon/react/runtime/JSRuntimeFactory.h +++ b/packages/react-native/ReactCommon/react/runtime/JSRuntimeFactory.h @@ -30,6 +30,14 @@ class JSRuntime { */ virtual jsinspector_modern::RuntimeTargetDelegate& getRuntimeTargetDelegate(); + /** + * Run initialize work that must happen on the runtime's JS thread. Used for + * initializing TLS and registering profiling. + * + * TODO T194671568 Move the runtime constructor to the JsThread + */ + virtual void unstable_initializeOnJsThread() {} + private: /** * Initialized by \c getRuntimeTargetDelegate if not overridden, and then diff --git a/packages/react-native/ReactCommon/react/runtime/ReactInstance.cpp b/packages/react-native/ReactCommon/react/runtime/ReactInstance.cpp index 5a1d10fdaaa362..0b8128c586f4b0 100644 --- a/packages/react-native/ReactCommon/react/runtime/ReactInstance.cpp +++ b/packages/react-native/ReactCommon/react/runtime/ReactInstance.cpp @@ -346,6 +346,8 @@ void ReactInstance::initializeRuntime( RuntimeSchedulerBinding::createAndInstallIfNeeded( runtime, runtimeScheduler_); + runtime_->unstable_initializeOnJsThread(); + defineReactInstanceFlags(runtime, options); defineReadOnlyGlobal( diff --git a/packages/react-native/ReactCommon/react/runtime/hermes/HermesInstance.cpp b/packages/react-native/ReactCommon/react/runtime/hermes/HermesInstance.cpp index 25f877a401e167..0a42996a11e391 100644 --- a/packages/react-native/ReactCommon/react/runtime/hermes/HermesInstance.cpp +++ b/packages/react-native/ReactCommon/react/runtime/hermes/HermesInstance.cpp @@ -109,6 +109,10 @@ class HermesJSRuntime : public JSRuntime { return *targetDelegate_; } + void unstable_initializeOnJsThread() override { + runtime_->registerForProfiling(); + } + private: std::shared_ptr runtime_; std::optional