Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 0b3e732

Browse files
committed
Create root isolate asynchronously
1 parent 9398bc4 commit 0b3e732

File tree

2 files changed

+57
-39
lines changed

2 files changed

+57
-39
lines changed

runtime/runtime_controller.cc

Lines changed: 56 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5+
#define FML_USED_ON_EMBEDDER
6+
57
#include "flutter/runtime/runtime_controller.h"
68

79
#include "flutter/fml/message_loop.h"
@@ -44,47 +46,62 @@ RuntimeController::RuntimeController(
4446
window_data_(std::move(p_window_data)),
4547
isolate_create_callback_(p_isolate_create_callback),
4648
isolate_shutdown_callback_(p_isolate_shutdown_callback),
47-
persistent_isolate_data_(std::move(p_persistent_isolate_data)) {
48-
// Create the root isolate as soon as the runtime controller is initialized.
49+
persistent_isolate_data_(std::move(p_persistent_isolate_data)),
50+
weak_factory_(this) {
51+
// Create the root isolate as soon as the runtime controller is initialized,
52+
// but not using a synchronous way to avoid blocking the platform thread a
53+
// long time as it is waiting while creating `Shell` on that platform thread.
4954
// It will be run at a later point when the engine provides a run
5055
// configuration and then runs the isolate.
51-
auto strong_root_isolate =
52-
DartIsolate::CreateRootIsolate(vm_->GetVMData()->GetSettings(), //
53-
isolate_snapshot_, //
54-
task_runners_, //
55-
std::make_unique<Window>(this), //
56-
snapshot_delegate_, //
57-
io_manager_, //
58-
unref_queue_, //
59-
image_decoder_, //
60-
p_advisory_script_uri, //
61-
p_advisory_script_entrypoint, //
62-
nullptr, //
63-
isolate_create_callback_, //
64-
isolate_shutdown_callback_ //
65-
)
66-
.lock();
67-
68-
FML_CHECK(strong_root_isolate) << "Could not create root isolate.";
69-
70-
// The root isolate ivar is weak.
71-
root_isolate_ = strong_root_isolate;
72-
73-
strong_root_isolate->SetReturnCodeCallback([this](uint32_t code) {
74-
root_isolate_return_code_ = {true, code};
75-
});
76-
77-
if (auto* window = GetWindowIfAvailable()) {
78-
tonic::DartState::Scope scope(strong_root_isolate);
79-
window->DidCreateIsolate();
80-
if (!FlushRuntimeStateToIsolate()) {
81-
FML_DLOG(ERROR) << "Could not setup initial isolate state.";
82-
}
83-
} else {
84-
FML_DCHECK(false) << "RuntimeController created without window binding.";
85-
}
86-
87-
FML_DCHECK(Dart_CurrentIsolate() == nullptr);
56+
fml::MessageLoop::GetCurrent().GetTaskRunner()->PostTask(
57+
[self = weak_factory_.GetWeakPtr()]() {
58+
if (!self) {
59+
return;
60+
}
61+
62+
auto strong_root_isolate =
63+
DartIsolate::CreateRootIsolate(
64+
self->vm_->GetVMData()->GetSettings(), //
65+
self->isolate_snapshot_, //
66+
self->task_runners_, //
67+
std::make_unique<Window>(self.get()), //
68+
self->snapshot_delegate_, //
69+
self->io_manager_, //
70+
self->unref_queue_, //
71+
self->image_decoder_, //
72+
self->advisory_script_uri_, //
73+
self->advisory_script_entrypoint_, //
74+
nullptr, //
75+
self->isolate_create_callback_, //
76+
self->isolate_shutdown_callback_ //
77+
)
78+
.lock();
79+
FML_CHECK(strong_root_isolate) << "Could not create root isolate.";
80+
81+
// The root isolate ivar is weak.
82+
self->root_isolate_ = strong_root_isolate;
83+
84+
strong_root_isolate->SetReturnCodeCallback([self](uint32_t code) {
85+
if (!self) {
86+
return;
87+
}
88+
89+
self->root_isolate_return_code_ = {true, code};
90+
});
91+
92+
if (auto* window = self->GetWindowIfAvailable()) {
93+
tonic::DartState::Scope scope(strong_root_isolate);
94+
window->DidCreateIsolate();
95+
if (!self->FlushRuntimeStateToIsolate()) {
96+
FML_DLOG(ERROR) << "Could not setup initial isolate state.";
97+
}
98+
} else {
99+
FML_DCHECK(false)
100+
<< "RuntimeController created without window binding.";
101+
}
102+
103+
FML_DCHECK(Dart_CurrentIsolate() == nullptr);
104+
});
88105
}
89106

90107
RuntimeController::~RuntimeController() {

runtime/runtime_controller.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,7 @@ class RuntimeController final : public WindowClient {
472472
const fml::closure isolate_create_callback_;
473473
const fml::closure isolate_shutdown_callback_;
474474
std::shared_ptr<const fml::Mapping> persistent_isolate_data_;
475+
fml::WeakPtrFactory<RuntimeController> weak_factory_;
475476

476477
Window* GetWindowIfAvailable();
477478

0 commit comments

Comments
 (0)