SuperTinyKernel™ RTOS 1.06.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
31// ARM TrustZone
32#ifdef _STK_CORTEX_M_TRUSTZONE_NON_SECURE
33extern "C" void NSC_stk_ISyncObject_WakeOne(ISyncObject *sobj);
34extern "C" void NSC_stk_ISyncObject_WakeAll(ISyncObject *sobj);
35#endif
36
41enum EAccessMode : uint32_t
42{
44 ACCESS_PRIVILEGED = (1 << 0),
45 ACCESS_SECURE = (1 << 1),
46};
47
51enum EKernelMode : uint8_t
52{
53 KERNEL_STATIC = (1 << 0),
54 KERNEL_DYNAMIC = (1 << 1),
55 KERNEL_HRT = (1 << 2),
56 KERNEL_SYNC = (1 << 3),
57 KERNEL_TICKLESS = (1 << 4),
58};
59
77
88
98
103{
104 SYS_TASK_ID_SLEEP = 0xFFFFFFFF,
105 SYS_TASK_ID_EXIT = 0xFFFFFFFE
106};
107
118
128
136typedef uintptr_t Word;
137
141typedef Word TId;
142
146typedef int32_t Timeout;
147
151typedef int64_t Ticks;
152
156typedef int64_t Time;
157
161typedef uint64_t Cycles;
162
166typedef int32_t Weight;
167
193constexpr TId TID_ISR_N = static_cast<TId>(0xFFFFF000U);
194
198constexpr TId TID_NONE = static_cast<TId>(0U);
199
204constexpr Timeout WAIT_INFINITE = INT32_MAX;
205
210constexpr Timeout NO_WAIT = 0;
211
215constexpr Weight NO_WEIGHT = -1;
216
220constexpr Weight DEFAULT_WEIGHT = 1;
221
233static __stk_forceinline bool IsIsrTid(TId id) { return ((id & TID_ISR_N) == TID_ISR_N); }
234
246template <typename T> class ArrayView
247{
248public:
253 ArrayView(T *ptr, size_t size) : m_ptr(ptr), m_size(size)
254 {}
255
261 T &operator[](size_t index) const
262 {
263 STK_ASSERT(index < m_size);
264 //MISRA 5-0-15 deviation: bounds are checked via STK_ASSERT
265 return m_ptr[index];
266 }
267
271 size_t GetSize() const { return m_size; }
272
273private:
274 T *m_ptr;
275 size_t m_size;
276};
277
288template <size_t TStackSize> struct StackMemoryDef
289{
290 enum { SIZE = TStackSize };
291
296};
297
301struct Stack
302{
304 uint32_t access_mode;
305#if STK_STACK_NEEDS_TASK_ID
306 TId tid;
307#endif
308#ifdef _STK_ARCH_ARM_CORTEX_M
309#if STK_TLS && !STK_TLS_PREFER_REGISTER
310 Word tls;
311#endif
312#endif
313};
314
319{
320public:
323 virtual const Word *GetStack() const = 0;
324
327 virtual size_t GetStackSize() const = 0;
328
336 virtual size_t GetStackSpace() const
337 {
339 const size_t total_size = stack.GetSize();
340 size_t space = 0U;
341
342 for (size_t i = 0U; i < total_size; ++i)
343 {
344 if (stack[i] == STK_STACK_MEMORY_FILLER)
345 {
346 space = i + 1U;
347 }
348 else
349 {
350 break; // terminate loop as soon as watermark ends
351 }
352 }
353
354 return space;
355 }
356};
357
361class IWaitObject : public util::DListEntry<IWaitObject, false>
362{
363public:
368
373
377 virtual TId GetTid() const = 0;
378
385 virtual void Wake(bool timeout) = 0;
386
390 virtual bool IsTimeout() const = 0;
391
397 virtual bool Tick(Timeout elapsed_ticks) = 0;
398
399protected:
402 ~IWaitObject() = default;
403};
404
410{
411public:
412#if STK_SYNC_DEBUG_NAMES
413 ITraceable() : m_trace_name(nullptr)
414 {}
415#endif
416
421 void SetTraceName(const char *name)
422 {
423 #if STK_SYNC_DEBUG_NAMES
424 m_trace_name = name;
425 #else
426 STK_UNUSED(name);
427 #endif
428 }
429
433 const char *GetTraceName() const
434 {
435 #if STK_SYNC_DEBUG_NAMES
436 return m_trace_name;
437 #else
438 return nullptr;
439 #endif
440 }
441
442protected:
445 ~ITraceable() = default;
446
447#if STK_SYNC_DEBUG_NAMES
448 const char *m_trace_name;
449#endif
450};
451
455class ISyncObject : public util::DListEntry<ISyncObject, false>
456{
457 friend class IKernel;
459
460public:
465
470
475 static inline void AddWaitObject(IWaitObject::ListHeadType &wlist, IWaitObject *wobj)
476 {
477 STK_ASSERT(wobj->GetHead() == nullptr);
478 wlist.LinkBack(wobj);
479 }
480
485 virtual void AddWaitObject(IWaitObject *wobj)
486 {
488 }
489
495 {
496 STK_ASSERT(wobj->GetHead() == &wlist);
497 wlist.Unlink(wobj);
498 }
499
504 virtual void RemoveWaitObject(IWaitObject *wobj)
505 {
507 }
508
521 virtual bool Tick(Timeout elapsed_ticks);
522
529
536 static inline void WakeOne(IWaitObject::ListHeadType &wlist)
537 {
539 {
540 obj->Wake(false);
541 }
542 }
543
550 static inline void WakeAll(IWaitObject::ListHeadType &wlist)
551 {
553 {
554 obj->Wake(false);
555 }
556 }
557
558protected:
563 {}
564
568 ~ISyncObject() = default;
569
576 virtual void WakeOne()
577 {
578 #ifdef _STK_CORTEX_M_TRUSTZONE_NON_SECURE
579 NSC_stk_ISyncObject_WakeOne(this);
580 #else
582 #endif
583 }
584
591 virtual void WakeAll()
592 {
593 #ifdef _STK_CORTEX_M_TRUSTZONE_NON_SECURE
594 NSC_stk_ISyncObject_WakeAll(this);
595 #else
597 #endif
598 }
599
604
606};
607
613{
614public:
621 {
622 public:
623 explicit ScopedLock(IMutex &mutex) : m_mutex(mutex) { m_mutex.Lock(); }
624 ~ScopedLock() { m_mutex.Unlock(); }
625
626 private:
628
630 };
631
634 virtual void Lock() = 0;
635
638 virtual void Unlock() = 0;
639
640protected:
643 ~IMutex() = default;
644};
645
669class ITask : public IStackMemory
670{
671public:
686 virtual void Run() = 0;
687
692 virtual IStackMemory *GetSecureStackMemory() { return nullptr; }
693
696 virtual EAccessMode GetAccessMode() const = 0;
697
705 virtual void OnDeadlineMissed(uint32_t duration) { STK_UNUSED(duration); }
706
715 virtual void OnExit() {}
716
722 virtual Weight GetWeight() const { return DEFAULT_WEIGHT; }
723
728 TId GetId() const;
729
735 virtual const char *GetTraceName() const { return nullptr; }
736};
737
747class IKernelTask : public util::DListEntry<IKernelTask, true>
748{
749public:
754
759
762 virtual ITask *GetUserTask() = 0;
763
767 virtual Stack GetUserStack() const = 0;
768
774 virtual Weight GetWeight() const = 0;
775
781 virtual void SetCurrentWeight(Weight weight) = 0;
782
788 virtual Weight GetCurrentWeight() const = 0;
789
793 virtual Timeout GetHrtPeriodicity() const = 0;
794
798 virtual Timeout GetHrtDeadline() const = 0;
799
807 virtual Timeout GetHrtRelativeDeadline() const = 0;
808
812 virtual bool IsSleeping() const = 0;
813
819 virtual void Wake() = 0;
820
821protected:
824 ~IKernelTask() = default;
825};
826
840{
841public:
848 {
849 public:
854 virtual void OnStart(Stack *&active) = 0;
855
860 virtual void OnStop() = 0;
861
876 virtual bool OnTick(Stack *&idle, Stack *&active
877 #if STK_TICKLESS_IDLE
878 , Timeout &ticks
879 #endif
880 ) = 0;
881
885 virtual void OnTaskSwitch(Word caller_SP) = 0;
886
891 virtual void OnTaskSleep(Word caller_SP, Timeout ticks) = 0;
892
898 virtual bool OnTaskSleepUntil(Word caller_SP, Ticks timestamp) = 0;
899
903 virtual void OnTaskExit(Stack *stack) = 0;
904
911 virtual EWaitResult OnTaskWait(Word caller_SP, ISyncObject *sync_obj, IMutex *mutex, Timeout timeout) = 0;
912
917 virtual TId OnGetTid(Word caller_SP) = 0;
918
922 virtual void OnSuspend(bool suspended) = 0;
923 };
924
930 {
931 public:
935 virtual bool OnSleep(Timeout sleep_ticks) { STK_UNUSED(sleep_ticks); return false; }
936
941 virtual bool OnHardFault() { return false; }
942 };
943
951 virtual void Initialize(IEventHandler *event_handler, IKernelService *service, uint32_t resolution_us, Stack *exit_trap) = 0;
952
957 virtual void Start() = 0;
958
961 virtual void Stop() = 0;
962
969 virtual void InitStack(EStackType stack_type, Stack *stack, IStackMemory *stack_memory, ITask *user_task) = 0;
970
975 virtual uint32_t GetTickResolution() const = 0;
976
981 virtual Cycles GetSysTimerCount() const = 0;
982
987 virtual uint32_t GetSysTimerFrequency() const = 0;
988
991 virtual void SwitchToNext() = 0;
992
997 virtual void Sleep(Timeout ticks) = 0;
998
1006 virtual bool SleepUntil(Ticks timestamp) = 0;
1007
1021 virtual EWaitResult Wait(ISyncObject *sobj, IMutex *mutex, Timeout timeout) = 0;
1022
1032 virtual void ProcessTick() = 0;
1033
1037 virtual void ProcessHardFault() = 0;
1038
1043 virtual void SetEventOverrider(IEventOverrider *overrider) = 0;
1044
1049 virtual Word GetCallerSP() const = 0;
1050
1056 virtual TId GetTid() const = 0;
1057
1064 virtual Timeout Suspend() = 0;
1065
1071 virtual void Resume(Timeout elapsed_ticks) = 0;
1072
1073protected:
1076 ~IPlatform() = default;
1077};
1078
1103{
1104public:
1109 virtual void AddTask(IKernelTask *task) = 0;
1110
1115 virtual void RemoveTask(IKernelTask *task) = 0;
1116
1120 virtual IKernelTask *GetFirst() = 0;
1121
1128 virtual IKernelTask *GetNext() = 0;
1129
1133 virtual size_t GetSize() const = 0;
1134
1139 virtual void OnTaskSleep(IKernelTask *task) = 0;
1140
1145 virtual void OnTaskWake(IKernelTask *task) = 0;
1146
1168 {
1169 STK_UNUSED(task);
1170 return false;
1171 }
1172
1185 virtual void OnTaskWeightChange(IKernelTask *task, Weight old_weight)
1186 {
1187 STK_UNUSED(task);
1188 STK_UNUSED(old_weight);
1189 }
1190
1191protected:
1195};
1196
1203{
1204public:
1215
1226 virtual void Initialize(uint32_t resolution_us = PERIODICITY_DEFAULT) = 0;
1227
1233 virtual void AddTask(ITask *user_task) = 0;
1234
1242 virtual void AddTask(ITask *user_task, Timeout periodicity_tc, Timeout deadline_tc, Timeout start_delay_tc) = 0;
1243
1254 virtual void RemoveTask(ITask *user_task) = 0;
1255
1262 virtual void ScheduleTaskRemoval(ITask *user_task) = 0;
1263
1270 virtual void SuspendTask(ITask *user_task, bool &suspended) = 0;
1271
1275 virtual void ResumeTask(ITask *user_task) = 0;
1276
1283
1289 virtual size_t EnumerateTasks(ArrayView<ITask *> user_tasks) = 0;
1290
1311 template <size_t TMaxCount, typename TCallback>
1312 size_t EnumerateTasksT(TCallback &&callback)
1313 {
1314 STK_STATIC_ASSERT(TMaxCount > 0U);
1315
1316 ITask *tasks[TMaxCount] = {};
1317 size_t count = EnumerateTasks(ArrayView<ITask *>(tasks, TMaxCount));
1318 size_t i = 0U;
1319 bool fetch_next = true;
1320
1321 while ((i < count) && fetch_next)
1322 {
1323 fetch_next = callback(tasks[i]);
1324 ++i;
1325 }
1326
1327 return i;
1328 }
1329
1334 virtual void Start() = 0;
1335
1340 virtual EKernelState GetState() const = 0;
1341
1345 virtual IPlatform *GetPlatform() = 0;
1346
1351
1352protected:
1355 ~IKernel() = default;
1356};
1357
1367{
1368public:
1372
1378 virtual TId GetTid() const = 0;
1379
1384 virtual Ticks GetTicks() const = 0;
1385
1391 virtual uint32_t GetTickResolution() const = 0;
1392
1397 virtual Cycles GetSysTimerCount() const = 0;
1398
1403 virtual uint32_t GetSysTimerFrequency() const = 0;
1404
1412 virtual void Delay(Timeout ticks) = 0;
1413
1420 virtual void Sleep(Timeout ticks) = 0;
1421
1429 virtual bool SleepUntil(Ticks timestamp) = 0;
1430
1436 virtual void SleepCancel(TId task_id) = 0;
1437
1442 virtual void SwitchToNext() = 0;
1443
1453 virtual EWaitResult Wait(ISyncObject *sobj, IMutex *mutex, Timeout timeout) = 0;
1454
1464 virtual Timeout Suspend() = 0;
1465
1476 virtual void Resume(Timeout elapsed_ticks) = 0;
1477
1483 virtual void InheritWeight(TId tid, Weight weight) = 0;
1484
1491 virtual void RestoreWeight(TId tid, ISyncObject *sobj = nullptr) = 0;
1492
1493protected:
1496 ~IKernelService() = default;
1497};
1498
1499} // namespace stk
1500
1501#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:608
#define __stk_forceinline
Forces compiler to always inline the decorated function, regardless of optimisation level.
Definition stk_defs.h:175
#define __stk_aligned(x)
Specifies minimum alignment in bytes for the decorated variable or struct member (data instance prefi...
Definition stk_defs.h:187
#define STK_NONCOPYABLE_CLASS(TYPE)
Disables copy construction and assignment for a class.
Definition stk_defs.h:601
#define STK_ASSERT(e)
Runtime assertion. Halts execution if the expression e evaluates to false.
Definition stk_defs.h:409
#define STK_STACK_SIZE_MIN
Minimum stack size in elements of Word, shared by all stack allocation lower-bound checks.
Definition stk_defs.h:533
#define STK_STACK_MEMORY_ALIGN
Stack memory alignment.
Definition stk_defs.h:468
#define STK_STACK_MEMORY_FILLER
Sentinel value written to the entire stack region at initialization (stack watermark pattern).
Definition stk_defs.h:456
#define STK_STATIC_ASSERT(X)
Compile-time assertion. Produces a compilation error if X is false.
Definition stk_defs.h:446
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:136
constexpr TId TID_ISR_N
Bitmask sentinel for ISR-context task identifiers.
Definition stk_common.h:193
ETraceEventId
Trace event identifiers for tracing task suspension and resume with debugging tools (e....
Definition stk_common.h:113
@ TRACE_EVENT_UNKNOWN
Unknown / uninitialized trace event.
Definition stk_common.h:114
@ TRACE_EVENT_SLEEP
Task entered sleep / blocked state.
Definition stk_common.h:116
@ TRACE_EVENT_SWITCH
Task context switch event (task became active).
Definition stk_common.h:115
EAccessMode
Hardware access modes by the task.
Definition stk_common.h:42
@ ACCESS_USER
Unprivileged access mode (access to some hardware is restricted, see CPU manual for details)....
Definition stk_common.h:43
@ ACCESS_PRIVILEGED
Privileged access mode (access to hardware is fully unrestricted).
Definition stk_common.h:44
@ ACCESS_SECURE
Secure access mode (ARM TrustZone, Secure binary).
Definition stk_common.h:45
constexpr Timeout NO_WAIT
Timeout value: return immediately if the synchronization object is not yet signaled (non-blocking pol...
Definition stk_common.h:210
int64_t Ticks
Ticks value.
Definition stk_common.h:151
int32_t Timeout
Timeout time (ticks).
Definition stk_common.h:146
EStackType
Stack type.
Definition stk_common.h:83
@ STACK_SLEEP_TRAP
Stack of the Sleep trap.
Definition stk_common.h:85
@ STACK_USER_TASK
Stack of the user task.
Definition stk_common.h:84
@ STACK_EXIT_TRAP
Stack of the Exit trap.
Definition stk_common.h:86
static bool IsIsrTid(TId id)
Test whether a task identifier represents an ISR context.
Definition stk_common.h:233
int64_t Time
Time value.
Definition stk_common.h:156
EWaitResult
Wait result (see IKernelService::Wait).
Definition stk_common.h:123
@ WAIT_RESULT_FAIL
IKernelService::Wait returned with error without waiting.
Definition stk_common.h:124
@ WAIT_RESULT_TIMEOUT
The wake was caused by a timeout expiry.
Definition stk_common.h:126
@ WAIT_RESULT_SIGNAL
The wake was caused by a signal.
Definition stk_common.h:125
constexpr Weight DEFAULT_WEIGHT
Weight value: default weight of value (1) (see SwitchStrategySmoothWeightedRoundRobin).
Definition stk_common.h:220
constexpr Weight NO_WEIGHT
Weight value: weight is not set.
Definition stk_common.h:215
EConsts
Constants.
Definition stk_common.h:93
@ PERIODICITY_DEFAULT
Default periodicity (microseconds), 1 millisecond.
Definition stk_common.h:95
@ 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:96
@ PERIODICITY_MAX
Maximum periodicity (microseconds), 99 milliseconds (note: this value is the highest working on a rea...
Definition stk_common.h:94
constexpr Timeout WAIT_INFINITE
Timeout value: block indefinitely until the synchronization object is signaled.
Definition stk_common.h:204
constexpr TId TID_NONE
Reserved task/thread id representing zero/none thread id.
Definition stk_common.h:198
ESystemTaskId
System task id.
Definition stk_common.h:103
@ SYS_TASK_ID_EXIT
Exit trap.
Definition stk_common.h:105
@ SYS_TASK_ID_SLEEP
Sleep trap.
Definition stk_common.h:104
uint64_t Cycles
Cycles value.
Definition stk_common.h:161
Word TId
Task (thread) id.
Definition stk_common.h:141
int32_t Weight
Weight value (aka priority).
Definition stk_common.h:166
EKernelMode
Kernel operating mode.
Definition stk_common.h:52
@ KERNEL_TICKLESS
Tickless mode. To use this mode STK_TICKLESS_IDLE must be defined to 1 in stk_config....
Definition stk_common.h:57
@ KERNEL_SYNC
Synchronization support (see Event).
Definition stk_common.h:56
@ KERNEL_HRT
Hard Real-Time (HRT) behavior (tasks are scheduled periodically and have an execution deadline,...
Definition stk_common.h:55
@ KERNEL_STATIC
All tasks are static and can not exit.
Definition stk_common.h:53
@ KERNEL_DYNAMIC
Tasks can be added or removed and therefore exit when done.
Definition stk_common.h:54
EKernelPanicId
Identifies the source of a kernel panic.
Definition stk_common.h:64
@ KERNEL_PANIC_UNKNOWN_SVC
Unknown service command received by SVC handler.
Definition stk_common.h:72
@ KERNEL_PANIC_BAD_STACK_TYPE
Stack type is unknown.
Definition stk_common.h:75
@ KERNEL_PANIC_BAD_MODE
Kernel is in bad/unsupported mode for the current operation.
Definition stk_common.h:74
@ KERNEL_PANIC_HRT_HARD_FAULT
Kernel running in KERNEL_HRT mode reported deadline failure of the task.
Definition stk_common.h:69
@ KERNEL_PANIC_CS_NESTING_OVERFLOW
Critical section nesting limit exceeded: violation of STK_CRITICAL_SECTION_NESTINGS_MAX.
Definition stk_common.h:71
@ KERNEL_PANIC_NONE
Panic is absent (no fault).
Definition stk_common.h:65
@ KERNEL_PANIC_CPU_EXCEPTION
CPU reported an exception and halted execution.
Definition stk_common.h:70
@ KERNEL_PANIC_STACK_CORRUPT
Stack integrity check failed.
Definition stk_common.h:67
@ KERNEL_PANIC_SPINLOCK_DEADLOCK
Spin-lock timeout expired: lock owner never released.
Definition stk_common.h:66
@ KERNEL_PANIC_BAD_STATE
Kernel entered unexpected (bad) state.
Definition stk_common.h:73
@ KERNEL_PANIC_ASSERT
Internal assertion failed (maps from STK_ASSERT).
Definition stk_common.h:68
Internal utility namespace containing data structure helpers (linked lists, etc.) used by the kernel ...
Lightweight, non-owning view over a contiguous sequence of elements.
Definition stk_common.h:247
ArrayView(T *ptr, size_t size)
Construct an ArrayView from a raw pointer and size.
Definition stk_common.h:253
size_t GetSize() const
Get number of elements in the view.
Definition stk_common.h:271
T & operator[](size_t index) const
Subscript operator for element access.
Definition stk_common.h:261
T * m_ptr
Pointer to the underlying memory block.
Definition stk_common.h:274
size_t m_size
Total number of elements in the view.
Definition stk_common.h:275
Stack memory type definition.
Definition stk_common.h:289
Word Type[TStackSize]
Stack memory type.
Definition stk_common.h:295
Stack descriptor.
Definition stk_common.h:302
uint32_t access_mode
Bitfield with hardware access mode of the task (see EAccessMode).
Definition stk_common.h:304
Word SP
Stack Pointer (SP) register (note: must be the first entry in this struct).
Definition stk_common.h:303
Interface for a stack memory region.
Definition stk_common.h:319
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:336
virtual const Word * GetStack() const =0
Get pointer to the stack memory.
Wait object.
Definition stk_common.h:362
DLEntryType ListEntryType
List entry type of IWaitObject elements.
Definition stk_common.h:372
DLHeadType ListHeadType
List head type for IWaitObject elements.
Definition stk_common.h:367
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:410
const char * GetTraceName() const
Get name.
Definition stk_common.h:433
void SetTraceName(const char *name)
Set name.
Definition stk_common.h:421
~ITraceable()=default
Destructor.
Synchronization object.
Definition stk_common.h:456
IWaitObject::ListHeadType m_wait_list
tasks blocked on this object
Definition stk_common.h:605
virtual void WakeOne()
Wake the first task in the wait list (FIFO order).
Definition stk_common.h:576
virtual bool Tick(Timeout elapsed_ticks)
Called by kernel on every system tick to handle timeout logic of waiting tasks.
Definition stk_helper.h:175
DLEntryType ListEntryType
List entry type of ISyncObject elements.
Definition stk_common.h:469
~ISyncObject()=default
Destructor.
IWaitObject::ListHeadType & GetWaitList()
Definition stk_common.h:600
ISyncObject()
Constructor.
Definition stk_common.h:562
virtual void AddWaitObject(IWaitObject *wobj)
Called by kernel when a new task starts waiting on this event.
Definition stk_common.h:485
friend class tz::nsec::util::CmseISyncObjectWrapper
Definition stk_common.h:458
DLHeadType ListHeadType
List head type for ISyncObject elements.
Definition stk_common.h:464
static void AddWaitObject(IWaitObject::ListHeadType &wlist, IWaitObject *wobj)
Called by kernel when a new task starts waiting on this event.
Definition stk_common.h:475
virtual void RemoveWaitObject(IWaitObject *wobj)
Called by kernel when a waiting task is being removed (timeout expired, wait aborted,...
Definition stk_common.h:504
virtual void WakeAll()
Wake all tasks currently in the wait list.
Definition stk_common.h:591
static void WakeAll(IWaitObject::ListHeadType &wlist)
Wake all tasks currently in the wait list.
Definition stk_common.h:550
friend class IKernel
Definition stk_common.h:457
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:494
Weight FindWeightHigherThan(Weight comp) const
Find higher weight within linked wait objects.
Definition stk_helper.h:208
static void WakeOne(IWaitObject::ListHeadType &wlist)
Wake the first task in the wait list (FIFO order).
Definition stk_common.h:536
Interface for mutex synchronization primitive.
Definition stk_common.h:613
~IMutex()=default
Destructor.
virtual void Unlock()=0
Unlock the mutex.
virtual void Lock()=0
Lock the mutex.
ScopedLock(IMutex &mutex)
Definition stk_common.h:623
Interface for a user task.
Definition stk_common.h:670
virtual IStackMemory * GetSecureStackMemory()
Get pointer to the stack memory.
Definition stk_common.h:692
virtual Weight GetWeight() const
Get static base weight of the task.
Definition stk_common.h:722
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:735
virtual void Run()=0
Entry point of the user task.
TId GetId() const
Get task Id set by application.
Definition stk_helper.h:228
virtual void OnExit()
Called by the kernel before removal from the scheduling (see stk::KERNEL_DYNAMIC).
Definition stk_common.h:715
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:705
Scheduling-strategy-facing interface for a kernel task slot.
Definition stk_common.h:748
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:758
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:753
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:840
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 void SetEventOverrider(IEventOverrider *overrider)=0
Set platform event overrider.
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 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:848
virtual void OnTaskExit(Stack *stack)=0
Called from the Thread process when task finished (its Run function exited by return).
virtual bool OnTick(Stack *&idle, Stack *&active, Timeout &ticks)=0
Called by ISR handler to notify about the next system tick.
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 void OnStart(Stack *&active)=0
Called by ISR handler to notify that scheduling is about to start.
virtual TId OnGetTid(Word caller_SP)=0
Called from the Thread process when for getting task/thread id of the process.
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.
Definition stk_common.h:930
virtual bool OnSleep(Timeout sleep_ticks)
Called by the Kernel when it is entering a sleep mode.
Definition stk_common.h:935
virtual bool OnHardFault()
Called by Kernel when hard fault happens.
Definition stk_common.h:941
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 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.
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.