1 /******************************************************************************
2  *
3  *  Copyright (C) 2014 Google, Inc.
4  *
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:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  ******************************************************************************/
18 
19 #pragma once
20 
21 #include <stdbool.h>
22 
23 #include "osi/include/future.h"
24 #include "osi/include/thread.h"
25 
26 typedef future_t *(*module_lifecycle_fn)(void);
27 
28 typedef struct {
29   const char *name;
30   module_lifecycle_fn init;
31   module_lifecycle_fn start_up;
32   module_lifecycle_fn shut_down;
33   module_lifecycle_fn clean_up;
34   const char *dependencies[];
35 } module_t;
36 
37 // Prepares module management. Must be called before doing anything with modules.
38 void module_management_start(void);
39 // Cleans up all module management resources.
40 void module_management_stop(void);
41 
42 const module_t *get_module(const char *name);
43 
44 // Initialize the provided module. |module| may not be NULL
45 // and must not be initialized.
46 bool module_init(const module_t *module);
47 // Start up the provided module. |module| may not be NULL
48 // and must be initialized or have no init function.
49 bool module_start_up(const module_t *module);
50 // Shut down the provided module. |module| may not be NULL.
51 // If not started, does nothing.
52 void module_shut_down(const module_t *module);
53 // Clean up the provided module. |module| may not be NULL.
54 // If not initialized, does nothing.
55 void module_clean_up(const module_t *module);
56 
57 // Temporary callbacked wrapper for module start up, so real modules can be
58 // spliced into the current janky startup sequence. Runs on a separate thread,
59 // which terminates when the module start up has finished. When module startup
60 // has finished, |callback| is called within the context of |callback_thread|
61 // with |FUTURE_SUCCESS| or |FUTURE_FAIL| depending on whether startup succeeded
62 // or not.
63 void module_start_up_callbacked_wrapper(
64   const module_t *module,
65   thread_t *callback_thread,
66   thread_fn callback
67 );
68