Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
module_init.h File Reference
#include <atomic>
Include dependency graph for module_init.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Typedefs

typedef std::atomic< StatusModuleInitState
 initialization state of a module (class, source file, etc.) must be initialized to zero (e.g. More...
 

Functions

Status ModuleInit (volatile ModuleInitState *initState, Status(*init)())
 calls a user-defined init function if initState is zero. More...
 
Status ModuleShutdown (volatile ModuleInitState *initState, void(*shutdown)())
 calls a user-defined shutdown function if initState is "initialized". More...
 

Typedef Documentation

◆ ModuleInitState

typedef std::atomic<Status> ModuleInitState

initialization state of a module (class, source file, etc.) must be initialized to zero (e.g.

by defining as a static variable). DO NOT change the value!

Function Documentation

◆ ModuleInit()

Status ModuleInit ( volatile ModuleInitState initState,
Status(*)()  init 
)

calls a user-defined init function if initState is zero.

Returns
INFO::SKIPPED if already initialized, a Status if the previous invocation failed, or the value returned by the callback.

postcondition: initState is "initialized" if the callback returned INFO::OK, otherwise its Status return value (which prevents shutdown from being called).

thread-safe: subsequent callers spin until the callback returns (this prevents using partially-initialized modules)

note that callbacks typically reference static data and thus do not require a function argument, but that can later be added if necessary.

◆ ModuleShutdown()

Status ModuleShutdown ( volatile ModuleInitState initState,
void(*)()  shutdown 
)

calls a user-defined shutdown function if initState is "initialized".

Returns
INFO::OK if shutdown occurred, INFO::SKIPPED if initState was zero (uninitialized), otherwise the Status returned by ModuleInit.

postcondition: initState remains set to the Status, or has been reset to zero to allow multiple init/shutdown pairs, e.g. in self-tests.

note: there is no provision for reference-counting because that turns out to be problematic (a user might call shutdown immediately after init; if this is the first use of the module, it will be shutdown prematurely, which is at least inefficient and possibly dangerous). instead, shutdown should only be called when cleanup is necessary (e.g. at exit before leak reporting) and it is certain that the module is no longer in use.