Zth (libzth)
config.h
Go to the documentation of this file.
1 #ifndef ZTH_CONFIG_H
2 #define ZTH_CONFIG_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 
17 #include <libzth/macros.h>
18 
19 #ifdef __cplusplus
20 # include <cstddef>
21 
22 # if __cplusplus >= 201103L
23 # include <cstdint>
24 # else
25 # include <stdint.h>
26 # endif
27 
28 # include <memory>
29 
30 namespace zth {
31 
32 struct Env {
34 };
35 
36 bool config(int env /* one of Env::* */, bool whenUnset);
37 
46 # define zth_config(name) (::zth::config(::zth::Env::name, ::zth::Config::name))
47 
48 struct DefaultConfig {
50  static bool const Debug =
51 # ifndef NDEBUG
52  true;
53 # else
54  false;
55 # endif
56 
58  static bool const EnableAssert =
59 # ifndef NDEBUG
60  Debug;
61 # else
62  false;
63 # endif
64 
69  static bool const EnableFullAssert =
70 # ifdef ZTH_OS_BAREMETAL
71  // Assume we are a bit short on memory.
72  false;
73 # else
75 # endif
76 
78  static bool const EnableThreads =
79 # if ZTH_THREADS
80  true;
81 # else
82  false;
83 # endif
84 
91  static bool const EnableDebugPrint =
92 # ifdef ZTH_CONFIG_ENABLE_DEBUG_PRINT
93  ZTH_CONFIG_ENABLE_DEBUG_PRINT;
94 # else
95  false;
96 # endif
97 
103  static bool const SupportDebugPrint =
104 # ifdef ZTH_OS_BAREMETAL
105  // Without OS, there is no environment to enable debugging when
106  // it is not enabled right away.
108 # else
109  Debug;
110 # endif
111 
113  static bool const EnableColorLog = true;
114 
119  static int const Print_banner = 12; // bright blue
120  static int const Print_worker = 5; // magenta
121  static int const Print_waiter = 1; // red
122  static int const Print_io = 9; // bright red
123  static int const Print_perf = 6; // cyan
124  static int const Print_fiber = 10; // bright green
125  static int const Print_context = 2; // green
126  static int const Print_sync = 11; // bright yellow
127  static int const Print_list = 8; // bright black
128  static int const Print_zmq = 9; // bright red
129  static int const Print_fsm = 14; // bright cyan
130  static int const Print_thread = 3; // yellow
131 
133  static size_t const DefaultFiberStackSize = 0x20000;
135  static bool const EnableStackGuard = Debug;
137  static bool const EnableStackWaterMark = Debug;
139  static bool const ContextSignals = false;
141  constexpr static double MinTimeslice_s()
142  {
143  return 1e-4;
144  }
145 
149  static bool const CheckTimesliceOverrun = Debug;
151  static bool const NamedSynchronizer = SupportDebugPrint && Print_sync > 0;
152 
154  static size_t const PerfEventBufferSize = 128;
158  static size_t const PerfVCDFileBuffer = 0x1000;
159 
161  static bool const DoPerfEvent = false;
163  static bool const EnablePerfEvent =
164 # ifdef ZTH_OS_BAREMETAL
165  // No environment, so only enable when we are actually saving it.
166  DoPerfEvent;
167 # else
168  true;
169 # endif
171  static bool const PerfSyscall = true;
172 
174  static bool const NamedFsm = Debug || (EnableDebugPrint && Print_fsm > 0);
175 
177  static bool const UseZMQ =
178 # ifdef ZTH_HAVE_LIBZMQ
179  true;
180 # else
181  false;
182 # endif
183 
191  template <typename T>
192  struct Allocator {
193  typedef std::allocator<T> type;
194  };
195 };
196 } // namespace zth
197 #endif // __cplusplus
198 
199 #include "zth_config.h"
200 
201 #endif // ZTH_CONFIG_H
Definition: allocator.h:23
bool config(int env, bool whenUnset)
Checks if a given environment option is set.
Definition: config.cpp:41
std::allocator< T > type
Definition: config.h:193
static int const Print_zmq
Definition: config.h:128
static int const Print_perf
Definition: config.h:123
static int const Print_context
Definition: config.h:125
static bool const EnablePerfEvent
Enable (but not necessarily record) perf.
Definition: config.h:163
static bool const DoPerfEvent
Record and output perf events.
Definition: config.h:161
static size_t const DefaultFiberStackSize
Default fiber stack size in bytes.
Definition: config.h:133
static bool const EnableDebugPrint
Actually do print the debug output.
Definition: config.h:91
static int const Print_waiter
Definition: config.h:121
static bool const Debug
This is a debug build when set to true.
Definition: config.h:50
static bool const EnableAssert
When true, enable zth_assert().
Definition: config.h:58
static int const TimesliceOverrunFactorReportThreshold
Print an overrun reported when MinTimeslice_s() is exceeded by this factor.
Definition: config.h:147
static int const Print_fiber
Definition: config.h:124
static int const Print_worker
Definition: config.h:120
static int const Print_banner
ANSI color used by zth_dbg(). Printing this category is disabled when set to 0.
Definition: config.h:119
static size_t const PerfEventBufferSize
Buffer size for perf events.
Definition: config.h:154
static bool const ContextSignals
Take POSIX signal into account when doing a context switch.
Definition: config.h:139
static bool const EnableStackWaterMark
When true, enable stack watermark to detect maximum stack usage.
Definition: config.h:137
static bool const EnableThreads
Add (Worker) thread support when true.
Definition: config.h:78
static int const Print_thread
Definition: config.h:130
static bool const NamedFsm
Use named FSM guards/actions.
Definition: config.h:174
static bool const EnableStackGuard
When true, enable stack guards.
Definition: config.h:135
static bool const CheckTimesliceOverrun
Check time slice overrun at every context switch.
Definition: config.h:149
static bool const UseZMQ
Enable ZeroMQ support.
Definition: config.h:177
static int const Print_list
Definition: config.h:127
static int const Print_io
Definition: config.h:122
static bool const EnableColorLog
Enable colored output.
Definition: config.h:113
static int const Print_sync
Definition: config.h:126
static size_t const PerfVCDFileBuffer
VCD file buffer in bytes.
Definition: config.h:158
static bool const PerfSyscall
Also record syscalls by perf.
Definition: config.h:171
static int const Print_fsm
Definition: config.h:129
static bool const SupportDebugPrint
Add support to enable debug output prints.
Definition: config.h:103
constexpr static double MinTimeslice_s()
Minimum time slice before zth::yield() actually yields.
Definition: config.h:141
static bool const EnableFullAssert
Show failing expression in case of a failed assert.
Definition: config.h:69
static size_t const PerfEventBufferThresholdToTriggerVCDWrite
Threshold when to force writing out VCD buffer.
Definition: config.h:156
static bool const NamedSynchronizer
Save names for all zth::Synchronizer instances.
Definition: config.h:151
@ DoPerfEvent
Definition: config.h:33
@ PerfSyscall
Definition: config.h:33
@ CheckTimesliceOverrun
Definition: config.h:33
@ EnableDebugPrint
Definition: config.h:33