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