SuperTinyKernel™ RTOS 1.08.x
Lightweight, high-performance, deterministic, bare-metal C++ RTOS for resource-constrained embedded systems. MIT Open Source License.
Loading...
Searching...
No Matches
stk_common.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_COMMON_H_
11#define STK_COMMON_H_
12
13#include "stk_defs.h"
14#include "stk_linked_list.h"
15
19
20namespace stk {
21
22// Forward declarations:
23class IKernelService;
24class IKernelTask;
25class ITask;
26class ISyncObject;
27namespace tz { namespace nsec { namespace util {
28 class CmseISyncObjectWrapper;
29}}}
30
35enum EAccessMode : uint32_t
36{
38 ACCESS_PRIVILEGED = (1 << 0),
39 ACCESS_SECURE = (1 << 1),
40};
41
45enum EKernelMode : uint8_t
46{
47 KERNEL_STATIC = (1 << 0),
48 KERNEL_DYNAMIC = (1 << 1),
49 KERNEL_HRT = (1 << 2),
50 KERNEL_SYNC = (1 << 3),
51 KERNEL_TICKLESS = (1 << 4),
52};
53
72
83
93
97enum ESystemTaskId : uint32_t
98{
99 SYS_TASK_ID_SLEEP = 0xFFFFFFFF,
100 SYS_TASK_ID_EXIT = 0xFFFFFFFE
101};
102
107enum ETraceEventId : uint32_t
108{
112};
113
123
132
140typedef uintptr_t Word;
141
145typedef Word TId;
146
150typedef int32_t Timeout;
151
155typedef int64_t Ticks;
156
160typedef int64_t Time;
161
165typedef uint64_t Cycles;
166
170typedef int32_t Weight;
171
197static constexpr TId TID_ISR_N = static_cast<TId>(0xFFFFF000U);
198
202static constexpr TId TID_NONE = static_cast<TId>(0U);
203
208static constexpr Timeout WAIT_INFINITE = INT32_MAX;
209
214static constexpr Timeout NO_WAIT = 0;
215
219static constexpr Weight NO_WEIGHT = -1;
220
224static constexpr Weight DEFAULT_WEIGHT = 1;
225
237static __stk_forceinline bool IsIsrTid(TId id) { return ((id & TID_ISR_N) == TID_ISR_N); }
238
250template <typename T> class ArrayView
251{
252public:
253 typedef T value_type;
254
257 ArrayView() : m_ptr(nullptr), m_size(0U)
258 {}
259
264 ArrayView(T *ptr, size_t size) : m_ptr(ptr), m_size(size)
265 {}
266
272 T &operator[](size_t index) const
273 {
274 STK_ASSERT(index < m_size); // enforces MISRA Rule 5-0-16
275 return UncheckedAt(index); // MISRA Rule 5-0-15 deviation centralized
276 }
277
281 T *GetPtr() { return m_ptr; }
282
286 const T *GetPtr() const { return m_ptr; }
287
291 size_t GetSize() const { return m_size; }
292
293private:
299 __stk_forceinline T &UncheckedAt(size_t index) const
300 {
301 return m_ptr[index]; // deviation: indexing non-array pointer
302 }
303
304 T *m_ptr;
305 size_t m_size;
306};
307
312#if STK_MPU
313struct MpuRegion
314{
315 Word addr;
316 Word attr;
317};
318#endif
319
324#if STK_MPU && STK_MPU_STACK_GUARD
325struct TaskMpu
326{
334 static constexpr uint8_t NUM_REGIONS = STK_MPU_TASK_REGIONS;
335
336 MpuRegion region[NUM_REGIONS];
337};
338#endif
339
345
353{
357 static constexpr uint32_t MPU_OFF = static_cast<TId>(0U);
358
363
368 MpuConfig(const MpuRegionList &regions_, uint32_t mode_) : regions(regions_), mode(mode_)
369 {}
370
372 uint32_t mode;
373};
374
379struct Stack
380{
382 uint32_t access_mode;
383#if STK_MPU && STK_MPU_STACK_GUARD
384 TaskMpu mpu;
385 #ifdef _STK_CORTEX_M_TRUSTZONE
386 TaskMpu mpu_ns;
387 #endif
388#endif
389#if STK_TLS && !STK_TLS_PREFER_REGISTER
390 Word tls;
391#endif
392#if STK_STACK_NEEDS_TASK_ID
394#endif
395};
396
401{
402public:
405 virtual const Word *GetStack() const = 0;
406
409 virtual size_t GetStackSize() const = 0;
410
418 virtual size_t GetStackSpace() const
419 {
421 const size_t total_size = stack.GetSize();
422 size_t space = 0U;
423
424 for (size_t i = 0U; i < total_size; ++i)
425 {
426 if (stack[i] == STK_STACK_MEMORY_FILLER)
427 {
428 space = i + 1U;
429 }
430 else
431 {
432 break; // terminate loop as soon as watermark ends
433 }
434 }
435
436 return space;
437 }
438};
439
443class IWaitObject : public util::DListEntry<IWaitObject, false>
444{
445public:
450
455
459 virtual TId GetTid() const = 0;
460
467 virtual void Wake(bool timeout) = 0;
468
472 virtual bool IsTimeout() const = 0;
473
479 virtual bool Tick(Timeout elapsed_ticks) = 0;
480
481protected:
484 ~IWaitObject() = default;
485};
486
492{
493public:
494#if STK_SYNC_DEBUG_NAMES
495 ITraceable() : m_trace_name(nullptr)
496 {}
497#endif
498
503 void SetTraceName(const char *name)
504 {
505 #if STK_SYNC_DEBUG_NAMES
506 m_trace_name = name;
507 #else
508 STK_UNUSED(name);
509 #endif
510 }
511
515 const char *GetTraceName() const
516 {
517 #if STK_SYNC_DEBUG_NAMES
518 return m_trace_name;
519 #else
520 return nullptr;
521 #endif
522 }
523
524protected:
527 ~ITraceable() = default;
528
529#if STK_SYNC_DEBUG_NAMES
530 const char *m_trace_name;
531#endif
532};
533
543class ISyncObject : public util::DListEntry<ISyncObject, false>
544{
545 friend class IKernelService;
547
548public:
553
558
563 static inline void AddWaitObject(IWaitObject::ListHeadType &wlist, IWaitObject *wobj)
564 {
565 STK_ASSERT(wobj->GetHead() == nullptr);
566 wlist.LinkBack(wobj);
567 }
568
573 virtual void AddWaitObject(IWaitObject *wobj) = 0;
574
580 {
581 STK_ASSERT(wobj->GetHead() == &wlist);
582 wlist.Unlink(wobj);
583 }
584
589 virtual void RemoveWaitObject(IWaitObject *wobj) = 0;
590
603 virtual bool Tick(Timeout elapsed_ticks);
604
611
618 static inline void WakeOne(IWaitObject::ListHeadType &wlist)
619 {
621 {
622 obj->Wake(false);
623 }
624 }
625
632 static inline void WakeAll(IWaitObject::ListHeadType &wlist)
633 {
635 {
636 obj->Wake(false);
637 }
638 }
639
643 virtual const IWaitObject::ListHeadType &GetWaitList() const = 0;
644
645protected:
649 ~ISyncObject() = default;
650
657 virtual void WakeOne() = 0;
658
665 virtual void WakeAll() = 0;
666
670};
671
677{
678public:
685 {
686 public:
687 explicit ScopedLock(IMutex &mutex) : m_mutex(mutex) { m_mutex.Lock(); }
688 ~ScopedLock() { m_mutex.Unlock(); }
689
690 private:
692
694 };
695
698 virtual void Lock() = 0;
699
702 virtual void Unlock() = 0;
703
704protected:
707 ~IMutex() = default;
708};
709
733class ITask : public IStackMemory
734{
735public:
750 virtual void Run() = 0;
751
756 virtual IStackMemory *GetSecureStackMemory() { return nullptr; }
757
801 virtual const MpuRegionList *GetMpuRegions() const
802 {
803 return nullptr;
804 }
805
808 virtual EAccessMode GetAccessMode() const = 0;
809
817 virtual void OnDeadlineMissed(uint32_t duration) { STK_UNUSED(duration); }
818
827 virtual void OnExit() {}
828
834 virtual Weight GetWeight() const { return DEFAULT_WEIGHT; }
835
841 virtual const char *GetTraceName() const { return nullptr; }
842};
843
853class IKernelTask : public util::DListEntry<IKernelTask, true>
854{
855public:
860
865
868 virtual ITask *GetUserTask() = 0;
869
873 virtual Stack GetUserStack() const = 0;
874
880 virtual Weight GetWeight() const = 0;
881
887 virtual void SetCurrentWeight(Weight weight) = 0;
888
894 virtual Weight GetCurrentWeight() const = 0;
895
899 virtual Timeout GetHrtPeriodicity() const = 0;
900
904 virtual Timeout GetHrtDeadline() const = 0;
905
913 virtual Timeout GetHrtRelativeDeadline() const = 0;
914
918 virtual bool IsSleeping() const = 0;
919
925 virtual void Wake() = 0;
926
927protected:
930 ~IKernelTask() = default;
931};
932
946{
947public:
954 {
955 public:
960 virtual void OnStart(Stack *&enable) = 0;
961
966 virtual void OnStop() = 0;
967
982 virtual bool OnTick(Stack *&idle, Stack *&enable
983 #if STK_TICKLESS_IDLE
984 , Timeout &ticks
985 #endif
986 ) = 0;
987
991 virtual void OnTaskSwitch(Word caller_SP) = 0;
992
997 virtual void OnTaskSleep(Word caller_SP, Timeout ticks) = 0;
998
1004 virtual bool OnTaskSleepUntil(Word caller_SP, Ticks timestamp) = 0;
1005
1009 virtual void OnTaskExit(Stack *stack) = 0;
1010
1017 virtual EWaitResult OnTaskWait(Word caller_SP, ISyncObject *sync_obj, IMutex *mutex, Timeout timeout) = 0;
1018
1023 virtual TId OnGetTid(Word caller_SP) = 0;
1024
1028 virtual void OnSuspend(bool suspended) = 0;
1029 };
1030
1036 {
1037 public:
1041 virtual bool OnSleep(Timeout sleep_ticks)
1042 {
1043 STK_UNUSED(sleep_ticks);
1044 return false;
1045 }
1046
1051 virtual bool OnHardFault()
1052 {
1053 return false;
1054 }
1055
1067 virtual const MpuConfig *OnConfigureMpu() const
1068 {
1069 return nullptr;
1070 }
1071
1081 virtual bool OnException(EHwException exc_id, TId tid, const struct FaultContext *const ctx)
1082 {
1083 STK_UNUSED(exc_id);
1084 STK_UNUSED(tid);
1085 STK_UNUSED(ctx);
1086 return false;
1087 }
1088 };
1089
1097 virtual void Initialize(IEventHandler *event_handler, IKernelService *service, uint32_t resolution_us, Stack *exit_trap) = 0;
1098
1103 virtual void Start() = 0;
1104
1107 virtual void Stop() = 0;
1108
1115 virtual void InitStack(EStackType stack_type, Stack *stack, IStackMemory *stack_memory, ITask *user_task) = 0;
1116
1121 virtual uint32_t GetTickResolution() const = 0;
1122
1127 virtual Cycles GetSysTimerCount() const = 0;
1128
1133 virtual uint32_t GetSysTimerFrequency() const = 0;
1134
1137 virtual void SwitchToNext() = 0;
1138
1143 virtual void Sleep(Timeout ticks) = 0;
1144
1152 virtual bool SleepUntil(Ticks timestamp) = 0;
1153
1167 virtual EWaitResult Wait(ISyncObject *sobj, IMutex *mutex, Timeout timeout) = 0;
1168
1178 virtual void ProcessTick() = 0;
1179
1183 virtual void ProcessHardFault() = 0;
1184
1190 virtual void SetEventOverrider(IEventOverrider *overrider, bool non_secure = false) = 0;
1191
1196 virtual Word GetCallerSP() const = 0;
1197
1203 virtual TId GetTid() const = 0;
1204
1211 virtual Timeout Suspend() = 0;
1212
1218 virtual void Resume(Timeout elapsed_ticks) = 0;
1219
1220protected:
1223 ~IPlatform() = default;
1224};
1225
1250{
1251public:
1256 virtual void AddTask(IKernelTask *task) = 0;
1257
1262 virtual void RemoveTask(IKernelTask *task) = 0;
1263
1267 virtual IKernelTask *GetFirst() = 0;
1268
1275 virtual IKernelTask *GetNext() = 0;
1276
1280 virtual size_t GetSize() const = 0;
1281
1286 virtual void OnTaskSleep(IKernelTask *task) = 0;
1287
1292 virtual void OnTaskWake(IKernelTask *task) = 0;
1293
1315 {
1316 STK_UNUSED(task);
1317 return false;
1318 }
1319
1332 virtual void OnTaskWeightChange(IKernelTask *task, Weight old_weight)
1333 {
1334 STK_UNUSED(task);
1335 STK_UNUSED(old_weight);
1336 }
1337
1338protected:
1342};
1343
1350{
1351public:
1362
1373 virtual void Initialize(uint32_t resolution_us = PERIODICITY_DEFAULT) = 0;
1374
1380 virtual void AddTask(ITask *user_task) = 0;
1381
1389 virtual void AddTask(ITask *user_task, Timeout periodicity_tc, Timeout deadline_tc, Timeout start_delay_tc) = 0;
1390
1401 virtual void RemoveTask(ITask *user_task) = 0;
1402
1410 virtual void ScheduleTaskRemoval(ITask *user_task) = 0;
1411
1418 virtual void SuspendTask(ITask *user_task, bool &suspended) = 0;
1419
1423 virtual void ResumeTask(ITask *user_task) = 0;
1424
1431
1437 virtual size_t EnumerateTasks(ArrayView<ITask *> user_tasks) = 0;
1438
1459 template <size_t TMaxCount, typename TCallback>
1460 size_t EnumerateTasksT(TCallback &&callback)
1461 {
1462 STK_STATIC_ASSERT(TMaxCount > 0U);
1463
1464 ITask *tasks[TMaxCount] = {};
1465 size_t count = EnumerateTasks(ArrayView<ITask *>(tasks, TMaxCount));
1466 size_t i = 0U;
1467 bool fetch_next = true;
1468
1469 while ((i < count) && fetch_next)
1470 {
1471 fetch_next = callback(tasks[i]);
1472 ++i;
1473 }
1474
1475 return i;
1476 }
1477
1482 virtual void Start() = 0;
1483
1488 virtual EKernelState GetState() const = 0;
1489
1493 virtual IPlatform *GetPlatform() = 0;
1494
1499
1500protected:
1503 ~IKernel() = default;
1504};
1505
1515{
1516public:
1520
1526 virtual TId GetTid() const = 0;
1527
1532 virtual Ticks GetTicks() const = 0;
1533
1539 virtual uint32_t GetTickResolution() const = 0;
1540
1545 virtual Cycles GetSysTimerCount() const = 0;
1546
1551 virtual uint32_t GetSysTimerFrequency() const = 0;
1552
1560 virtual void Delay(Timeout ticks) = 0;
1561
1568 virtual void Sleep(Timeout ticks) = 0;
1569
1577 virtual bool SleepUntil(Ticks timestamp) = 0;
1578
1584 virtual void SleepCancel(TId task_id) = 0;
1585
1590 virtual void SwitchToNext() = 0;
1591
1605 virtual EWaitResult Wait(ISyncObject *sobj, IMutex *mutex, Timeout timeout) = 0;
1606
1614 virtual void Wake(ISyncObject *sobj, bool all) = 0;
1615
1626 virtual Timeout Suspend() = 0;
1627
1639 virtual void Resume(Timeout elapsed_ticks) = 0;
1640
1646 virtual void InheritWeight(TId tid, Weight weight) = 0;
1647
1654 virtual void RestoreWeight(TId tid, ISyncObject *sobj = nullptr) = 0;
1655
1656protected:
1659 ~IKernelService() = default;
1660
1664 {
1665 return sobj->GetWaitList();
1666 }
1667};
1668
1669} // namespace stk
1670
1671#endif /* STK_COMMON_H_ */
Compiler and platform low-level definitions for STK.
#define STK_UNUSED(X)
Explicitly marks a variable as unused to suppress compiler warnings.
Definition stk_defs.h:654
#define __stk_forceinline
Forces compiler to always inline the decorated function, regardless of optimisation level.
Definition stk_defs.h:218
#define STK_NONCOPYABLE_CLASS(TYPE)
Disables copy construction and assignment for a class.
Definition stk_defs.h:647
#define STK_ASSERT(e)
Runtime assertion. Halts execution if the expression e evaluates to false.
Definition stk_defs.h:455
#define STK_STACK_SIZE_MIN
Minimum stack size in elements of Word, shared by all stack allocation lower-bound checks.
Definition stk_defs.h:579
#define STK_MPU_TASK_REGIONS
Number of hardware MPU region slots reserved per task (stk::TaskMpu::NUM_REGIONS).
Definition stk_defs.h:163
#define STK_STACK_MEMORY_FILLER
Sentinel value written to the entire stack region at initialization (stack watermark pattern).
Definition stk_defs.h:502
#define STK_STATIC_ASSERT(X)
Compile-time assertion. Produces a compilation error if X is false.
Definition stk_defs.h:492
Intrusive doubly-linked list implementation used internally by the kernel.
Namespace of STK package.
uintptr_t Word
Native processor word type.
Definition stk_common.h:140
ArrayView< const struct MpuRegionConfig > MpuRegionList
Definition stk_common.h:344
static constexpr TId TID_ISR_N
Bitmask sentinel for ISR-context task identifiers.
Definition stk_common.h:197
EAccessMode
Hardware access modes by the task.
Definition stk_common.h:36
@ ACCESS_USER
Unprivileged access mode (access to some hardware is restricted, see CPU manual for details)....
Definition stk_common.h:37
@ ACCESS_PRIVILEGED
Privileged access mode (access to hardware is fully unrestricted).
Definition stk_common.h:38
@ ACCESS_SECURE
Secure access mode (ARM TrustZone, Secure binary).
Definition stk_common.h:39
static constexpr Timeout NO_WAIT
Timeout value: return immediately if the synchronization object is not yet signaled (non-blocking pol...
Definition stk_common.h:214
EWaitResult
Wait result (see IKernelService::Wait).
Definition stk_common.h:118
@ WAIT_RESULT_FAIL
IKernelService::Wait returned with error without waiting.
Definition stk_common.h:119
@ WAIT_RESULT_TIMEOUT
The wake was caused by a timeout expiry.
Definition stk_common.h:121
@ WAIT_RESULT_SIGNAL
The wake was caused by a signal.
Definition stk_common.h:120
EConsts
Constants.
Definition stk_common.h:88
@ PERIODICITY_DEFAULT
Default periodicity (microseconds), 1 millisecond.
Definition stk_common.h:90
@ 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:91
@ PERIODICITY_MAX
Maximum periodicity (microseconds), 99 milliseconds (note: this value is the highest working on a rea...
Definition stk_common.h:89
int64_t Ticks
Ticks value.
Definition stk_common.h:155
int32_t Timeout
Timeout time (ticks).
Definition stk_common.h:150
static bool IsIsrTid(TId id)
Test whether a task identifier represents an ISR context.
Definition stk_common.h:237
int64_t Time
Time value.
Definition stk_common.h:160
ESystemTaskId
System task id.
Definition stk_common.h:98
@ SYS_TASK_ID_EXIT
Exit trap.
Definition stk_common.h:100
@ SYS_TASK_ID_SLEEP
Sleep trap.
Definition stk_common.h:99
static constexpr Weight DEFAULT_WEIGHT
Weight value: default weight of value (1) (see SwitchStrategySmoothWeightedRoundRobin).
Definition stk_common.h:224
static constexpr Weight NO_WEIGHT
Weight value: weight is not set.
Definition stk_common.h:219
static constexpr Timeout WAIT_INFINITE
Timeout value: block indefinitely until the synchronization object is signaled.
Definition stk_common.h:208
static constexpr TId TID_NONE
Reserved task/thread id representing zero/none thread id.
Definition stk_common.h:202
ETraceEventId
Trace event identifiers for tracing task suspension and resume with debugging tools (e....
Definition stk_common.h:108
@ TRACE_EVENT_UNKNOWN
Unknown / uninitialized trace event.
Definition stk_common.h:109
@ TRACE_EVENT_SLEEP
Task entered sleep / blocked state.
Definition stk_common.h:111
@ TRACE_EVENT_SWITCH
Task context switch event (task became active).
Definition stk_common.h:110
EStackType
Stack type.
Definition stk_common.h:78
@ STACK_SLEEP_TRAP
Stack of the Sleep trap.
Definition stk_common.h:80
@ STACK_USER_TASK
Stack of the user task.
Definition stk_common.h:79
@ STACK_EXIT_TRAP
Stack of the Exit trap.
Definition stk_common.h:81
uint64_t Cycles
Cycles value.
Definition stk_common.h:165
EHwException
Hardware exception id (see IPlatform::IEventOverrider::OnException).
Definition stk_common.h:128
@ HW_EXCEPT_MEMACCESS
MemManage on ARM / Page Fault or PMP violation on RISC-V.
Definition stk_common.h:130
@ HW_EXCEPT_FATAL
HardFault on ARM / Unhandled Fatal Trap on RISC-V.
Definition stk_common.h:129
Word TId
Task (thread) id.
Definition stk_common.h:145
int32_t Weight
Weight value (aka priority).
Definition stk_common.h:170
EKernelMode
Kernel operating mode.
Definition stk_common.h:46
@ KERNEL_TICKLESS
Tickless mode. To use this mode STK_TICKLESS_IDLE must be defined to 1 in stk_config....
Definition stk_common.h:51
@ KERNEL_SYNC
Synchronization support (see Event).
Definition stk_common.h:50
@ KERNEL_HRT
Hard Real-Time (HRT) behavior (tasks are scheduled periodically and have an execution deadline,...
Definition stk_common.h:49
@ KERNEL_STATIC
All tasks are static and can not exit.
Definition stk_common.h:47
@ KERNEL_DYNAMIC
Tasks can be added or removed and therefore exit when done.
Definition stk_common.h:48
EKernelPanicId
Identifies the source of a kernel panic.
Definition stk_common.h:58
@ KERNEL_PANIC_UNKNOWN_SVC
Unknown service command received by SVC handler.
Definition stk_common.h:66
@ KERNEL_PANIC_BAD_STACK_TYPE
Stack type is unknown.
Definition stk_common.h:69
@ KERNEL_PANIC_NS_ACCESS
Non-secure access to protected resource.
Definition stk_common.h:70
@ KERNEL_PANIC_BAD_MODE
Kernel is in bad/unsupported mode for the current operation.
Definition stk_common.h:68
@ KERNEL_PANIC_HRT_HARD_FAULT
Kernel running in KERNEL_HRT mode reported deadline failure of the task.
Definition stk_common.h:63
@ KERNEL_PANIC_CS_NESTING_OVERFLOW
Critical section nesting limit exceeded: violation of STK_CS_NESTINGS_MAX.
Definition stk_common.h:65
@ KERNEL_PANIC_NONE
Panic is absent (no fault).
Definition stk_common.h:59
@ KERNEL_PANIC_CPU_EXCEPTION
CPU reported an exception and halted execution.
Definition stk_common.h:64
@ KERNEL_PANIC_STACK_CORRUPT
Stack integrity check failed.
Definition stk_common.h:61
@ KERNEL_PANIC_SPINLOCK_DEADLOCK
Spin-lock timeout expired: lock owner never released.
Definition stk_common.h:60
@ KERNEL_PANIC_BAD_STATE
Kernel entered unexpected (bad) state.
Definition stk_common.h:67
@ KERNEL_PANIC_ASSERT
Internal assertion failed (maps from STK_ASSERT).
Definition stk_common.h:62
Internal utility namespace containing data structure helpers (linked lists, etc.) used by the kernel ...
ARMv7-M/ARMv8-M system fault exception context state capture.
Lightweight, non-owning view over a contiguous sequence of elements.
Definition stk_common.h:251
ArrayView()
Construct an empty ArrayView with a null pointer and zero size.
Definition stk_common.h:257
T & UncheckedAt(size_t index) const
Deviation MISRA-CPP-2008-5-0-15 \reason Array indexing on a base pointer is required for a dynamic-si...
Definition stk_common.h:299
T * GetPtr()
Get pointer to the beginning of elements in the view.
Definition stk_common.h:281
ArrayView(T *ptr, size_t size)
Construct an ArrayView from a raw pointer and size.
Definition stk_common.h:264
size_t GetSize() const
Get number of elements in the view.
Definition stk_common.h:291
const T * GetPtr() const
Get constant pointer to the beginning of elements in the view.
Definition stk_common.h:286
T & operator[](size_t index) const
Subscript operator for element access.
Definition stk_common.h:272
const struct MpuRegionConfig * m_ptr
Definition stk_common.h:304
Aggregated hardware MPU setup configuration and state descriptor.
Definition stk_common.h:353
MpuRegionList regions
Fixed-capacity collection of configured MPU region descriptors.
Definition stk_common.h:371
MpuConfig()
Default constructor initializing an empty, disabled MPU configuration.
Definition stk_common.h:361
static constexpr uint32_t MPU_OFF
Definition stk_common.h:357
uint32_t mode
Mode flags (hardware-dependent, see hw::mpu::EMpuConfigFlags).
Definition stk_common.h:372
MpuConfig(const MpuRegionList &regions_, uint32_t mode_)
Parameterized constructor initializing MPU region settings and enable state.
Definition stk_common.h:368
Stack descriptor.
Definition stk_common.h:380
uint32_t access_mode
Bitfield with hardware access mode of the task (see EAccessMode).
Definition stk_common.h:382
TId tid
Task id (see STK_SEGGER_SYSVIEW, STK_MPU_STACK_GUARD).
Definition stk_common.h:393
Word SP
Offset 0: Stack Pointer (SP) register (note: must always be at offset 0).
Definition stk_common.h:381
Interface for a stack memory region.
Definition stk_common.h:401
virtual size_t GetStackSize() const =0
Get number of elements of the stack memory array.
virtual size_t GetStackSpace() const
Get available stack space.
Definition stk_common.h:418
virtual const Word * GetStack() const =0
Get pointer to the stack memory.
Wait object.
Definition stk_common.h:444
DLEntryType ListEntryType
List entry type of IWaitObject elements.
Definition stk_common.h:454
DLHeadType ListHeadType
List head type for IWaitObject elements.
Definition stk_common.h:449
virtual TId GetTid() const =0
Get thread Id of the task owning .
virtual bool IsTimeout() const =0
Check if task woke up due to a timeout.
virtual void Wake(bool timeout)=0
Wake task.
~IWaitObject()=default
Destructor.
virtual bool Tick(Timeout elapsed_ticks)=0
Update wait object's waiting time.
Traceable object.
Definition stk_common.h:492
const char * GetTraceName() const
Get name.
Definition stk_common.h:515
void SetTraceName(const char *name)
Set name.
Definition stk_common.h:503
~ITraceable()=default
Destructor.
Synchronization object interface.
Definition stk_common.h:544
virtual void WakeAll()=0
Wake all tasks currently in the wait list.
virtual bool Tick(Timeout elapsed_ticks)
Called by kernel on every system tick to handle timeout logic of waiting tasks.
Definition stk_helper.h:299
DLEntryType ListEntryType
List entry type of ISyncObject elements.
Definition stk_common.h:557
~ISyncObject()=default
Destructor.
virtual void WakeOne()=0
Wake the first task in the wait list (FIFO order).
virtual IWaitObject::ListHeadType & GetWaitList()=0
Get list of tasks blocked on this object.
friend class tz::nsec::util::CmseISyncObjectWrapper
Definition stk_common.h:546
DLHeadType ListHeadType
List head type for ISyncObject elements.
Definition stk_common.h:552
static void AddWaitObject(IWaitObject::ListHeadType &wlist, IWaitObject *wobj)
Called by kernel when a new task starts waiting on this event.
Definition stk_common.h:563
friend class IKernelService
Definition stk_common.h:545
static void WakeAll(IWaitObject::ListHeadType &wlist)
Wake all tasks currently in the wait list.
Definition stk_common.h:632
static void RemoveWaitObject(IWaitObject::ListHeadType &wlist, IWaitObject *wobj)
Called by kernel when a waiting task is being removed (timeout expired, wait aborted,...
Definition stk_common.h:579
virtual const IWaitObject::ListHeadType & GetWaitList() const =0
Get list of tasks blocked on this object.
Weight FindWeightHigherThan(Weight comp) const
Find higher weight within linked wait objects.
Definition stk_helper.h:334
virtual void RemoveWaitObject(IWaitObject *wobj)=0
Called by kernel when a waiting task is being removed (timeout expired, wait aborted,...
virtual void AddWaitObject(IWaitObject *wobj)=0
Called by kernel when a new task starts waiting on this event.
static void WakeOne(IWaitObject::ListHeadType &wlist)
Wake the first task in the wait list (FIFO order).
Definition stk_common.h:618
Interface for mutex synchronization primitive.
Definition stk_common.h:677
~IMutex()=default
Destructor.
virtual void Unlock()=0
Unlock the mutex.
virtual void Lock()=0
Lock the mutex.
ScopedLock(IMutex &mutex)
Definition stk_common.h:687
Interface for a user task.
Definition stk_common.h:734
virtual IStackMemory * GetSecureStackMemory()
Get pointer to the stack memory.
Definition stk_common.h:756
virtual Weight GetWeight() const
Get static base weight of the task.
Definition stk_common.h:834
virtual const MpuRegionList * GetMpuRegions() const
Get up to (STK_MPU_TASK_REGIONS - 1) application-defined MPU regions for this task.
Definition stk_common.h:801
virtual EAccessMode GetAccessMode() const =0
Get hardware access mode of the user task.
virtual const char * GetTraceName() const
Get task trace name set by application.
Definition stk_common.h:841
virtual void Run()=0
Entry point of the user task.
virtual void OnExit()
Called by the kernel before removal from the scheduling (see stk::KERNEL_DYNAMIC).
Definition stk_common.h:827
virtual void OnDeadlineMissed(uint32_t duration)
Called by the scheduler if deadline of the task is missed when Kernel is operating in Hard Real-Time ...
Definition stk_common.h:817
Scheduling-strategy-facing interface for a kernel task slot.
Definition stk_common.h:854
virtual void Wake()=0
Wake a sleeping task on the next scheduling tick.
DLEntryType ListEntryType
List entry type of IKernelTask elements.
Definition stk_common.h:864
virtual Weight GetCurrentWeight() const =0
Get the current dynamic weight value of this task.
virtual Weight GetWeight() const =0
Get static base weight assigned to the task.
virtual Timeout GetHrtRelativeDeadline() const =0
Get HRT task's relative deadline.
virtual Timeout GetHrtDeadline() const =0
Get HRT task deadline (max allowed task execution time).
DLHeadType ListHeadType
List head type for IKernelTask elements.
Definition stk_common.h:859
virtual bool IsSleeping() const =0
Check whether the task is currently sleeping.
virtual Timeout GetHrtPeriodicity() const =0
Get HRT task execution periodicity.
virtual ITask * GetUserTask()=0
Get user task.
virtual void SetCurrentWeight(Weight weight)=0
Set the current dynamic weight value used by the scheduling strategy.
virtual Stack GetUserStack() const =0
Get user task's Stack info.
~IKernelTask()=default
Destructor.
Interface for a platform driver.
Definition stk_common.h:946
virtual void ProcessTick()=0
Process one tick.
virtual Word GetCallerSP() const =0
Get caller's Stack Pointer (SP).
virtual TId GetTid() const =0
Get thread Id.
~IPlatform()=default
Destructor.
virtual void Initialize(IEventHandler *event_handler, IKernelService *service, uint32_t resolution_us, Stack *exit_trap)=0
Initialize scheduler's context.
virtual EWaitResult Wait(ISyncObject *sobj, IMutex *mutex, Timeout timeout)=0
Put calling process into a waiting state until synchronization object is signaled or timeout occurs.
virtual Timeout Suspend()=0
Suspend scheduling.
virtual Cycles GetSysTimerCount() const =0
Get system timer count value.
virtual void Start()=0
Start scheduling.
virtual void Stop()=0
Stop scheduling.
virtual uint32_t GetSysTimerFrequency() const =0
Get system timer frequency.
virtual void Resume(Timeout elapsed_ticks)=0
Resume scheduling after a prior Suspend() call.
virtual void SwitchToNext()=0
Switch to a next task.
virtual void Sleep(Timeout ticks)=0
Put calling process into a sleep state.
virtual uint32_t GetTickResolution() const =0
Get resolution of the system tick timer in microseconds. Resolution means a number of microseconds be...
virtual void InitStack(EStackType stack_type, Stack *stack, IStackMemory *stack_memory, ITask *user_task)=0
Initialize stack memory of the user task.
virtual void SetEventOverrider(IEventOverrider *overrider, bool non_secure=false)=0
Set platform event overrider.
virtual void ProcessHardFault()=0
Cause a hard fault of the system.
virtual bool SleepUntil(Ticks timestamp)=0
Put calling process into a sleep state until the specified timestamp.
Interface for a back-end event handler.
Definition stk_common.h:954
virtual void OnTaskExit(Stack *stack)=0
Called from the Thread process when task finished (its Run function exited by return).
virtual void OnStart(Stack *&enable)=0
Called by ISR handler to notify that scheduling is about to start.
virtual EWaitResult OnTaskWait(Word caller_SP, ISyncObject *sync_obj, IMutex *mutex, Timeout timeout)=0
Called from the Thread process when task needs to wait.
virtual TId OnGetTid(Word caller_SP)=0
Called from the Thread process when for getting task/thread id of the process.
virtual bool OnTick(Stack *&idle, Stack *&enable, Timeout &ticks)=0
Called by ISR handler to notify about the next system tick.
virtual void OnStop()=0
Called by driver to notify that scheduling is stopped.
virtual void OnTaskSleep(Word caller_SP, Timeout ticks)=0
Called by Thread process (via IKernelService::Sleep) for exclusion of the calling process from schedu...
virtual void OnSuspend(bool suspended)=0
Called from the Thread process to suspend scheduling.
virtual void OnTaskSwitch(Word caller_SP)=0
Called by Thread process (via IKernelService::SwitchToNext) to switch to a next task.
virtual bool OnTaskSleepUntil(Word caller_SP, Ticks timestamp)=0
Called by Thread process (via IKernelService::SleepUntil) for exclusion of the calling process from s...
Interface for a platform event overrider.
virtual bool OnSleep(Timeout sleep_ticks)
Called by the Kernel when it is entering a sleep mode.
virtual bool OnHardFault()
Called by Kernel when hard fault happens.
virtual bool OnException(EHwException exc_id, TId tid, const struct FaultContext *const ctx)
Called by platform driver when hardware exception occurred.
virtual const MpuConfig * OnConfigureMpu() const
Called by the platform driver during initialization to obtain global, application-defined MPU configu...
Interface for a task switching strategy implementation.
virtual void RemoveTask(IKernelTask *task)=0
Remove task.
virtual void OnTaskSleep(IKernelTask *task)=0
Notification that a task has entered sleep/blocked state.
virtual void OnTaskWake(IKernelTask *task)=0
Notification that a task is becoming runnable again.
~ITaskSwitchStrategy()=default
Destructor.
virtual bool OnTaskDeadlineMissed(IKernelTask *task)
Notification that a task has exceeded its HRT deadline; returns whether the strategy can recover with...
virtual IKernelTask * GetFirst()=0
Get first task.
virtual void OnTaskWeightChange(IKernelTask *task, Weight old_weight)
Notification that a runnable task's scheduling weight has changed.
virtual size_t GetSize() const =0
Get number of tasks currently managed by this strategy.
virtual IKernelTask * GetNext()=0
Advance the internal iterator and return the next runnable task.
virtual void AddTask(IKernelTask *task)=0
Add task.
Interface for the implementation of the kernel of the scheduler. It supports Soft and Hard Real-Time ...
EKernelState
Kernel state.
@ KSTATE_RUNNING
Initialized and running, IKernel::Start() was called successfully.
@ KSTATE_SUSPENDED
Scheduling is suspended with IKernelService::Suspend().
@ KSTATE_INACTIVE
Not ready, IKernel::Initialize() must be called.
@ KSTATE_READY
Ready to start, IKernel::Start() must be called.
virtual void ResumeTask(ITask *user_task)=0
Resume task.
virtual void SuspendTask(ITask *user_task, bool &suspended)=0
Suspend task.
virtual IPlatform * GetPlatform()=0
Get platform driver instance.
virtual size_t EnumerateTasks(ArrayView< ITask * > user_tasks)=0
Enumerate user tasks.
virtual EKernelState GetState() const =0
Get a snapshot of the kernel state.
~IKernel()=default
Destructor.
virtual void RemoveTask(ITask *user_task)=0
Remove a previously added task from the kernel when it is not started.
virtual void AddTask(ITask *user_task)=0
Add user task.
size_t EnumerateTasksT(TCallback &&callback)
Enumerate tasks, invoking a callback for each active task.
virtual size_t EnumerateKernelTasks(ArrayView< IKernelTask * > tasks)=0
Enumerate kernel tasks.
virtual void ScheduleTaskRemoval(ITask *user_task)=0
Schedule task removal from scheduling (exit).
virtual void Initialize(uint32_t resolution_us=PERIODICITY_DEFAULT)=0
Initialize kernel.
virtual ITaskSwitchStrategy * GetSwitchStrategy()=0
Get switch strategy instance.
virtual void Start()=0
Start kernel scheduling.
virtual void AddTask(ITask *user_task, Timeout periodicity_tc, Timeout deadline_tc, Timeout start_delay_tc)=0
Add user task.
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.
virtual void Wake(ISyncObject *sobj, bool all)=0
Wake one or all tasks currently waiting on a synchronization object.
virtual void SleepCancel(TId task_id)=0
Cancel sleep of the task.
virtual void InheritWeight(TId tid, Weight weight)=0
Inherit weight for the task.
static IKernelService * GetInstance()
Get CPU-local instance of the kernel service.
virtual uint32_t GetTickResolution() const =0
Get number of microseconds in one tick.
~IKernelService()=default
Destructor.
virtual bool SleepUntil(Ticks timestamp)=0
Put calling process into a sleep state until the specified timestamp.
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 Resume(Timeout elapsed_ticks)=0
Resume scheduling after a prior Suspend() call.
virtual void Sleep(Timeout ticks)=0
Put calling process into a sleep state.
static IWaitObject::ListHeadType & GetWaitList(ISyncObject *sobj)
IWaitObject::GetWaitList() access helper.
virtual EWaitResult Wait(ISyncObject *sobj, IMutex *mutex, Timeout timeout)=0
Put calling process into a waiting state until synchronization object is signaled or timeout occurs.
virtual Cycles GetSysTimerCount() const =0
Get system timer count value.
virtual uint32_t GetSysTimerFrequency() const =0
Get system timer frequency.
virtual Timeout Suspend()=0
Suspend scheduling.
virtual void RestoreWeight(TId tid, ISyncObject *sobj=nullptr)=0
Restore weight of the task to the original value.
virtual void Delay(Timeout ticks)=0
Delay calling process.
void LinkBack(DLEntryType *entry)
Append entry to the back of the list (pointer overload).
void Unlink(DLEntryType *entry)
Remove entry from this list.
DLEntryType * GetFirst()
Get the first (front) entry without removing it.
Intrusive doubly-linked list node. Embed this as a base class in any object (T) that needs to partici...
DListEntry< IWaitObject, TClosedLoop > DLEntryType
DLHeadType * GetHead()
Get the list head this entry currently belongs to.
DListHead< IWaitObject, TClosedLoop > DLHeadType
static __stk_forceinline TTargetType * ListEntryToParent(TSourceType *const lentry)
Safely casts an intrusive list entry to its concrete parent container object type.
MPU region descriptor.
MPU descriptor of the task.