Create handlers for every module upon module start.

Test: atest --host bluetooth_test_gd and ./cert/run_cert.sh
Change-Id: I360556ef3bb2ccf34d5658f00b30cc33949a77c1
diff --git a/system/gd/facade/stack_with_grpc_main.cc b/system/gd/facade/stack_with_grpc_main.cc
index c798a71..54ca5e4 100644
--- a/system/gd/facade/stack_with_grpc_main.cc
+++ b/system/gd/facade/stack_with_grpc_main.cc
@@ -28,6 +28,7 @@
 using ::bluetooth::StackManager;
 using ::bluetooth::grpc::GrpcModule;
 using ::bluetooth::ModuleList;
+using ::bluetooth::os::Thread;
 
 namespace {
 static StackManager* stack;
@@ -59,8 +60,9 @@
   ModuleList modules;
   modules.add<::bluetooth::hal::facade::HalFacadeModule>();
 
+  Thread* stack_thread = new Thread("stack_thread", Thread::Priority::NORMAL);
   stack = new StackManager();
-  stack->StartUp(&modules);
+  stack->StartUp(&modules, stack_thread);
 
   GrpcModule* grpc_module = stack->GetInstance<GrpcModule>();
   grpc_module->StartServer("0.0.0.0", port);
diff --git a/system/gd/hal/hci_hal_android_hidl_test.cc b/system/gd/hal/hci_hal_android_hidl_test.cc
index 0a302ed..5b8b68d 100644
--- a/system/gd/hal/hci_hal_android_hidl_test.cc
+++ b/system/gd/hal/hci_hal_android_hidl_test.cc
@@ -21,6 +21,10 @@
 
 #include <gtest/gtest.h>
 
+#include "os/thread.h"
+
+using ::bluetooth::os::Thread;
+
 namespace bluetooth {
 namespace hal {
 namespace {
@@ -28,16 +32,19 @@
 class HciHalHidlTest : public ::testing::Test {
  protected:
   void SetUp() override {
+    thread_ = new Thread("test_thread", Thread::Priority::NORMAL);
   }
 
   void TearDown() override {
+    delete thread_;
   }
 
   ModuleRegistry fake_registry_;
+  Thread* thread_;
 };
 
 TEST_F(HciHalHidlTest, init_and_close) {
-  fake_registry_.Start<HciHal>();
+  fake_registry_.Start<HciHal>(thread_);
   fake_registry_.StopAll();
 }
 }  // namespace
diff --git a/system/gd/hal/hci_hal_host_rootcanal_test.cc b/system/gd/hal/hci_hal_host_rootcanal_test.cc
index bb4649e..27a20a0 100644
--- a/system/gd/hal/hci_hal_host_rootcanal_test.cc
+++ b/system/gd/hal/hci_hal_host_rootcanal_test.cc
@@ -33,8 +33,11 @@
 #include <gtest/gtest.h>
 
 #include "os/log.h"
+#include "os/thread.h"
 #include "os/utils.h"
 
+using ::bluetooth::os::Thread;
+
 namespace bluetooth {
 namespace hal {
 namespace {
@@ -132,9 +135,11 @@
 class HciHalRootcanalTest : public ::testing::Test {
  protected:
   void SetUp() override {
+    thread_ = new Thread("test_thread", Thread::Priority::NORMAL);
+
     HciHalHostRootcanalConfig::Get()->SetPort(kTestPort);
     fake_server_ = new FakeRootcanalDesktopHciServer;
-    fake_registry_.Start<HciHal>();
+    fake_registry_.Start<HciHal>(thread_);
     hal_ = fake_registry_.GetInstance<HciHal>();
     hal_->registerIncomingPacketCallback(&callbacks_);
     fake_server_socket_ = fake_server_->Accept();  // accept() after client is connected to avoid blocking
@@ -146,6 +151,7 @@
     fake_registry_.StopAll();
     close(fake_server_socket_);
     delete fake_server_;
+    delete thread_;
   }
 
   void SetFakeServerSocketToBlocking() {
@@ -159,6 +165,7 @@
   ModuleRegistry fake_registry_;
   TestHciHalCallbacks callbacks_;
   int fake_server_socket_ = -1;
+  Thread* thread_;
 };
 
 void check_packet_equal(std::pair<uint8_t, HciPacket> hci_packet1_type_data_pair, H4Packet h4_packet2) {
diff --git a/system/gd/module.cc b/system/gd/module.cc
index cf8b7e9..c984283 100644
--- a/system/gd/module.cc
+++ b/system/gd/module.cc
@@ -16,30 +16,39 @@
 
 #include "module.h"
 
+using ::bluetooth::os::Handler;
+using ::bluetooth::os::Thread;
+
 namespace bluetooth {
 
 ModuleFactory::ModuleFactory(std::function<Module*()> ctor) : ctor_(ctor) {
 }
 
+Handler* Module::GetHandler() {
+  return handler_;
+}
+
 bool ModuleRegistry::IsStarted(const ModuleFactory* factory) const {
   return started_modules_.find(factory) != started_modules_.end();
 }
 
-void ModuleRegistry::Start(ModuleList* modules) {
+void ModuleRegistry::Start(ModuleList* modules, Thread* thread) {
   for (auto it = modules->list_.begin(); it != modules->list_.end(); it++) {
-    Start(*it);
+    Start(*it, thread);
   }
 }
 
-void ModuleRegistry::Start(const ModuleFactory* module) {
+void ModuleRegistry::Start(const ModuleFactory* module, Thread* thread) {
   if (IsStarted(module)) {
     return;
   }
 
   Module* instance = module->ctor_();
+  instance->handler_ = new Handler(thread);
+
   ModuleList dependencies;
   instance->ListDependencies(&dependencies);
-  Start(&dependencies);
+  Start(&dependencies, thread);
 
   instance->Start(this);
   start_order_.push_back(module);
@@ -54,6 +63,7 @@
     ASSERT(instance != started_modules_.end());
     instance->second->Stop(this);
 
+    delete instance->second->handler_;
     delete instance->second;
     started_modules_.erase(instance);
   }
diff --git a/system/gd/module.h b/system/gd/module.h
index a073649..fc3292d 100644
--- a/system/gd/module.h
+++ b/system/gd/module.h
@@ -21,6 +21,8 @@
 #include <map>
 
 #include "os/log.h"
+#include "os/handler.h"
+#include "os/thread.h"
 
 namespace bluetooth {
 
@@ -53,7 +55,7 @@
 // static const ModuleFactory Factory;
 //
 // which will provide a constructor for the module registry to call.
-// The module registry will also use the Factory as the identifier
+// The module registry will also use the factory as the identifier
 // for that module.
 class Module {
  friend ModuleRegistry;
@@ -68,6 +70,11 @@
 
   // Release all resources, you're about to be deleted
   virtual void Stop(const ModuleRegistry* registry) = 0;
+
+  ::bluetooth::os::Handler* GetHandler();
+
+ private:
+  ::bluetooth::os::Handler* handler_;
 };
 
 class ModuleRegistry {
@@ -88,14 +95,14 @@
 
   // Start all the modules on this list and their dependencies
   // in dependency order
-  void Start(ModuleList* modules);
+  void Start(ModuleList* modules, ::bluetooth::os::Thread* thread);
 
   template <class T>
-  void Start() {
-    Start(&T::Factory);
+  void Start(::bluetooth::os::Thread* thread) {
+    Start(&T::Factory, thread);
   }
 
-  void Start(const ModuleFactory* id);
+  void Start(const ModuleFactory* id, ::bluetooth::os::Thread* thread);
 
   // Stop all running modules in reverse order of start
   void StopAll();
diff --git a/system/gd/module_unittest.cc b/system/gd/module_unittest.cc
index 62e8966..6c6d68d 100644
--- a/system/gd/module_unittest.cc
+++ b/system/gd/module_unittest.cc
@@ -18,20 +18,25 @@
 
 #include "gtest/gtest.h"
 
+using ::bluetooth::os::Thread;
+
 namespace bluetooth {
 namespace {
 
 class ModuleTest : public ::testing::Test {
  protected:
   void SetUp() override {
+    thread_ = new Thread("test_thread", Thread::Priority::NORMAL);
     registry_ = new ModuleRegistry();
   }
 
   void TearDown() override {
     delete registry_;
+    delete thread_;
   }
 
   ModuleRegistry* registry_;
+  Thread* thread_;
 };
 
 class TestModuleNoDependency : public Module {
@@ -143,7 +148,7 @@
 TEST_F(ModuleTest, no_dependency) {
   ModuleList list;
   list.add<TestModuleNoDependency>();
-  registry_->Start(&list);
+  registry_->Start(&list, thread_);
 
   EXPECT_TRUE(registry_->IsStarted<TestModuleNoDependency>());
   EXPECT_FALSE(registry_->IsStarted<TestModuleOneDependency>());
@@ -161,7 +166,7 @@
 TEST_F(ModuleTest, one_dependency) {
   ModuleList list;
   list.add<TestModuleOneDependency>();
-  registry_->Start(&list);
+  registry_->Start(&list, thread_);
 
   EXPECT_TRUE(registry_->IsStarted<TestModuleNoDependency>());
   EXPECT_TRUE(registry_->IsStarted<TestModuleOneDependency>());
@@ -179,7 +184,7 @@
 TEST_F(ModuleTest, two_dependencies) {
   ModuleList list;
   list.add<TestModuleTwoDependencies>();
-  registry_->Start(&list);
+  registry_->Start(&list, thread_);
 
   EXPECT_TRUE(registry_->IsStarted<TestModuleNoDependency>());
   EXPECT_TRUE(registry_->IsStarted<TestModuleOneDependency>());
diff --git a/system/gd/stack_manager.cc b/system/gd/stack_manager.cc
index e3749ac..ad83674 100644
--- a/system/gd/stack_manager.cc
+++ b/system/gd/stack_manager.cc
@@ -31,13 +31,13 @@
 
 namespace bluetooth {
 
-void StackManager::StartUp(ModuleList* modules) {
+void StackManager::StartUp(ModuleList* modules, Thread* stack_thread) {
   management_thread_ = new Thread("management_thread", Thread::Priority::NORMAL);
   handler_ = new Handler(management_thread_);
 
   std::promise<void>* promise = new std::promise<void>();
-  handler_->Post([this, promise, modules]() {
-    registry_.Start(modules);
+  handler_->Post([this, promise, modules, stack_thread]() {
+    registry_.Start(modules, stack_thread);
     promise->set_value();
   });
 
diff --git a/system/gd/stack_manager.h b/system/gd/stack_manager.h
index b9fb985..4d97479 100644
--- a/system/gd/stack_manager.h
+++ b/system/gd/stack_manager.h
@@ -24,7 +24,7 @@
 
 class StackManager {
  public:
-  void StartUp(ModuleList *modules);
+  void StartUp(ModuleList *modules, os::Thread* stack_thread);
   void ShutDown();
 
   template <class T>