aboutsummaryrefslogtreecommitdiff
path: root/btcore/src/module.cc
diff options
context:
space:
mode:
Diffstat (limited to 'btcore/src/module.cc')
-rw-r--r--btcore/src/module.cc77
1 files changed, 64 insertions, 13 deletions
diff --git a/btcore/src/module.cc b/btcore/src/module.cc
index aba223422..d22600656 100644
--- a/btcore/src/module.cc
+++ b/btcore/src/module.cc
@@ -18,8 +18,6 @@
#define LOG_TAG "bt_core_module"
-#include "btcore/include/module.h"
-
#include <base/logging.h>
#include <dlfcn.h>
#include <string.h>
@@ -27,6 +25,7 @@
#include <mutex>
#include <unordered_map>
+#include "btcore/include/module.h"
#include "common/message_loop_thread.h"
#include "osi/include/allocator.h"
#include "osi/include/log.h"
@@ -51,7 +50,9 @@ static void set_module_state(const module_t* module, module_state_t state);
void module_management_start(void) {}
-void module_management_stop(void) { metadata.clear(); }
+void module_management_stop(void) {
+ metadata.clear();
+}
const module_t* get_module(const char* name) {
module_t* module = (module_t*)dlsym(RTLD_DEFAULT, name);
@@ -64,7 +65,8 @@ bool module_init(const module_t* module) {
CHECK(get_module_state(module) == MODULE_STATE_NONE);
if (!call_lifecycle_function(module->init)) {
- LOG_ERROR("%s Failed to initialize module \"%s\"", __func__, module->name);
+ LOG_ERROR(LOG_TAG, "%s Failed to initialize module \"%s\"", __func__,
+ module->name);
return false;
}
@@ -82,12 +84,13 @@ bool module_start_up(const module_t* module) {
CHECK(get_module_state(module) == MODULE_STATE_INITIALIZED ||
module->init == NULL);
- LOG_INFO("%s Starting module \"%s\"", __func__, module->name);
+ LOG_INFO(LOG_TAG, "%s Starting module \"%s\"", __func__, module->name);
if (!call_lifecycle_function(module->start_up)) {
- LOG_ERROR("%s Failed to start up module \"%s\"", __func__, module->name);
+ LOG_ERROR(LOG_TAG, "%s Failed to start up module \"%s\"", __func__,
+ module->name);
return false;
}
- LOG_INFO("%s Started module \"%s\"", __func__, module->name);
+ LOG_INFO(LOG_TAG, "%s Started module \"%s\"", __func__, module->name);
set_module_state(module, MODULE_STATE_STARTED);
return true;
@@ -101,12 +104,14 @@ void module_shut_down(const module_t* module) {
// Only something to do if the module was actually started
if (state < MODULE_STATE_STARTED) return;
- LOG_INFO("%s Shutting down module \"%s\"", __func__, module->name);
+ LOG_INFO(LOG_TAG, "%s Shutting down module \"%s\"", __func__, module->name);
if (!call_lifecycle_function(module->shut_down)) {
- LOG_ERROR("%s Failed to shutdown module \"%s\". Continuing anyway.",
+ LOG_ERROR(LOG_TAG,
+ "%s Failed to shutdown module \"%s\". Continuing anyway.",
__func__, module->name);
}
- LOG_INFO("%s Shutdown of module \"%s\" completed", __func__, module->name);
+ LOG_INFO(LOG_TAG, "%s Shutdown of module \"%s\" completed", __func__,
+ module->name);
set_module_state(module, MODULE_STATE_INITIALIZED);
}
@@ -119,12 +124,13 @@ void module_clean_up(const module_t* module) {
// Only something to do if the module was actually initialized
if (state < MODULE_STATE_INITIALIZED) return;
- LOG_INFO("%s Cleaning up module \"%s\"", __func__, module->name);
+ LOG_INFO(LOG_TAG, "%s Cleaning up module \"%s\"", __func__, module->name);
if (!call_lifecycle_function(module->clean_up)) {
- LOG_ERROR("%s Failed to cleanup module \"%s\". Continuing anyway.",
+ LOG_ERROR(LOG_TAG, "%s Failed to cleanup module \"%s\". Continuing anyway.",
__func__, module->name);
}
- LOG_INFO("%s Cleanup of module \"%s\" completed", __func__, module->name);
+ LOG_INFO(LOG_TAG, "%s Cleanup of module \"%s\" completed", __func__,
+ module->name);
set_module_state(module, MODULE_STATE_NONE);
}
@@ -153,3 +159,48 @@ static void set_module_state(const module_t* module, module_state_t state) {
std::lock_guard<std::mutex> lock(metadata_mutex);
metadata[module] = state;
}
+
+// TODO(zachoverflow): remove when everything modulized
+// Temporary callback-wrapper-related code
+class CallbackWrapper {
+ public:
+ explicit CallbackWrapper(const module_t* module,
+ MessageLoopThread* callback_thread,
+ thread_fn callback)
+ : module(module),
+ lifecycle_thread("bt_module_lifecycle_thread"),
+ callback_thread(callback_thread),
+ callback(callback),
+ success(false) {}
+ const module_t* module;
+ MessageLoopThread lifecycle_thread;
+ // we don't own this thread
+ MessageLoopThread* callback_thread;
+ thread_fn callback;
+ bool success;
+};
+
+static void post_result_to_callback(std::shared_ptr<CallbackWrapper> wrapper) {
+ CHECK(wrapper);
+ wrapper->lifecycle_thread.ShutDown();
+ wrapper->callback(wrapper->success ? FUTURE_SUCCESS : FUTURE_FAIL);
+}
+
+static void run_wrapped_start_up(std::shared_ptr<CallbackWrapper> wrapper) {
+ CHECK(wrapper);
+ wrapper->success = module_start_up(wrapper->module);
+ // Post the result back to the callback
+ wrapper->callback_thread->DoInThread(
+ FROM_HERE, base::BindOnce(post_result_to_callback, wrapper));
+}
+
+void module_start_up_callbacked_wrapper(const module_t* module,
+ MessageLoopThread* callback_thread,
+ thread_fn callback) {
+ std::shared_ptr<CallbackWrapper> wrapper =
+ std::make_shared<CallbackWrapper>(module, callback_thread, callback);
+ wrapper->lifecycle_thread.StartUp();
+ // Run the actual module start up
+ wrapper->lifecycle_thread.DoInThread(
+ FROM_HERE, base::BindOnce(run_wrapped_start_up, wrapper));
+}