From 7d14b58d2d5b0bb45a02d7e8e3bf8158ca22090d Mon Sep 17 00:00:00 2001 From: Ren You Date: Fri, 11 Jun 2021 15:09:02 -0700 Subject: [PATCH] Revert "Replace flutter_runner::Thread with fml::Thread. (#26193)" (flutter/engine#26717) --- .../ci/licenses_golden/licenses_flutter | 8 +- engine/src/flutter/fml/BUILD.gn | 2 - .../platform/fuchsia/message_loop_fuchsia.cc | 18 +--- .../fml/platform/fuchsia/task_observers.h | 40 ------- .../shell/platform/fuchsia/flutter/BUILD.gn | 6 ++ .../platform/fuchsia/flutter/component.cc | 22 ++-- .../platform/fuchsia/flutter/component.h | 5 +- .../shell/platform/fuchsia/flutter/engine.cc | 29 +++-- .../shell/platform/fuchsia/flutter/engine.h | 3 +- .../platform/fuchsia/flutter/fuchsia_intl.cc | 1 + .../shell/platform/fuchsia/flutter/loop.cc | 47 ++++++++ .../shell/platform/fuchsia/flutter/loop.h | 17 +++ .../shell/platform/fuchsia/flutter/main.cc | 15 +-- .../shell/platform/fuchsia/flutter/runner.cc | 78 +++++++------- .../shell/platform/fuchsia/flutter/runner.h | 9 +- .../fuchsia/flutter}/task_observers.cc | 6 +- .../platform/fuchsia/flutter/task_observers.h | 21 ++++ .../shell/platform/fuchsia/flutter/thread.cc | 102 ++++++++++++++++++ .../shell/platform/fuchsia/flutter/thread.h | 44 ++++++++ 19 files changed, 323 insertions(+), 150 deletions(-) delete mode 100644 engine/src/flutter/fml/platform/fuchsia/task_observers.h create mode 100644 engine/src/flutter/shell/platform/fuchsia/flutter/loop.cc create mode 100644 engine/src/flutter/shell/platform/fuchsia/flutter/loop.h rename engine/src/flutter/{fml/platform/fuchsia => shell/platform/fuchsia/flutter}/task_observers.cc (87%) create mode 100644 engine/src/flutter/shell/platform/fuchsia/flutter/task_observers.h create mode 100644 engine/src/flutter/shell/platform/fuchsia/flutter/thread.cc create mode 100644 engine/src/flutter/shell/platform/fuchsia/flutter/thread.h diff --git a/engine/src/flutter/ci/licenses_golden/licenses_flutter b/engine/src/flutter/ci/licenses_golden/licenses_flutter index 8e4e5c6858..b89002271a 100755 --- a/engine/src/flutter/ci/licenses_golden/licenses_flutter +++ b/engine/src/flutter/ci/licenses_golden/licenses_flutter @@ -227,8 +227,6 @@ FILE: ../../../flutter/fml/platform/darwin/string_range_sanitization_unittests.m FILE: ../../../flutter/fml/platform/fuchsia/message_loop_fuchsia.cc FILE: ../../../flutter/fml/platform/fuchsia/message_loop_fuchsia.h FILE: ../../../flutter/fml/platform/fuchsia/paths_fuchsia.cc -FILE: ../../../flutter/fml/platform/fuchsia/task_observers.cc -FILE: ../../../flutter/fml/platform/fuchsia/task_observers.h FILE: ../../../flutter/fml/platform/linux/message_loop_linux.cc FILE: ../../../flutter/fml/platform/linux/message_loop_linux.h FILE: ../../../flutter/fml/platform/linux/paths_linux.cc @@ -1363,6 +1361,8 @@ FILE: ../../../flutter/shell/platform/fuchsia/flutter/keyboard.cc FILE: ../../../flutter/shell/platform/fuchsia/flutter/keyboard.h FILE: ../../../flutter/shell/platform/fuchsia/flutter/keyboard_unittest.cc FILE: ../../../flutter/shell/platform/fuchsia/flutter/logging.h +FILE: ../../../flutter/shell/platform/fuchsia/flutter/loop.cc +FILE: ../../../flutter/shell/platform/fuchsia/flutter/loop.h FILE: ../../../flutter/shell/platform/fuchsia/flutter/main.cc FILE: ../../../flutter/shell/platform/fuchsia/flutter/meta/aot_product_runtime FILE: ../../../flutter/shell/platform/fuchsia/flutter/meta/aot_runtime @@ -1384,8 +1384,12 @@ FILE: ../../../flutter/shell/platform/fuchsia/flutter/runner_tzdata_unittest.cc FILE: ../../../flutter/shell/platform/fuchsia/flutter/runner_unittest.cc FILE: ../../../flutter/shell/platform/fuchsia/flutter/surface.cc FILE: ../../../flutter/shell/platform/fuchsia/flutter/surface.h +FILE: ../../../flutter/shell/platform/fuchsia/flutter/task_observers.cc +FILE: ../../../flutter/shell/platform/fuchsia/flutter/task_observers.h FILE: ../../../flutter/shell/platform/fuchsia/flutter/task_runner_adapter.cc FILE: ../../../flutter/shell/platform/fuchsia/flutter/task_runner_adapter.h +FILE: ../../../flutter/shell/platform/fuchsia/flutter/thread.cc +FILE: ../../../flutter/shell/platform/fuchsia/flutter/thread.h FILE: ../../../flutter/shell/platform/fuchsia/flutter/unique_fdio_ns.h FILE: ../../../flutter/shell/platform/fuchsia/flutter/vsync_waiter.cc FILE: ../../../flutter/shell/platform/fuchsia/flutter/vsync_waiter.h diff --git a/engine/src/flutter/fml/BUILD.gn b/engine/src/flutter/fml/BUILD.gn index cd0f1d5d61..267f7f193e 100644 --- a/engine/src/flutter/fml/BUILD.gn +++ b/engine/src/flutter/fml/BUILD.gn @@ -180,8 +180,6 @@ source_set("fml") { "platform/fuchsia/message_loop_fuchsia.cc", "platform/fuchsia/message_loop_fuchsia.h", "platform/fuchsia/paths_fuchsia.cc", - "platform/fuchsia/task_observers.cc", - "platform/fuchsia/task_observers.h", ] public_deps += [ diff --git a/engine/src/flutter/fml/platform/fuchsia/message_loop_fuchsia.cc b/engine/src/flutter/fml/platform/fuchsia/message_loop_fuchsia.cc index 0fac826a74..9a3c1c0ef8 100644 --- a/engine/src/flutter/fml/platform/fuchsia/message_loop_fuchsia.cc +++ b/engine/src/flutter/fml/platform/fuchsia/message_loop_fuchsia.cc @@ -8,25 +8,11 @@ #include #include #include -#include "flutter/fml/platform/fuchsia/task_observers.h" namespace fml { -namespace { - -// See comment on `ExecuteAfterTaskObservers` for explanation. -static void LoopEpilogue(async_loop_t*, void*) { - ExecuteAfterTaskObservers(); -} - -constexpr async_loop_config_t kLoopConfig = { - .make_default_for_current_thread = false, - .epilogue = &LoopEpilogue, -}; - -} // namespace - -MessageLoopFuchsia::MessageLoopFuchsia() : loop_(&kLoopConfig) { +MessageLoopFuchsia::MessageLoopFuchsia() + : loop_(&kAsyncLoopConfigNoAttachToCurrentThread) { async_set_default_dispatcher(loop_.dispatcher()); } diff --git a/engine/src/flutter/fml/platform/fuchsia/task_observers.h b/engine/src/flutter/fml/platform/fuchsia/task_observers.h deleted file mode 100644 index de3c2e4731..0000000000 --- a/engine/src/flutter/fml/platform/fuchsia/task_observers.h +++ /dev/null @@ -1,40 +0,0 @@ -// Copyright 2013 The Flutter Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef FLUTTER_SHELL_PLATFORM_FUCHSIA_TASK_OBSERVERS_H_ -#define FLUTTER_SHELL_PLATFORM_FUCHSIA_TASK_OBSERVERS_H_ - -#include - -namespace fml { - -// Executes all closures that were registered via -// `CurrentMessageLoopAddAfterTaskObserver` on this thread. -// -// WARNING(fxbug.dev/77957): These task observers are distinct from the task -// observers that can be specified via `fml::MessageLoop::AddTaskObserver` and -// they behave differently! -// -// Task observers registered via `fml::MessageLoop::AddTaskObserver` only fire -// after work that was posted via the `fml::MessageLoop`'s `TaskRunner` -// completes. Work that is posted directly to the Fuchsia message loop (e.g. -// using `async::PostTask(async_get_default_dispatcher(), ...)`) is invisible to -// `fml::MessageLoop`, so the `fml::MessageLoop`'s task observers don't fire. -// -// The task observers registered with `CurrentMessageLoopAddAfterTaskObserver`, -// however, fire after _every_ work item is completed, regardless of whether it -// was posted to the Fuchsia message loop directly or via `fml::MessageLoop`. -// -// These two mechanisms are redundant and confusing, so we should fix it -// somehow. -void ExecuteAfterTaskObservers(); - -void CurrentMessageLoopAddAfterTaskObserver(intptr_t key, - fit::closure observer); - -void CurrentMessageLoopRemoveAfterTaskObserver(intptr_t key); - -} // namespace fml - -#endif // FLUTTER_SHELL_PLATFORM_FUCHSIA_TASK_OBSERVERS_H_ diff --git a/engine/src/flutter/shell/platform/fuchsia/flutter/BUILD.gn b/engine/src/flutter/shell/platform/fuchsia/flutter/BUILD.gn index 9a5af1c8b4..893ef9dd05 100644 --- a/engine/src/flutter/shell/platform/fuchsia/flutter/BUILD.gn +++ b/engine/src/flutter/shell/platform/fuchsia/flutter/BUILD.gn @@ -68,14 +68,20 @@ template("runner_sources") { "keyboard.cc", "keyboard.h", "logging.h", + "loop.cc", + "loop.h", "platform_view.cc", "platform_view.h", "runner.cc", "runner.h", "surface.cc", "surface.h", + "task_observers.cc", + "task_observers.h", "task_runner_adapter.cc", "task_runner_adapter.h", + "thread.cc", + "thread.h", "unique_fdio_ns.h", "vsync_waiter.cc", "vsync_waiter.h", diff --git a/engine/src/flutter/shell/platform/fuchsia/flutter/component.cc b/engine/src/flutter/shell/platform/fuchsia/flutter/component.cc index dfe39431dd..d73dc5d2f9 100644 --- a/engine/src/flutter/shell/platform/fuchsia/flutter/component.cc +++ b/engine/src/flutter/shell/platform/fuchsia/flutter/component.cc @@ -2,8 +2,6 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#define FML_USED_ON_EMBEDDER - #include "component.h" #include @@ -29,7 +27,6 @@ #include #include "flutter/fml/mapping.h" -#include "flutter/fml/platform/fuchsia/task_observers.h" #include "flutter/fml/synchronization/waitable_event.h" #include "flutter/fml/unique_fd.h" #include "flutter/runtime/dart_vm_lifecycle.h" @@ -40,6 +37,9 @@ #include "runtime/dart/utils/tempfs.h" #include "runtime/dart/utils/vmo.h" +#include "task_observers.h" +#include "task_runner_adapter.h" + // TODO(kaushikiska): Use these constants from ::llcpp::fuchsia::io // Can read from target object. constexpr uint32_t OPEN_RIGHT_READABLE = 1u; @@ -186,11 +186,11 @@ ActiveApplication Application::Create( fuchsia::sys::StartupInfo startup_info, std::shared_ptr runner_incoming_services, fidl::InterfaceRequest controller) { - auto thread = std::make_unique(); + std::unique_ptr thread = std::make_unique(); std::unique_ptr application; fml::AutoResetWaitableEvent latch; - thread->GetTaskRunner()->PostTask([&]() mutable { + async::PostTask(thread->dispatcher(), [&]() mutable { application.reset( new Application(std::move(termination_callback), std::move(package), std::move(startup_info), runner_incoming_services, @@ -199,8 +199,7 @@ ActiveApplication Application::Create( }); latch.Wait(); - return {.platform_thread = std::move(thread), - .application = std::move(application)}; + return {.thread = std::move(thread), .application = std::move(application)}; } Application::Application( @@ -492,11 +491,11 @@ Application::Application( settings_.leak_vm = false; settings_.task_observer_add = - std::bind(&fml::CurrentMessageLoopAddAfterTaskObserver, - std::placeholders::_1, std::placeholders::_2); + std::bind(&CurrentMessageLoopAddAfterTaskObserver, std::placeholders::_1, + std::placeholders::_2); settings_.task_observer_remove = std::bind( - &fml::CurrentMessageLoopRemoveAfterTaskObserver, std::placeholders::_1); + &CurrentMessageLoopRemoveAfterTaskObserver, std::placeholders::_1); settings_.log_message_callback = [](const std::string& tag, const std::string& message) { @@ -518,7 +517,8 @@ Application::Application( #endif // defined(__aarch64__) auto weak_application = weak_factory_.GetWeakPtr(); - auto platform_task_runner = fml::MessageLoop::GetCurrent().GetTaskRunner(); + auto platform_task_runner = + CreateFMLTaskRunner(async_get_default_dispatcher()); const std::string component_url = package.resolved_url; settings_.unhandled_exception_callback = [weak_application, platform_task_runner, diff --git a/engine/src/flutter/shell/platform/fuchsia/flutter/component.h b/engine/src/flutter/shell/platform/fuchsia/flutter/component.h index 7955c10225..df9498b070 100644 --- a/engine/src/flutter/shell/platform/fuchsia/flutter/component.h +++ b/engine/src/flutter/shell/platform/fuchsia/flutter/component.h @@ -26,6 +26,7 @@ #include "engine.h" #include "flutter_runner_product_configuration.h" +#include "thread.h" #include "unique_fdio_ns.h" namespace flutter_runner { @@ -33,12 +34,12 @@ namespace flutter_runner { class Application; struct ActiveApplication { - std::unique_ptr platform_thread; + std::unique_ptr thread; std::unique_ptr application; ActiveApplication& operator=(ActiveApplication&& other) noexcept { if (this != &other) { - this->platform_thread.reset(other.platform_thread.release()); + this->thread.reset(other.thread.release()); this->application.reset(other.application.release()); } return *this; diff --git a/engine/src/flutter/shell/platform/fuchsia/flutter/engine.cc b/engine/src/flutter/shell/platform/fuchsia/flutter/engine.cc index a489f98d6e..0e4da2afbc 100644 --- a/engine/src/flutter/shell/platform/fuchsia/flutter/engine.cc +++ b/engine/src/flutter/shell/platform/fuchsia/flutter/engine.cc @@ -2,8 +2,6 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#define FML_USED_ON_EMBEDDER - #include "engine.h" #include @@ -12,7 +10,6 @@ #include "flutter/common/graphics/persistent_cache.h" #include "flutter/common/task_runners.h" #include "flutter/fml/make_copyable.h" -#include "flutter/fml/message_loop.h" #include "flutter/fml/synchronization/waitable_event.h" #include "flutter/fml/task_runner.h" #include "flutter/runtime/dart_vm_lifecycle.h" @@ -27,6 +24,7 @@ #include "fuchsia_intl.h" #include "platform_view.h" #include "surface.h" +#include "task_runner_adapter.h" #include "vsync_waiter.h" #if defined(LEGACY_FUCHSIA_EMBEDDER) @@ -82,15 +80,12 @@ Engine::Engine(Delegate& delegate, // Get the task runners from the managed threads. The current thread will be // used as the "platform" thread. - fml::RefPtr platform_task_runner = - fml::MessageLoop::GetCurrent().GetTaskRunner(); - const flutter::TaskRunners task_runners( - thread_label_, // Dart thread labels - platform_task_runner, // platform - threads_[0].GetTaskRunner(), // raster - threads_[1].GetTaskRunner(), // ui - threads_[2].GetTaskRunner() // io + thread_label_, // Dart thread labels + CreateFMLTaskRunner(async_get_default_dispatcher()), // platform + CreateFMLTaskRunner(threads_[0].dispatcher()), // raster + CreateFMLTaskRunner(threads_[1].dispatcher()), // ui + CreateFMLTaskRunner(threads_[2].dispatcher()) // io ); UpdateNativeThreadLabelNames(thread_label_, task_runners); @@ -125,9 +120,10 @@ Engine::Engine(Delegate& delegate, // This handles the fidl error callback when the Session connection is // broken. The SessionListener interface also has an OnError method, which is // invoked on the platform thread (in PlatformView). - fml::closure session_error_callback = [task_runner = platform_task_runner, + fml::closure session_error_callback = [dispatcher = + async_get_default_dispatcher(), weak = weak_factory_.GetWeakPtr()]() { - task_runner->PostTask([weak]() { + async::PostTask(dispatcher, [weak]() { if (weak) { weak->Terminate(); } @@ -225,9 +221,9 @@ Engine::Engine(Delegate& delegate, // platform thread when that happens. The Session itself should also be // disconnected when this happens, and it will also attempt to terminate. fit::closure on_session_listener_error_callback = - [task_runner = platform_task_runner, + [dispatcher = async_get_default_dispatcher(), weak = weak_factory_.GetWeakPtr()]() { - task_runner->PostTask([weak]() { + async::PostTask(dispatcher, [weak]() { if (weak) { weak->Terminate(); } @@ -515,6 +511,9 @@ Engine::Engine(Delegate& delegate, Engine::~Engine() { shell_.reset(); + for (auto& thread : threads_) { + thread.Quit(); + } for (auto& thread : threads_) { thread.Join(); } diff --git a/engine/src/flutter/shell/platform/fuchsia/flutter/engine.h b/engine/src/flutter/shell/platform/fuchsia/flutter/engine.h index 866c757303..3e2b991940 100644 --- a/engine/src/flutter/shell/platform/fuchsia/flutter/engine.h +++ b/engine/src/flutter/shell/platform/fuchsia/flutter/engine.h @@ -27,6 +27,7 @@ #include "flutter_runner_product_configuration.h" #include "fuchsia_external_view_embedder.h" #include "isolate_configurator.h" +#include "thread.h" #include "vulkan_surface_producer.h" #if defined(LEGACY_FUCHSIA_EMBEDDER) @@ -72,7 +73,7 @@ class Engine final { Delegate& delegate_; const std::string thread_label_; - std::array threads_; + std::array threads_; std::shared_ptr session_connection_; std::optional surface_producer_; diff --git a/engine/src/flutter/shell/platform/fuchsia/flutter/fuchsia_intl.cc b/engine/src/flutter/shell/platform/fuchsia/flutter/fuchsia_intl.cc index 85c91b4d86..b3235385ce 100644 --- a/engine/src/flutter/shell/platform/fuchsia/flutter/fuchsia_intl.cc +++ b/engine/src/flutter/shell/platform/fuchsia/flutter/fuchsia_intl.cc @@ -8,6 +8,7 @@ #include #include +#include "loop.h" #include "rapidjson/document.h" #include "rapidjson/stringbuffer.h" #include "rapidjson/writer.h" diff --git a/engine/src/flutter/shell/platform/fuchsia/flutter/loop.cc b/engine/src/flutter/shell/platform/fuchsia/flutter/loop.cc new file mode 100644 index 0000000000..6518828c7a --- /dev/null +++ b/engine/src/flutter/shell/platform/fuchsia/flutter/loop.cc @@ -0,0 +1,47 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "loop.h" + +#include +#include + +#include "task_observers.h" + +namespace flutter_runner { + +namespace { + +static void LoopEpilogue(async_loop_t*, void*) { + ExecuteAfterTaskObservers(); +} + +constexpr async_loop_config_t kAttachedLoopConfig = { + .default_accessors = + { + .getter = async_get_default_dispatcher, + .setter = async_set_default_dispatcher, + }, + .make_default_for_current_thread = true, + .epilogue = &LoopEpilogue, +}; + +constexpr async_loop_config_t kDetachedLoopConfig = { + .default_accessors = + { + .getter = async_get_default_dispatcher, + .setter = async_set_default_dispatcher, + }, + .make_default_for_current_thread = false, + .epilogue = &LoopEpilogue, +}; + +} // namespace + +async::Loop* MakeObservableLoop(bool attachToThread) { + return new async::Loop( + &(attachToThread ? kAttachedLoopConfig : kDetachedLoopConfig)); +} + +} // namespace flutter_runner diff --git a/engine/src/flutter/shell/platform/fuchsia/flutter/loop.h b/engine/src/flutter/shell/platform/fuchsia/flutter/loop.h new file mode 100644 index 0000000000..02339febad --- /dev/null +++ b/engine/src/flutter/shell/platform/fuchsia/flutter/loop.h @@ -0,0 +1,17 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef FLUTTER_SHELL_PLATFORM_FUCHSIA_LOOP_H_ +#define FLUTTER_SHELL_PLATFORM_FUCHSIA_LOOP_H_ + +#include + +namespace flutter_runner { + +// Creates a loop which allows task observers to be attached to it. +async::Loop* MakeObservableLoop(bool attachToThread); + +} // namespace flutter_runner + +#endif // FLUTTER_SHELL_PLATFORM_FUCHSIA_LOOP_H_ diff --git a/engine/src/flutter/shell/platform/fuchsia/flutter/main.cc b/engine/src/flutter/shell/platform/fuchsia/flutter/main.cc index d5bfebb66f..8f7d0e6ad5 100644 --- a/engine/src/flutter/shell/platform/fuchsia/flutter/main.cc +++ b/engine/src/flutter/shell/platform/fuchsia/flutter/main.cc @@ -2,8 +2,6 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#define FML_USED_ON_EMBEDDER - #include #include #include @@ -11,8 +9,7 @@ #include -#include "fml/message_loop.h" -#include "lib/async/default.h" +#include "loop.h" #include "platform/utils.h" #include "runner.h" #include "runtime/dart/utils/build_info.h" @@ -20,7 +17,7 @@ #include "runtime/dart/utils/tempfs.h" int main(int argc, char const* argv[]) { - fml::MessageLoop::EnsureInitializedForCurrentThread(); + std::unique_ptr loop(flutter_runner::MakeObservableLoop(true)); // Create our component context which is served later. auto context = sys::ComponentContext::Create(); @@ -38,8 +35,7 @@ int main(int argc, char const* argv[]) { bool already_started; // Use CreateSynchronously to prevent loss of early events. trace::TraceProviderWithFdio::CreateSynchronously( - async_get_default_dispatcher(), "flutter_runner", &provider, - &already_started); + loop->dispatcher(), "flutter_runner", &provider, &already_started); } // Set up the process-wide /tmp memfs. @@ -47,13 +43,12 @@ int main(int argc, char const* argv[]) { FML_DLOG(INFO) << "Flutter application services initialized."; - fml::MessageLoop& loop = fml::MessageLoop::GetCurrent(); - flutter_runner::Runner runner(loop.GetTaskRunner(), context.get()); + flutter_runner::Runner runner(loop.get(), context.get()); // Wait to serve until we have finished all of our setup. context->outgoing()->ServeFromStartupInfo(); - loop.Run(); + loop->Run(); FML_DLOG(INFO) << "Flutter application services terminated."; return EXIT_SUCCESS; diff --git a/engine/src/flutter/shell/platform/fuchsia/flutter/runner.cc b/engine/src/flutter/shell/platform/fuchsia/flutter/runner.cc index 0a7aab15de..328a940734 100644 --- a/engine/src/flutter/shell/platform/fuchsia/flutter/runner.cc +++ b/engine/src/flutter/shell/platform/fuchsia/flutter/runner.cc @@ -19,7 +19,6 @@ #include "flutter/fml/make_copyable.h" #include "flutter/lib/ui/text/font_collection.h" #include "flutter/runtime/dart_vm.h" -#include "lib/async/default.h" #include "lib/sys/cpp/component_context.h" #include "runtime/dart/utils/files.h" #include "runtime/dart/utils/root_inspect_node.h" @@ -149,9 +148,8 @@ static void RegisterProfilerSymbols(const char* symbols_path, } #endif // !defined(DART_PRODUCT) -Runner::Runner(fml::RefPtr task_runner, - sys::ComponentContext* context) - : task_runner_(task_runner), context_(context) { +Runner::Runner(async::Loop* loop, sys::ComponentContext* context) + : loop_(loop), context_(context) { #if !defined(DART_PRODUCT) // The VM service isolate uses the process-wide namespace. It writes the // vm service protocol port under /tmp. The VMServiceObject exposes that @@ -234,11 +232,12 @@ void Runner::StartComponent( // there being multiple application runner instance in the process at the same // time. So it is safe to use the raw pointer. Application::TerminationCallback termination_callback = - [application_runner = this](const Application* application) { - application_runner->task_runner_->PostTask( - [application_runner, application]() { - application_runner->OnApplicationTerminate(application); - }); + [task_runner = loop_->dispatcher(), // + application_runner = this // + ](const Application* application) { + async::PostTask(task_runner, [application_runner, application]() { + application_runner->OnApplicationTerminate(application); + }); }; auto active_application = Application::Create( @@ -267,17 +266,21 @@ void Runner::OnApplicationTerminate(const Application* application) { // Grab the items out of the entry because we will have to rethread the // destruction. auto application_to_destroy = std::move(active_application.application); - auto application_thread = std::move(active_application.platform_thread); + auto application_thread = std::move(active_application.thread); // Delegate the entry. active_applications_.erase(application); // Post the task to destroy the application and quit its message loop. - application_thread->GetTaskRunner()->PostTask(fml::MakeCopyable( - [instance = std::move(application_to_destroy), - thread = application_thread.get()]() mutable { instance.reset(); })); + async::PostTask( + application_thread->dispatcher(), + fml::MakeCopyable([instance = std::move(application_to_destroy), + thread = application_thread.get()]() mutable { + instance.reset(); + thread->Quit(); + })); - // Terminate and join the thread's message loop. + // This works because just posted the quit task on the hosted thread. application_thread->Join(); } @@ -301,36 +304,27 @@ bool Runner::SetupTZDataInternal() { #if !defined(DART_PRODUCT) void Runner::SetupTraceObserver() { - fml::AutoResetWaitableEvent latch; - - fml::TaskRunner::RunNowOrPostTask(task_runner_, [&]() { - // Running this initialization code on task_runner_ ensures that the call to - // `async_get_default_dispatcher()` will capture the correct dispatcher. - trace_observer_ = std::make_unique(); - trace_observer_->Start(async_get_default_dispatcher(), [runner = this]() { - if (!trace_is_category_enabled("dart:profiler")) { - return; + trace_observer_ = std::make_unique(); + trace_observer_->Start(loop_->dispatcher(), [runner = this]() { + if (!trace_is_category_enabled("dart:profiler")) { + return; + } + if (trace_state() == TRACE_STARTED) { + runner->prolonged_context_ = trace_acquire_prolonged_context(); + Dart_StartProfiling(); + } else if (trace_state() == TRACE_STOPPING) { + for (auto& it : runner->active_applications_) { + fml::AutoResetWaitableEvent latch; + async::PostTask(it.second.thread->dispatcher(), [&]() { + it.second.application->WriteProfileToTrace(); + latch.Signal(); + }); + latch.Wait(); } - if (trace_state() == TRACE_STARTED) { - runner->prolonged_context_ = trace_acquire_prolonged_context(); - Dart_StartProfiling(); - } else if (trace_state() == TRACE_STOPPING) { - for (auto& it : runner->active_applications_) { - fml::AutoResetWaitableEvent latch; - fml::TaskRunner::RunNowOrPostTask( - it.second.platform_thread->GetTaskRunner(), [&]() { - it.second.application->WriteProfileToTrace(); - latch.Signal(); - }); - latch.Wait(); - } - Dart_StopProfiling(); - trace_release_prolonged_context(runner->prolonged_context_); - } - }); - latch.Signal(); + Dart_StopProfiling(); + trace_release_prolonged_context(runner->prolonged_context_); + } }); - latch.Wait(); } #endif // !defined(DART_PRODUCT) diff --git a/engine/src/flutter/shell/platform/fuchsia/flutter/runner.h b/engine/src/flutter/shell/platform/fuchsia/flutter/runner.h index ccd7614cc1..0290275a66 100644 --- a/engine/src/flutter/shell/platform/fuchsia/flutter/runner.h +++ b/engine/src/flutter/shell/platform/fuchsia/flutter/runner.h @@ -16,8 +16,6 @@ #include "component.h" #include "flutter/fml/macros.h" -#include "fml/memory/ref_ptr.h" -#include "fml/task_runner.h" #include "lib/fidl/cpp/binding_set.h" #include "runtime/dart/utils/vmservice_object.h" @@ -27,14 +25,13 @@ namespace flutter_runner { // their own threads. class Runner final : public fuchsia::sys::Runner { public: - // Does not take ownership of context. - Runner(fml::RefPtr task_runner, - sys::ComponentContext* context); + // Does not take ownership of loop or context. + Runner(async::Loop* loop, sys::ComponentContext* context); ~Runner(); private: - fml::RefPtr task_runner_; + async::Loop* loop_; sys::ComponentContext* context_; fidl::BindingSet active_applications_bindings_; diff --git a/engine/src/flutter/fml/platform/fuchsia/task_observers.cc b/engine/src/flutter/shell/platform/fuchsia/flutter/task_observers.cc similarity index 87% rename from engine/src/flutter/fml/platform/fuchsia/task_observers.cc rename to engine/src/flutter/shell/platform/fuchsia/flutter/task_observers.cc index d3a9ea1ede..7a623684ff 100644 --- a/engine/src/flutter/fml/platform/fuchsia/task_observers.cc +++ b/engine/src/flutter/shell/platform/fuchsia/flutter/task_observers.cc @@ -2,11 +2,11 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "flutter/fml/platform/fuchsia/task_observers.h" +#include "task_observers.h" #include -namespace fml { +namespace flutter_runner { thread_local std::map tTaskObservers; @@ -27,4 +27,4 @@ void CurrentMessageLoopRemoveAfterTaskObserver(intptr_t key) { tTaskObservers.erase(key); } -} // namespace fml +} // namespace flutter_runner diff --git a/engine/src/flutter/shell/platform/fuchsia/flutter/task_observers.h b/engine/src/flutter/shell/platform/fuchsia/flutter/task_observers.h new file mode 100644 index 0000000000..5ee27d77e0 --- /dev/null +++ b/engine/src/flutter/shell/platform/fuchsia/flutter/task_observers.h @@ -0,0 +1,21 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef FLUTTER_SHELL_PLATFORM_FUCHSIA_TASK_OBSERVERS_H_ +#define FLUTTER_SHELL_PLATFORM_FUCHSIA_TASK_OBSERVERS_H_ + +#include + +namespace flutter_runner { + +void ExecuteAfterTaskObservers(); + +void CurrentMessageLoopAddAfterTaskObserver(intptr_t key, + fit::closure observer); + +void CurrentMessageLoopRemoveAfterTaskObserver(intptr_t key); + +} // namespace flutter_runner + +#endif // FLUTTER_SHELL_PLATFORM_FUCHSIA_TASK_OBSERVERS_H_ diff --git a/engine/src/flutter/shell/platform/fuchsia/flutter/thread.cc b/engine/src/flutter/shell/platform/fuchsia/flutter/thread.cc new file mode 100644 index 0000000000..c0c68e86e2 --- /dev/null +++ b/engine/src/flutter/shell/platform/fuchsia/flutter/thread.cc @@ -0,0 +1,102 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "thread.h" + +#include +#include + +#include +#include + +#include "flutter/fml/logging.h" +#include "loop.h" + +namespace flutter_runner { + +typedef void (*ThreadEntry)(Thread*); + +namespace { + +size_t NextPageSizeMultiple(size_t size) { + const size_t page_size = sysconf(_SC_PAGE_SIZE); + FML_CHECK(page_size != 0); + + size = std::max(size, page_size); + + size_t rem = size % page_size; + + if (rem == 0) { + return size; + } + + return size + page_size - rem; +} + +bool CreateThread(pthread_t* thread, + ThreadEntry main, + Thread* argument, + size_t stack_size) { + pthread_attr_t thread_attributes; + + if (pthread_attr_init(&thread_attributes) != 0) { + return false; + } + + stack_size = std::max(NextPageSizeMultiple(PTHREAD_STACK_MIN), + NextPageSizeMultiple(stack_size)); + + if (pthread_attr_setstacksize(&thread_attributes, stack_size) != 0) { + return false; + } + + auto result = + pthread_create(thread, &thread_attributes, + reinterpret_cast(main), argument); + + pthread_attr_destroy(&thread_attributes); + + return result == 0; +} + +} // anonymous namespace + +Thread::Thread() { + loop_.reset(MakeObservableLoop(false)); + valid_ = CreateThread( + &thread_, [](Thread* thread) { thread->Main(); }, this, 1 << 20); +} + +Thread::~Thread() { + Join(); +} + +bool Thread::IsValid() const { + return valid_; +} + +async_dispatcher_t* Thread::dispatcher() const { + return loop_->dispatcher(); +} + +void Thread::Main() { + async_set_default_dispatcher(loop_->dispatcher()); + loop_->Run(); +} + +void Thread::Quit() { + loop_->Quit(); +} + +bool Thread::Join() { + if (!valid_) { + return false; + } + + bool result = pthread_join(thread_, nullptr) == 0; + valid_ = false; + return result; +} + +} // namespace flutter_runner diff --git a/engine/src/flutter/shell/platform/fuchsia/flutter/thread.h b/engine/src/flutter/shell/platform/fuchsia/flutter/thread.h new file mode 100644 index 0000000000..ace85c3240 --- /dev/null +++ b/engine/src/flutter/shell/platform/fuchsia/flutter/thread.h @@ -0,0 +1,44 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef FLUTTER_SHELL_PLATFORM_FUCHSIA_THREAD_H_ +#define FLUTTER_SHELL_PLATFORM_FUCHSIA_THREAD_H_ + +#include +#include +#include + +#include + +#include "flutter/fml/macros.h" + +namespace flutter_runner { + +class Thread { + public: + Thread(); + + ~Thread(); + + void Quit(); + + bool Join(); + + bool IsValid() const; + + async_dispatcher_t* dispatcher() const; + + private: + bool valid_; + pthread_t thread_; + std::unique_ptr loop_; + + void Main(); + + FML_DISALLOW_COPY_AND_ASSIGN(Thread); +}; + +} // namespace flutter_runner + +#endif // FLUTTER_SHELL_PLATFORM_FUCHSIA_THREAD_H_