Zth (libzth)
Loading...
Searching...
No Matches
init.h
Go to the documentation of this file.
1#ifndef ZTH_INIT_H
2#define ZTH_INIT_H
3/*
4 * SPDX-FileCopyrightText: 2019-2026 Jochem Rutgers
5 *
6 * SPDX-License-Identifier: MPL-2.0
7 */
8
9#include <libzth/macros.h>
10
11#include <stdlib.h>
12
14#ifdef __cplusplus
15 zth_init_entry(void (*f_)(void), zth_init_entry const* next_) noexcept;
16#endif // C++
17 void (*f)(void);
18 struct zth_init_entry const* next;
19};
20
21extern struct zth_init_entry const* zth_init_head;
22extern struct zth_init_entry* zth_init_tail;
23EXTERN_C ZTH_EXPORT void zth_init();
24EXTERN_C ZTH_EXPORT void zth_preinit();
25EXTERN_C ZTH_EXPORT int zth_postdeinit();
26EXTERN_C ZTH_EXPORT int zth_run(void(fiber)(void*), void* arg);
27EXTERN_C ZTH_EXPORT int zth_main(int argc, char** argv);
28
29#ifdef __cplusplus
30# ifndef ZTH_INIT_CALL
34# define ZTH_INIT_CALL_(f, ...) \
35 struct f##__init : public zth_init_entry { \
36 f##__init() noexcept \
37 : zth_init_entry(&exec, nullptr) \
38 {} \
39 static void exec() { __VA_ARGS__ }; \
40 };
41# define ZTH_INIT_CALL(f) \
42 ZTH_INIT_CALL_(f, f();) \
43 static f##__init const f##__init_;
44# endif
45
46# ifndef ZTH_DEINIT_CALL
47# ifdef ZTH_OS_BAREMETAL
48# define ZTH_DEINIT_CALL(...) // Don't bother doing any cleanup during shutdown.
49# else
53# define ZTH_DEINIT_CALL(f) \
54 ZTH_INIT_CALL_(f, atexit(f);) \
55 static f##__init f##__deinit_;
56# endif
57# endif
58#endif // __cplusplus
59
60#if defined(ZTH_BUILD_LIB) || !defined(__cplusplus)
61# define ZTH_APP_INIT_CALL(f)
62#else
63# if __cplusplus >= 201703L
64# define ZTH_APP_INIT_CALL_(f, ...) \
65 struct f##__app_init { \
66 f##__app_init() noexcept \
67 { \
68 __VA_ARGS__ \
69 } \
70 }; \
71 inline f##__app_init const f##__app_init_;
72# else
73# define ZTH_APP_INIT_CALL_(f, ...) \
74 inline void f##__app_init_once_() noexcept \
75 { \
76 static bool done = false; \
77 if(!done) { \
78 done = true; \
79 __VA_ARGS__ \
80 } \
81 } \
82 struct f##__app_init { \
83 f##__app_init() noexcept \
84 { \
85 f##__app_init_once_(); \
86 } \
87 }; \
88 static f##__app_init const f##__app_init_;
89# endif
90
91# define ZTH_APP_INIT_CALL(f) ZTH_APP_INIT_CALL_(f, f();)
92#endif
93
94#endif // ZTH_INIT_H
struct zth_init_entry * zth_init_tail
Definition init.cpp:17
void zth_init()
Perform one-time global initialization of the Zth library.
Definition init.cpp:38
int zth_run(void(fiber)(void *), void *arg)
Start Zth given the given fiber function.
Definition init.cpp:64
struct zth_init_entry const * zth_init_head
Definition init.cpp:15
int zth_postdeinit()
Initialization function to be called by the default-supplied main(), just before shutting down.
void zth_preinit()
Initialization function to be called by the default-supplied main(), before doing anything else.
int zth_main(int argc, char **argv)
Default main function that runs main_fiber.
Definition init.cpp:104
Definition init.h:13
void(* f)(void)
Definition init.h:17
struct zth_init_entry const * next
Definition init.h:18