[flutter_runner] Use a Thread for the platform thread

Change-Id: I41ce401967cf98f24b3cd443a081b82a8e90c83a
diff --git a/runtime/flutter_runner/component.cc b/runtime/flutter_runner/component.cc
index ecb083a..97e0940 100644
--- a/runtime/flutter_runner/component.cc
+++ b/runtime/flutter_runner/component.cc
@@ -28,9 +28,9 @@
 #include "topaz/runtime/dart/utils/tempfs.h"
 #include "topaz/runtime/dart/utils/vmo.h"
 
-#include "loop.h"
 #include "service_provider_dir.h"
 #include "task_observers.h"
+#include "thread.h"
 
 namespace flutter_runner {
 
@@ -38,18 +38,17 @@
 constexpr char kTmpPath[] = "/tmp";
 constexpr char kServiceRootPath[] = "/svc";
 
-std::pair<std::unique_ptr<async::Loop>, std::unique_ptr<Application>>
+std::pair<std::unique_ptr<Thread>, std::unique_ptr<Application>>
 Application::Create(
     TerminationCallback termination_callback, fuchsia::sys::Package package,
     fuchsia::sys::StartupInfo startup_info,
     std::shared_ptr<sys::ServiceDirectory> runner_incoming_services,
     fidl::InterfaceRequest<fuchsia::sys::ComponentController> controller) {
-  std::unique_ptr<async::Loop> loop(MakeObservableLoop(false));
-  loop->StartThread();
+  std::unique_ptr<Thread> thread = std::make_unique<Thread>();
   std::unique_ptr<Application> application;
 
   fml::AutoResetWaitableEvent latch;
-  async::PostTask(loop->dispatcher(), [&]() mutable {
+  async::PostTask(thread->dispatcher(), [&]() mutable {
     application.reset(
         new Application(std::move(termination_callback), std::move(package),
                         std::move(startup_info), runner_incoming_services,
@@ -58,7 +57,7 @@
   });
 
   latch.Wait();
-  return {std::move(loop), std::move(application)};
+  return {std::move(thread), std::move(application)};
 }
 
 static std::string DebugLabelForURL(const std::string& url) {
@@ -309,6 +308,7 @@
 #endif  // defined(__aarch64__)
 
   auto dispatcher = async_get_default_dispatcher();
+  FML_CHECK(dispatcher);
   const std::string component_url = package.resolved_url;
   settings_.unhandled_exception_callback =
       [dispatcher, runner_incoming_services, component_url](
diff --git a/runtime/flutter_runner/component.h b/runtime/flutter_runner/component.h
index db0fd43..ca65ae4 100644
--- a/runtime/flutter_runner/component.h
+++ b/runtime/flutter_runner/component.h
@@ -25,6 +25,7 @@
 #include "flutter/common/settings.h"
 #include "flutter/fml/macros.h"
 
+#include "thread.h"
 #include "unique_fdio_ns.h"
 
 namespace flutter_runner {
@@ -40,7 +41,7 @@
   // Creates a dedicated thread to run the application and constructions the
   // application on it. The application can be accessed only on this thread.
   // This is a synchronous operation.
-  static std::pair<std::unique_ptr<async::Loop>, std::unique_ptr<Application>>
+  static std::pair<std::unique_ptr<Thread>, std::unique_ptr<Application>>
   Create(TerminationCallback termination_callback,
          fuchsia::sys::Package package, fuchsia::sys::StartupInfo startup_info,
          std::shared_ptr<sys::ServiceDirectory> runner_incoming_services,
diff --git a/runtime/flutter_runner/runner.cc b/runtime/flutter_runner/runner.cc
index a376a2f..ee2810c 100644
--- a/runtime/flutter_runner/runner.cc
+++ b/runtime/flutter_runner/runner.cc
@@ -146,7 +146,7 @@
         });
       };
 
-  auto loop_application_pair = Application::Create(
+  auto thread_application_pair = Application::Create(
       std::move(termination_callback),  // termination callback
       std::move(package),               // application pacakge
       std::move(startup_info),          // startup info
@@ -154,9 +154,9 @@
       std::move(controller)             // controller request
   );
 
-  auto key = loop_application_pair.second.get();
+  auto key = thread_application_pair.second.get();
 
-  active_applications_[key] = std::move(loop_application_pair);
+  active_applications_[key] = std::move(thread_application_pair);
 }
 
 void Runner::OnApplicationTerminate(const Application* application) {
@@ -173,22 +173,22 @@
   // 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_loop = std::move(active_application.loop);
+  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.
   async::PostTask(
-      application_loop->dispatcher(),
+      application_thread->dispatcher(),
       fml::MakeCopyable([instance = std::move(application_to_destroy),
-                         loop = application_loop.get()]() mutable {
+                         thread = application_thread.get()]() mutable {
         instance.reset();
-        loop->Quit();
+        thread->Quit();
       }));
 
   // This works because just posted the quit task on the hosted thread.
-  application_loop->JoinThreads();
+  application_thread->Join();
 }
 
 void Runner::SetupICU() {
@@ -210,7 +210,7 @@
     } else if (trace_state() == TRACE_STOPPING) {
       for (auto& it : runner->active_applications_) {
         fml::AutoResetWaitableEvent latch;
-        async::PostTask(it.second.loop->dispatcher(), [&]() {
+        async::PostTask(it.second.thread->dispatcher(), [&]() {
           it.second.application->WriteProfileToTrace();
           latch.Signal();
         });
diff --git a/runtime/flutter_runner/runner.h b/runtime/flutter_runner/runner.h
index 6ab3784..5176873 100644
--- a/runtime/flutter_runner/runner.h
+++ b/runtime/flutter_runner/runner.h
@@ -17,6 +17,7 @@
 #include "component.h"
 #include "flutter/fml/macros.h"
 #include "lib/fidl/cpp/binding_set.h"
+#include "thread.h"
 #include "topaz/runtime/dart/utils/vmservice_object.h"
 
 namespace flutter_runner {
@@ -33,13 +34,13 @@
   async::Loop* loop_;
 
   struct ActiveApplication {
-    std::unique_ptr<async::Loop> loop;
+    std::unique_ptr<Thread> thread;
     std::unique_ptr<Application> application;
 
-    ActiveApplication(std::pair<std::unique_ptr<async::Loop>,
+    ActiveApplication(std::pair<std::unique_ptr<Thread>,
                                 std::unique_ptr<Application>>
                           pair)
-        : loop(std::move(pair.first)), application(std::move(pair.second)) {}
+        : thread(std::move(pair.first)), application(std::move(pair.second)) {}
 
     ActiveApplication() = default;
   };