Skip to content

Commit

Permalink
Enable shutting down all root isolates in a VM. (flutter#8402)
Browse files Browse the repository at this point in the history
  • Loading branch information
chinmaygarde authored Apr 3, 2019
1 parent 9acdf18 commit b59c443
Show file tree
Hide file tree
Showing 15 changed files with 372 additions and 39 deletions.
1 change: 1 addition & 0 deletions ci/licenses_golden/licenses_flutter
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,7 @@ FILE: ../../../flutter/lib/ui/window/window.h
FILE: ../../../flutter/runtime/dart_isolate.cc
FILE: ../../../flutter/runtime/dart_isolate.h
FILE: ../../../flutter/runtime/dart_isolate_unittests.cc
FILE: ../../../flutter/runtime/dart_lifecycle_unittests.cc
FILE: ../../../flutter/runtime/dart_service_isolate.cc
FILE: ../../../flutter/runtime/dart_service_isolate.h
FILE: ../../../flutter/runtime/dart_service_isolate_unittests.cc
Expand Down
8 changes: 6 additions & 2 deletions common/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ using UnhandledExceptionCallback =
// callback that generates the mapping from these paths.
// https://github.com/flutter/flutter/issues/26783
using MappingCallback = std::function<std::unique_ptr<fml::Mapping>(void)>;
using MappingsCallback =
std::function<std::vector<std::unique_ptr<const fml::Mapping>>(void)>;

struct Settings {
Settings();
Expand All @@ -53,8 +55,10 @@ struct Settings {
MappingCallback dart_library_sources_kernel;

std::string application_library_path;
std::string application_kernel_asset;
std::string application_kernel_list_asset;

std::string application_kernel_asset; // deprecated
std::string application_kernel_list_asset; // deprecated
MappingsCallback application_kernels;

std::string temp_directory_path;
std::vector<std::string> dart_flags;
Expand Down
39 changes: 34 additions & 5 deletions runtime/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -101,18 +101,17 @@ test_fixtures("runtime_fixtures") {
fixtures = [ "fixtures/simple_main.dart" ]
}

executable("runtime_unittests") {
source_set("runtime_unittests_common") {
visibility = [ ":*" ]

testonly = true

sources = [
"dart_isolate_unittests.cc",
"dart_service_isolate_unittests.cc",
"dart_vm_unittests.cc",
"runtime_test.cc",
"runtime_test.h",
]

deps = [
public_deps = [
":libdart",
":runtime",
":runtime_fixtures",
Expand All @@ -124,6 +123,36 @@ executable("runtime_unittests") {
"//third_party/skia",
"//third_party/tonic",
]
}

executable("runtime_unittests") {
testonly = true

sources = [
"dart_isolate_unittests.cc",
"dart_service_isolate_unittests.cc",
"dart_vm_unittests.cc",
]

deps = [
":runtime_unittests_common",
]

if (is_linux) {
ldflags = [ "-rdynamic" ]
}
}

executable("runtime_lifecycle_unittests") {
testonly = true

sources = [
"dart_lifecycle_unittests.cc",
]

deps = [
":runtime_unittests_common",
]

if (is_linux) {
ldflags = [ "-rdynamic" ]
Expand Down
71 changes: 58 additions & 13 deletions runtime/dart_isolate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -162,17 +162,8 @@ bool DartIsolate::Initialize(Dart_Isolate dart_isolate, bool is_root_isolate) {

tonic::DartIsolateScope scope(isolate());

if (is_root_isolate) {
if (auto task_runner = GetTaskRunners().GetUITaskRunner()) {
// Isolates may not have any particular thread affinity. Only initialize
// the task dispatcher if a task runner is explicitly specified.
tonic::DartMessageHandler::TaskDispatcher dispatcher =
[task_runner](std::function<void()> task) {
task_runner->PostTask(task);
};
message_handler().Initialize(dispatcher);
}
}
SetMessageHandlingTaskRunner(GetTaskRunners().GetUITaskRunner(),
is_root_isolate);

if (tonic::LogIfError(
Dart_SetLibraryTagHandler(tonic::DartState::HandleLibraryTag))) {
Expand All @@ -187,6 +178,23 @@ bool DartIsolate::Initialize(Dart_Isolate dart_isolate, bool is_root_isolate) {
return true;
}

fml::RefPtr<fml::TaskRunner> DartIsolate::GetMessageHandlingTaskRunner() const {
return message_handling_task_runner_;
}

void DartIsolate::SetMessageHandlingTaskRunner(
fml::RefPtr<fml::TaskRunner> runner,
bool is_root_isolate) {
if (!is_root_isolate || !runner) {
return;
}

message_handling_task_runner_ = runner;

message_handler().Initialize(
[runner](std::function<void()> task) { runner->PostTask(task); });
}

// Updating thread names here does not change the underlying OS thread names.
// Instead, this is just additional metadata for the Observatory to show the
// thread name of the isolate.
Expand Down Expand Up @@ -361,6 +369,34 @@ bool DartIsolate::PrepareForRunningFromKernel(
return true;
}

FML_WARN_UNUSED_RESULT
bool DartIsolate::PrepareForRunningFromKernels(
std::vector<std::shared_ptr<const fml::Mapping>> kernels) {
const auto count = kernels.size();
if (count == 0) {
return false;
}

for (size_t i = 0; i < count; ++i) {
bool last = (i == (count - 1));
if (!PrepareForRunningFromKernel(kernels[i], last)) {
return false;
}
}

return true;
}

FML_WARN_UNUSED_RESULT
bool DartIsolate::PrepareForRunningFromKernels(
std::vector<std::unique_ptr<const fml::Mapping>> kernels) {
std::vector<std::shared_ptr<const fml::Mapping>> shared_kernels;
for (auto& kernel : kernels) {
shared_kernels.emplace_back(std::move(kernel));
}
return PrepareForRunningFromKernels(shared_kernels);
}

bool DartIsolate::MarkIsolateRunnable() {
TRACE_EVENT0("flutter", "DartIsolate::MarkIsolateRunnable");
if (phase_ != Phase::LibrariesSetup) {
Expand Down Expand Up @@ -484,7 +520,6 @@ bool DartIsolate::Shutdown() {
// the isolate to shutdown as a parameter.
FML_DCHECK(Dart_CurrentIsolate() == nullptr);
Dart_EnterIsolate(vm_isolate);
shutdown_callbacks_.clear();
Dart_ShutdownIsolate();
FML_DCHECK(Dart_CurrentIsolate() == nullptr);
}
Expand Down Expand Up @@ -686,6 +721,8 @@ DartIsolate::CreateDartVMAndEmbedderObjectPair(
}
}

DartVMRef::GetRunningVM()->RegisterActiveIsolate(*embedder_isolate);

// The ownership of the embedder object is controlled by the Dart VM. So the
// only reference returned to the caller is weak.
embedder_isolate.release();
Expand All @@ -694,7 +731,9 @@ DartIsolate::CreateDartVMAndEmbedderObjectPair(

// |Dart_IsolateShutdownCallback|
void DartIsolate::DartIsolateShutdownCallback(
std::shared_ptr<DartIsolate>* embedder_isolate) {}
std::shared_ptr<DartIsolate>* embedder_isolate) {
embedder_isolate->get()->OnShutdownCallback();
}

// |Dart_IsolateCleanupCallback|
void DartIsolate::DartIsolateCleanupCallback(
Expand All @@ -719,6 +758,12 @@ void DartIsolate::AddIsolateShutdownCallback(fml::closure closure) {
std::make_unique<AutoFireClosure>(std::move(closure)));
}

void DartIsolate::OnShutdownCallback() {
shutdown_callbacks_.clear();
DartVMRef::GetRunningVM()->UnregisterActiveIsolate(
std::static_pointer_cast<DartIsolate>(shared_from_this()));
}

DartIsolate::AutoFireClosure::AutoFireClosure(fml::closure closure)
: closure_(std::move(closure)) {}

Expand Down
17 changes: 17 additions & 0 deletions runtime/dart_isolate.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#ifndef FLUTTER_RUNTIME_DART_ISOLATE_H_
#define FLUTTER_RUNTIME_DART_ISOLATE_H_

#include <memory>
#include <set>
#include <string>

Expand Down Expand Up @@ -75,6 +76,14 @@ class DartIsolate : public UIDartState {
bool PrepareForRunningFromKernel(std::shared_ptr<const fml::Mapping> kernel,
bool last_piece = true);

FML_WARN_UNUSED_RESULT
bool PrepareForRunningFromKernels(
std::vector<std::shared_ptr<const fml::Mapping>> kernels);

FML_WARN_UNUSED_RESULT
bool PrepareForRunningFromKernels(
std::vector<std::unique_ptr<const fml::Mapping>> kernels);

FML_WARN_UNUSED_RESULT
bool Run(const std::string& entrypoint, fml::closure on_run = nullptr);

Expand All @@ -94,6 +103,8 @@ class DartIsolate : public UIDartState {

std::weak_ptr<DartIsolate> GetWeakIsolatePtr();

fml::RefPtr<fml::TaskRunner> GetMessageHandlingTaskRunner() const;

private:
bool LoadKernel(std::shared_ptr<const fml::Mapping> mapping, bool last_piece);

Expand All @@ -116,10 +127,14 @@ class DartIsolate : public UIDartState {
std::vector<std::shared_ptr<const fml::Mapping>> kernel_buffers_;
std::vector<std::unique_ptr<AutoFireClosure>> shutdown_callbacks_;
ChildIsolatePreparer child_isolate_preparer_ = nullptr;
fml::RefPtr<fml::TaskRunner> message_handling_task_runner_;

FML_WARN_UNUSED_RESULT
bool Initialize(Dart_Isolate isolate, bool is_root_isolate);

void SetMessageHandlingTaskRunner(fml::RefPtr<fml::TaskRunner> runner,
bool is_root_isolate);

FML_WARN_UNUSED_RESULT
bool LoadLibraries(bool is_root_isolate);

Expand All @@ -128,6 +143,8 @@ class DartIsolate : public UIDartState {
FML_WARN_UNUSED_RESULT
bool MarkIsolateRunnable();

void OnShutdownCallback();

// |Dart_IsolateCreateCallback|
static Dart_Isolate DartIsolateCreateCallback(
const char* advisory_script_uri,
Expand Down
Loading

0 comments on commit b59c443

Please sign in to comment.