SuperTinyKernel™ RTOS 1.05.3
Lightweight, high-performance, deterministic, bare-metal C++ RTOS for resource-constrained embedded systems. MIT Open Source License.
Loading...
Searching...
No Matches
stk_defs.h
Go to the documentation of this file.
1/*
2 * SuperTinyKernel(TM) RTOS: Lightweight High-Performance Deterministic C++ RTOS for Embedded Systems.
3 *
4 * Source: https://github.com/SuperTinyKernel-RTOS
5 *
6 * Copyright (c) 2022-2026 Neutron Code Limited <stk@neutroncode.com>. All Rights Reserved.
7 * License: MIT License, see LICENSE for a full text.
8 */
9
10#ifndef STK_DEFS_H_
11#define STK_DEFS_H_
12
13#include <stddef.h>
14#include <stdint.h>
15
22#include "stk_config.h"
23
35#ifndef STK_TICKLESS_IDLE
36 #define STK_TICKLESS_IDLE 0
37#endif
38
47#ifndef STK_TICKLESS_USE_ARM_DWT
48 #define STK_TICKLESS_USE_ARM_DWT 1
49#endif
50
61#ifndef STK_TICKLESS_TICKS_MAX
62 #define STK_TICKLESS_TICKS_MAX 1000
63#endif
64#if STK_TICKLESS_TICKS_MAX > 100000
65 #error "STK_TICKLESS_TICKS_MAX is too large: cpu_ticks_requested may overflow uint32_t."
66#endif
67
76#if STK_SEGGER_SYSVIEW
77 #define STK_NEED_TASK_ID 1
78#endif
79
87#if !defined(STK_SYNC_DEBUG_NAMES) && STK_SEGGER_SYSVIEW
88 #define STK_SYNC_DEBUG_NAMES 1
89#elif !defined(STK_SYNC_DEBUG_NAMES)
90 #define STK_SYNC_DEBUG_NAMES 0
91#endif
92
99#ifdef __GNUC__
100 #define __stk_forceinline __attribute__((always_inline)) inline
101#elif defined(__ICCARM__) || defined(_MSC_VER)
102 #define __stk_forceinline __forceinline
103#else
104 #define __stk_forceinline
105#endif
106
113#ifdef __GNUC__
114 #define __stk_aligned(x) __attribute__((aligned(x)))
115#elif defined(__ICCARM__)
116 #define __stk_aligned(x) __attribute__((aligned(x)))
117#else
118 #define __stk_aligned(x)
119#endif
120
128#ifdef __GNUC__
129 #define __stk_attr_naked __attribute__((naked))
130#elif defined(__ICCARM__)
131 #define __stk_attr_naked __attribute__((naked))
132#else
133 #define __stk_attr_naked
134#endif
135
141#ifdef __GNUC__
142 #define __stk_attr_noreturn __attribute__((__noreturn__))
143#elif defined(__ICCARM__)
144 #define __stk_attr_noreturn __attribute__((noreturn))
145#else
146 #define __stk_attr_noreturn
147#endif
148
154#ifdef __GNUC__
155 #define __stk_attr_unused __attribute__((unused))
156#elif defined(__ICCARM__)
157 #define __stk_attr_unused __attribute__((unused))
158#else
159 #define __stk_attr_unused
160#endif
161
167#ifdef __GNUC__
168 #define __stk_attr_used __attribute__((used))
169#elif defined(__ICCARM__)
170 #define __stk_attr_used __attribute__((used))
171#else
172 #define __stk_attr_used
173#endif
174
180#ifdef __GNUC__
181 #define __stk_attr_noinline __attribute__((noinline))
182#elif defined(__ICCARM__)
183 #define __stk_attr_noinline __attribute__((noinline))
184#else
185 #define __stk_attr_noinline
186#endif
187
193#ifdef __GNUC__
194 #define __stk_attr_deprecated __attribute__((deprecated))
195#elif defined(__ICCARM__)
196 #define __stk_attr_deprecated __attribute__((deprecated))
197#elif defined(_MSC_VER)
198 #define __stk_attr_deprecated __declspec(deprecated)
199#else
200 #define __stk_attr_deprecated
201#endif
202
209#if defined(__GNUC__) || defined(__clang__)
210 #define __stk_full_memfence() __sync_synchronize()
211#elif defined(_MSC_VER)
212 #define __stk_full_memfence() __stk_dmb()
213#else
214 #error "__stk_full_memfence() is not implemented for this compiler. Add a definition to stk_defs.h."
215#endif
216
226#if defined(__GNUC__) || defined(__clang__)
227 #define __stk_compiler_barrier() __asm volatile("" ::: "memory")
228#elif defined(_MSC_VER)
229 #define __stk_compiler_barrier() _ReadWriteBarrier()
230#else
231 #error "__stk_compiler_barrier() is not implemented for this compiler. Add a definition to stk_defs.h."
232#endif
233
247#ifndef __stk_relax_cpu
248#if defined(__GNUC__) || defined(__clang__)
249 #if defined(__i386__) || defined(__x86_64__)
250 #define __stk_relax_cpu() __builtin_ia32_pause()
251 #elif defined(__riscv)
252 #ifdef __riscv_zihintpause
253 #define __stk_relax_cpu() __builtin_riscv_pause()
254 #else
255 #define __stk_relax_cpu() __stk_full_memfence()
256 #endif
257 #else
258 #define __stk_relax_cpu() __stk_full_memfence()
259 #endif
260#elif defined(_MSC_VER)
261 #include <intrin.h>
262 #if defined(_M_IX86) || defined(_M_X64)
263 // Maps to the PAUSE instruction
264 #define __stk_relax_cpu() _mm_pause()
265 #elif defined(_M_ARM) || defined(_M_ARM64)
266 // Maps to the YIELD instruction on ARM
267 #define __stk_relax_cpu() __yield()
268 #else
269 #define __stk_relax_cpu() __stk_full_memfence()
270 #endif
271#else
272 #error "__stk_relax_cpu() is not implemented for this compiler. Add a definition to stk_defs.h."
273#endif
274#endif
275
290#if defined(DEBUG) || defined(_DEBUG)
291 #if defined(_STK_ARCH_ARM_CORTEX_M)
292 #define __stk_debug_break() __asm volatile("bkpt 0")
293 #elif defined(_STK_ARCH_RISC_V)
294 #define __stk_debug_break() __asm volatile("ebreak")
295 #elif defined(_STK_ARCH_X86_WIN32)
296 #ifdef _MSC_VER
297 #define __stk_debug_break() __debugbreak()
298 #else
299 #define __stk_debug_break() __asm volatile("int $3")
300 #endif
301 #endif
302#else
303 #define __stk_debug_break()
304#endif
305
322#ifdef _STK_ASSERT_REDIRECT
323 extern void STK_ASSERT_HANDLER(const char *, const char *, int32_t);
324 #define STK_ASSERT(e) ((e) ? (void)0 : STK_ASSERT_HANDLER(#e, __FILE__, __LINE__))
325#else
326 #if defined(DEBUG) || defined(_DEBUG)
327 #include <assert.h>
328 #define STK_ASSERT(e) assert(e)
329 #else
330 #define STK_ASSERT(e)
331 #endif
332#endif
333
342#define STK_STATIC_ASSERT_DESC_N(NAME, X, DESC) static_assert((X), DESC)
343
350#define STK_STATIC_ASSERT_DESC(X, DESC) STK_STATIC_ASSERT_DESC_N(_, X, DESC)
351
359#define STK_STATIC_ASSERT_N(NAME, X) STK_STATIC_ASSERT_DESC_N(N, (X), #X)
360
367#define STK_STATIC_ASSERT(X) STK_STATIC_ASSERT_DESC_N(_, (X), #X)
368
376#ifndef STK_STACK_MEMORY_FILLER
377 #define STK_STACK_MEMORY_FILLER ((Word)(sizeof(Word) <= 4 ? 0xdeadbeef : 0xdeadbeefdeadbeef))
378#endif
379
383#ifndef STK_STACK_MEMORY_ALIGN
384 #if defined(__riscv)
385 #define STK_STACK_MEMORY_ALIGN 16
386 #elif defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_X64)
387 #define STK_STACK_MEMORY_ALIGN 8
388 #else // ARM, others
389 #define STK_STACK_MEMORY_ALIGN 4
390 #endif
391#endif
392
403#ifndef STK_CRITICAL_SECTION_NESTINGS_MAX
404 #define STK_CRITICAL_SECTION_NESTINGS_MAX 16
405#endif
406
413#ifndef STK_ARCH_CPU_COUNT
414 #define STK_ARCH_CPU_COUNT 1
415#endif
416
432#ifndef STK_STACK_SIZE_MIN
433 #ifdef __riscv
434 #if defined(__riscv_32e) && (__riscv_32e == 1)
435 // RISC-V RV32E (Embedded): Small 16-register file
436 #if !defined(__riscv_flen) || (__riscv_flen == 0)
437 #define STK_STACK_SIZE_MIN 32
438 #else
439 // FPU present: Requires additional space for 32 FP registers
440 #define STK_STACK_SIZE_MIN (32 + (__riscv_flen * 2))
441 #endif
442 #else
443 // Standard RISC-V (RV32I/RV64I): Large 32-register file
444 // Higher minimum to prevent memory corruption on platforms like RP2350
445 #if !defined(__riscv_flen) || (__riscv_flen == 0)
446 #define STK_STACK_SIZE_MIN 256
447 #else
448 // Standard RISC-V with FPU: Maximum frame allocation
449 #define STK_STACK_SIZE_MIN (512 + (__riscv_flen * 2))
450 #endif
451 #endif
452 #else
453 // ARM Cortex-M and other architectures
454 #define STK_STACK_SIZE_MIN 32
455 #endif
456#endif
457
466#ifndef STK_SLEEP_TRAP_STACK_SIZE
467 #define STK_SLEEP_TRAP_STACK_SIZE (STK_STACK_SIZE_MIN)
468#endif
469
482#ifdef _MSC_VER
483 #define STK_ALLOCATE_COUNT(MODE, FLAG, ONTRUE, ONFALSE) ((ONTRUE) > (ONFALSE) ? (ONTRUE) : (ONFALSE))
484#else
485 #define STK_ALLOCATE_COUNT(MODE, FLAG, ONTRUE, ONFALSE) ((((MODE) & (FLAG)) != 0U) ? (ONTRUE) : (ONFALSE))
486#endif
487
498#if defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
499 #define STK_ENDIAN_IDX_HI (0) // big-endian: high word at index 0
500 #define STK_ENDIAN_IDX_LO (1) // big-endian: low word at index 1
501#else
502 #define STK_ENDIAN_IDX_HI (1) // little-endian (default): high word at index 1
503 #define STK_ENDIAN_IDX_LO (0) // little-endian (default): low word at index 0
504#endif
505
517#define STK_NONCOPYABLE_CLASS(TYPE)\
518 TYPE(const TYPE &) = delete;\
519 TYPE &operator=(const TYPE &) = delete;
520
524namespace stk {
525
529template <typename T>
530constexpr T Min(T a, T b) noexcept { return (a < b) ? a : b; }
531
535template <typename T>
536constexpr T Max(T a, T b) noexcept { return (a < b) ? b : a; }
537
542namespace util {}
543
544} // namespace stk
545
546#endif /* STK_DEFS_H_ */
void STK_ASSERT_HANDLER(const char *message, const char *file, int32_t line)
Namespace of STK package.
constexpr T Max(T a, T b) noexcept
Compile-time maximum of two values.
Definition stk_defs.h:536
constexpr T Min(T a, T b) noexcept
Compile-time minimum of two values.
Definition stk_defs.h:530
Internal utility namespace containing data structure helpers (linked lists, etc.) used by the kernel ...