P99

◆ P99_INIT_FUNCTION_DECLARE

#define P99_INIT_FUNCTION_DECLARE (   FUNC,
  NR 
)

Request that FUNC is to be called at initialization time with priority NR.

Parameters
FUNCmust be compatible to a function pointer of type void (*)(void)
NRmust be chosen uniquely in the range 0 .. P99_MAX_NUMBER. It can be omitted, in which case a low valued number is provided.
Warning
To ensure that NR is unique the file "p99_init.h" must be included before any usage of P99_INIT_FUNCTION_DECLARE

The mechanism that ensures this feature is relatively fragile. You should in particular ensure that the compilation unit that contains p99_init_main (or main if you intercept main) see all such calls to P99_INIT_FUNCTION_DECLARE that are used by the program.

This works by initializing a bunch of static const variables with function and data pointers. The order in which these then are processed depends on the file inclusion order. So if you do not use the NR you should not rely on a particular order. If you need initialization dependencies between "modules" (= compilation units) you should use

// ***** file module1.h
// always include this immediately before, even several times
#include "p99_init.h"
P99_INIT_FUNCTION_DECLARE(module1_trigger);
// ***** file module2.c
// here initialize some global variables e.g
}
// always include this immediately before, even several times
#include "p99_init.h"
void module1_trigger(void) {
P99_INIT_CHAIN(module1);
}
// ***** file module2.h
// always include this immediately before, even several times
#include "p99_init.h"
P99_INIT_FUNCTION_DECLARE(module2_trigger);
// ***** file module2.c
// module2 depends upon module1 being properly initialized
P99_DEFINE_ONCE_CHAIN(module2, module1) {
// here initialize some global variables e.g
}
// always include this immediately before, even several times
#include "p99_init.h"
void module2_trigger(void) {
P99_INIT_CHAIN(module2);
}
See also
P99_INIT_VARIABLE for a similar mechanism with that receive a pointer argument and are compatible with void (*)(void*).

Definition at line 143 of file p99_init.h.

p99_init.h
Implement initialization functions that are executed early.
P99_INIT_FUNCTION_DECLARE
#define P99_INIT_FUNCTION_DECLARE(FUNC, NR)
Request that FUNC is to be called at initialization time with priority NR.
Definition: p99_init.h:143
P99_DECLARE_ONCE_CHAIN
#define P99_DECLARE_ONCE_CHAIN(T)
Declare the symbols that are needed for the macro P99_INIT_CHAIN().
Definition: p99_threads.h:276
P99_INIT_CHAIN
#define P99_INIT_CHAIN(T)
Ensure that the function that was defined with P99_DEFINE_ONCE_CHAIN has been called exactly once bef...
Definition: p99_threads.h:291
P99_DEFINE_ONCE_CHAIN
#define P99_DEFINE_ONCE_CHAIN(T,...)
Define a function that will be called exactly once by P99_INIT_CHAIN(T).
Definition: p99_threads.h:237