support exit callback registration
[protos/libecoli.git] / include / ecoli_init.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2016, Olivier MATZ <zer0@droids-corp.org>
3  */
4
5 /**
6  * Register initialization routines.
7  */
8
9 #ifndef ECOLI_INIT_
10 #define ECOLI_INIT_
11
12 #include <sys/queue.h>
13
14 #include <ecoli_log.h>
15 #include <ecoli_node.h>
16
17 /**
18  * Register initialization and exit callbacks. These callbacks are
19  * ordered by priority: for initialization, the lowest priority is called
20  * first. For exit, the callbacks are invoked in reverse order.
21  */
22 #define EC_INIT_REGISTER(t)                                             \
23         static void ec_init_init_##t(void);                             \
24         static void __attribute__((constructor, used))                  \
25         ec_init_init_##t(void)                                          \
26         {                                                               \
27                  ec_init_register(&t);                                  \
28         }
29
30 /**
31  * Type of init function. Return 0 on success, -1 on error.
32  */
33 typedef int (ec_init_t)(void);
34
35 /**
36  * Type of exit function.
37  */
38 typedef void (ec_exit_t)(void);
39
40 TAILQ_HEAD(ec_init_list, ec_init);
41
42 /**
43  * A structure describing a test case.
44  */
45 struct ec_init {
46         TAILQ_ENTRY(ec_init) next;  /**< Next in list. */
47         ec_init_t *init;            /**< Init function. */
48         ec_exit_t *exit;            /**< Exit function. */
49         unsigned int priority;      /**< Priority (0=first, 99=last) */
50 };
51
52 /**
53  * Register an initialization function.
54  *
55  * @param init
56  *   A pointer to a ec_init structure to be registered.
57  */
58 void ec_init_register(struct ec_init *test);
59
60 /**
61  * Initialize ecoli library.
62  *
63  * Must be called before any other function from libecoli, except
64  * ec_malloc_register().
65  *
66  * @return
67  *   0 on success, -1 on error (errno is set).
68  */
69 int ec_init(void);
70
71 /**
72  * Uninitialize ecoli library.
73  */
74 void ec_exit(void);
75
76 #endif