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_helper.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_HELPER_H_
11#define STK_HELPER_H_
12
13#include "stk_common.h"
14#include "stk_arch.h"
15
19
20namespace stk {
21
48template <size_t _StackSize, EAccessMode _AccessMode>
49class Task : public ITask
50{
51public:
52 enum { STACK_SIZE = _StackSize };
53
54 Word *GetStack() const { return const_cast<Word *>(m_stack); }
55 size_t GetStackSize() const { return _StackSize; }
56 size_t GetStackSizeBytes() const { return _StackSize * sizeof(Word); }
57 EAccessMode GetAccessMode() const { return _AccessMode; }
58
62 virtual void OnDeadlineMissed(uint32_t duration) { STK_UNUSED(duration); }
63
67 virtual void OnExit() {}
68
73 virtual int32_t GetWeight() const { return 1; }
74
77 virtual TId GetId() const { return hw::PtrToWord(this); }
78
81 virtual const char *GetTraceName() const { return nullptr; }
82
83protected:
85
94 {}
95
100 {}
101
102private:
104};
105
120template <int32_t _Weight, size_t _StackSize, EAccessMode _AccessMode>
121class TaskW : public ITask
122{
123public:
124 enum { STACK_SIZE = _StackSize };
125
126 Word *GetStack() const { return const_cast<Word *>(m_stack); }
127 size_t GetStackSize() const { return _StackSize; }
128 size_t GetStackSizeBytes() const { return _StackSize * sizeof(Word); }
129 EAccessMode GetAccessMode() const { return _AccessMode; }
130
134 virtual void OnDeadlineMissed(uint32_t duration) { STK_ASSERT(false); STK_UNUSED(duration); }
135
139 virtual void OnExit() {}
140
143 virtual int32_t GetWeight() const { return _Weight; }
144
147 virtual TId GetId() const { return hw::PtrToWord(this); }
148
151 virtual const char *GetTraceName() const { return nullptr; }
152
153protected:
155
163 TaskW() : m_stack() {}
164
169 {}
170
171private:
173};
174
182template <size_t _StackSize>
184{
185public:
190
196 explicit StackMemoryWrapper(MemoryType *stack) : m_stack(stack)
197 {
198 STK_STATIC_ASSERT(_StackSize >= STACK_SIZE_MIN);
199 }
200
206
209 Word *GetStack() const { return (*m_stack); }
210
213 size_t GetStackSize() const { return _StackSize; }
214
217 size_t GetStackSizeBytes() const { return _StackSize * sizeof(Word); }
218
219private:
221};
222
231
238__stk_forceinline int64_t GetMsFromTicks(int64_t ticks, int32_t resolution)
239{
240 return (ticks * resolution) / 1000;
241}
242
249__stk_forceinline Ticks GetTicksFromMs(int64_t ms, int32_t resolution)
250{
251 return ms * 1000 / resolution;
252}
253
262
272
284
291static inline int64_t GetTimeNowMs()
292{
294 int32_t resolution = service->GetTickResolution();
295
296 if (resolution == 1000) // fast path: tick == 1 ms, no conversion needed
297 return service->GetTicks();
298 else
299 return (service->GetTicks() * resolution) / 1000;
300}
301
310
319
330
339static inline void SleepMs(Timeout ms)
340{
341 Sleep(static_cast<Timeout>(GetTicksFromMs(ms)));
342}
343
351{
353}
354
363
374
381static inline void DelayMs(Timeout ms)
382{
383 Delay(static_cast<Timeout>(GetTicksFromMs(ms)));
384}
385
386} // namespace stk
387
388#endif /* STK_HELPER_H_ */
Hardware Abstraction Layer (HAL) declarations for the stk::hw namespace.
Contains interface definitions of the library.
#define STK_UNUSED(X)
Explicitly marks a variable as unused to suppress compiler warnings.
Definition stk_defs.h:524
#define __stk_forceinline
Forces compiler to always inline the decorated function, regardless of optimisation level.
Definition stk_defs.h:104
#define STK_NONCOPYABLE_CLASS(TYPE)
Disables copy construction and assignment for a class.
Definition stk_defs.h:517
#define STK_ASSERT(e)
Runtime assertion. Halts execution if the expression e evaluates to false.
Definition stk_defs.h:330
#define STK_STATIC_ASSERT(X)
Compile-time assertion. Produces a compilation error if X is false.
Definition stk_defs.h:367
Namespace of STK package.
uintptr_t Word
Native processor word type.
Definition stk_common.h:113
static int64_t GetTimeNowMs()
Get current time in milliseconds since kernel start.
Definition stk_helper.h:291
uint32_t GetSysTimerFrequency()
Get system timer frequency.
Definition stk_helper.h:315
static void SleepMs(Timeout ms)
Put calling process into a sleep state.
Definition stk_helper.h:339
void Sleep(Timeout ticks)
Put calling process into a sleep state.
Definition stk_helper.h:326
int64_t GetMsFromTicks(int64_t ticks, int32_t resolution)
Convert ticks to milliseconds.
Definition stk_helper.h:238
Ticks GetTicks()
Get number of ticks elapsed since kernel start.
Definition stk_helper.h:258
Cycles GetSysTimerCount()
Get system timer count value.
Definition stk_helper.h:306
int64_t Ticks
Ticks value.
Definition stk_common.h:128
static void DelayMs(Timeout ms)
Delay calling process by busy-waiting until the deadline expires.
Definition stk_helper.h:381
int32_t Timeout
Timeout time (ticks).
Definition stk_common.h:123
void Yield()
Notify scheduler to switch to the next runnable task.
Definition stk_helper.h:359
Ticks GetTicksFromMs(int64_t ms, int32_t resolution)
Convert milliseconds to ticks.
Definition stk_helper.h:249
void SleepUntil(Ticks timestamp)
Put calling process into a sleep state until the specified timestamp.
Definition stk_helper.h:350
@ STACK_SIZE_MIN
Minimum stack size in elements of Word. Used as a lower bound for all stack allocations (user task,...
Definition stk_common.h:83
TId GetTid()
Get task/thread Id of the calling task.
Definition stk_helper.h:227
void Delay(Timeout ticks)
Delay calling process by busy-waiting until the deadline expires.
Definition stk_helper.h:370
int32_t GetTickResolution()
Get number of microseconds in one tick.
Definition stk_helper.h:268
uint64_t Cycles
Cycles value.
Definition stk_common.h:133
Word TId
Definition stk_common.h:118
EAccessMode
Hardware access mode by the user task.
Definition stk_common.h:31
__stk_forceinline Word PtrToWord(T *ptr) noexcept
Cast a pointer to a CPU register-width integer.
Definition stk_arch.h:94
Word Type[_StackSize]
Stack memory type.
Definition stk_common.h:212
Interface for a stack memory region.
Definition stk_common.h:231
Interface for a user task.
Definition stk_common.h:491
Interface for the kernel services exposed to the user processes during run-time when Kernel started s...
virtual TId GetTid() const =0
Get thread Id of the currently running task.
static IKernelService * GetInstance()
Get CPU-local instance of the kernel service.
Definition stktest.cpp:69
virtual uint32_t GetTickResolution() const =0
Get number of microseconds in one tick.
virtual Ticks GetTicks() const =0
Get number of ticks elapsed since kernel start.
virtual void SwitchToNext()=0
Notify scheduler to switch to the next task (yield).
virtual void Sleep(Timeout ticks)=0
Put calling process into a sleep state.
virtual Cycles GetSysTimerCount() const =0
Get system timer count value.
virtual void SleepUntil(Ticks timestamp)=0
Put calling process into a sleep state until the specified timestamp.
virtual uint32_t GetSysTimerFrequency() const =0
Get system timer frequency.
virtual void Delay(Timeout ticks)=0
Delay calling process.
virtual void OnExit()
Default no-op handler. Override to implement join semantics (signal a waiting joiner).
Definition stk_helper.h:67
virtual const char * GetTraceName() const
Override in subclass to supply a name for SEGGER SystemView tracing. Returns NULL by default.
Definition stk_helper.h:81
Task()
Initializes task instance and zero-initializes its internal stack memory.
Definition stk_helper.h:93
size_t GetStackSizeBytes() const
Get size of the memory in bytes.
Definition stk_helper.h:56
Task(const Task &)=delete
virtual TId GetId() const
Get object's own address as its Id. Unique per task instance, requires no manual assignment.
Definition stk_helper.h:77
EAccessMode GetAccessMode() const
Get hardware access mode of the user task.
Definition stk_helper.h:57
virtual void OnDeadlineMissed(uint32_t duration)
Default no-op handler. Override in subclass to log or handle missed deadlines.
Definition stk_helper.h:62
~Task()
Destructor.
Definition stk_helper.h:99
size_t GetStackSize() const
Get number of elements of the stack memory array.
Definition stk_helper.h:55
StackMemoryDef< _StackSize >::Type m_stack
Stack memory region, 16-byte aligned.
Definition stk_helper.h:103
virtual int32_t GetWeight() const
Default weight of 1. Override in subclass if custom scheduling weight is needed.
Definition stk_helper.h:73
Word * GetStack() const
Get pointer to the stack memory.
Definition stk_helper.h:54
virtual const char * GetTraceName() const
Override in subclass to supply a name for SEGGER SystemView tracing. Returns NULL by default.
Definition stk_helper.h:151
size_t GetStackSize() const
Get number of elements of the stack memory array.
Definition stk_helper.h:127
TaskW(const TaskW &)=delete
virtual int32_t GetWeight() const
Returns the compile-time weight _Weight.
Definition stk_helper.h:143
virtual void OnExit()
Default no-op handler. Override to implement join semantics (signal a waiting joiner).
Definition stk_helper.h:139
virtual void OnDeadlineMissed(uint32_t duration)
Hard Real-Time mode is unsupported for weighted tasks. Triggers an assertion if called.
Definition stk_helper.h:134
Word * GetStack() const
Get pointer to the stack memory.
Definition stk_helper.h:126
virtual TId GetId() const
Get object's own address as its Id. Unique per task instance, requires no manual assignment.
Definition stk_helper.h:147
TaskW()
Initializes task instance and zero-initializes its internal stack memory.
Definition stk_helper.h:163
size_t GetStackSizeBytes() const
Get size of the memory in bytes.
Definition stk_helper.h:128
~TaskW()
Destructor.
Definition stk_helper.h:168
StackMemoryDef< _StackSize >::Type m_stack
Stack memory region, 16-byte aligned.
Definition stk_helper.h:172
EAccessMode GetAccessMode() const
Get hardware access mode of the user task.
Definition stk_helper.h:129
size_t GetStackSize() const
Get number of elements in the wrapped stack array.
Definition stk_helper.h:213
StackMemoryDef< _StackSize >::Type MemoryType
Definition stk_helper.h:189
StackMemoryWrapper(MemoryType *stack)
Construct a wrapper around an existing stack memory array.
Definition stk_helper.h:196
size_t GetStackSizeBytes() const
Get size of the wrapped stack array in bytes.
Definition stk_helper.h:217
Word * GetStack() const
Get pointer to the first element of the wrapped stack array.
Definition stk_helper.h:209
~StackMemoryWrapper()
Destructor.
Definition stk_helper.h:204