1 /******************************************************************************
3 * Copyright (C) 2014 Google, Inc.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 ******************************************************************************/
19 #define LOG_TAG "bt_core_module"
26 #include "btcore/include/module.h"
27 #include "osi/include/allocator.h"
28 #include "osi/include/hash_functions.h"
29 #include "osi/include/hash_map.h"
30 #include "osi/include/log.h"
31 #include "osi/include/osi.h"
34 MODULE_STATE_NONE = 0,
35 MODULE_STATE_INITIALIZED = 1,
36 MODULE_STATE_STARTED = 2
39 static const size_t number_of_metadata_buckets = 42;
40 static hash_map_t *metadata;
41 // Include this lock for now for correctness, while the startup sequence is being refactored
42 static pthread_mutex_t metadata_lock;
44 static bool call_lifecycle_function(module_lifecycle_fn function);
45 static module_state_t get_module_state(const module_t *module);
46 static void set_module_state(const module_t *module, module_state_t state);
48 void module_management_start(void) {
49 metadata = hash_map_new(
50 number_of_metadata_buckets,
51 hash_function_pointer,
57 pthread_mutex_init(&metadata_lock, NULL);
60 void module_management_stop(void) {
64 hash_map_free(metadata);
67 pthread_mutex_destroy(&metadata_lock);
70 const module_t *get_module(const char *name) {
71 module_t* module = (module_t *)dlsym(RTLD_DEFAULT, name);
76 bool module_init(const module_t *module) {
77 assert(metadata != NULL);
78 assert(module != NULL);
79 assert(get_module_state(module) == MODULE_STATE_NONE);
81 LOG_INFO(LOG_TAG, "%s Initializing module \"%s\"", __func__, module->name);
82 if (!call_lifecycle_function(module->init)) {
83 LOG_ERROR(LOG_TAG, "%s Failed to initialize module \"%s\"",
84 __func__, module->name);
87 LOG_INFO(LOG_TAG, "%s Initialized module \"%s\"", __func__, module->name);
89 set_module_state(module, MODULE_STATE_INITIALIZED);
93 bool module_start_up(const module_t *module) {
94 assert(metadata != NULL);
95 assert(module != NULL);
96 // TODO(zachoverflow): remove module->init check once automagic order/call is in place.
97 // This hack is here so modules which don't require init don't have to have useless calls
98 // as we're converting the startup sequence.
99 assert(get_module_state(module) == MODULE_STATE_INITIALIZED || module->init == NULL);
101 LOG_INFO(LOG_TAG, "%s Starting module \"%s\"", __func__, module->name);
102 if (!call_lifecycle_function(module->start_up)) {
103 LOG_ERROR(LOG_TAG, "%s Failed to start up module \"%s\"",
104 __func__, module->name);
107 LOG_INFO(LOG_TAG, "%s Started module \"%s\"", __func__, module->name);
109 set_module_state(module, MODULE_STATE_STARTED);
113 void module_shut_down(const module_t *module) {
114 assert(metadata != NULL);
115 assert(module != NULL);
116 module_state_t state = get_module_state(module);
117 assert(state <= MODULE_STATE_STARTED);
119 // Only something to do if the module was actually started
120 if (state < MODULE_STATE_STARTED)
123 LOG_INFO(LOG_TAG, "%s Shutting down module \"%s\"", __func__, module->name);
124 if (!call_lifecycle_function(module->shut_down)) {
125 LOG_ERROR(LOG_TAG, "%s Failed to shutdown module \"%s\". Continuing anyway.",
126 __func__, module->name);
128 LOG_INFO(LOG_TAG, "%s Shutdown of module \"%s\" completed",
129 __func__, module->name);
131 set_module_state(module, MODULE_STATE_INITIALIZED);
134 void module_clean_up(const module_t *module) {
135 assert(metadata != NULL);
136 assert(module != NULL);
137 module_state_t state = get_module_state(module);
138 assert(state <= MODULE_STATE_INITIALIZED);
140 // Only something to do if the module was actually initialized
141 if (state < MODULE_STATE_INITIALIZED)
144 LOG_INFO(LOG_TAG, "%s Cleaning up module \"%s\"", __func__, module->name);
145 if (!call_lifecycle_function(module->clean_up)) {
146 LOG_ERROR(LOG_TAG, "%s Failed to cleanup module \"%s\". Continuing anyway.",
147 __func__, module->name);
149 LOG_INFO(LOG_TAG, "%s Cleanup of module \"%s\" completed",
150 __func__, module->name);
152 set_module_state(module, MODULE_STATE_NONE);
155 static bool call_lifecycle_function(module_lifecycle_fn function) {
156 // A NULL lifecycle function means it isn't needed, so assume success
160 future_t *future = function();
162 // A NULL future means synchronous success
166 // Otherwise fall back to the future
167 return future_await(future);
170 static module_state_t get_module_state(const module_t *module) {
171 pthread_mutex_lock(&metadata_lock);
172 module_state_t *state_ptr = hash_map_get(metadata, module);
173 pthread_mutex_unlock(&metadata_lock);
175 return state_ptr ? *state_ptr : MODULE_STATE_NONE;
178 static void set_module_state(const module_t *module, module_state_t state) {
179 pthread_mutex_lock(&metadata_lock);
181 module_state_t *state_ptr = hash_map_get(metadata, module);
183 state_ptr = osi_malloc(sizeof(module_state_t));
184 hash_map_set(metadata, module, state_ptr);
187 pthread_mutex_unlock(&metadata_lock);
192 // TODO(zachoverflow): remove when everything modulized
193 // Temporary callback-wrapper-related code
196 const module_t *module;
197 thread_t *lifecycle_thread;
198 thread_t *callback_thread; // we don't own this thread
201 } callbacked_wrapper_t;
203 static void run_wrapped_start_up(void *context);
204 static void post_result_to_callback(void *context);
206 void module_start_up_callbacked_wrapper(
207 const module_t *module,
208 thread_t *callback_thread,
209 thread_fn callback) {
210 callbacked_wrapper_t *wrapper = osi_calloc(sizeof(callbacked_wrapper_t));
212 wrapper->module = module;
213 wrapper->lifecycle_thread = thread_new("module_wrapper");
214 wrapper->callback_thread = callback_thread;
215 wrapper->callback = callback;
217 // Run the actual module start up
218 thread_post(wrapper->lifecycle_thread, run_wrapped_start_up, wrapper);
221 static void run_wrapped_start_up(void *context) {
224 callbacked_wrapper_t *wrapper = context;
225 wrapper->success = module_start_up(wrapper->module);
227 // Post the result back to the callback
228 thread_post(wrapper->callback_thread, post_result_to_callback, wrapper);
231 static void post_result_to_callback(void *context) {
234 callbacked_wrapper_t *wrapper = context;
236 // Save the values we need for callback
237 void *result = wrapper->success ? FUTURE_SUCCESS : FUTURE_FAIL;
238 thread_fn callback = wrapper->callback;
240 // Clean up the resources we used
241 thread_free(wrapper->lifecycle_thread);