From 128398be60d438ff76f658101f850f789254f806 Mon Sep 17 00:00:00 2001 From: Dan Field Date: Fri, 20 Sep 2019 12:58:08 -0700 Subject: [PATCH] Sync dart_runner (flutter/engine#12375) --- .../platform/fuchsia/dart_runner/BUILD.gn | 3 + .../dart_runner/dart_component_controller.cc | 7 ++- .../embedder/script_runner_snapshot.dart | 2 +- .../fuchsia/dart_runner/integration/main.dart | 6 +- .../platform/fuchsia/dart_runner/main.cc | 6 +- .../platform/fuchsia/flutter/component.cc | 2 +- .../shell/platform/fuchsia/flutter/main.cc | 2 +- .../fuchsia/runtime/dart/utils/BUILD.gn | 1 + .../fuchsia/runtime/dart/utils/tempfs.cc | 56 +++++++++++-------- .../fuchsia/runtime/dart/utils/tempfs.h | 31 +++++++--- 10 files changed, 74 insertions(+), 42 deletions(-) diff --git a/engine/src/flutter/shell/platform/fuchsia/dart_runner/BUILD.gn b/engine/src/flutter/shell/platform/fuchsia/dart_runner/BUILD.gn index 78f26dbcca..b04c4f9d55 100644 --- a/engine/src/flutter/shell/platform/fuchsia/dart_runner/BUILD.gn +++ b/engine/src/flutter/shell/platform/fuchsia/dart_runner/BUILD.gn @@ -53,9 +53,12 @@ template("runner") { "$flutter_root/shell/platform/fuchsia/dart-pkg/fuchsia", "$flutter_root/shell/platform/fuchsia/dart-pkg/zircon", "$flutter_root/shell/platform/fuchsia/runtime/dart/utils", + "$fuchsia_sdk_root/pkg:async", "$fuchsia_sdk_root/pkg:async-cpp", + "$fuchsia_sdk_root/pkg:async-default", "$fuchsia_sdk_root/pkg:async-loop", "$fuchsia_sdk_root/pkg:async-loop-cpp", + "$fuchsia_sdk_root/pkg:async-loop-default", "$fuchsia_sdk_root/pkg:fidl_cpp", "$fuchsia_sdk_root/pkg:sys_cpp", "$fuchsia_sdk_root/pkg:syslog", diff --git a/engine/src/flutter/shell/platform/fuchsia/dart_runner/dart_component_controller.cc b/engine/src/flutter/shell/platform/fuchsia/dart_runner/dart_component_controller.cc index a47de51f0f..e69c8073d6 100644 --- a/engine/src/flutter/shell/platform/fuchsia/dart_runner/dart_component_controller.cc +++ b/engine/src/flutter/shell/platform/fuchsia/dart_runner/dart_component_controller.cc @@ -56,6 +56,11 @@ void AfterTask(async_loop_t*, void*) { } constexpr async_loop_config_t kLoopConfig = { + .default_accessors = + { + .getter = async_get_default_dispatcher, + .setter = async_set_default_dispatcher, + }, .make_default_for_current_thread = true, .epilogue = &AfterTask, }; @@ -157,7 +162,7 @@ bool DartComponentController::SetupNamespace() { return false; } - dart_utils::SetupComponentTemp(namespace_); + dart_utils::RunnerTemp::SetupComponent(namespace_); for (size_t i = 0; i < flat->paths.size(); ++i) { if (flat->paths.at(i) == kTmpPath) { diff --git a/engine/src/flutter/shell/platform/fuchsia/dart_runner/embedder/script_runner_snapshot.dart b/engine/src/flutter/shell/platform/fuchsia/dart_runner/embedder/script_runner_snapshot.dart index a3d7e41f5a..39c0b98a0e 100644 --- a/engine/src/flutter/shell/platform/fuchsia/dart_runner/embedder/script_runner_snapshot.dart +++ b/engine/src/flutter/shell/platform/fuchsia/dart_runner/embedder/script_runner_snapshot.dart @@ -28,4 +28,4 @@ import 'package:lib.app.dart/app.dart'; import 'package:fidl/fidl.dart'; // From //sdk/fidl/fuchsia.modular -import 'package:fidl_fuchsia_modular/fidl.dart'; +import 'package:fidl_fuchsia_modular/fidl_async.dart'; diff --git a/engine/src/flutter/shell/platform/fuchsia/dart_runner/integration/main.dart b/engine/src/flutter/shell/platform/fuchsia/dart_runner/integration/main.dart index 0464f8a0f3..695aff78fc 100644 --- a/engine/src/flutter/shell/platform/fuchsia/dart_runner/integration/main.dart +++ b/engine/src/flutter/shell/platform/fuchsia/dart_runner/integration/main.dart @@ -40,16 +40,16 @@ void main(List args) { test('communicate with a fidl service (hello_app_dart)', () async { final HelloProxy service = HelloProxy(); - final dirProxy = DirectoryProxy(); + final incoming = Incoming(); final ComponentControllerProxy actl = ComponentControllerProxy(); final LaunchInfo info = LaunchInfo( url: 'fuchsia-pkg://fuchsia.com/hello_app_dart_jit#meta/hello_app_dart_jit.cmx', - directoryRequest: dirProxy.ctrl.request().passChannel()); + directoryRequest: incoming.request().passChannel()); await launcher.createComponent(info, actl.ctrl.request()); - Incoming(dirProxy).connectToService(service); + incoming.connectToService(service); expect(await service.say('hello'), equals('hola from Dart!')); diff --git a/engine/src/flutter/shell/platform/fuchsia/dart_runner/main.cc b/engine/src/flutter/shell/platform/fuchsia/dart_runner/main.cc index 05388888e4..7133ecc964 100644 --- a/engine/src/flutter/shell/platform/fuchsia/dart_runner/main.cc +++ b/engine/src/flutter/shell/platform/fuchsia/dart_runner/main.cc @@ -3,6 +3,7 @@ // found in the LICENSE file. #include +#include #include #include @@ -33,7 +34,7 @@ static void RegisterProfilerSymbols(const char* symbols_path, #endif // !defined(DART_PRODUCT) int main(int argc, const char** argv) { - async::Loop loop(&kAsyncLoopConfigAttachToThread); + async::Loop loop(&kAsyncLoopConfigAttachToCurrentThread); #if !defined(FUCHSIA_SDK) syslog::InitLogger(); @@ -61,8 +62,7 @@ int main(int argc, const char** argv) { #endif // defined(AOT_RUNTIME) #endif // !defined(DART_PRODUCT) - dart_utils::SetupRunnerTemp(); - + dart_utils::RunnerTemp runner_temp; dart_runner::DartRunner runner; loop.Run(); return 0; diff --git a/engine/src/flutter/shell/platform/fuchsia/flutter/component.cc b/engine/src/flutter/shell/platform/fuchsia/flutter/component.cc index 0858929603..2b0b4fffd4 100644 --- a/engine/src/flutter/shell/platform/fuchsia/flutter/component.cc +++ b/engine/src/flutter/shell/platform/fuchsia/flutter/component.cc @@ -110,7 +110,7 @@ Application::Application( } // Setup /tmp to be mapped to the process-local memfs. - dart_utils::SetupComponentTemp(fdio_ns_.get()); + dart_utils::RunnerTemp::SetupComponent(fdio_ns_.get()); // LaunchInfo::flat_namespace optional. for (size_t i = 0; i < startup_info.flat_namespace.paths.size(); ++i) { diff --git a/engine/src/flutter/shell/platform/fuchsia/flutter/main.cc b/engine/src/flutter/shell/platform/fuchsia/flutter/main.cc index 28853d3628..e37a12d275 100644 --- a/engine/src/flutter/shell/platform/fuchsia/flutter/main.cc +++ b/engine/src/flutter/shell/platform/fuchsia/flutter/main.cc @@ -25,7 +25,7 @@ int main(int argc, char const* argv[]) { } // Set up the process-wide /tmp memfs. - dart_utils::SetupRunnerTemp(); + dart_utils::RunnerTemp runner_temp; FML_DLOG(INFO) << "Flutter application services initialized."; diff --git a/engine/src/flutter/shell/platform/fuchsia/runtime/dart/utils/BUILD.gn b/engine/src/flutter/shell/platform/fuchsia/runtime/dart/utils/BUILD.gn index b2324ec255..b1c94a762d 100644 --- a/engine/src/flutter/shell/platform/fuchsia/runtime/dart/utils/BUILD.gn +++ b/engine/src/flutter/shell/platform/fuchsia/runtime/dart/utils/BUILD.gn @@ -31,6 +31,7 @@ source_set("utils") { "$fuchsia_sdk_root/fidl:fuchsia.mem", "$fuchsia_sdk_root/pkg:async-loop", "$fuchsia_sdk_root/pkg:async-loop-cpp", + "$fuchsia_sdk_root/pkg:async-loop-default", "$fuchsia_sdk_root/pkg:fdio", "$fuchsia_sdk_root/pkg:memfs", "$fuchsia_sdk_root/pkg:syslog", diff --git a/engine/src/flutter/shell/platform/fuchsia/runtime/dart/utils/tempfs.cc b/engine/src/flutter/shell/platform/fuchsia/runtime/dart/utils/tempfs.cc index 03d1464399..8c5f0f64ea 100644 --- a/engine/src/flutter/shell/platform/fuchsia/runtime/dart/utils/tempfs.cc +++ b/engine/src/flutter/shell/platform/fuchsia/runtime/dart/utils/tempfs.cc @@ -4,10 +4,13 @@ #include "runtime/dart/utils/tempfs.h" +#include #include #include #include +#include +#include #include #include #include @@ -22,36 +25,41 @@ namespace { constexpr char kTmpPath[] = "/tmp"; [[maybe_unused]] constexpr size_t kMaxTmpPages = 1024; -void DispatchTempMemFS() { - async::Loop loop(&kAsyncLoopConfigAttachToThread); -#if defined(DART_PRODUCT) - zx_status_t status = memfs_install_at_with_page_limit(loop.dispatcher(), - kMaxTmpPages, kTmpPath); -#else - // Hot reload uses /tmp to hold the updated dills and assets so do not - // impose any size limitation in non product runners. - zx_status_t status = memfs_install_at(loop.dispatcher(), kTmpPath); -#endif - if (status != ZX_OK) { - FX_LOGF(ERROR, LOG_TAG, "Failed to install a /tmp memfs: %s", - zx_status_get_string(status)); - return; - } - loop.Run(); -} - } // namespace namespace dart_utils { -// Set up a memfs bound to /tmp in the process-wide namespace that has the -// lifetime of the process. -void SetupRunnerTemp() { - std::thread thread(DispatchTempMemFS); - thread.detach(); +RunnerTemp::RunnerTemp() + : loop_(std::make_unique( + &kAsyncLoopConfigNoAttachToCurrentThread)) { + loop_->StartThread("RunnerTemp"); + Start(); } -void SetupComponentTemp(fdio_ns_t* ns) { +RunnerTemp::~RunnerTemp() = default; + +void RunnerTemp::Start() { + std::promise finished; + async::PostTask(loop_->dispatcher(), [this, &finished]() { +#if defined(DART_PRODUCT) + zx_status_t status = memfs_install_at_with_page_limit( + loop_->dispatcher(), kMaxTmpPages, kTmpPath); +#else + // Hot reload uses /tmp to hold the updated dills and assets so do not + // impose any size limitation in non product runners. + zx_status_t status = memfs_install_at(loop_->dispatcher(), kTmpPath); +#endif + finished.set_value(); + if (status != ZX_OK) { + FX_LOGF(ERROR, LOG_TAG, "Failed to install a /tmp memfs: %s", + zx_status_get_string(status)); + return; + } + }); + finished.get_future().wait(); +} + +void RunnerTemp::SetupComponent(fdio_ns_t* ns) { // TODO(zra): Should isolates share a /tmp file system within a process, or // should isolates each get their own private memfs for /tmp? For now, // sharing the process-wide /tmp simplifies hot reload since the hot reload diff --git a/engine/src/flutter/shell/platform/fuchsia/runtime/dart/utils/tempfs.h b/engine/src/flutter/shell/platform/fuchsia/runtime/dart/utils/tempfs.h index 33b07e92a5..1861ee1e6d 100644 --- a/engine/src/flutter/shell/platform/fuchsia/runtime/dart/utils/tempfs.h +++ b/engine/src/flutter/shell/platform/fuchsia/runtime/dart/utils/tempfs.h @@ -5,19 +5,34 @@ #ifndef FLUTTER_SHELL_PLATFORM_FUCHSIA_RUNTIME_DART_UTILS_TEMPFS_H_ #define FLUTTER_SHELL_PLATFORM_FUCHSIA_RUNTIME_DART_UTILS_TEMPFS_H_ -#include +#include -// Utility functions that set up /tmp for the dart_runner and flutter_runner. +#include +#include namespace dart_utils { -// Set up a memfs bound to /tmp in the process-wide namespace that has the -// lifetime of the process. -void SetupRunnerTemp(); +// Sets up /tmp for the dart_runner and flutter_runner. +class RunnerTemp { + public: + // Sets up a memfs bound to /tmp in the process-wide namespace that has the + // lifetime of this instance. + RunnerTemp(); + ~RunnerTemp(); -// Take the memfs mapped into the process-wide namespace for /tmp, and map it to -// /tmp in the given namespace. -void SetupComponentTemp(fdio_ns_t* ns); + // Take the memfs mapped into the process-wide namespace for /tmp, and map it + // to /tmp in the given namespace. + static void SetupComponent(fdio_ns_t* ns); + + private: + void Start(); + + std::unique_ptr loop_; + + // Disallow copy and assignment. + RunnerTemp(const RunnerTemp&) = delete; + RunnerTemp& operator=(const RunnerTemp&) = delete; +}; } // namespace dart_utils