#include <stdbool.h>
#include "future.h"
+#include "thread.h"
typedef future_t *(*module_lifecycle_fn)(void);
// If not initialized, does nothing.
void module_clean_up(const module_t *module);
+// Temporary callbacked wrapper for module start up, so real modules can be
+// spliced into the current janky startup sequence. Runs on a separate thread,
+// which terminates when the module start up has finished. When module startup
+// has finished, |callback| is called within the context of |callback_thread|
+// with |FUTURE_SUCCESS| or |FUTURE_FAIL| depending on whether startup succeeded
+// or not.
+void module_start_up_callbacked_wrapper(
+ const module_t *module,
+ thread_t *callback_thread,
+ thread_fn callback
+);
*state_ptr = state;
}
+
+// TODO(zachoverflow): remove when everything modulized
+// Temporary callback-wrapper-related code
+
+typedef struct {
+ const module_t *module;
+ thread_t *lifecycle_thread;
+ thread_t *callback_thread; // we don't own this thread
+ thread_fn callback;
+ bool success;
+} callbacked_wrapper_t;
+
+static void run_wrapped_start_up(void *context);
+static void post_result_to_callback(void *context);
+
+void module_start_up_callbacked_wrapper(
+ const module_t *module,
+ thread_t *callback_thread,
+ thread_fn callback) {
+ callbacked_wrapper_t *wrapper = osi_calloc(sizeof(callbacked_wrapper_t));
+
+ wrapper->module = module;
+ wrapper->lifecycle_thread = thread_new("module_wrapper");
+ wrapper->callback_thread = callback_thread;
+ wrapper->callback = callback;
+
+ // Run the actual module start up
+ thread_post(wrapper->lifecycle_thread, run_wrapped_start_up, wrapper);
+}
+
+static void run_wrapped_start_up(void *context) {
+ assert(context);
+
+ callbacked_wrapper_t *wrapper = context;
+ wrapper->success = module_start_up(wrapper->module);
+
+ // Post the result back to the callback
+ thread_post(wrapper->callback_thread, post_result_to_callback, wrapper);
+}
+
+static void post_result_to_callback(void *context) {
+ assert(context);
+
+ callbacked_wrapper_t *wrapper = context;
+
+ // Save the values we need for callback
+ void *result = wrapper->success ? FUTURE_SUCCESS : FUTURE_FAIL;
+ thread_fn callback = wrapper->callback;
+
+ // Clean up the resources we used
+ thread_stop(wrapper->lifecycle_thread);
+ osi_free(wrapper);
+
+ callback(result);
+}