Zth (libzth)
Loading...
Searching...
No Matches
init.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2019-2026 Jochem Rutgers
3 *
4 * SPDX-License-Identifier: MPL-2.0
5 */
6
7#include <libzth/init.h>
8
9#include <libzth/async.h>
10#include <libzth/config.h>
11#include <libzth/util.h>
12#include <libzth/worker.h>
13
14// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
15struct zth_init_entry const* zth_init_head = nullptr;
16// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
17struct zth_init_entry* zth_init_tail = nullptr;
18
19zth_init_entry::zth_init_entry(void (*f_)(void), zth_init_entry const* next_) noexcept
20 : f(f_)
21 , next(next_)
22{
24 zth_init_tail->next = this;
25 zth_init_tail = this;
26 if(!zth_init_head)
27 zth_init_head = this;
28}
29
39{
41 // Already initialized.
42 return;
43
44 struct zth_init_entry const* p = zth_init_head;
45 zth_init_head = nullptr;
46
47 while(p) {
48 // p might be overwritten during p->f(), copy next first.
49 struct zth_init_entry const* p_next = p->next;
50 if(p->f)
51 p->f();
52 p = p_next;
53 }
54}
55
64int zth_run(void(fiber)(void*), void* arg)
65{
66 int res = 0;
67 try {
69 return EINVAL;
70
73 w.run();
74
75 if(!f.get().valid()) {
76 zth_dbg(thread, "zth_run() fiber did not exit normally");
77 res = EFAULT;
78 }
79#ifdef __cpp_exceptions
80 } catch(zth::errno_exception const& e) {
81 zth_dbg(thread, "zth_run() caught exception with errno %d", e.code);
82 res = e.code;
83 } catch(std::exception const& e) {
84 zth_dbg(thread, "zth_run() caught exception: %s", e.what());
85 res = EFAULT;
86 } catch(zth::exception const& e) {
87 zth_dbg(thread, "zth_run() caught zth::exception");
88 res = EFAULT;
89#endif
90 } catch(...) {
91 zth_dbg(thread, "zth_run() caught unknown exception");
92 res = EFAULT;
93 }
94
95 return res;
96}
97
104int zth_main(int argc, char** argv)
105{
106 zth_preinit();
107 zth_dbg(thread, "main()");
108
109 int res = EXIT_SUCCESS;
110 try {
111 zth::Worker w;
113 zth::fiber(main_fiber, argc, argv) << zth::setName(
114 zth_config(EnableDebugPrint) || zth::Config::EnablePerfEvent
116 ? "main_fiber"
117 : nullptr);
118
119 w.run();
120
121 if(!f.get().valid()) {
122 zth_dbg(thread, "main_fiber() did not exit normally");
123 res = EXIT_FAILURE;
124 } else {
125 res = *f;
126 }
127#ifdef __cpp_exceptions
128 } catch(zth::errno_exception const& e) {
129 zth_dbg(thread, "main() caught exception with errno %d", e.code);
130 res = EXIT_FAILURE;
131 } catch(std::exception const& e) {
132 zth_dbg(thread, "main() caught exception: %s", e.what());
133 res = EXIT_FAILURE;
134 } catch(zth::exception const& e) {
135 zth_dbg(thread, "main() caught zth::exception");
136 res = EXIT_FAILURE;
137#endif
138 } catch(...) {
139 zth_dbg(thread, "main() caught unknown exception");
140 res = EXIT_FAILURE;
141 }
142
143 zth_dbg(thread, "main() returns %d", res);
144
145 int res_post = zth_postdeinit();
146 // cppcheck-suppress knownConditionTrueFalse
147 if(res_post)
148 res = res_post;
149
150 return res;
151}
static safe_ptr< singleton_type >::type instance() noexcept
Return the only instance of T within this thread.
Definition util.h:1196
The class that manages the fibers within this thread.
Definition worker.h:35
void run(TimeInterval const &duration=TimeInterval())
Definition worker.h:326
int main_fiber(int argc, char **argv)
#define zth_config(name)
Checks if the given zth::Config field is enabled.
Definition config.h:46
fiber_type< F >::fiber fiber(F &&f, Args &&... args)
Create and start a new fiber.
Definition async.h:1221
#define zth_dbg(group, fmt, a...)
Debug printf()-like function.
Definition util.h:194
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_main(int argc, char **argv)
Default main function that runs main_fiber.
Definition init.cpp:104
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.
static bool const EnablePerfEvent
Enable (but not necessarily record) perf.
Definition config.h:265
static bool const EnableStackWaterMark
When true, enable stack watermark to detect maximum stack usage.
Definition config.h:187
Wrapper for an errno.
Definition exception.h:41
Base exception class for Zth exceptions.
Definition exception.h:16
The future returned by a fiber.
Definition async.h:1321
Change the name of a fiber returned by zth_async.
Definition async.h:202
Definition init.h:13
void(* f)(void)
Definition init.h:17
struct zth_init_entry const * next
Definition init.h:18
zth_init_entry(void(*f_)(void), zth_init_entry const *next_) noexcept
Definition init.cpp:19
#define likely(expr)
Marks the given expression to likely be evaluated to true.
Definition util.h:45