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;
26
31enum EAccessMode : uint32_t
32{
35};
36
40enum EKernelMode : uint8_t
41{
42 KERNEL_STATIC = (1 << 0),
43 KERNEL_DYNAMIC = (1 << 1),
44 KERNEL_HRT = (1 << 2),
45 KERNEL_SYNC = (1 << 3),
46 KERNEL_TICKLESS = (1 << 4),
47};
48
66
77
87
92{
93 SYS_TASK_ID_SLEEP = 0xFFFFFFFF,
94 SYS_TASK_ID_EXIT = 0xFFFFFFFE
95};
96
107
115typedef uintptr_t Word;
116
120typedef Word TId;
121
125typedef int32_t Timeout;
126
130typedef int64_t Ticks;
131
135typedef int64_t Time;
136
140typedef uint64_t Cycles;
141
145typedef int32_t Weight;
146
172constexpr TId TID_ISR_N = static_cast<TId>(0xFFFFF000U);
173
177constexpr TId TID_NONE = static_cast<TId>(0U);
178
183constexpr Timeout WAIT_INFINITE = INT32_MAX;
184
189constexpr Timeout NO_WAIT = 0;
190
194constexpr Weight NO_WEIGHT = -1;
195
199constexpr Weight DEFAULT_WEIGHT = 1;
200
212static __stk_forceinline bool IsIsrTid(TId id) { return ((id & TID_ISR_N) == TID_ISR_N); }
213
225template <typename T> class ArrayView
226{
227public:
232 ArrayView(T *ptr, size_t size) : m_ptr(ptr), m_size(size)
233 {}
234
240 T &operator[](size_t index) const
241 {
242 STK_ASSERT(index < m_size);
243 //MISRA 5-0-15 deviation: bounds are checked via STK_ASSERT
244 return m_ptr[index];
245 }
246
250 size_t GetSize() const { return m_size; }
251
252private:
253 T *m_ptr;
254 size_t m_size;
255};
256
267template <size_t TStackSize> struct StackMemoryDef
268{
269 enum { SIZE = TStackSize };
270
275};
276
280struct Stack
281{
284#if STK_STACK_NEEDS_TASK_ID
285 TId tid;
286#endif
287#ifdef _STK_ARCH_ARM_CORTEX_M
288#if STK_TLS && !STK_TLS_PREFER_REGISTER
289 Word tls;
290#endif
291#endif
292};
293
298{
299public:
302 virtual const Word *GetStack() const = 0;
303
306 virtual size_t GetStackSize() const = 0;
307
310 virtual size_t GetStackSizeBytes() const = 0;
311
319 virtual size_t GetStackSpace() const
320 {
322 const size_t total_size = stack.GetSize();
323 size_t space = 0U;
324
325 for (size_t i = 0U; i < total_size; ++i)
326 {
327 if (stack[i] == STK_STACK_MEMORY_FILLER)
328 {
329 space = i + 1U;
330 }
331 else
332 {
333 break; // terminate loop as soon as watermark ends
334 }
335 }
336
337 return space;
338 }
339};
340
344class IWaitObject : public util::DListEntry<IWaitObject, false>
345{
346public:
351
356
360 virtual TId GetTid() const = 0;
361
368 virtual void Wake(bool timeout) = 0;
369
373 virtual bool IsTimeout() const = 0;
374
380 virtual bool Tick(Timeout elapsed_ticks) = 0;
381
382protected:
385 ~IWaitObject() = default;
386};
387
393{
394public:
395#if STK_SYNC_DEBUG_NAMES
396 ITraceable() : m_trace_name(nullptr)
397 {}
398#endif
399
404 void SetTraceName(const char *name)
405 {
406 #if STK_SYNC_DEBUG_NAMES
407 m_trace_name = name;
408 #else
409 STK_UNUSED(name);
410 #endif
411 }
412
416 const char *GetTraceName() const
417 {
418 #if STK_SYNC_DEBUG_NAMES
419 return m_trace_name;
420 #else
421 return nullptr;
422 #endif
423 }
424
425protected:
428 ~ITraceable() = default;
429
430#if STK_SYNC_DEBUG_NAMES
431 const char *m_trace_name;
432#endif
433};
434
438class ISyncObject : public util::DListEntry<ISyncObject, false>
439{
440 friend class IKernel;
441
442public:
447
452
457 virtual void AddWaitObject(IWaitObject *wobj)
458 {
459 STK_ASSERT(wobj->GetHead() == nullptr);
460 m_wait_list.LinkBack(wobj);
461 }
462
467 virtual void RemoveWaitObject(IWaitObject *wobj)
468 {
469 STK_ASSERT(wobj->GetHead() == &m_wait_list);
470 m_wait_list.Unlink(wobj);
471 }
472
485 virtual bool Tick(Timeout elapsed_ticks);
486
493
494protected:
499 {}
500
504 ~ISyncObject() = default;
505
512 void WakeOne()
513 {
515 {
516 obj->Wake(false);
517 }
518 }
519
526 void WakeAll()
527 {
529 {
530 obj->Wake(false);
531 }
532 }
533
535};
536
542{
543public:
550 {
551 public:
552 explicit ScopedLock(IMutex &mutex) : m_mutex(mutex) { m_mutex.Lock(); }
553 ~ScopedLock() { m_mutex.Unlock(); }
554
555 private:
557
559 };
560
563 virtual void Lock() = 0;
564
567 virtual void Unlock() = 0;
568
569protected:
572 ~IMutex() = default;
573};
574
598class ITask : public IStackMemory
599{
600public:
615 virtual void Run() = 0;
616
619 virtual EAccessMode GetAccessMode() const = 0;
620
628 virtual void OnDeadlineMissed(uint32_t duration) { STK_UNUSED(duration); }
629
638 virtual void OnExit() {}
639
645 virtual Weight GetWeight() const { return DEFAULT_WEIGHT; }
646
651 TId GetId() const;
652
658 virtual const char *GetTraceName() const { return nullptr; }
659};
660
670class IKernelTask : public util::DListEntry<IKernelTask, true>
671{
672public:
677
682
685 virtual ITask *GetUserTask() = 0;
686
690 virtual Stack GetUserStack() const = 0;
691
697 virtual Weight GetWeight() const = 0;
698
704 virtual void SetCurrentWeight(Weight weight) = 0;
705
711 virtual Weight GetCurrentWeight() const = 0;
712
716 virtual Timeout GetHrtPeriodicity() const = 0;
717
721 virtual Timeout GetHrtDeadline() const = 0;
722
730 virtual Timeout GetHrtRelativeDeadline() const = 0;
731
735 virtual bool IsSleeping() const = 0;
736
742 virtual void Wake() = 0;
743
744protected:
747 ~IKernelTask() = default;
748};
749
763{
764public:
771 {
772 public:
777 virtual void OnStart(Stack *&active) = 0;
778
783 virtual void OnStop() = 0;
784
799 virtual bool OnTick(Stack *&idle, Stack *&active
800 #if STK_TICKLESS_IDLE
801 , Timeout &ticks
802 #endif
803 ) = 0;
804
808 virtual void OnTaskSwitch(Word caller_SP) = 0;
809
814 virtual void OnTaskSleep(Word caller_SP, Timeout ticks) = 0;
815
821 virtual bool OnTaskSleepUntil(Word caller_SP, Ticks timestamp) = 0;
822
826 virtual void OnTaskExit(Stack *stack) = 0;
827
834 virtual IWaitObject *OnTaskWait(Word caller_SP, ISyncObject *sync_obj, IMutex *mutex, Timeout timeout) = 0;
835
840 virtual TId OnGetTid(Word caller_SP) = 0;
841
845 virtual void OnSuspend(bool suspended) = 0;
846 };
847
853 {
854 public:
858 virtual bool OnSleep(Timeout sleep_ticks) { STK_UNUSED(sleep_ticks); return false; }
859
864 virtual bool OnHardFault() { return false; }
865 };
866
874 virtual void Initialize(IEventHandler *event_handler, IKernelService *service, uint32_t resolution_us, Stack *exit_trap) = 0;
875
880 virtual void Start() = 0;
881
884 virtual void Stop() = 0;
885
892 virtual void InitStack(EStackType stack_type, Stack *stack, IStackMemory *stack_memory, ITask *user_task) = 0;
893
898 virtual uint32_t GetTickResolution() const = 0;
899
904 virtual Cycles GetSysTimerCount() const = 0;
905
910 virtual uint32_t GetSysTimerFrequency() const = 0;
911
914 virtual void SwitchToNext() = 0;
915
920 virtual void Sleep(Timeout ticks) = 0;
921
929 virtual bool SleepUntil(Ticks timestamp) = 0;
930
944 virtual IWaitObject *Wait(ISyncObject *sobj, IMutex *mutex, Timeout timeout) = 0;
945
955 virtual void ProcessTick() = 0;
956
960 virtual void ProcessHardFault() = 0;
961
966 virtual void SetEventOverrider(IEventOverrider *overrider) = 0;
967
972 virtual Word GetCallerSP() const = 0;
973
979 virtual TId GetTid() const = 0;
980
987 virtual Timeout Suspend() = 0;
988
994 virtual void Resume(Timeout elapsed_ticks) = 0;
995
996protected:
999 ~IPlatform() = default;
1000};
1001
1026{
1027public:
1032 virtual void AddTask(IKernelTask *task) = 0;
1033
1038 virtual void RemoveTask(IKernelTask *task) = 0;
1039
1043 virtual IKernelTask *GetFirst() = 0;
1044
1051 virtual IKernelTask *GetNext() = 0;
1052
1056 virtual size_t GetSize() const = 0;
1057
1062 virtual void OnTaskSleep(IKernelTask *task) = 0;
1063
1068 virtual void OnTaskWake(IKernelTask *task) = 0;
1069
1091 {
1092 STK_UNUSED(task);
1093 return false;
1094 }
1095
1108 virtual void OnTaskWeightChange(IKernelTask *task, Weight old_weight)
1109 {
1110 STK_UNUSED(task);
1111 STK_UNUSED(old_weight);
1112 }
1113
1114protected:
1118};
1119
1126{
1127public:
1138
1149 virtual void Initialize(uint32_t resolution_us = PERIODICITY_DEFAULT) = 0;
1150
1156 virtual void AddTask(ITask *user_task) = 0;
1157
1165 virtual void AddTask(ITask *user_task, Timeout periodicity_tc, Timeout deadline_tc, Timeout start_delay_tc) = 0;
1166
1177 virtual void RemoveTask(ITask *user_task) = 0;
1178
1185 virtual void ScheduleTaskRemoval(ITask *user_task) = 0;
1186
1193 virtual void SuspendTask(ITask *user_task, bool &suspended) = 0;
1194
1198 virtual void ResumeTask(ITask *user_task) = 0;
1199
1206
1212 virtual size_t EnumerateTasks(ArrayView<ITask *> user_tasks) = 0;
1213
1234 template <size_t TMaxCount, typename TCallback>
1235 size_t EnumerateTasksT(TCallback &&callback)
1236 {
1237 STK_STATIC_ASSERT(TMaxCount > 0U);
1238
1239 ITask *tasks[TMaxCount] = {};
1240 size_t count = EnumerateTasks(ArrayView<ITask *>(tasks, TMaxCount));
1241 size_t i = 0U;
1242 bool fetch_next = true;
1243
1244 while ((i < count) && fetch_next)
1245 {
1246 fetch_next = callback(tasks[i]);
1247 ++i;
1248 }
1249
1250 return i;
1251 }
1252
1257 virtual void Start() = 0;
1258
1263 virtual EKernelState GetState() const = 0;
1264
1268 virtual IPlatform *GetPlatform() = 0;
1269
1274
1275protected:
1278 ~IKernel() = default;
1279};
1280
1290{
1291public:
1295
1301 virtual TId GetTid() const = 0;
1302
1307 virtual Ticks GetTicks() const = 0;
1308
1314 virtual uint32_t GetTickResolution() const = 0;
1315
1320 virtual Cycles GetSysTimerCount() const = 0;
1321
1326 virtual uint32_t GetSysTimerFrequency() const = 0;
1327
1335 virtual void Delay(Timeout ticks) = 0;
1336
1343 virtual void Sleep(Timeout ticks) = 0;
1344
1352 virtual bool SleepUntil(Ticks timestamp) = 0;
1353
1359 virtual void SleepCancel(TId task_id) = 0;
1360
1365 virtual void SwitchToNext() = 0;
1366
1380 virtual IWaitObject *Wait(ISyncObject *sobj, IMutex *mutex, Timeout timeout) = 0;
1381
1391 virtual Timeout Suspend() = 0;
1392
1403 virtual void Resume(Timeout elapsed_ticks) = 0;
1404
1410 virtual void InheritWeight(TId tid, Weight weight) = 0;
1411
1418 virtual void RestoreWeight(TId tid, ISyncObject *sobj = nullptr) = 0;
1419
1420protected:
1423 ~IKernelService() = default;
1424};
1425
1426} // namespace stk
1427
1428#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:115
constexpr TId TID_ISR_N
Bitmask sentinel for ISR-context task identifiers.
Definition stk_common.h:172
ETraceEventId
Trace event identifiers for tracing task suspension and resume with debugging tools (e....
Definition stk_common.h:102
@ TRACE_EVENT_UNKNOWN
Unknown / uninitialized trace event.
Definition stk_common.h:103
@ TRACE_EVENT_SLEEP
Task entered sleep / blocked state.
Definition stk_common.h:105
@ TRACE_EVENT_SWITCH
Task context switch event (task became active).
Definition stk_common.h:104
EAccessMode
Hardware access mode by the user task.
Definition stk_common.h:32
@ ACCESS_USER
Unprivileged access mode (access to some hardware is restricted, see CPU manual for details).
Definition stk_common.h:33
@ ACCESS_PRIVILEGED
Privileged access mode (access to hardware is fully unrestricted).
Definition stk_common.h:34
constexpr Timeout NO_WAIT
Timeout value: return immediately if the synchronization object is not yet signaled (non-blocking pol...
Definition stk_common.h:189
int64_t Ticks
Ticks value.
Definition stk_common.h:130
int32_t Timeout
Timeout time (ticks).
Definition stk_common.h:125
EStackType
Stack type.
Definition stk_common.h:72
@ STACK_SLEEP_TRAP
Stack of the Sleep trap.
Definition stk_common.h:74
@ STACK_USER_TASK
Stack of the user task.
Definition stk_common.h:73
@ STACK_EXIT_TRAP
Stack of the Exit trap.
Definition stk_common.h:75
static bool IsIsrTid(TId id)
Test whether a task identifier represents an ISR context.
Definition stk_common.h:212
int64_t Time
Time value.
Definition stk_common.h:135
constexpr Weight DEFAULT_WEIGHT
Weight value: default weight of value (1) (see SwitchStrategySmoothWeightedRoundRobin).
Definition stk_common.h:199
constexpr Weight NO_WEIGHT
Weight value: weight is not set.
Definition stk_common.h:194
EConsts
Constants.
Definition stk_common.h:82
@ PERIODICITY_DEFAULT
Default periodicity (microseconds), 1 millisecond.
Definition stk_common.h:84
@ 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:85
@ PERIODICITY_MAX
Maximum periodicity (microseconds), 99 milliseconds (note: this value is the highest working on a rea...
Definition stk_common.h:83
constexpr Timeout WAIT_INFINITE
Timeout value: block indefinitely until the synchronization object is signaled.
Definition stk_common.h:183
constexpr TId TID_NONE
Reserved task/thread id representing zero/none thread id.
Definition stk_common.h:177
ESystemTaskId
System task id.
Definition stk_common.h:92
@ SYS_TASK_ID_EXIT
Exit trap.
Definition stk_common.h:94
@ SYS_TASK_ID_SLEEP
Sleep trap.
Definition stk_common.h:93
uint64_t Cycles
Cycles value.
Definition stk_common.h:140
Word TId
Task (thread) id.
Definition stk_common.h:120
int32_t Weight
Weight value (aka priority).
Definition stk_common.h:145
EKernelMode
Kernel operating mode.
Definition stk_common.h:41
@ KERNEL_TICKLESS
Tickless mode. To use this mode STK_TICKLESS_IDLE must be defined to 1 in stk_config....
Definition stk_common.h:46
@ KERNEL_SYNC
Synchronization support (see Event).
Definition stk_common.h:45
@ KERNEL_HRT
Hard Real-Time (HRT) behavior (tasks are scheduled periodically and have an execution deadline,...
Definition stk_common.h:44
@ KERNEL_STATIC
All tasks are static and can not exit.
Definition stk_common.h:42
@ KERNEL_DYNAMIC
Tasks can be added or removed and therefore exit when done.
Definition stk_common.h:43
EKernelPanicId
Identifies the source of a kernel panic.
Definition stk_common.h:53
@ KERNEL_PANIC_UNKNOWN_SVC
Unknown service command received by SVC handler.
Definition stk_common.h:61
@ KERNEL_PANIC_BAD_STACK_TYPE
Stack type is unknown.
Definition stk_common.h:64
@ KERNEL_PANIC_BAD_MODE
Kernel is in bad/unsupported mode for the current operation.
Definition stk_common.h:63
@ KERNEL_PANIC_HRT_HARD_FAULT
Kernel running in KERNEL_HRT mode reported deadline failure of the task.
Definition stk_common.h:58
@ KERNEL_PANIC_CS_NESTING_OVERFLOW
Critical section nesting limit exceeded: violation of STK_CRITICAL_SECTION_NESTINGS_MAX.
Definition stk_common.h:60
@ KERNEL_PANIC_NONE
Panic is absent (no fault).
Definition stk_common.h:54
@ KERNEL_PANIC_CPU_EXCEPTION
CPU reported an exception and halted execution.
Definition stk_common.h:59
@ KERNEL_PANIC_STACK_CORRUPT
Stack integrity check failed.
Definition stk_common.h:56
@ KERNEL_PANIC_SPINLOCK_DEADLOCK
Spin-lock timeout expired: lock owner never released.
Definition stk_common.h:55
@ KERNEL_PANIC_BAD_STATE
Kernel entered unexpected (bad) state.
Definition stk_common.h:62
@ KERNEL_PANIC_ASSERT
Internal assertion failed (maps from STK_ASSERT).
Definition stk_common.h:57
Lightweight, non-owning view over a contiguous sequence of elements.
Definition stk_common.h:226
ArrayView(T *ptr, size_t size)
Construct an ArrayView from a raw pointer and size.
Definition stk_common.h:232
size_t GetSize() const
Get number of elements in the view.
Definition stk_common.h:250
T & operator[](size_t index) const
Subscript operator for element access.
Definition stk_common.h:240
T * m_ptr
Pointer to the underlying memory block.
Definition stk_common.h:253
size_t m_size
Total number of elements in the view.
Definition stk_common.h:254
Stack memory type definition.
Definition stk_common.h:268
Word Type[TStackSize]
Stack memory type.
Definition stk_common.h:274
Stack descriptor.
Definition stk_common.h:281
Word SP
Stack Pointer (SP) register (note: must be the first entry in this struct).
Definition stk_common.h:282
EAccessMode access_mode
Hardware access mode of the owning task (see EAccessMode).
Definition stk_common.h:283
Interface for a stack memory region.
Definition stk_common.h:298
virtual size_t GetStackSizeBytes() const =0
Get size of the memory in bytes.
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:319
virtual const Word * GetStack() const =0
Get pointer to the stack memory.
Wait object.
Definition stk_common.h:345
DLEntryType ListEntryType
List entry type of IWaitObject elements.
Definition stk_common.h:355
DLHeadType ListHeadType
List head type for IWaitObject elements.
Definition stk_common.h:350
virtual TId GetTid() const =0
Get thread Id of this task.
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:393
const char * GetTraceName() const
Get name.
Definition stk_common.h:416
void SetTraceName(const char *name)
Set name.
Definition stk_common.h:404
~ITraceable()=default
Destructor.
Synchronization object.
Definition stk_common.h:439
IWaitObject::ListHeadType m_wait_list
tasks blocked on this object
Definition stk_common.h:534
virtual bool Tick(Timeout elapsed_ticks)
Called by kernel on every system tick to handle timeout logic of waiting tasks.
Definition stk_helper.h:181
DLEntryType ListEntryType
List entry type of ISyncObject elements.
Definition stk_common.h:451
~ISyncObject()=default
Destructor.
void WakeOne()
Wake the first task in the wait list (FIFO order).
Definition stk_common.h:512
ISyncObject()
Constructor.
Definition stk_common.h:498
virtual void AddWaitObject(IWaitObject *wobj)
Called by kernel when a new task starts waiting on this event.
Definition stk_common.h:457
DLHeadType ListHeadType
List head type for ISyncObject elements.
Definition stk_common.h:446
void WakeAll()
Wake all tasks currently in the wait list.
Definition stk_common.h:526
virtual void RemoveWaitObject(IWaitObject *wobj)
Called by kernel when a waiting task is being removed (timeout expired, wait aborted,...
Definition stk_common.h:467
friend class IKernel
Definition stk_common.h:440
Weight FindWeightHigherThan(Weight comp) const
Find higher weight within linked wait objects.
Definition stk_helper.h:214
Interface for mutex synchronization primitive.
Definition stk_common.h:542
~IMutex()=default
Destructor.
virtual void Unlock()=0
Unlock the mutex.
virtual void Lock()=0
Lock the mutex.
ScopedLock(IMutex &mutex)
Definition stk_common.h:552
Interface for a user task.
Definition stk_common.h:599
virtual Weight GetWeight() const
Get static base weight of the task.
Definition stk_common.h:645
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:658
virtual void Run()=0
Entry point of the user task.
TId GetId() const
Get task Id set by application.
Definition stk_helper.h:234
virtual void OnExit()
Called by the kernel before removal from the scheduling (see stk::KERNEL_DYNAMIC).
Definition stk_common.h:638
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:628
Scheduling-strategy-facing interface for a kernel task slot.
Definition stk_common.h:671
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:681
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:676
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:763
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 Timeout Suspend()=0
Suspend scheduling.
virtual IWaitObject * 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 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:771
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 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 IWaitObject * OnTaskWait(Word caller_SP, ISyncObject *sync_obj, IMutex *mutex, Timeout timeout)=0
Called from the Thread process when task needs to wait.
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:853
virtual bool OnSleep(Timeout sleep_ticks)
Called by the Kernel when it is entering a sleep mode.
Definition stk_common.h:858
virtual bool OnHardFault()
Called by Kernel when hard fault happens.
Definition stk_common.h:864
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 before Start().
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 Cycles GetSysTimerCount() const =0
Get system timer count value.
virtual uint32_t GetSysTimerFrequency() const =0
Get system timer frequency.
virtual IWaitObject * 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 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.
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.