Zth (libzth)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
init.h
Go to the documentation of this file.
1 #ifndef ZTH_INIT_H
2 #define ZTH_INIT_H
3 /*
4  * Zth (libzth), a cooperative userspace multitasking library.
5  * Copyright (C) 2019-2022 Jochem Rutgers
6  *
7  * This Source Code Form is subject to the terms of the Mozilla Public
8  * License, v. 2.0. If a copy of the MPL was not distributed with this
9  * file, You can obtain one at https://mozilla.org/MPL/2.0/.
10  */
11 
12 #include <libzth/macros.h>
13 
14 #include <stdlib.h>
15 
17 #ifdef __cplusplus
18  zth_init_entry(void (*f)(void), zth_init_entry const* next)
19  : f(f)
20  , next(next)
21  {}
22 #endif
23  void (*f)(void);
24  struct zth_init_entry const* next;
25 };
26 
27 extern struct zth_init_entry const* zth_init_head;
28 extern struct zth_init_entry* zth_init_tail;
29 EXTERN_C ZTH_EXPORT void zth_init();
30 EXTERN_C ZTH_EXPORT void zth_preinit();
31 EXTERN_C ZTH_EXPORT int zth_postdeinit();
32 
33 #ifdef __cplusplus
34 # ifndef ZTH_INIT_CALL
38 # define ZTH_INIT_CALL_(f, ...) \
39  struct f##__init : public zth_init_entry { \
40  f##__init() \
41  : zth_init_entry(&exec, nullptr) \
42  { \
43  if(zth_init_tail) \
44  zth_init_tail->next = this; \
45  zth_init_tail = this; \
46  if(!zth_init_head) \
47  zth_init_head = this; \
48  } \
49  static void exec(){__VA_ARGS__}; \
50  };
51 # define ZTH_INIT_CALL(f) \
52  ZTH_INIT_CALL_(f, f();) \
53  static f##__init const f##__init_;
54 # endif
55 
56 # ifndef ZTH_DEINIT_CALL
57 # ifdef ZTH_OS_BAREMETAL
58 # define ZTH_DEINIT_CALL( \
59  ...) // Don't bother doing any cleanup during shutdown.
60 # else
64 # define ZTH_DEINIT_CALL(f) \
65  ZTH_INIT_CALL_(f, atexit(f);) \
66  static f##__init f##__deinit_;
67 # endif
68 # endif
69 
70 #endif // __cplusplus
71 #endif // ZTH_INIT_H
struct zth_init_entry * zth_init_tail
Definition: init.cpp:15
void zth_init()
Perform one-time global initialization of the Zth library.
Definition: init.cpp:25
struct zth_init_entry const * zth_init_head
Definition: init.cpp:14
int zth_postdeinit()
Initialization function to be called by the default-supplied main(), just before shutting down.
Definition: main.cpp:42
void zth_preinit()
Initialization function to be called by the default-supplied main(), before doing anything else.
Definition: main.cpp:30
Definition: init.h:16
void(* f)(void)
Definition: init.h:23
struct zth_init_entry const * next
Definition: init.h:24
zth_init_entry(void(*f)(void), zth_init_entry const *next)
Definition: init.h:18