summaryrefslogtreecommitdiff
path: root/cesar/lib/init.h
blob: ede9e837f57ccc3cff9318a8529a8ed1a38e3cef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#ifndef lib_init_h
#define lib_init_h
/* Cesar project {{{
 *
 * Copyright (C) 2009 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    lib/init.h
 * \brief   Init compiled lib modules.
 * \ingroup lib
 *
 * Permit to modules to register their init function in this init module
 * and particularly inside a specified init list.
 * The init programmer will be able to launch a specific list of init function.
 *
 * Practical example:
 *
 * Supposing you have a module called "bob" who is a part of the HAL list to
 * initialise and you may a bob_init function. Once your bob_init function
 * is defined, you have to put this line:
 *
 * INIT_REGISTER (INIT_LIST_HAL, bob)
 *
 * When you want to launch all initialise function of a specific list, you may
 * call init_exec () function with the list in argument. In this example:
 *
 * init_exec (INIT_LIST_HAL);
 *
 */

/** All lists. */
enum init_list_id_t
{
    INIT_LIST_HAL,
    INIT_LIST_HAL_SERVICES,
    INIT_LIST_SERVICES,
    INIT_LIST_NB
};
typedef enum init_list_id_t init_list_id_t;

/** List structure. */
struct init_list_t
{
    /** Next function to call. */
    struct init_list_t *next;
    /** Init function to call. */
    void (*init_function) (void);
};
typedef struct init_list_t init_list_t;

/**
 * INIT_REGISTER is a macro who need to be inserted in each module
 * in order to get registered inside the init system.
 */
#define INIT_REGISTER(list, module) \
  void module ## _init_register (void) __attribute__ ((constructor)); \
  void module ## _init_register (void) \
  { \
    static init_list_t i; \
    i.init_function = &module ## _init; \
    init_register (list, &i); \
  }

/**
 * Register a module's init to a specific list.
 * \param  list  list id, see init_enum for defined lists
 * \param  module  a list node containing a pointer to an init function
 *
 * This function may be called during the constructor function of
 * a module to be added to the list before the main function.
 */
void
init_register (int list, init_list_t *module);

/**
 * Execute all functions registered in a specific list.
 * \param  list  List ID, see init_enum for defined lists
 */
void
init_exec (int list);

#endif /* lib_init_h */