Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
module_init.h
Go to the documentation of this file.
1/* Copyright (C) 2024 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#include <atomic>
31
32/**
33 * initialization state of a module (class, source file, etc.)
34 * must be initialized to zero (e.g. by defining as a static variable).
35 * DO NOT change the value!
36 **/
37typedef std::atomic<Status> ModuleInitState;
38
39/**
40 * calls a user-defined init function if initState is zero.
41 *
42 * @return INFO::SKIPPED if already initialized, a Status if the
43 * previous invocation failed, or the value returned by the callback.
44 *
45 * postcondition: initState is "initialized" if the callback returned
46 * INFO::OK, otherwise its Status return value (which prevents
47 * shutdown from being called).
48 *
49 * thread-safe: subsequent callers spin until the callback returns
50 * (this prevents using partially-initialized modules)
51 *
52 * note that callbacks typically reference static data and thus do not
53 * require a function argument, but that can later be added if necessary.
54 **/
56
57/**
58 * calls a user-defined shutdown function if initState is "initialized".
59 *
60 * @return INFO::OK if shutdown occurred, INFO::SKIPPED if initState was
61 * zero (uninitialized), otherwise the Status returned by ModuleInit.
62 *
63 * postcondition: initState remains set to the Status, or has been
64 * reset to zero to allow multiple init/shutdown pairs, e.g. in self-tests.
65 *
66 * note: there is no provision for reference-counting because that
67 * turns out to be problematic (a user might call shutdown immediately
68 * after init; if this is the first use of the module, it will
69 * be shutdown prematurely, which is at least inefficient and
70 * possibly dangerous). instead, shutdown should only be called when
71 * cleanup is necessary (e.g. at exit before leak reporting) and
72 * it is certain that the module is no longer in use.
73 **/
74Status ModuleShutdown(volatile ModuleInitState* initState, void (*shutdown)());
75
76#endif // #ifndef INCLUDED_MODULE_INIT
Status ModuleShutdown(volatile ModuleInitState *initState, void(*shutdown)())
calls a user-defined shutdown function if initState is "initialized".
Definition: module_init.cpp:65
std::atomic< Status > ModuleInitState
initialization state of a module (class, source file, etc.) must be initialized to zero (e....
Definition: module_init.h:37
Status ModuleInit(volatile ModuleInitState *initState, Status(*init)())
calls a user-defined init function if initState is zero.
Definition: module_init.cpp:40
i64 Status
Error handling system.
Definition: status.h:173
static ModuleInitState initState
Definition: wnuma.cpp:274