Pyrogenesis  trunk
module_init.h
Go to the documentation of this file.
1 /* Copyright (c) 2010 Wildfire Games
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining
4  * a copy of this software and associated documentation files (the
5  * "Software"), to deal in the Software without restriction, including
6  * without limitation the rights to use, copy, modify, merge, publish,
7  * distribute, sublicense, and/or sell copies of the Software, and to
8  * permit persons to whom the Software is furnished to do so, subject to
9  * the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included
12  * in all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  */
22 
23 /*
24  * helpers for module initialization/shutdown.
25  */
26 
27 #ifndef INCLUDED_MODULE_INIT
28 #define INCLUDED_MODULE_INIT
29 
30 /**
31  * initialization state of a module (class, source file, etc.)
32  * must be initialized to zero (e.g. by defining as a static variable).
33  * DO NOT change the value!
34  **/
35 typedef intptr_t ModuleInitState; // intptr_t is required by cpu_CAS
36 
37 /**
38  * calls a user-defined init function if initState is zero.
39  *
40  * @return INFO::SKIPPED if already initialized, a Status if the
41  * previous invocation failed, or the value returned by the callback.
42  *
43  * postcondition: initState is "initialized" if the callback returned
44  * INFO::OK, otherwise its Status return value (which prevents
45  * shutdown from being called).
46  *
47  * thread-safe: subsequent callers spin until the callback returns
48  * (this prevents using partially-initialized modules)
49  *
50  * note that callbacks typically reference static data and thus do not
51  * require a function argument, but that can later be added if necessary.
52  **/
53 LIB_API Status ModuleInit(volatile ModuleInitState* initState, Status (*init)());
54 
55 /**
56  * calls a user-defined shutdown function if initState is "initialized".
57  *
58  * @return INFO::OK if shutdown occurred, INFO::SKIPPED if initState was
59  * zero (uninitialized), otherwise the Status returned by ModuleInit.
60  *
61  * postcondition: initState remains set to the Status, or has been
62  * reset to zero to allow multiple init/shutdown pairs, e.g. in self-tests.
63  *
64  * note: there is no provision for reference-counting because that
65  * turns out to be problematic (a user might call shutdown immediately
66  * after init; if this is the first use of the module, it will
67  * be shutdown prematurely, which is at least inefficient and
68  * possibly dangerous). instead, shutdown should only be called when
69  * cleanup is necessary (e.g. at exit before leak reporting) and
70  * it is certain that the module is no longer in use.
71  **/
72 LIB_API Status ModuleShutdown(volatile ModuleInitState* initState, void (*shutdown)());
73 
74 #endif // #ifndef INCLUDED_MODULE_INIT
LIB_API Status ModuleInit(volatile ModuleInitState *initState, Status(*init)())
calls a user-defined init function if initState is zero.
Definition: module_init.cpp:40
static ModuleInitState initState
Definition: h_mgr.cpp:742
LIB_API Status ModuleShutdown(volatile ModuleInitState *initState, void(*shutdown)())
calls a user-defined shutdown function if initState is "initialized".
Definition: module_init.cpp:65
intptr_t ModuleInitState
initialization state of a module (class, source file, etc.) must be initialized to zero (e...
Definition: module_init.h:35
i64 Status
Error handling system.
Definition: status.h:171
void init(ScriptInterface &scriptInterface)
Definition: JSInterface_GUITypes.cpp:260