Zth (libzth)
main.cpp
Go to the documentation of this file.
1 /*
2  * Zth (libzth), a cooperative userspace multitasking library.
3  * Copyright (C) 2019-2022 Jochem Rutgers
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at https://mozilla.org/MPL/2.0/.
8  */
9 
10 #include <libzth/async.h>
11 #include <libzth/config.h>
12 #include <libzth/worker.h>
13 
14 __attribute__((weak)) int main_fiber(int UNUSED_PAR(argc), char** UNUSED_PAR(argv))
15 {
16  return 0;
17 }
18 #ifndef DOXYGEN
20 #endif
21 
30 __attribute__((weak)) void zth_preinit() {}
31 
42 __attribute__((weak)) int zth_postdeinit()
43 {
44  return 0;
45 }
46 
47 #ifndef ZTH_OS_WINDOWS
48 __attribute__((weak))
49 #endif
50 int main(int argc, char** argv)
51 {
52  zth_preinit();
53  zth_dbg(thread, "main()");
54 
55  int res = 0;
56  {
57  zth::Worker w;
58  main_fiber_future f =
59  async main_fiber(argc, argv) << zth::setName(
62  ? "main_fiber"
63  : nullptr);
64 
65  w.run();
66  if(f->valid())
67  res = f->value();
68  }
69 
70  zth_dbg(thread, "main() returns %d", res);
71 
72  int res_post = zth_postdeinit();
73  // cppcheck-suppress knownConditionTrueFalse
74  if(res_post)
75  res = res_post;
76 
77  return res;
78 }
The class that manages the fibers within this thread.
Definition: worker.h:38
void run(TimeInterval const &duration=TimeInterval())
Definition: worker.h:306
Change the name of a fiber returned by async.
Definition: async.h:167
#define zth_fiber(...)
Prepare every given function to become a fiber by async.
Definition: async.h:765
#define async
Run a function as a new fiber.
Definition: async.h:789
#define zth_dbg(group, fmt, a...)
Debug printf()-like function.
Definition: util.h:210
#define UNUSED_PAR(name)
Definition: macros.h:79
int main(int argc, char **argv)
Definition: main.cpp:50
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
int main_fiber(int argc, char **argv)
Definition: main.cpp:14
static bool const EnablePerfEvent
Enable (but not necessarily record) perf.
Definition: config.h:163
static bool const EnableDebugPrint
Actually do print the debug output.
Definition: config.h:91
static bool const EnableStackWaterMark
When true, enable stack watermark to detect maximum stack usage.
Definition: config.h:137