Skip to content

Commit 69be405

Browse files
Run Dart VM tasks on the engine's ConcurrentMessageLoop instead the VM's separate thread pool. (#29819)
Bug: dart-lang/sdk#44228
1 parent 5ad06c2 commit 69be405

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

runtime/dart_isolate_unittests.cc

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,54 @@ TEST_F(DartSecondaryIsolateTest, CanLaunchSecondaryIsolates) {
335335
// root isolate will be auto-shutdown
336336
}
337337

338-
TEST_F(DartIsolateTest, CanReceiveArguments) {
338+
static thread_local bool is_engine_worker = false;
339+
340+
TEST_F(DartSecondaryIsolateTest, VMTasksRunOnEngineThreads) {
341+
AddNativeCallback("NotifyNative",
342+
CREATE_NATIVE_ENTRY(([this](Dart_NativeArguments args) {
343+
LatchCountDown();
344+
})));
345+
AddNativeCallback("PassMessage",
346+
CREATE_NATIVE_ENTRY(([this](Dart_NativeArguments args) {
347+
// Child isolate is running on concurrent message loop
348+
// worker.
349+
ASSERT_TRUE(is_engine_worker);
350+
LatchCountDown();
351+
})));
352+
auto settings = CreateSettingsForFixture();
353+
settings.root_isolate_shutdown_callback = [this]() {
354+
RootIsolateShutdownSignal();
355+
};
356+
settings.isolate_shutdown_callback = [this]() { ChildShutdownSignal(); };
357+
auto vm_ref = DartVMRef::Create(settings);
358+
359+
auto loop = vm_ref->GetConcurrentMessageLoop();
360+
fml::CountDownLatch latch(loop->GetWorkerCount());
361+
vm_ref->GetConcurrentMessageLoop()->PostTaskToAllWorkers([&] {
362+
is_engine_worker = true;
363+
latch.CountDown();
364+
});
365+
latch.Wait();
366+
367+
auto thread = CreateNewThread();
368+
TaskRunners task_runners(GetCurrentTestName(), //
369+
thread, //
370+
thread, //
371+
thread, //
372+
thread //
373+
);
374+
auto isolate = RunDartCodeInIsolate(vm_ref, settings, task_runners,
375+
"testCanLaunchSecondaryIsolate", {},
376+
GetDefaultKernelFilePath());
377+
ASSERT_TRUE(isolate);
378+
ASSERT_EQ(isolate->get()->GetPhase(), DartIsolate::Phase::Running);
379+
ChildShutdownWait(); // wait for child isolate to shutdown first
380+
ASSERT_FALSE(RootIsolateIsSignaled());
381+
LatchWait(); // wait for last NotifyNative called by main isolate
382+
// root isolate will be auto-shutdown
383+
}
384+
385+
TEST_F(DartIsolateTest, CanRecieveArguments) {
339386
AddNativeCallback("NotifyNative",
340387
CREATE_NATIVE_ENTRY(([this](Dart_NativeArguments args) {
341388
ASSERT_TRUE(tonic::DartConverter<bool>::FromDart(

runtime/dart_vm.cc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,14 @@ Dart_Handle GetVMServiceAssetsArchiveCallback() {
170170
#endif
171171
}
172172

173+
void PostTaskCallback(void* post_task_data,
174+
Dart_Task task,
175+
Dart_TaskData task_data) {
176+
auto* dart_vm = reinterpret_cast<DartVM*>(post_task_data);
177+
dart_vm->GetConcurrentWorkerTaskRunner()->PostTask(
178+
[task] { Dart_RunTask(task); });
179+
}
180+
173181
static const char kStdoutStreamId[] = "Stdout";
174182
static const char kStderrStreamId[] = "Stderr";
175183

@@ -442,6 +450,8 @@ DartVM::DartVM(std::shared_ptr<const DartVMData> vm_data,
442450
params.thread_exit = ThreadExitCallback;
443451
params.get_service_assets = GetVMServiceAssetsArchiveCallback;
444452
params.entropy_source = dart::bin::GetEntropy;
453+
params.post_task = PostTaskCallback;
454+
params.post_task_data = this;
445455
DartVMInitializer::Initialize(&params);
446456
// Send the earliest available timestamp in the application lifecycle to
447457
// timeline. The difference between this timestamp and the time we render
@@ -476,6 +486,10 @@ DartVM::DartVM(std::shared_ptr<const DartVMData> vm_data,
476486
Dart_SetDartLibrarySourcesKernel(dart_library_sources->GetMapping(),
477487
dart_library_sources->GetSize());
478488
}
489+
490+
// Update thread names now that the Dart VM is initialized.
491+
concurrent_message_loop_->PostTaskToAllWorkers(
492+
[] { Dart_SetThreadName("FlutterConcurrentMessageLoopWorker"); });
479493
}
480494

481495
DartVM::~DartVM() {

0 commit comments

Comments
 (0)