Zth (libzth)
Loading...
Searching...
No Matches
main.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/async.h>
8#include <libzth/config.h>
9#include <libzth/worker.h>
10
11__attribute__((weak)) int main_fiber(int UNUSED_PAR(argc), char** UNUSED_PAR(argv))
12{
13 return 0;
14}
15
24__attribute__((weak)) void zth_preinit() {}
25
36__attribute__((weak)) int zth_postdeinit()
37{
38 return 0;
39}
40
41#ifndef ZTH_OS_WINDOWS
42__attribute__((weak))
43#endif
44int main(int argc, char** argv)
45{
47 zth_dbg(thread, "main()");
48
49 int res = EXIT_SUCCESS;
50 try {
53 zth::fiber(main_fiber, argc, argv) << zth::setName(
56 ? "main_fiber"
57 : nullptr);
58
59 w.run();
60
61 if(!f.get().valid()) {
62 zth_dbg(thread, "main_fiber() did not exit normally");
63 res = EXIT_FAILURE;
64 } else {
65 res = *f;
66 }
67#ifdef __cpp_exceptions
68 } catch(zth::errno_exception const& e) {
69 zth_dbg(thread, "main() caught exception with errno %d", e.code);
70 res = EXIT_FAILURE;
71 } catch(std::exception const& e) {
72 zth_dbg(thread, "main() caught exception: %s", e.what());
73 res = EXIT_FAILURE;
74#endif
75 } catch(...) {
76 zth_dbg(thread, "main() caught unknown exception");
77 res = EXIT_FAILURE;
78 }
79
80 zth_dbg(thread, "main() returns %d", res);
81
82 int res_post = zth_postdeinit();
83 // cppcheck-suppress knownConditionTrueFalse
84 if(res_post)
85 res = res_post;
86
87 return res;
88}
constexpr T & get() const noexcept
Definition sync.h:163
The class that manages the fibers within this thread.
Definition worker.h:35
void run(TimeInterval const &duration=TimeInterval())
Definition worker.h:316
fiber_type< F >::fiber fiber(F &&f, Args &&... args)
Create and start a new fiber.
Definition async.h:1192
#define zth_dbg(group, fmt, a...)
Debug printf()-like function.
Definition util.h:194
#define UNUSED_PAR(name)
Definition macros.h:78
int main(int argc, char **argv)
Definition main.cpp:44
int zth_postdeinit()
Initialization function to be called by the default-supplied main(), just before shutting down.
Definition main.cpp:36
void zth_preinit()
Initialization function to be called by the default-supplied main(), before doing anything else.
Definition main.cpp:24
int main_fiber(int argc, char **argv)
Definition main.cpp:11
static bool const EnablePerfEvent
Enable (but not necessarily record) perf.
Definition config.h:174
static bool const EnableDebugPrint
Actually do print the debug output.
Definition config.h:97
static bool const EnableStackWaterMark
When true, enable stack watermark to detect maximum stack usage.
Definition config.h:144
Wrapper for an errno.
Definition exception.h:41
The future returned by a fiber.
Definition async.h:1292
Change the name of a fiber returned by zth_async.
Definition async.h:201