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 : int32_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
172static constexpr TId TID_ISR_N = static_cast<TId>(0xFFFFF000U);
173
177static constexpr TId TID_NONE = static_cast<TId>(0U);
178
183static constexpr Timeout WAIT_INFINITE = INT32_MAX;
184
189static constexpr Timeout NO_WAIT = 0;
190
194static constexpr Weight NO_WEIGHT = static_cast<Weight>(-1);
195
199static constexpr Weight DEFAULT_WEIGHT = static_cast<Weight>(1);
200
212static inline 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 return m_ptr[index];
244 }
245
249 size_t GetSize() const { return m_size; }
250
251private:
252 T *m_ptr;
253 size_t m_size;
254};
255
266template <size_t _StackSize> struct StackMemoryDef
267{
268 enum { SIZE = _StackSize };
269
274};
275
279struct Stack
280{
283#if STK_STACK_NEEDS_TASK_ID
284 TId tid;
285#endif
286#ifdef _STK_ARCH_ARM_CORTEX_M
287#if STK_TLS && !STK_TLS_PREFER_REGISTER
288 Word tls;
289#endif
290#endif
291};
292
297{
298public:
301 virtual const Word *GetStack() const = 0;
302
305 virtual size_t GetStackSize() const = 0;
306
309 virtual size_t GetStackSizeBytes() const = 0;
310
318 virtual size_t GetStackSpace() const
319 {
321
322 // count leading Words equal to STK_STACK_MEMORY_FILLER (watermark)
323 size_t space = 0U;
324 for ( ; (space < stack.GetSize()) && (stack[space] == STK_STACK_MEMORY_FILLER); ++space)
325 {}
326
327 return space;
328 }
329};
330
334class IWaitObject : public util::DListEntry<IWaitObject, false>
335{
336public:
341
346
350 virtual TId GetTid() const = 0;
351
358 virtual void Wake(bool timeout) = 0;
359
363 virtual bool IsTimeout() const = 0;
364
370 virtual bool Tick(Timeout elapsed_ticks) = 0;
371
372protected:
375 ~IWaitObject() = default;
376};
377
383{
384public:
385#if STK_SYNC_DEBUG_NAMES
386 ITraceable() : m_trace_name(nullptr)
387 {}
388#endif
389
394 void SetTraceName(const char *name)
395 {
396 #if STK_SYNC_DEBUG_NAMES
397 m_trace_name = name;
398 #else
399 STK_UNUSED(name);
400 #endif
401 }
402
406 const char *GetTraceName() const
407 {
408 #if STK_SYNC_DEBUG_NAMES
409 return m_trace_name;
410 #else
411 return nullptr;
412 #endif
413 }
414
415protected:
418 ~ITraceable() = default;
419
420#if STK_SYNC_DEBUG_NAMES
421 const char *m_trace_name;
422#endif
423};
424
428class ISyncObject : public util::DListEntry<ISyncObject, false>
429{
430 friend class IKernel;
431
432public:
437
442
447 virtual void AddWaitObject(IWaitObject *wobj)
448 {
449 STK_ASSERT(wobj->GetHead() == nullptr);
450 m_wait_list.LinkBack(wobj);
451 }
452
457 virtual void RemoveWaitObject(IWaitObject *wobj)
458 {
459 STK_ASSERT(wobj->GetHead() == &m_wait_list);
460 m_wait_list.Unlink(wobj);
461 }
462
475 virtual bool Tick(Timeout elapsed_ticks);
476
483
484protected:
489 {}
490
494 ~ISyncObject() = default;
495
502 void WakeOne()
503 {
504 if (!m_wait_list.IsEmpty())
505 {
506 static_cast<IWaitObject *>(m_wait_list.GetFirst())->Wake(false);
507 }
508 }
509
516 void WakeAll()
517 {
518 while (!m_wait_list.IsEmpty())
519 {
520 static_cast<IWaitObject *>(m_wait_list.GetFirst())->Wake(false);
521 }
522 }
523
525};
526
532{
533public:
540 {
541 public:
542 explicit ScopedLock(IMutex &mutex) : m_mutex(mutex) { m_mutex.Lock(); }
543 ~ScopedLock() { m_mutex.Unlock(); }
544
545 private:
547
549 };
550
553 virtual void Lock() = 0;
554
557 virtual void Unlock() = 0;
558
559protected:
562 ~IMutex() = default;
563};
564
588class ITask : public IStackMemory
589{
590public:
605 virtual void Run() = 0;
606
609 virtual EAccessMode GetAccessMode() const = 0;
610
618 virtual void OnDeadlineMissed(uint32_t duration) { STK_UNUSED(duration); }
619
628 virtual void OnExit() {}
629
635 virtual Weight GetWeight() const { return DEFAULT_WEIGHT; }
636
641 TId GetId() const;
642
648 virtual const char *GetTraceName() const { return nullptr; }
649};
650
660class IKernelTask : public util::DListEntry<IKernelTask, true>
661{
662public:
667
672
675 virtual ITask *GetUserTask() = 0;
676
680 virtual Stack GetUserStack() const = 0;
681
687 virtual Weight GetWeight() const = 0;
688
694 virtual void SetCurrentWeight(Weight weight) = 0;
695
701 virtual Weight GetCurrentWeight() const = 0;
702
706 virtual Timeout GetHrtPeriodicity() const = 0;
707
711 virtual Timeout GetHrtDeadline() const = 0;
712
720 virtual Timeout GetHrtRelativeDeadline() const = 0;
721
725 virtual bool IsSleeping() const = 0;
726
732 virtual void Wake() = 0;
733
734protected:
737 ~IKernelTask() = default;
738};
739
753{
754public:
761 {
762 public:
767 virtual void OnStart(Stack *&active) = 0;
768
773 virtual void OnStop() = 0;
774
789 virtual bool OnTick(Stack *&idle, Stack *&active
790 #if STK_TICKLESS_IDLE
791 , Timeout &ticks
792 #endif
793 ) = 0;
794
798 virtual void OnTaskSwitch(Word caller_SP) = 0;
799
804 virtual void OnTaskSleep(Word caller_SP, Timeout ticks) = 0;
805
811 virtual bool OnTaskSleepUntil(Word caller_SP, Ticks timestamp) = 0;
812
816 virtual void OnTaskExit(Stack *stack) = 0;
817
824 virtual IWaitObject *OnTaskWait(Word caller_SP, ISyncObject *sync_obj, IMutex *mutex, Timeout timeout) = 0;
825
830 virtual TId OnGetTid(Word caller_SP) = 0;
831
835 virtual void OnSuspend(bool suspended) = 0;
836 };
837
843 {
844 public:
848 virtual bool OnSleep(Timeout sleep_ticks) { STK_UNUSED(sleep_ticks); return false; }
849
854 virtual bool OnHardFault() { return false; }
855 };
856
864 virtual void Initialize(IEventHandler *event_handler, IKernelService *service, uint32_t resolution_us, Stack *exit_trap) = 0;
865
870 virtual void Start() = 0;
871
874 virtual void Stop() = 0;
875
883 virtual bool InitStack(EStackType stack_type, Stack *stack, IStackMemory *stack_memory, ITask *user_task) = 0;
884
889 virtual uint32_t GetTickResolution() const = 0;
890
895 virtual Cycles GetSysTimerCount() const = 0;
896
901 virtual uint32_t GetSysTimerFrequency() const = 0;
902
905 virtual void SwitchToNext() = 0;
906
911 virtual void Sleep(Timeout ticks) = 0;
912
920 virtual bool SleepUntil(Ticks timestamp) = 0;
921
935 virtual IWaitObject *Wait(ISyncObject *sobj, IMutex *mutex, Timeout timeout) = 0;
936
946 virtual void ProcessTick() = 0;
947
951 virtual void ProcessHardFault() = 0;
952
957 virtual void SetEventOverrider(IEventOverrider *overrider) = 0;
958
963 virtual Word GetCallerSP() const = 0;
964
970 virtual TId GetTid() const = 0;
971
978 virtual Timeout Suspend() = 0;
979
985 virtual void Resume(Timeout elapsed_ticks) = 0;
986
987protected:
990 ~IPlatform() = default;
991};
992
1017{
1018public:
1023 virtual void AddTask(IKernelTask *task) = 0;
1024
1029 virtual void RemoveTask(IKernelTask *task) = 0;
1030
1034 virtual IKernelTask *GetFirst() const = 0;
1035
1042 virtual IKernelTask *GetNext() = 0;
1043
1047 virtual size_t GetSize() const = 0;
1048
1053 virtual void OnTaskSleep(IKernelTask *task) = 0;
1054
1059 virtual void OnTaskWake(IKernelTask *task) = 0;
1060
1082 {
1083 STK_UNUSED(task);
1084 return false;
1085 }
1086
1099 virtual void OnTaskWeightChange(IKernelTask *task, Weight old_weight)
1100 {
1101 STK_UNUSED(task);
1102 STK_UNUSED(old_weight);
1103 }
1104
1105protected:
1109};
1110
1117{
1118public:
1129
1140 virtual void Initialize(uint32_t resolution_us = PERIODICITY_DEFAULT) = 0;
1141
1147 virtual void AddTask(ITask *user_task) = 0;
1148
1156 virtual void AddTask(ITask *user_task, Timeout periodicity_tc, Timeout deadline_tc, Timeout start_delay_tc) = 0;
1157
1168 virtual void RemoveTask(ITask *user_task) = 0;
1169
1176 virtual void ScheduleTaskRemoval(ITask *user_task) = 0;
1177
1184 virtual void SuspendTask(ITask *user_task, bool &suspended) = 0;
1185
1189 virtual void ResumeTask(ITask *user_task) = 0;
1190
1197
1203 virtual size_t EnumerateTasks(ArrayView<ITask *> user_tasks) = 0;
1204
1225 template <size_t TMaxCount, typename TCallback>
1226 size_t EnumerateTasksT(TCallback &&callback)
1227 {
1228 STK_STATIC_ASSERT(TMaxCount > 0U);
1229
1230 ITask *tasks[TMaxCount] = {};
1231 size_t count = EnumerateTasks(ArrayView<ITask *>(tasks, TMaxCount));
1232 size_t i = 0U;
1233 bool fetch_next = true;
1234
1235 while ((i < count) && fetch_next)
1236 {
1237 fetch_next = callback(tasks[i]);
1238 ++i;
1239 }
1240
1241 return i;
1242 }
1243
1248 virtual void Start() = 0;
1249
1254 virtual EKernelState GetState() const = 0;
1255
1259 virtual IPlatform *GetPlatform() = 0;
1260
1265
1266protected:
1269 ~IKernel() = default;
1270};
1271
1281{
1282public:
1286
1292 virtual TId GetTid() const = 0;
1293
1298 virtual Ticks GetTicks() const = 0;
1299
1305 virtual uint32_t GetTickResolution() const = 0;
1306
1311 virtual Cycles GetSysTimerCount() const = 0;
1312
1317 virtual uint32_t GetSysTimerFrequency() const = 0;
1318
1326 virtual void Delay(Timeout ticks) = 0;
1327
1334 virtual void Sleep(Timeout ticks) = 0;
1335
1343 virtual bool SleepUntil(Ticks timestamp) = 0;
1344
1350 virtual void SleepCancel(TId task_id) = 0;
1351
1356 virtual void SwitchToNext() = 0;
1357
1371 virtual IWaitObject *Wait(ISyncObject *sobj, IMutex *mutex, Timeout timeout) = 0;
1372
1382 virtual Timeout Suspend() = 0;
1383
1394 virtual void Resume(Timeout elapsed_ticks) = 0;
1395
1401 virtual void InheritWeight(TId tid, Weight weight) = 0;
1402
1409 virtual void RestoreWeight(TId tid, ISyncObject *sobj = nullptr) = 0;
1410
1411protected:
1414 ~IKernelService() = default;
1415};
1416
1417} // namespace stk
1418
1419#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:610
#define __stk_aligned(x)
Specifies minimum alignment in bytes for the decorated variable or struct member (data instance prefi...
Definition stk_defs.h:186
#define STK_NONCOPYABLE_CLASS(TYPE)
Disables copy construction and assignment for a class.
Definition stk_defs.h:603
#define STK_ASSERT(e)
Runtime assertion. Halts execution if the expression e evaluates to false.
Definition stk_defs.h:411
#define STK_STACK_SIZE_MIN
Minimum stack size in elements of Word, shared by all stack allocation lower-bound checks.
Definition stk_defs.h:535
#define STK_STACK_MEMORY_ALIGN
Stack memory alignment.
Definition stk_defs.h:470
#define STK_STACK_MEMORY_FILLER
Sentinel value written to the entire stack region at initialization (stack watermark pattern).
Definition stk_defs.h:458
#define STK_STATIC_ASSERT(X)
Compile-time assertion. Produces a compilation error if X is false.
Definition stk_defs.h:448
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
static 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
static 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
static constexpr Weight DEFAULT_WEIGHT
Weight value: default weight of value (1) (see SwitchStrategySmoothWeightedRoundRobin).
Definition stk_common.h:199
static 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
static constexpr Timeout WAIT_INFINITE
Timeout value: block indefinitely until the synchronization object is signaled.
Definition stk_common.h:183
static 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
Definition stk_common.h:120
int32_t Weight
Weight value (aka priority).
Definition stk_common.h:145
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
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:249
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:252
size_t m_size
Total number of elements in the view.
Definition stk_common.h:253
Stack memory type definition.
Definition stk_common.h:267
Word Type[_StackSize]
Stack memory type.
Definition stk_common.h:273
Stack descriptor.
Definition stk_common.h:280
Word SP
Stack Pointer (SP) register (note: must be the first entry in this struct).
Definition stk_common.h:281
EAccessMode access_mode
Hardware access mode of the owning task (see EAccessMode).
Definition stk_common.h:282
Interface for a stack memory region.
Definition stk_common.h:297
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:318
virtual const Word * GetStack() const =0
Get pointer to the stack memory.
Wait object.
Definition stk_common.h:335
DLEntryType ListEntryType
List entry type of IWaitObject elements.
Definition stk_common.h:345
DLHeadType ListHeadType
List head type for IWaitObject elements.
Definition stk_common.h:340
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:383
const char * GetTraceName() const
Get name.
Definition stk_common.h:406
void SetTraceName(const char *name)
Set name.
Definition stk_common.h:394
~ITraceable()=default
Destructor.
Synchronization object.
Definition stk_common.h:429
IWaitObject::ListHeadType m_wait_list
tasks blocked on this object
Definition stk_common.h:524
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:441
~ISyncObject()=default
Destructor.
void WakeOne()
Wake the first task in the wait list (FIFO order).
Definition stk_common.h:502
ISyncObject()
Constructor.
Definition stk_common.h:488
virtual void AddWaitObject(IWaitObject *wobj)
Called by kernel when a new task starts waiting on this event.
Definition stk_common.h:447
DLHeadType ListHeadType
List head type for ISyncObject elements.
Definition stk_common.h:436
void WakeAll()
Wake all tasks currently in the wait list.
Definition stk_common.h:516
virtual void RemoveWaitObject(IWaitObject *wobj)
Called by kernel when a waiting task is being removed (timeout expired, wait aborted,...
Definition stk_common.h:457
friend class IKernel
Definition stk_common.h:430
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:532
~IMutex()=default
Destructor.
virtual void Unlock()=0
Unlock the mutex.
virtual void Lock()=0
Lock the mutex.
ScopedLock(IMutex &mutex)
Definition stk_common.h:542
Interface for a user task.
Definition stk_common.h:589
virtual Weight GetWeight() const
Get static base weight of the task.
Definition stk_common.h:635
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:648
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:628
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:618
Scheduling-strategy-facing interface for a kernel task slot.
Definition stk_common.h:661
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:671
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:666
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:753
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 bool InitStack(EStackType stack_type, Stack *stack, IStackMemory *stack_memory, ITask *user_task)=0
Initialize stack memory of the user task.
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 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:761
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:843
virtual bool OnSleep(Timeout sleep_ticks)
Called by the Kernel when it is entering a sleep mode.
Definition stk_common.h:848
virtual bool OnHardFault()
Called by Kernel when hard fault happens.
Definition stk_common.h:854
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 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 IKernelTask * GetFirst() const =0
Get first 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 ...
@ 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