Skip to content

Commit

Permalink
Register Thread For Hermes Sampling (#45331)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #45331

Let's add the a new JSRuntime API  to register the thread. This allows Hermes sampling to correctly work in Bridgeless/Activity.

## Changelog:
[General][Added] - Add experimental api to JSRuntimeFactory to initialize runtime on js thread

Reviewed By: RSNara

Differential Revision: D58787655

fbshipit-source-id: 2202271b9ad88cf8ba2145fb4e9e7bfd6e0c09fa
  • Loading branch information
Benoit Girard authored and facebook-github-bot committed Jul 8, 2024
1 parent c0d5622 commit c9fbc05
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<void (*)(const std::string&, unsigned int)>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,8 @@ void ReactInstance::initializeRuntime(
RuntimeSchedulerBinding::createAndInstallIfNeeded(
runtime, runtimeScheduler_);

runtime_->unstable_initializeOnJsThread();

defineReactInstanceFlags(runtime, options);

defineReadOnlyGlobal(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ class HermesJSRuntime : public JSRuntime {
return *targetDelegate_;
}

void unstable_initializeOnJsThread() override {
runtime_->registerForProfiling();
}

private:
std::shared_ptr<HermesRuntime> runtime_;
std::optional<jsinspector_modern::HermesRuntimeTargetDelegate>
Expand Down

0 comments on commit c9fbc05

Please sign in to comment.