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::Kernel< TMode, TSize, TStrategy, TPlatform > Class Template Referencefinal

Concrete implementation of IKernel. More...

#include <stk.h>

Inheritance diagram for stk::Kernel< TMode, TSize, TStrategy, TPlatform >:
Collaboration diagram for stk::Kernel< TMode, TSize, TStrategy, TPlatform >:

Classes

class  KernelTask
 Internal per-slot kernel descriptor that wraps a user ITask instance. More...
class  KernelService
 Concrete implementation of IKernelService exposed to running tasks. More...
class  SleepTrapStack
 Storage bundle for the sleep trap: a Stack descriptor paired with its backing memory. More...
class  ExitTrapStack
 Storage bundle for the exit trap: a Stack descriptor paired with its backing memory. More...

Public Types

enum  EKernelState : uint8_t {
  KSTATE_INACTIVE = 0 ,
  KSTATE_READY ,
  KSTATE_RUNNING ,
  KSTATE_SUSPENDED
}

Public Member Functions

 Kernel ()
 Construct the kernel with all storage zero-initialized and the request flag set to ~0 (indicating uninitialized state; cleared to REQ_NONE by Initialize()).
 ~Kernel ()=default
 Destructor.
void Initialize (uint32_t resolution_us=PERIODICITY_DEFAULT) override
 Initialize kernel.
void AddTask (ITask *user_task) override
 Register task for a soft real-time (SRT) scheduling.
void AddTask (ITask *user_task, Timeout periodicity_tc, Timeout deadline_tc, Timeout start_delay_tc) override
 Register a task for hard real-time (HRT) scheduling.
void RemoveTask (ITask *user_task) override
 Remove a previously added task from the kernel before Start().
void ScheduleTaskRemoval (ITask *user_task) override
 Schedule task removal from scheduling (exit).
void SuspendTask (ITask *user_task, bool &suspended) override
 Suspend task.
void ResumeTask (ITask *user_task) override
 Resume task.
size_t EnumerateKernelTasks (ArrayView< IKernelTask * > tasks) override
 Enumerate kernel tasks.
size_t EnumerateTasks (ArrayView< ITask * > user_tasks) override
 Enumerate user tasks.
void Start () override
 Start the scheduler. This call does not return until all tasks have exited (KERNEL_DYNAMIC mode) or indefinitely (KERNEL_STATIC mode).
bool IsStarted () const
 Check whether scheduler is currently running.
IPlatformGetPlatform () override
 Get platform driver instance owned by this kernel.
ITaskSwitchStrategyGetSwitchStrategy () override
 Get task-switching strategy instance owned by this kernel.
EKernelState GetState () const override
 Get kernel state.
template<size_t TMaxCount, typename TCallback>
size_t EnumerateTasksT (TCallback &&callback)
 Enumerate tasks, invoking a callback for each active task.

Static Public Attributes

static constexpr size_t TASKS_MAX = TSize
 Maximum number of concurrently registered tasks. Fixed at compile time. Exceeding this limit in AddTask() triggers a compile-time assert (TASKS_MAX > 0) and a runtime STK_ASSERT.

Protected Types

enum  ERequest : uint8_t {
  REQ_NONE = 0 ,
  REQ_ADD_TASK = (1 << 0)
}
 Bitmask flags for pending inter-task requests that must be processed by the kernel on the next tick (in UpdateTaskRequest()). More...
enum  EFsmState : int8_t {
  FSM_STATE_NONE = -1 ,
  FSM_STATE_SWITCHING ,
  FSM_STATE_SLEEPING ,
  FSM_STATE_WAKING ,
  FSM_STATE_EXITING ,
  FSM_STATE_MAX
}
 Finite-state machine (FSM) state. Encodes what the kernel is currently doing between two consecutive tick events. More...
enum  EFsmEvent : int8_t {
  FSM_EVENT_SWITCH = 0 ,
  FSM_EVENT_SLEEP ,
  FSM_EVENT_WAKE ,
  FSM_EVENT_EXIT ,
  FSM_EVENT_MAX
}
 Finite-state machine (FSM) event. Computed by FetchNextEvent() each tick based on strategy output and current kernel state. More...
typedef StackMemoryWrapper<((32U))> SleepTrapStackMemory
 Stack memory wrapper type for the sleep trap.
typedef StackMemoryWrapper< STACK_SIZE_MINExitTrapStackMemory
 Stack memory wrapper type for the exit trap.
typedef KernelTask TaskStorageType[TASKS_MAX]
 KernelTask array type used as a storage for the KernelTask instances.
typedef ISyncObject::ListHeadType SyncObjectList
 Intrusive list of active ISyncObject instances registered with this kernel. Each sync object in this list receives a Tick() call every kernel tick for timeout tracking. Allocated only when KERNEL_SYNC is set (zero-size otherwise).

Protected Member Functions

void InitTraps ()
 Initialize stack of the traps.
KernelTaskAllocateNewTask (ITask *user_task)
 Allocate new instance of KernelTask.
void AddKernelTask (KernelTask *task)
 Add kernel task to the scheduling strategy.
void AllocateAndAddNewTask (ITask *user_task)
 Allocate new instance of KernelTask and add it into the scheduling process.
void HrtAllocateAndAddNewTask (ITask *user_task, Timeout periodicity_tc, Timeout deadline_tc, Timeout start_delay_tc)
 Allocate new instance of KernelTask and add it into the HRT scheduling process.
void RequestAddTask (ITask *const user_task)
 Request to add new task.
KernelTaskFindTaskByUserTask (const ITask *user_task)
 Find kernel task by the bound ITask instance.
KernelTaskFindTaskByStack (const Stack *stack)
 Find kernel task by the bound Stack instance.
KernelTaskFindTaskBySP (Word SP)
 Find kernel task for a Stack Pointer (SP).
void RemoveTask (KernelTask *task)
 Remove kernel task.
void OnStart (Stack *&active) override
 Called by platform driver immediately after a scheduler start (first tick).
void OnStop () override
 Called by the platform driver after a scheduler stop (all tasks have exited).
bool OnTick (Stack *&idle, Stack *&active, Timeout &ticks) override
 Process one scheduler tick. Called from the platform timer/tick ISR.
void OnTaskSwitch (Word caller_SP) override
 Called by Thread process (via IKernelService::SwitchToNext) to switch to a next task.
void OnTaskSleep (Word caller_SP, Timeout ticks) override
 Called by Thread process (via IKernelService::Sleep) for exclusion of the calling process from scheduling (sleeping).
bool OnTaskSleepUntil (Word caller_SP, Ticks timestamp) override
 Called by Thread process (via IKernelService::SleepUntil) for exclusion of the calling process from scheduling (sleeping).
void OnTaskSleepCancel (TId task_id)
void OnTaskExit (Stack *stack) override
 Called from the Thread process when task finished (its Run function exited by return).
IWaitObjectOnTaskWait (Word caller_SP, ISyncObject *sync_obj, IMutex *mutex, Timeout timeout) override
 Called from the Thread process when task needs to wait.
TId OnGetTid (Word caller_SP) override
 Called from the Thread process when for getting task/thread id of the process.
void OnSuspend (bool suspended) override
 Called from the Thread process to suspend scheduling.
void OnInheritWeight (TId tid, Weight weight)
void OnRestoreWeight (TId tid, ISyncObject *sobj)
Timeout UpdateTasks (const Timeout elapsed_ticks)
 Update tasks (sleep, requests).
Timeout UpdateTaskState (const Timeout elapsed_ticks)
 Update task state: process removals, advance sleep timers, and track HRT durations.
void UpdateSyncObjects (const Timeout elapsed_ticks)
 Update synchronization objects.
void UpdateTaskRequest ()
 Update pending task requests.
EFsmEvent FetchNextEvent (KernelTask *&next)
 Fetch next event for the FSM.
EFsmState GetNewFsmState (KernelTask *&next)
 Get new FSM state.
bool UpdateFsmState (Stack *&idle, Stack *&active)
 Update FSM state.
bool StateSwitch (KernelTask *now, KernelTask *next, Stack *&idle, Stack *&active)
 Switches contexts.
bool StateWake (KernelTask *now, KernelTask *next, Stack *&idle, Stack *&active)
 Wakes up after sleeping.
bool StateSleep (KernelTask *now, KernelTask *next, Stack *&idle, Stack *&active)
 Enters into a sleeping mode.
bool StateExit (KernelTask *now, KernelTask *next, Stack *&idle, Stack *&active)
 Exits from scheduling.
bool IsInitialized () const
 Check whether Initialize() has been called and completed successfully.
void ScheduleAddTask ()
 Signal the kernel to process a pending AddTask request on the next tick.

Static Protected Member Functions

static bool IsValidFsmState (EFsmState state)
 Check if FSM state is valid.
static constexpr bool IsStaticMode ()
static constexpr bool IsDynamicMode ()
static constexpr bool IsHrtMode ()
static constexpr bool IsSyncMode ()
static constexpr bool IsTicklessMode ()

Protected Attributes

KernelService m_service
 Kernel service singleton exposed to running tasks via IKernelService::GetInstance().
TPlatform m_platform
 Platform driver (SysTick, PendSV, context switch implementation).
TStrategy m_strategy
 Task-switching strategy (determines which task runs next).
KernelTaskm_task_now
 Currently executing task, or nullptr before Start() or after all tasks exit.
TaskStorageType m_task_storage
 Static pool of TSize KernelTask slots (free slots have m_user == nullptr).
SleepTrapStack m_sleep_trap [1]
 Sleep trap (always present): executed when all tasks are sleeping.
ExitTrapStack m_exit_trap [STK_ALLOCATE_COUNT< TMode, KERNEL_DYNAMIC, 1U, 0U >::Value]
 Exit trap: zero-size in KERNEL_STATIC mode; one entry in KERNEL_DYNAMIC mode.
EFsmState m_fsm_state
 Current FSM state. Drives context-switch decision on every tick.
volatile uint8_t m_request
 Bitmask of pending ERequest flags from running tasks. Written by tasks, read/cleared by UpdateTaskRequest() in tick context.
volatile EKernelState m_kstate
 Current kernel state.
SyncObjectList m_sync_list [STK_ALLOCATE_COUNT< TMode, KERNEL_SYNC, 1U, 0U >::Value]
 List of active sync objects. Zero-size (no memory) if KERNEL_SYNC is not set.
const EFsmState m_fsm [FSM_STATE_MAX][FSM_EVENT_MAX]

Static Protected Attributes

static constexpr Timeout YIELD_TICKS = 2
 Ticks to yield.

Detailed Description

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
class stk::Kernel< TMode, TSize, TStrategy, TPlatform >

Concrete implementation of IKernel.

All configuration is expressed as template parameters. No virtual dispatch, no heap allocation - the entire kernel, tasks, and traps live in statically reserved storage.

Template Parameters
TModeBitmask of EKernelMode flags that configures kernel features:
  • KERNEL_STATIC - fixed task list, no add/remove after Start().
  • KERNEL_DYNAMIC - tasks may be added or removed at runtime.
  • KERNEL_HRT - Hard Real-Time mode (must combine with STATIC or DYNAMIC).
  • KERNEL_SYNC - enables synchronization primitives (Mutex, Event, etc.).
  • KERNEL_TICKLESS - enables tickless low-power operation. Requires STK_TICKLESS_IDLE=1 in stk_config.h. Incompatible with KERNEL_HRT (tickless suppresses the timer, which destroys the precise periodicity HRT depends on - enforced by the compile-time assertion TICKLESS_HRT_CONFLICT). KERNEL_STATIC and KERNEL_DYNAMIC are mutually exclusive.
TSizeMaximum number of concurrent tasks. Must be > 0.
TStrategyTask-switching strategy type (e.g. SwitchStrategyRoundRobin). Must inherit ITaskSwitchStrategy.
TPlatformPlatform driver type (e.g. PlatformArmCortexM, or PlatformDefault). Must inherit IPlatform.
Note
At least 1 task is required: TSize must be > 0 (enforced by compile-time assertion).
KERNEL_HRT is incompatible with weighted scheduling strategies (WEIGHT_API == true), also enforced by a compile-time assertion.
KERNEL_TICKLESS is incompatible with KERNEL_HRT, also enforced by a compile-time assertion (TICKLESS_HRT_CONFLICT).

Usage example:

static MyTask1<256, ACCESS_PRIVILEGED> task1;
static MyTask2<512, ACCESS_USER> task2;
static MyTask3<512, ACCESS_USER> task3;
kernel.Initialize();
kernel.AddTask(&task1);
kernel.AddTask(&task2);
kernel.AddTask(&task3);
kernel.Start();
Concrete implementation of IKernel.
Definition stk.h:85
void AddTask(ITask *user_task) override
Register task for a soft real-time (SRT) scheduling.
Definition stk.h:977
void Start() override
Start the scheduler. This call does not return until all tasks have exited (KERNEL_DYNAMIC mode) or i...
Definition stk.h:1203
void Initialize(uint32_t resolution_us=PERIODICITY_DEFAULT) override
Initialize kernel.
Definition stk.h:940

Definition at line 80 of file stk.h.

Member Typedef Documentation

◆ ExitTrapStackMemory

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
typedef StackMemoryWrapper<STACK_SIZE_MIN> stk::Kernel< TMode, TSize, TStrategy, TPlatform >::ExitTrapStackMemory
protected

Stack memory wrapper type for the exit trap.

See also
ExitTrapStack, STACK_SIZE_MIN

Definition at line 97 of file stk.h.

◆ SleepTrapStackMemory

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
typedef StackMemoryWrapper< ( (32U) )> stk::Kernel< TMode, TSize, TStrategy, TPlatform >::SleepTrapStackMemory
protected

Stack memory wrapper type for the sleep trap.

See also
SleepTrapStack, STK_SLEEP_TRAP_STACK_SIZE

Definition at line 91 of file stk.h.

◆ SyncObjectList

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
typedef ISyncObject::ListHeadType stk::Kernel< TMode, TSize, TStrategy, TPlatform >::SyncObjectList
protected

Intrusive list of active ISyncObject instances registered with this kernel. Each sync object in this list receives a Tick() call every kernel tick for timeout tracking. Allocated only when KERNEL_SYNC is set (zero-size otherwise).

Definition at line 2392 of file stk.h.

◆ TaskStorageType

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
typedef KernelTask stk::Kernel< TMode, TSize, TStrategy, TPlatform >::TaskStorageType[TASKS_MAX]
protected

KernelTask array type used as a storage for the KernelTask instances.

Definition at line 2354 of file stk.h.

Member Enumeration Documentation

◆ EFsmEvent

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
enum stk::Kernel::EFsmEvent : int8_t
protected

Finite-state machine (FSM) event. Computed by FetchNextEvent() each tick based on strategy output and current kernel state.

Enumerator
FSM_EVENT_SWITCH 

Strategy returned a runnable task, perform a context switch.

FSM_EVENT_SLEEP 

No runnable tasks, enter sleep trap.

FSM_EVENT_WAKE 

A task became runnable while the kernel was sleeping, wake from sleep trap.

FSM_EVENT_EXIT 

No tasks remain (KERNEL_DYNAMIC), exit scheduling and return from Start().

FSM_EVENT_MAX 

Sentinel: number of valid events (used to size the FSM table).

Definition at line 1268 of file stk.h.

1269 {
1270 FSM_EVENT_SWITCH = 0,
1275 };
@ FSM_EVENT_EXIT
No tasks remain (KERNEL_DYNAMIC), exit scheduling and return from Start().
Definition stk.h:1273
@ FSM_EVENT_WAKE
A task became runnable while the kernel was sleeping, wake from sleep trap.
Definition stk.h:1272
@ FSM_EVENT_SLEEP
No runnable tasks, enter sleep trap.
Definition stk.h:1271
@ FSM_EVENT_SWITCH
Strategy returned a runnable task, perform a context switch.
Definition stk.h:1270
@ FSM_EVENT_MAX
Sentinel: number of valid events (used to size the FSM table).
Definition stk.h:1274

◆ EFsmState

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
enum stk::Kernel::EFsmState : int8_t
protected

Finite-state machine (FSM) state. Encodes what the kernel is currently doing between two consecutive tick events.

Enumerator
FSM_STATE_NONE 

Sentinel / uninitialized value. Set by the constructor, replaced by FSM_STATE_SWITCHING on the first tick.

FSM_STATE_SWITCHING 

Normal operation: switching between runnable tasks each tick.

FSM_STATE_SLEEPING 

All tasks are sleeping, the sleep trap is executing (CPU in low-power state).

FSM_STATE_WAKING 

At least one task woke up, transitioning from sleep trap back to a user task.

FSM_STATE_EXITING 

All tasks exited (KERNEL_DYNAMIC only), executing the exit trap to return from Start().

FSM_STATE_MAX 

Sentinel: number of valid states (used to size the FSM table), denotes uninitialized state.

Definition at line 1254 of file stk.h.

1255 {
1256 FSM_STATE_NONE = -1,
1262 };
@ FSM_STATE_EXITING
All tasks exited (KERNEL_DYNAMIC only), executing the exit trap to return from Start().
Definition stk.h:1260
@ FSM_STATE_SLEEPING
All tasks are sleeping, the sleep trap is executing (CPU in low-power state).
Definition stk.h:1258
@ FSM_STATE_SWITCHING
Normal operation: switching between runnable tasks each tick.
Definition stk.h:1257
@ FSM_STATE_NONE
Sentinel / uninitialized value. Set by the constructor, replaced by FSM_STATE_SWITCHING on the first ...
Definition stk.h:1256
@ FSM_STATE_MAX
Sentinel: number of valid states (used to size the FSM table), denotes uninitialized state.
Definition stk.h:1261
@ FSM_STATE_WAKING
At least one task woke up, transitioning from sleep trap back to a user task.
Definition stk.h:1259

◆ EKernelState

enum stk::IKernel::EKernelState : uint8_t
inherited
Enumerator
KSTATE_INACTIVE 

Not ready, IKernel::Initialize() must be called.

KSTATE_READY 

Ready to start, IKernel::Start() must be called.

KSTATE_RUNNING 

Initialized and running, IKernel::Start() was called successfully.

KSTATE_SUSPENDED 

Scheduling is suspended with IKernelService::Suspend().

Definition at line 1122 of file stk_common.h.

1123 {
1124 KSTATE_INACTIVE = 0,
1125 KSTATE_READY,
1128 };
@ 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.

◆ ERequest

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
enum stk::Kernel::ERequest : uint8_t
protected

Bitmask flags for pending inter-task requests that must be processed by the kernel on the next tick (in UpdateTaskRequest()).

Enumerator
REQ_NONE 

No pending requests.

REQ_ADD_TASK 

An AddTask() request is pending from a running task (KERNEL_DYNAMIC only).

Definition at line 103 of file stk.h.

104 {
105 REQ_NONE = 0,
106 REQ_ADD_TASK = (1 << 0)
107 };
@ REQ_ADD_TASK
An AddTask() request is pending from a running task (KERNEL_DYNAMIC only).
Definition stk.h:106
@ REQ_NONE
No pending requests.
Definition stk.h:105

Constructor & Destructor Documentation

◆ Kernel()

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
stk::Kernel< TMode, TSize, TStrategy, TPlatform >::Kernel ( )
inlineexplicit

Construct the kernel with all storage zero-initialized and the request flag set to ~0 (indicating uninitialized state; cleared to REQ_NONE by Initialize()).

Note
In debug builds also verifies that TPlatform derives from IPlatform and TStrategy from ITaskSwitchStrategy.
If TMode includes KERNEL_TICKLESS, a compile-time assertion fires unless STK_TICKLESS_IDLE is defined to 1 in stk_config.h.

Definition at line 907 of file stk.h.

909 {
910 #ifdef _DEBUG
911 // TPlatform must inherit IPlatform
914
915 // TStrategy must inherit ITaskSwitchStrategy
918 #endif
919
920 #if !STK_TICKLESS_IDLE
922 "STK_TICKLESS_IDLE must be defined to 1 for KERNEL_TICKLESS");
923 #endif
924 }
#define STK_UNUSED(X)
Explicitly marks a variable as unused to suppress compiler warnings.
Definition stk_defs.h:610
#define STK_STATIC_ASSERT_DESC(X, DESC)
Compile-time assertion with a custom error description. Produces a compilation error if X is false.
Definition stk_defs.h:431
volatile uint8_t m_request
Bitmask of pending ERequest flags from running tasks. Written by tasks, read/cleared by UpdateTaskReq...
Definition stk.h:2402
TStrategy m_strategy
Task-switching strategy (determines which task runs next).
Definition stk.h:2396
SleepTrapStack m_sleep_trap[1]
Sleep trap (always present): executed when all tasks are sleeping.
Definition stk.h:2399
volatile EKernelState m_kstate
Current kernel state.
Definition stk.h:2403
ExitTrapStack m_exit_trap[STK_ALLOCATE_COUNT< TMode, KERNEL_DYNAMIC, 1U, 0U >::Value]
Exit trap: zero-size in KERNEL_STATIC mode; one entry in KERNEL_DYNAMIC mode.
Definition stk.h:2400
TaskStorageType m_task_storage
Static pool of TSize KernelTask slots (free slots have m_user == nullptr).
Definition stk.h:2398
KernelTask * m_task_now
Currently executing task, or nullptr before Start() or after all tasks exit.
Definition stk.h:2397
TPlatform m_platform
Platform driver (SysTick, PendSV, context switch implementation).
Definition stk.h:2395
EFsmState m_fsm_state
Current FSM state. Drives context-switch decision on every tick.
Definition stk.h:2401

◆ ~Kernel()

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
stk::Kernel< TMode, TSize, TStrategy, TPlatform >::~Kernel ( )
default

Destructor.

Note
MISRA deviation: [STK-DEV-005] Rule 10-3-2.

Member Function Documentation

◆ AddKernelTask()

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
void stk::Kernel< TMode, TSize, TStrategy, TPlatform >::AddKernelTask ( KernelTask * task)
inlineprotected

Add kernel task to the scheduling strategy.

Parameters
[in]taskPointer to the kernel task.

Definition at line 1369 of file stk.h.

1370 {
1371 #if STK_SEGGER_SYSVIEW
1372 // start tracing new task
1373 SEGGER_SYSVIEW_OnTaskCreate(task->GetUserStackPtr()->tid);
1374 if (IsStarted())
1376 #endif
1377
1378 m_strategy.AddTask(task);
1379 }
bool IsStarted() const
Check whether scheduler is currently running.
Definition stk.h:1230

Referenced by stk::Kernel< stk::KERNEL_DYNAMIC|stk::KERNEL_SYNC|stk::KERNEL_TICKLESS, 16U, stk::SwitchStrategyFP32, stk::PlatformDefault >::AllocateAndAddNewTask(), and stk::Kernel< stk::KERNEL_DYNAMIC|stk::KERNEL_SYNC|stk::KERNEL_TICKLESS, 16U, stk::SwitchStrategyFP32, stk::PlatformDefault >::HrtAllocateAndAddNewTask().

Here is the caller graph for this function:

◆ AddTask() [1/2]

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
void stk::Kernel< TMode, TSize, TStrategy, TPlatform >::AddTask ( ITask * user_task)
inlineoverridevirtual

Register task for a soft real-time (SRT) scheduling.

Parameters
[in]user_taskUser task to add. Must not already be registered. Must not be nullptr.
Note
Before Start(): allocates a free KernelTask slot and adds it to the strategy immediately.
After Start() (KERNEL_DYNAMIC only): serialises the request via RequestAddTask() - the calling task yields and the kernel processes the request on the next tick.
Warning
Asserts if called in KERNEL_HRT mode (use the HRT overload instead), if called after Start() without KERNEL_DYNAMIC, or if TASKS_MAX is exceeded.

Implements stk::IKernel.

Definition at line 977 of file stk.h.

978 {
980 {
981 STK_ASSERT(user_task != nullptr);
983
984 // when started the operation must be serialized by switching out from processing until
985 // kernel processes this request
986 if (IsStarted())
987 {
989 {
991 }
992 else
993 {
994 STK_ASSERT(false);
995 }
996 }
997 else
998 {
1000 }
1001 }
1002 else
1003 {
1004 STK_ASSERT(false);
1005 }
1006 }
#define STK_ASSERT(e)
Runtime assertion. Halts execution if the expression e evaluates to false.
Definition stk_defs.h:411
#define __stk_constexpr_cpp17
constexpr definition for C++17 and above.
Definition stk_defs.h:384
void RequestAddTask(ITask *const user_task)
Request to add new task.
Definition stk.h:1413
static constexpr bool IsHrtMode()
Definition stk.h:2324
void AllocateAndAddNewTask(ITask *user_task)
Allocate new instance of KernelTask and add it into the scheduling process.
Definition stk.h:1384
bool IsInitialized() const
Check whether Initialize() has been called and completed successfully.
Definition stk.h:2288
static constexpr bool IsDynamicMode()
Definition stk.h:2323

◆ AddTask() [2/2]

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
void stk::Kernel< TMode, TSize, TStrategy, TPlatform >::AddTask ( ITask * user_task,
Timeout periodicity_tc,
Timeout deadline_tc,
Timeout start_delay_tc )
inlineoverridevirtual

Register a task for hard real-time (HRT) scheduling.

Parameters
[in]user_taskUser task to add. Must not already be registered. Must not be nullptr.
[in]periodicity_tcActivation period in ticks. Must be > 0 and < INT32_MAX.
[in]deadline_tcMaximum allowed active duration in ticks. Must be > 0 and < INT32_MAX.
[in]start_delay_tcInitial sleep delay in ticks before the first activation. 0 means activate immediately.
Note
Must be called before Start(). Dynamic (post-Start) HRT task addition is not supported.
Warning
Asserts if called outside KERNEL_HRT mode (use the SRT overload instead) or after Start().

Implements stk::IKernel.

Definition at line 1016 of file stk.h.

1018 {
1020 {
1021 STK_ASSERT(user_task != nullptr);
1024
1026 }
1027 else
1028 {
1029 STK_ASSERT(false);
1030 }
1031 }
void HrtAllocateAndAddNewTask(ITask *user_task, Timeout periodicity_tc, Timeout deadline_tc, Timeout start_delay_tc)
Allocate new instance of KernelTask and add it into the HRT scheduling process.
Definition stk.h:1399

◆ AllocateAndAddNewTask()

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
void stk::Kernel< TMode, TSize, TStrategy, TPlatform >::AllocateAndAddNewTask ( ITask * user_task)
inlineprotected

Allocate new instance of KernelTask and add it into the scheduling process.

Parameters
[in]user_taskUser task for which kernel task object is allocated.

Definition at line 1384 of file stk.h.

1385 {
1387 STK_ASSERT(task != nullptr);
1388
1390 }
KernelTask * AllocateNewTask(ITask *user_task)
Allocate new instance of KernelTask.
Definition stk.h:1329
void AddKernelTask(KernelTask *task)
Add kernel task to the scheduling strategy.
Definition stk.h:1369
Internal per-slot kernel descriptor that wraps a user ITask instance.
Definition stk.h:121

Referenced by stk::Kernel< stk::KERNEL_DYNAMIC|stk::KERNEL_SYNC|stk::KERNEL_TICKLESS, 16U, stk::SwitchStrategyFP32, stk::PlatformDefault >::AddTask(), and stk::Kernel< stk::KERNEL_DYNAMIC|stk::KERNEL_SYNC|stk::KERNEL_TICKLESS, 16U, stk::SwitchStrategyFP32, stk::PlatformDefault >::UpdateTaskRequest().

Here is the caller graph for this function:

◆ AllocateNewTask()

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
KernelTask * stk::Kernel< TMode, TSize, TStrategy, TPlatform >::AllocateNewTask ( ITask * user_task)
inlineprotected

Allocate new instance of KernelTask.

Parameters
[in]user_taskUser task for which kernel task object is allocated.
Returns
Kernel task.

Definition at line 1329 of file stk.h.

1330 {
1331 // look for a free kernel task
1332 KernelTask *new_task = nullptr;
1333 for (size_t i = 0U; i < TASKS_MAX; ++i)
1334 {
1335 KernelTask *const task = &m_task_storage[i];
1336 if (task->IsBusy())
1337 {
1338 // avoid task collision
1339 STK_ASSERT(task->m_user != user_task);
1340
1341 // avoid stack collision
1342 STK_ASSERT(task->m_user->GetStack() != user_task->GetStack());
1343 }
1344 else
1345 if (new_task == nullptr)
1346 {
1347 new_task = task;
1348 #if defined(NDEBUG) && !defined(_STK_ASSERT_REDIRECT)
1349 break; // break if assertions are inactive and do not try to validate collision with existing tasks
1350 #endif
1351 }
1352 else
1353 {
1354 // noop, continue to the next slot
1355 }
1356 }
1357
1358 // if nullptr - exceeded max supported kernel task count, application design failure
1359 STK_ASSERT(new_task != nullptr);
1360
1361 new_task->Bind(&m_platform, user_task);
1362
1363 return new_task;
1364 }
static constexpr size_t TASKS_MAX
Maximum number of concurrently registered tasks. Fixed at compile time. Exceeding this limit in AddTa...
Definition stk.h:898

Referenced by stk::Kernel< stk::KERNEL_DYNAMIC|stk::KERNEL_SYNC|stk::KERNEL_TICKLESS, 16U, stk::SwitchStrategyFP32, stk::PlatformDefault >::AllocateAndAddNewTask(), and stk::Kernel< stk::KERNEL_DYNAMIC|stk::KERNEL_SYNC|stk::KERNEL_TICKLESS, 16U, stk::SwitchStrategyFP32, stk::PlatformDefault >::HrtAllocateAndAddNewTask().

Here is the caller graph for this function:

◆ EnumerateKernelTasks()

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
size_t stk::Kernel< TMode, TSize, TStrategy, TPlatform >::EnumerateKernelTasks ( ArrayView< IKernelTask * > tasks)
inlineoverridevirtual

Enumerate kernel tasks.

Parameters
[in]tasksReference to the ArrayView of IKernelTask pointers.
Returns
Number of tasks in the array.

Implements stk::IKernel.

Definition at line 1151 of file stk.h.

1152 {
1153 size_t count = 0U;
1154 const size_t limit = Min(tasks.GetSize(), static_cast<size_t>(TASKS_MAX));
1155
1156 // avoid race with OnTick
1158
1159 for (size_t i = 0U; i < limit; ++i)
1160 {
1161 KernelTask *const task = &m_task_storage[i];
1162 if (task->IsBusy())
1163 {
1164 tasks[count++] = task;
1165 }
1166 }
1167
1168 return count;
1169 }
static constexpr T Min(T a, T b)
Compile-time minimum of two values.
Definition stk_defs.h:633

◆ EnumerateTasks()

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
size_t stk::Kernel< TMode, TSize, TStrategy, TPlatform >::EnumerateTasks ( ArrayView< ITask * > user_tasks)
inlineoverridevirtual

Enumerate user tasks.

Parameters
[in]user_tasksReference to the ArrayView of ITask pointers.
Returns
Number of tasks in the array.

Implements stk::IKernel.

Definition at line 1175 of file stk.h.

1176 {
1177 size_t count = 0U;
1178 const size_t limit = Min(user_tasks.GetSize(), static_cast<size_t>(TASKS_MAX));
1179
1180 // avoid race with OnTick
1182
1183 for (size_t i = 0U; i < limit; ++i)
1184 {
1185 KernelTask *const task = &m_task_storage[i];
1186 if (task->IsBusy())
1187 {
1188 user_tasks[count++] = task->GetUserTask();
1189 }
1190 }
1191
1192 return count;
1193 }

◆ EnumerateTasksT()

template<size_t TMaxCount, typename TCallback>
size_t stk::IKernel::EnumerateTasksT ( TCallback && callback)
inlineinherited

Enumerate tasks, invoking a callback for each active task.

Template Parameters
TMaxCountMaximum number of tasks to enumerate. Should match or exceed the kernel's task capacity. Determines the size of the internal stack-allocated buffer (TMaxCount * sizeof(ITask*) bytes on the stack).
TCallbackCallable type, deduced automatically. Must satisfy: bool(ITask*)
Parameters
[in]callbackCallable invoked for each active task. Return true to continue, false to stop early.
Returns
Number of tasks visited (up to TMaxCount).
Warning
ISR-safe.

Example:

kernel.EnumerateTasks<_STK_KERNEL_TASKS_COUNT>([](ITask *t) {
Log(t->GetTraceName());
return true; // continue
});
Interface for a user task.
Definition stk_common.h:589

Definition at line 1226 of file stk_common.h.

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 }
#define STK_STATIC_ASSERT(X)
Compile-time assertion. Produces a compilation error if X is false.
Definition stk_defs.h:448
virtual size_t EnumerateTasks(ArrayView< ITask * > user_tasks)=0
Enumerate user tasks.

References EnumerateTasks(), and STK_STATIC_ASSERT.

Here is the call graph for this function:

◆ FetchNextEvent()

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
EFsmEvent stk::Kernel< TMode, TSize, TStrategy, TPlatform >::FetchNextEvent ( KernelTask *& next)
inlineprotected

Fetch next event for the FSM.

Parameters
[out]nextNext kernel task to which Kernel can switch.
Returns
FSM event.

Definition at line 2045 of file stk.h.

2046 {
2048
2049 // try getting next task for scheduling
2050 next = static_cast<KernelTask *>(m_strategy.GetNext());
2051
2052 // sleep-aware strategy returns nullptr if no active tasks available
2053 if (next != nullptr)
2054 {
2055 // strategy must provide active-only task
2056 STK_ASSERT(!next->IsSleeping());
2057
2058 // if was sleeping, process wake event first
2060 }
2061 // start sleeping
2062 else
2063 {
2065 {
2066 // if nullptr is returned then either strategy has all tasks sleeping or none left,
2067 // if KERNEL_DYNAMIC mode and no tasks left then exit from scheduling
2068 if (m_strategy.GetSize() == 0U)
2069 {
2070 next = nullptr;
2072 }
2073 }
2074 }
2075
2076 return type;
2077 }
EFsmEvent
Finite-state machine (FSM) event. Computed by FetchNextEvent() each tick based on strategy output and...
Definition stk.h:1269

Referenced by stk::Kernel< stk::KERNEL_DYNAMIC|stk::KERNEL_SYNC|stk::KERNEL_TICKLESS, 16U, stk::SwitchStrategyFP32, stk::PlatformDefault >::GetNewFsmState().

Here is the caller graph for this function:

◆ FindTaskBySP()

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
KernelTask * stk::Kernel< TMode, TSize, TStrategy, TPlatform >::FindTaskBySP ( Word SP)
inlineprotected

Find kernel task for a Stack Pointer (SP).

Parameters
[in]SPStack pointer.
Returns
Kernel task.

Definition at line 1479 of file stk.h.

1480 {
1481 STK_ASSERT(m_task_now != nullptr);
1482
1483 KernelTask *found_task = nullptr;
1484
1485 if (m_task_now->IsMemoryOfSP(SP))
1486 {
1488 }
1489 else
1490 {
1491 for (size_t i = 0U; i < TASKS_MAX; ++i)
1492 {
1493 KernelTask *const task = &m_task_storage[i];
1494
1495 // skip finished tasks (applicable only for KERNEL_DYNAMIC mode)
1497 {
1498 if (!task->IsBusy())
1499 {
1500 continue;
1501 }
1502 }
1503
1504 if (task->IsMemoryOfSP(SP))
1505 {
1506 found_task = task;
1507 break;
1508 }
1509 }
1510 }
1511
1512 return found_task;
1513 }

Referenced by stk::Kernel< stk::KERNEL_DYNAMIC|stk::KERNEL_SYNC|stk::KERNEL_TICKLESS, 16U, stk::SwitchStrategyFP32, stk::PlatformDefault >::OnGetTid(), stk::Kernel< stk::KERNEL_DYNAMIC|stk::KERNEL_SYNC|stk::KERNEL_TICKLESS, 16U, stk::SwitchStrategyFP32, stk::PlatformDefault >::OnTaskSleep(), stk::Kernel< stk::KERNEL_DYNAMIC|stk::KERNEL_SYNC|stk::KERNEL_TICKLESS, 16U, stk::SwitchStrategyFP32, stk::PlatformDefault >::OnTaskSleepUntil(), stk::Kernel< stk::KERNEL_DYNAMIC|stk::KERNEL_SYNC|stk::KERNEL_TICKLESS, 16U, stk::SwitchStrategyFP32, stk::PlatformDefault >::OnTaskWait(), and stk::Kernel< stk::KERNEL_DYNAMIC|stk::KERNEL_SYNC|stk::KERNEL_TICKLESS, 16U, stk::SwitchStrategyFP32, stk::PlatformDefault >::RequestAddTask().

Here is the caller graph for this function:

◆ FindTaskByStack()

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
KernelTask * stk::Kernel< TMode, TSize, TStrategy, TPlatform >::FindTaskByStack ( const Stack * stack)
inlineprotected

Find kernel task by the bound Stack instance.

Parameters
[in]stackStack.
Returns
Kernel task.

Definition at line 1458 of file stk.h.

1459 {
1460 KernelTask *found_task = nullptr;
1461
1462 for (size_t i = 0U; i < TASKS_MAX; ++i)
1463 {
1464 KernelTask *const task = &m_task_storage[i];
1465 if (task->GetUserStackPtr() == stack)
1466 {
1467 found_task = task;
1468 break;
1469 }
1470 }
1471
1472 return found_task;
1473 }

Referenced by stk::Kernel< stk::KERNEL_DYNAMIC|stk::KERNEL_SYNC|stk::KERNEL_TICKLESS, 16U, stk::SwitchStrategyFP32, stk::PlatformDefault >::OnTaskExit().

Here is the caller graph for this function:

◆ FindTaskByUserTask()

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
KernelTask * stk::Kernel< TMode, TSize, TStrategy, TPlatform >::FindTaskByUserTask ( const ITask * user_task)
inlineprotected

◆ GetNewFsmState()

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
EFsmState stk::Kernel< TMode, TSize, TStrategy, TPlatform >::GetNewFsmState ( KernelTask *& next)
inlineprotected

Get new FSM state.

Parameters
[out]nextNext kernel task to which Kernel can switch.
Returns
FSM state.

Definition at line 2086 of file stk.h.

2087 {
2090 }
static bool IsValidFsmState(EFsmState state)
Check if FSM state is valid.
Definition stk.h:1287
const EFsmState m_fsm[FSM_STATE_MAX][FSM_EVENT_MAX]
Definition stk.h:2406
EFsmEvent FetchNextEvent(KernelTask *&next)
Fetch next event for the FSM.
Definition stk.h:2045

Referenced by stk::Kernel< stk::KERNEL_DYNAMIC|stk::KERNEL_SYNC|stk::KERNEL_TICKLESS, 16U, stk::SwitchStrategyFP32, stk::PlatformDefault >::OnStart(), and stk::Kernel< stk::KERNEL_DYNAMIC|stk::KERNEL_SYNC|stk::KERNEL_TICKLESS, 16U, stk::SwitchStrategyFP32, stk::PlatformDefault >::UpdateFsmState().

Here is the caller graph for this function:

◆ GetPlatform()

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
IPlatform * stk::Kernel< TMode, TSize, TStrategy, TPlatform >::GetPlatform ( )
inlineoverridevirtual

Get platform driver instance owned by this kernel.

Returns
Pointer to the internal TPlatform cast to IPlatform*.

Implements stk::IKernel.

Definition at line 1238 of file stk.h.

1238{ return &m_platform; }

◆ GetState()

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
EKernelState stk::Kernel< TMode, TSize, TStrategy, TPlatform >::GetState ( ) const
inlineoverridevirtual

Get kernel state.

Implements stk::IKernel.

Definition at line 1247 of file stk.h.

1247{ return m_kstate; }

◆ GetSwitchStrategy()

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
ITaskSwitchStrategy * stk::Kernel< TMode, TSize, TStrategy, TPlatform >::GetSwitchStrategy ( )
inlineoverridevirtual

Get task-switching strategy instance owned by this kernel.

Returns
Pointer to the internal TStrategy cast to ITaskSwitchStrategy*.

Implements stk::IKernel.

Definition at line 1243 of file stk.h.

1243{ return &m_strategy; }

◆ HrtAllocateAndAddNewTask()

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
void stk::Kernel< TMode, TSize, TStrategy, TPlatform >::HrtAllocateAndAddNewTask ( ITask * user_task,
Timeout periodicity_tc,
Timeout deadline_tc,
Timeout start_delay_tc )
inlineprotected

Allocate new instance of KernelTask and add it into the HRT scheduling process.

Note
Related to stk::KERNEL_HRT mode only.
Parameters
[in]user_taskUser task for which kernel task object is allocated.
[in]periodicity_tcPeriodicity time at which task is scheduled (ticks).
[in]deadline_tcDeadline time within which a task must complete its work (ticks).
[in]start_delay_tcInitial start delay for the task (ticks).

Definition at line 1399 of file stk.h.

1400 {
1402 STK_ASSERT(task != nullptr);
1403
1405
1407 }

Referenced by stk::Kernel< stk::KERNEL_DYNAMIC|stk::KERNEL_SYNC|stk::KERNEL_TICKLESS, 16U, stk::SwitchStrategyFP32, stk::PlatformDefault >::AddTask().

Here is the caller graph for this function:

◆ Initialize()

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
void stk::Kernel< TMode, TSize, TStrategy, TPlatform >::Initialize ( uint32_t resolution_us = PERIODICITY_DEFAULT)
inlineoverridevirtual

Initialize kernel.

Parameters
[in]resolution_usResolution of the system tick (SysTick) timer in microseconds. Defaults to PERIODICITY_DEFAULT (1000 µs = 1 ms).
Note
Must be called before AddTask() and Start().
If running on an STM32 device with HAL driver or on QEMU, do not change the default resolution (PERIODICITY_DEFAULT). STM32's HAL expects 1 millisecond resolution and QEMU does not have enough resolution on Windows to operate correctly at sub-millisecond resolution.
Kernel must be in STATE_INACTIVE state.

Implements stk::IKernel.

Definition at line 940 of file stk.h.

941 {
945
946 // reinitialize key state variables
947 m_task_now = nullptr;
950
951 // exit trap is required only for KERNEL_DYNAMIC mode
952 Stack *exit_trap = nullptr;
954 {
955 exit_trap = &m_exit_trap[0].stack;
956 }
957 else
958 {
960 }
961
962 m_service.Initialize(this);
963 m_platform.Initialize(this, &m_service, resolution_us, exit_trap);
964
965 // now ready to Start()
967 }
KernelService m_service
Kernel service singleton exposed to running tasks via IKernelService::GetInstance().
Definition stk.h:2394

◆ InitTraps()

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
void stk::Kernel< TMode, TSize, TStrategy, TPlatform >::InitTraps ( )
inlineprotected

Initialize stack of the traps.

Definition at line 1295 of file stk.h.

1296 {
1297 // init stack for a Sleep trap
1298 {
1300
1302 sleep.stack.access_mode = ACCESS_PRIVILEGED;
1303 #if STK_NEED_TASK_ID
1304 sleep.stack.tid = SYS_TASK_ID_SLEEP;
1305 #endif
1306
1307 STK_UNUSED(m_platform.InitStack(STACK_SLEEP_TRAP, &sleep.stack, &wrapper, nullptr));
1308 }
1309
1310 // init stack for an Exit trap
1312 {
1314
1316 exit.stack.access_mode = ACCESS_PRIVILEGED;
1317 #if STK_NEED_TASK_ID
1318 exit.stack.tid = SYS_TASK_ID_EXIT;
1319 #endif
1320
1321 STK_UNUSED(m_platform.InitStack(STACK_EXIT_TRAP, &exit.stack, &wrapper, nullptr));
1322 }
1323 }
StackMemoryWrapper< STACK_SIZE_MIN > ExitTrapStackMemory
Stack memory wrapper type for the exit trap.
Definition stk.h:97
StackMemoryWrapper<((32U))> SleepTrapStackMemory
Stack memory wrapper type for the sleep trap.
Definition stk.h:91
Storage bundle for the sleep trap: a Stack descriptor paired with its backing memory.
Definition stk.h:2364
Storage bundle for the exit trap: a Stack descriptor paired with its backing memory.
Definition stk.h:2380

Referenced by stk::Kernel< stk::KERNEL_DYNAMIC|stk::KERNEL_SYNC|stk::KERNEL_TICKLESS, 16U, stk::SwitchStrategyFP32, stk::PlatformDefault >::Start().

Here is the caller graph for this function:

◆ IsDynamicMode()

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
constexpr bool stk::Kernel< TMode, TSize, TStrategy, TPlatform >::IsDynamicMode ( )
inlinestaticconstexprprotected

Definition at line 2323 of file stk.h.

2323{ return ((TMode & KERNEL_DYNAMIC) != 0U); }

Referenced by stk::Kernel< stk::KERNEL_DYNAMIC|stk::KERNEL_SYNC|stk::KERNEL_TICKLESS, 16U, stk::SwitchStrategyFP32, stk::PlatformDefault >::AddTask(), stk::Kernel< stk::KERNEL_DYNAMIC|stk::KERNEL_SYNC|stk::KERNEL_TICKLESS, 16U, stk::SwitchStrategyFP32, stk::PlatformDefault >::FetchNextEvent(), stk::Kernel< stk::KERNEL_DYNAMIC|stk::KERNEL_SYNC|stk::KERNEL_TICKLESS, 16U, stk::SwitchStrategyFP32, stk::PlatformDefault >::FindTaskBySP(), stk::Kernel< stk::KERNEL_DYNAMIC|stk::KERNEL_SYNC|stk::KERNEL_TICKLESS, 16U, stk::SwitchStrategyFP32, stk::PlatformDefault >::Initialize(), stk::Kernel< stk::KERNEL_DYNAMIC|stk::KERNEL_SYNC|stk::KERNEL_TICKLESS, 16U, stk::SwitchStrategyFP32, stk::PlatformDefault >::InitTraps(), stk::Kernel< stk::KERNEL_DYNAMIC|stk::KERNEL_SYNC|stk::KERNEL_TICKLESS, 16U, stk::SwitchStrategyFP32, stk::PlatformDefault >::OnStop(), stk::Kernel< stk::KERNEL_DYNAMIC|stk::KERNEL_SYNC|stk::KERNEL_TICKLESS, 16U, stk::SwitchStrategyFP32, stk::PlatformDefault >::OnTaskExit(), stk::Kernel< stk::KERNEL_DYNAMIC|stk::KERNEL_SYNC|stk::KERNEL_TICKLESS, 16U, stk::SwitchStrategyFP32, stk::PlatformDefault >::RemoveTask(), stk::Kernel< stk::KERNEL_DYNAMIC|stk::KERNEL_SYNC|stk::KERNEL_TICKLESS, 16U, stk::SwitchStrategyFP32, stk::PlatformDefault >::ScheduleTaskRemoval(), stk::Kernel< stk::KERNEL_DYNAMIC|stk::KERNEL_SYNC|stk::KERNEL_TICKLESS, 16U, stk::SwitchStrategyFP32, stk::PlatformDefault >::StateExit(), stk::Kernel< stk::KERNEL_DYNAMIC|stk::KERNEL_SYNC|stk::KERNEL_TICKLESS, 16U, stk::SwitchStrategyFP32, stk::PlatformDefault >::UpdateTaskRequest(), and stk::Kernel< stk::KERNEL_DYNAMIC|stk::KERNEL_SYNC|stk::KERNEL_TICKLESS, 16U, stk::SwitchStrategyFP32, stk::PlatformDefault >::UpdateTaskState().

Here is the caller graph for this function:

◆ IsHrtMode()

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
constexpr bool stk::Kernel< TMode, TSize, TStrategy, TPlatform >::IsHrtMode ( )
inlinestaticconstexprprotected

Definition at line 2324 of file stk.h.

2324{ return ((TMode & KERNEL_HRT) != 0U); }

Referenced by stk::Kernel< stk::KERNEL_DYNAMIC|stk::KERNEL_SYNC|stk::KERNEL_TICKLESS, 16U, stk::SwitchStrategyFP32, stk::PlatformDefault >::AddTask(), stk::Kernel< stk::KERNEL_DYNAMIC|stk::KERNEL_SYNC|stk::KERNEL_TICKLESS, 16U, stk::SwitchStrategyFP32, stk::PlatformDefault >::AddTask(), stk::Kernel< TMode, TSize, TStrategy, TPlatform >::KernelTask::GetHrtDeadline(), stk::Kernel< TMode, TSize, TStrategy, TPlatform >::KernelTask::GetHrtPeriodicity(), stk::Kernel< TMode, TSize, TStrategy, TPlatform >::KernelTask::GetHrtRelativeDeadline(), stk::Kernel< stk::KERNEL_DYNAMIC|stk::KERNEL_SYNC|stk::KERNEL_TICKLESS, 16U, stk::SwitchStrategyFP32, stk::PlatformDefault >::OnStart(), stk::Kernel< stk::KERNEL_DYNAMIC|stk::KERNEL_SYNC|stk::KERNEL_TICKLESS, 16U, stk::SwitchStrategyFP32, stk::PlatformDefault >::OnTaskSleep(), stk::Kernel< TMode, TSize, TStrategy, TPlatform >::KernelTask::ScheduleRemoval(), stk::Kernel< TMode, TSize, TStrategy, TPlatform >::KernelService::Sleep(), stk::Kernel< TMode, TSize, TStrategy, TPlatform >::KernelService::SleepCancel(), stk::Kernel< TMode, TSize, TStrategy, TPlatform >::KernelService::SleepUntil(), stk::Kernel< stk::KERNEL_DYNAMIC|stk::KERNEL_SYNC|stk::KERNEL_TICKLESS, 16U, stk::SwitchStrategyFP32, stk::PlatformDefault >::StateSleep(), stk::Kernel< stk::KERNEL_DYNAMIC|stk::KERNEL_SYNC|stk::KERNEL_TICKLESS, 16U, stk::SwitchStrategyFP32, stk::PlatformDefault >::StateSwitch(), stk::Kernel< stk::KERNEL_DYNAMIC|stk::KERNEL_SYNC|stk::KERNEL_TICKLESS, 16U, stk::SwitchStrategyFP32, stk::PlatformDefault >::StateWake(), stk::Kernel< TMode, TSize, TStrategy, TPlatform >::KernelTask::Unbind(), stk::Kernel< stk::KERNEL_DYNAMIC|stk::KERNEL_SYNC|stk::KERNEL_TICKLESS, 16U, stk::SwitchStrategyFP32, stk::PlatformDefault >::UpdateTaskRequest(), and stk::Kernel< stk::KERNEL_DYNAMIC|stk::KERNEL_SYNC|stk::KERNEL_TICKLESS, 16U, stk::SwitchStrategyFP32, stk::PlatformDefault >::UpdateTaskState().

Here is the caller graph for this function:

◆ IsInitialized()

◆ IsStarted()

◆ IsStaticMode()

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
constexpr bool stk::Kernel< TMode, TSize, TStrategy, TPlatform >::IsStaticMode ( )
inlinestaticconstexprprotected

Definition at line 2322 of file stk.h.

2322{ return ((TMode & KERNEL_STATIC) != 0U); }

◆ IsSyncMode()

◆ IsTicklessMode()

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
constexpr bool stk::Kernel< TMode, TSize, TStrategy, TPlatform >::IsTicklessMode ( )
inlinestaticconstexprprotected

◆ IsValidFsmState()

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
bool stk::Kernel< TMode, TSize, TStrategy, TPlatform >::IsValidFsmState ( EFsmState state)
inlinestaticprotected

Check if FSM state is valid.

Definition at line 1287 of file stk.h.

1288 {
1289 return (state > FSM_STATE_NONE) &&
1290 (state < FSM_STATE_MAX);
1291 }

Referenced by stk::Kernel< stk::KERNEL_DYNAMIC|stk::KERNEL_SYNC|stk::KERNEL_TICKLESS, 16U, stk::SwitchStrategyFP32, stk::PlatformDefault >::GetNewFsmState().

Here is the caller graph for this function:

◆ OnGetTid()

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
TId stk::Kernel< TMode, TSize, TStrategy, TPlatform >::OnGetTid ( Word caller_SP)
inlineoverrideprotectedvirtual

Called from the Thread process when for getting task/thread id of the process.

Parameters
[in]caller_SPValue of Stack Pointer (SP) register (for locating the calling process inside the kernel).
Returns
Task/thread id of the process (returns always valid TId belonging to a task).

Implements stk::IPlatform::IEventHandler.

Definition at line 1803 of file stk.h.

1804 {
1806 STK_ASSERT(task != nullptr);
1807
1808 return task->GetTid();
1809 }
KernelTask * FindTaskBySP(Word SP)
Find kernel task for a Stack Pointer (SP).
Definition stk.h:1479

◆ OnInheritWeight()

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
void stk::Kernel< TMode, TSize, TStrategy, TPlatform >::OnInheritWeight ( TId tid,
Weight weight )
inlineprotected

Definition at line 1836 of file stk.h.

1837 {
1840
1841 if (weight != NO_WEIGHT)
1842 {
1844 STK_ASSERT(task != nullptr);
1845
1846 const Weight prev_weight = task->GetWeight();
1847
1848 if (prev_weight < weight)
1849 {
1850 task->SetCurrentWeight(weight);
1851 m_strategy.OnTaskWeightChange(task, prev_weight);
1852 }
1853 }
1854 }
static constexpr ITask * GetUserTaskFromTid(TId task_id) noexcept
Get task instance from its identifier.
Definition stk_arch.h:501
KernelTask * FindTaskByUserTask(const ITask *user_task)
Find kernel task by the bound ITask instance.
Definition stk.h:1437

◆ OnRestoreWeight()

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
void stk::Kernel< TMode, TSize, TStrategy, TPlatform >::OnRestoreWeight ( TId tid,
ISyncObject * sobj )
inlineprotected

Definition at line 1856 of file stk.h.

1857 {
1860
1862 STK_ASSERT(task != nullptr);
1863
1864 const Weight prev_weight = task->GetWeight();
1865
1866 // restore to original or boost from wait objects
1867 task->SetCurrentWeight(sobj != nullptr ? sobj->FindWeightHigherThan(task->GetWeight()) : NO_WEIGHT);
1868
1869 m_strategy.OnTaskWeightChange(task, prev_weight);
1870 }

◆ OnStart()

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
void stk::Kernel< TMode, TSize, TStrategy, TPlatform >::OnStart ( Stack *& active)
inlineoverrideprotectedvirtual

Called by platform driver immediately after a scheduler start (first tick).

Parameters
[out]activeSet to the stack of the first task to run, or to the sleep-trap stack if all tasks are initially sleeping.
Note
Delivers initial OnTaskSleep notifications to sleep-aware strategies for any tasks that were added in a sleeping state before Start() was called.
Selects the first runnable task via GetNewFsmState() and transitions the kernel to STATE_RUNNING.
If STK_SEGGER_SYSVIEW is enabled, emits a task-start trace event for the first task.
Warning
At least one task must have been added via AddTask(); asserts if the strategy pool is empty.

Implements stk::IPlatform::IEventHandler.

Definition at line 1544 of file stk.h.

1545 {
1546 STK_ASSERT(m_strategy.GetSize() != 0);
1547
1548 // iterate tasks and generate OnTaskSleep for a strategy for all initially sleeping tasks
1550 {
1551 for (size_t i = 0U; i < TASKS_MAX; ++i)
1552 {
1553 KernelTask *const task = &m_task_storage[i];
1554
1555 if (task->IsSleeping())
1556 {
1557 if ((task->m_state & KernelTask::STATE_SLEEP_PENDING) != 0U)
1558 {
1560
1561 // notify strategy that task is sleeping
1562 m_strategy.OnTaskSleep(task);
1563 }
1564 }
1565 }
1566 }
1567
1568 // get initial state and first task
1569 {
1571
1572 KernelTask *next = nullptr;
1574
1575 // expecting only SLEEPING or SWITCHING states
1577
1579 {
1580 m_task_now = next;
1581
1582 active = next->GetUserStackPtr();
1583
1585 {
1586 next->HrtOnSwitchedIn();
1587 }
1588 }
1589 else
1591 {
1592 // MISRA 5-2-3 deviation: GetNext/GetFirst returns IKernelTask*, all objects in
1593 // the strategy pool are KernelTask instances - downcast is guaranteed safe.
1594 m_task_now = static_cast<KernelTask *>(m_strategy.GetFirst());
1595
1596 active = &m_sleep_trap[0].stack;
1597 }
1598 else
1599 {
1600 // unexpected state
1602 }
1603 }
1604
1605 // is in running state
1607
1608 #if STK_SEGGER_SYSVIEW
1610 #endif
1611 }
static __stk_forceinline void STK_KERNEL_PANIC(stk::EKernelPanicId id)
Called when the kernel detects an unrecoverable internal fault.
Definition stk_arch.h:75
EFsmState GetNewFsmState(KernelTask *&next)
Get new FSM state.
Definition stk.h:2086
@ STATE_SLEEP_PENDING
Task called Sleep/SleepUntil/Yield; strategy's OnTaskSleep() will be invoked on the next tick (sleep-...
Definition stk.h:132

◆ OnStop()

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
void stk::Kernel< TMode, TSize, TStrategy, TPlatform >::OnStop ( )
inlineoverrideprotectedvirtual

Called by the platform driver after a scheduler stop (all tasks have exited).

Note
KERNEL_DYNAMIC mode only: resets FSM to FSM_STATE_NONE and transitions kernel back to STATE_READY so Start() may be called again.
Has no effect in KERNEL_STATIC mode (static kernels never stop).

Implements stk::IPlatform::IEventHandler.

Definition at line 1618 of file stk.h.

1619 {
1621 {
1623
1624 // is in stopped state, i.e. is ready to Start() again
1626 }
1627 }

◆ OnSuspend()

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
void stk::Kernel< TMode, TSize, TStrategy, TPlatform >::OnSuspend ( bool suspended)
inlineoverrideprotectedvirtual

Called from the Thread process to suspend scheduling.

Parameters
[in]suspendedtrue if scheduling was successfully suspended, false otherwise.

Implements stk::IPlatform::IEventHandler.

Definition at line 1811 of file stk.h.

1812 {
1813 // toggle kernel state
1814 if (suspended)
1815 {
1816 if (m_kstate == KSTATE_RUNNING)
1817 {
1819 }
1820 }
1821 else
1822 {
1823 if (m_kstate == KSTATE_SUSPENDED)
1824 {
1826 }
1827 }
1828
1829 // force yield for a currently active task
1830 if (!m_task_now->IsSleeping())
1831 {
1832 m_task_now->ScheduleSleep(YIELD_TICKS);
1833 }
1834 }
static constexpr Timeout YIELD_TICKS
Ticks to yield.
Definition stk.h:1283

◆ OnTaskExit()

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
void stk::Kernel< TMode, TSize, TStrategy, TPlatform >::OnTaskExit ( Stack * stack)
inlineoverrideprotectedvirtual

Called from the Thread process when task finished (its Run function exited by return).

Parameters
[out]stackStack of the exited task.

Implements stk::IPlatform::IEventHandler.

Definition at line 1744 of file stk.h.

1745 {
1747 {
1749 STK_ASSERT(task != nullptr);
1750
1751 // notify kernel to execute removal
1752 task->ScheduleRemoval();
1753 }
1754 else
1755 {
1756 // kernel operating mode must be KERNEL_DYNAMIC for tasks to be able to exit
1758 }
1759 }
KernelTask * FindTaskByStack(const Stack *stack)
Find kernel task by the bound Stack instance.
Definition stk.h:1458

◆ OnTaskSleep()

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
void stk::Kernel< TMode, TSize, TStrategy, TPlatform >::OnTaskSleep ( Word caller_SP,
Timeout ticks )
inlineoverrideprotectedvirtual

Called by Thread process (via IKernelService::Sleep) for exclusion of the calling process from scheduling (sleeping).

Parameters
[in]caller_SPValue of Stack Pointer (SP) register (for locating the calling process inside the kernel).
[in]ticksTime to sleep (ticks).

Implements stk::IPlatform::IEventHandler.

Definition at line 1677 of file stk.h.

1678 {
1680 STK_ASSERT(task != nullptr);
1681
1682 // make change to HRT state and sleep time atomic
1683 {
1685
1687 {
1688 task->HrtOnWorkCompleted();
1689 }
1690
1691 if (ticks > 0)
1692 {
1693 task->ScheduleSleep(ticks);
1694 }
1695 }
1696
1697 // note: we do not spin long here, kernel will switch this task out from scheduling on the next tick
1698 task->BusyWaitWhileSleeping();
1699 }

Referenced by stk::Kernel< stk::KERNEL_DYNAMIC|stk::KERNEL_SYNC|stk::KERNEL_TICKLESS, 16U, stk::SwitchStrategyFP32, stk::PlatformDefault >::OnTaskSwitch().

Here is the caller graph for this function:

◆ OnTaskSleepCancel()

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
void stk::Kernel< TMode, TSize, TStrategy, TPlatform >::OnTaskSleepCancel ( TId task_id)
inlineprotected

Definition at line 1730 of file stk.h.

1731 {
1733 if (task != nullptr)
1734 {
1736
1737 if (task->IsSleeping())
1738 {
1739 task->Wake();
1740 }
1741 }
1742 }

◆ OnTaskSleepUntil()

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
bool stk::Kernel< TMode, TSize, TStrategy, TPlatform >::OnTaskSleepUntil ( Word caller_SP,
Ticks timestamp )
inlineoverrideprotectedvirtual

Called by Thread process (via IKernelService::SleepUntil) for exclusion of the calling process from scheduling (sleeping).

Parameters
[in]caller_SPValue of Stack Pointer (SP) register (for locating the calling process inside the kernel).
[in]timestampAbsolute timestamp (ticks).
Returns
True if sleep succeeded, false otherwise.

Implements stk::IPlatform::IEventHandler.

Definition at line 1701 of file stk.h.

1702 {
1704 STK_ASSERT(task != nullptr);
1705
1706 bool result = true;
1707
1708 // make change to HRT state and sleep time atomic
1709 {
1711
1712 // calculate signed delta (handles wrap-around correctly)
1713 const Ticks delta = timestamp - m_service.m_ticks;
1714
1715 if (delta > 0)
1716 {
1717 task->ScheduleSleep(static_cast<Timeout>(Min(delta, static_cast<Ticks>(INT32_MAX))));
1718 }
1719 else
1720 {
1721 result = false; // deadline already hit or passed
1722 }
1723 }
1724
1725 // note: we do not spin long here, kernel will switch this task out from scheduling on the next tick
1726 task->BusyWaitWhileSleeping();
1727 return result;
1728 }

◆ OnTaskSwitch()

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
void stk::Kernel< TMode, TSize, TStrategy, TPlatform >::OnTaskSwitch ( Word caller_SP)
inlineoverrideprotectedvirtual

Called by Thread process (via IKernelService::SwitchToNext) to switch to a next task.

Parameters
[in]caller_SPValue of Stack Pointer (SP) register (for locating the calling process inside the kernel).

Implements stk::IPlatform::IEventHandler.

Definition at line 1672 of file stk.h.

1673 {
1675 }
void OnTaskSleep(Word caller_SP, Timeout ticks) override
Called by Thread process (via IKernelService::Sleep) for exclusion of the calling process from schedu...
Definition stk.h:1677

◆ OnTaskWait()

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
IWaitObject * stk::Kernel< TMode, TSize, TStrategy, TPlatform >::OnTaskWait ( Word caller_SP,
ISyncObject * sync_obj,
IMutex * mutex,
Timeout timeout )
inlineoverrideprotectedvirtual

Called from the Thread process when task needs to wait.

Parameters
[in]caller_SPValue of Stack Pointer (SP) register (for locating the calling process inside the kernel).
[in]sync_objISyncObject instance (passed by Wait).
[in]mutexIMutex instance (passed by Wait).
[in]timeoutTime to sleep (ticks).

Implements stk::IPlatform::IEventHandler.

Definition at line 1761 of file stk.h.

1762 {
1764 {
1765 STK_ASSERT(timeout != 0); // API contract: caller must not be in ISR
1766 STK_ASSERT(sync_obj != nullptr); // API contract: ISyncObject instance must be provided
1767 STK_ASSERT(mutex != nullptr); // API contract: IMutex instance must be provided
1768 STK_ASSERT((sync_obj->GetHead() == nullptr) || (sync_obj->GetHead() == &m_sync_list[0]));
1769
1771 STK_ASSERT(task != nullptr);
1772
1773 // configure waiting
1774 task->m_wait_obj->SetupWait(sync_obj, timeout);
1775
1776 // register ISyncObject if not yet
1777 if (sync_obj->GetHead() == nullptr)
1778 {
1779 m_sync_list->LinkBack(sync_obj);
1780 }
1781
1782 // start sleeping infinitely, we rely on a Wake call via WaitObject
1783 task->ScheduleSleep(WAIT_INFINITE);
1784
1785 // unlock mutex locked externally, so that we could wait in a busy-waiting loop
1786 mutex->Unlock();
1787
1788 // note: we do not spin long here, kernel will switch this task out from scheduling on the next tick
1789 task->BusyWaitWhileSleeping();
1790
1791 // re-lock mutex when returning to the task's execution space
1792 mutex->Lock();
1793
1794 return task->m_wait_obj;
1795 }
1796 else
1797 {
1798 STK_ASSERT(false);
1799 return nullptr;
1800 }
1801 }
SyncObjectList m_sync_list[STK_ALLOCATE_COUNT< TMode, KERNEL_SYNC, 1U, 0U >::Value]
List of active sync objects. Zero-size (no memory) if KERNEL_SYNC is not set.
Definition stk.h:2404
static constexpr bool IsSyncMode()
Definition stk.h:2325

◆ OnTick()

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
bool stk::Kernel< TMode, TSize, TStrategy, TPlatform >::OnTick ( Stack *& idle,
Stack *& active,
Timeout & ticks )
inlineoverrideprotectedvirtual

Process one scheduler tick. Called from the platform timer/tick ISR.

Parameters
[out]idleStack descriptor to context-switch out (nullptr if no switch needed).
[out]activeStack descriptor to context-switch in (nullptr if no switch needed).
[in,out]ticks(KERNEL_TICKLESS builds only) On entry: actual number of ticks elapsed since the last call, as measured by the platform driver. On return: the number of ticks the hardware timer may suppress before the next required wakeup, computed as the minimum remaining sleep across all active tasks, clamped to [1, STK_TICKLESS_TICKS_MAX]. The platform driver programs this value into the timer to avoid unnecessary wakeups. This parameter is absent in non-tickless builds.
Returns
true if a context switch is required (idle and active are valid); false if the current task continues running.
Note
In non-tickless mode the internal tick counter always advances by exactly 1 per call.
In tickless mode (KERNEL_TICKLESS) the counter advances by the ticks value supplied by the platform driver, which may be greater than 1 after a suppressed interval.

Implements stk::IPlatform::IEventHandler.

Definition at line 1645 of file stk.h.

1650 {
1651 #if !STK_TICKLESS_IDLE
1652 // in non-tickless mode kernel is advancing strictly by 1 tick on every OnTick call
1653 enum { ticks = 1 };
1654 #endif
1655
1656 // advance internal timestamp
1657 m_service.IncrementTicks(ticks);
1658
1659 // consume elapsed and update to ticks to sleep
1660 #if STK_TICKLESS_IDLE
1661 ticks = (
1662 #else
1663 // notify compiler that we ignore a return value of UpdateTasks
1664 static_cast<void>(
1665 #endif
1667
1668 // decide on a context switch
1669 return UpdateFsmState(idle, active);
1670 }
bool UpdateFsmState(Stack *&idle, Stack *&active)
Update FSM state.
Definition stk.h:2097
Timeout UpdateTasks(const Timeout elapsed_ticks)
Update tasks (sleep, requests).
Definition stk.h:1874

◆ RemoveTask() [1/2]

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
void stk::Kernel< TMode, TSize, TStrategy, TPlatform >::RemoveTask ( ITask * user_task)
inlineoverridevirtual

Remove a previously added task from the kernel before Start().

Parameters
[in]user_taskUser task to remove. Must not be nullptr.
Note
Only valid before Start() (i.e. while the kernel is not running). To remove tasks after Start() the task should return from its Run function (in KERNEL_DYNAMIC mode the slot is freed automatically on the next tick).
Warning
KERNEL_DYNAMIC mode only. Asserts if called in KERNEL_STATIC or KERNEL_HRT mode, or if called after Start().

Implements stk::IKernel.

Definition at line 1041 of file stk.h.

1042 {
1044 {
1045 STK_ASSERT(user_task != nullptr);
1047
1049 if (task != nullptr)
1050 {
1052 }
1053 }
1054 else
1055 {
1056 // kernel operating mode must be KERNEL_DYNAMIC for tasks to be able to be removed
1057 STK_ASSERT(false);
1058 }
1059 }
void RemoveTask(ITask *user_task) override
Remove a previously added task from the kernel before Start().
Definition stk.h:1041

Referenced by stk::Kernel< stk::KERNEL_DYNAMIC|stk::KERNEL_SYNC|stk::KERNEL_TICKLESS, 16U, stk::SwitchStrategyFP32, stk::PlatformDefault >::RemoveTask(), and stk::Kernel< stk::KERNEL_DYNAMIC|stk::KERNEL_SYNC|stk::KERNEL_TICKLESS, 16U, stk::SwitchStrategyFP32, stk::PlatformDefault >::UpdateTaskState().

Here is the caller graph for this function:

◆ RemoveTask() [2/2]

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
void stk::Kernel< TMode, TSize, TStrategy, TPlatform >::RemoveTask ( KernelTask * task)
inlineprotected

Remove kernel task.

Note
Removal of the kernel task means releasing it from the user task details.
Parameters
[in]taskKernel task.

Definition at line 1519 of file stk.h.

1520 {
1521 STK_ASSERT(task != nullptr);
1522
1523 #if STK_SEGGER_SYSVIEW
1524 SEGGER_SYSVIEW_OnTaskTerminate(task->GetUserStackPtr()->tid);
1525 #endif
1526
1527 // notify task about pending exit
1528 task->GetUserTask()->OnExit();
1529
1530 m_strategy.RemoveTask(task);
1531 task->Unbind();
1532 }

◆ RequestAddTask()

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
void stk::Kernel< TMode, TSize, TStrategy, TPlatform >::RequestAddTask ( ITask *const user_task)
inlineprotected

Request to add new task.

Note
Must be called by the task process only!
Parameters
[in]user_taskUser task to add.

Definition at line 1413 of file stk.h.

1414 {
1415 KernelTask *const caller = FindTaskBySP(m_platform.GetCallerSP());
1416 STK_ASSERT(caller != nullptr);
1417
1418 typename KernelTask::AddTaskRequest req = { .user_task = user_task };
1419 caller->m_srt[0].add_task_req = &req;
1420
1421 // notify kernel
1423
1424 // switch out and wait for completion (due to context switch request could be processed here)
1425 if (caller->m_srt[0].add_task_req != nullptr)
1426 {
1427 m_service.SwitchToNext();
1428 }
1429
1430 STK_ASSERT(caller->m_srt[0].add_task_req == nullptr);
1431 }
void ScheduleAddTask()
Signal the kernel to process a pending AddTask request on the next tick.
Definition stk.h:2294
Payload for an in-flight AddTask() request issued by a running task.
Definition stk.h:144

Referenced by stk::Kernel< stk::KERNEL_DYNAMIC|stk::KERNEL_SYNC|stk::KERNEL_TICKLESS, 16U, stk::SwitchStrategyFP32, stk::PlatformDefault >::AddTask().

Here is the caller graph for this function:

◆ ResumeTask()

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
void stk::Kernel< TMode, TSize, TStrategy, TPlatform >::ResumeTask ( ITask * user_task)
inlineoverridevirtual

Resume task.

Parameters
[in]user_taskPointer to the user task to resume.

Implements stk::IKernel.

Definition at line 1131 of file stk.h.

1132 {
1133 STK_ASSERT(user_task != nullptr);
1134
1135 // avoid race with OnTick
1137
1139 STK_ASSERT(task != nullptr);
1140
1141 if (task->IsSleeping())
1142 {
1143 task->Wake();
1144 }
1145 }

◆ ScheduleAddTask()

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
void stk::Kernel< TMode, TSize, TStrategy, TPlatform >::ScheduleAddTask ( )
inlineprotected

Signal the kernel to process a pending AddTask request on the next tick.

Note
Sets the REQ_ADD_TASK bit in m_request and emits a full memory fence so the ISR-side tick handler observes the flag without delay.

Definition at line 2294 of file stk.h.

2295 {
2298 }

Referenced by stk::Kernel< stk::KERNEL_DYNAMIC|stk::KERNEL_SYNC|stk::KERNEL_TICKLESS, 16U, stk::SwitchStrategyFP32, stk::PlatformDefault >::RequestAddTask().

Here is the caller graph for this function:

◆ ScheduleTaskRemoval()

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
void stk::Kernel< TMode, TSize, TStrategy, TPlatform >::ScheduleTaskRemoval ( ITask * user_task)
inlineoverridevirtual

Schedule task removal from scheduling (exit).

Parameters
[in]user_taskUser task to remove. Must not be nullptr.
Warning
KERNEL_DYNAMIC mode only. Asserts if called in KERNEL_STATIC or KERNEL_HRT mode, or if called after Start().

Implements stk::IKernel.

Definition at line 1066 of file stk.h.

1067 {
1069 {
1070 STK_ASSERT(user_task != nullptr);
1072
1074
1076 if (task != nullptr)
1077 {
1078 task->ScheduleRemoval();
1079 }
1080 }
1081 else
1082 {
1083 // kernel operating mode must be KERNEL_DYNAMIC for tasks to be able to be removed
1084 STK_ASSERT(false);
1085 }
1086 }

◆ Start()

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
void stk::Kernel< TMode, TSize, TStrategy, TPlatform >::Start ( )
inlineoverridevirtual

Start the scheduler. This call does not return until all tasks have exited (KERNEL_DYNAMIC mode) or indefinitely (KERNEL_STATIC mode).

Note
Re-initializes trap stacks on every call so Start() can be called again after a previous scheduling session ended.
If STK_SEGGER_SYSVIEW is enabled, starts tracing and registers all pre-added tasks.
Warning
At least one task must have been added via AddTask() before calling Start(). Asserts if called before Initialize().

Implements stk::IKernel.

Definition at line 1203 of file stk.h.

1204 {
1206
1207 // stacks of the traps must be re-initilized on every subsequent Start
1208 InitTraps();
1209
1210 // start tracing
1211 #if STK_SEGGER_SYSVIEW
1213 for (size_t i = 0U; i < TASKS_MAX; ++i)
1214 {
1216 if (task->IsBusy())
1217 {
1219 }
1220 }
1221 #endif
1222
1223 m_platform.Start();
1224 }
void InitTraps()
Initialize stack of the traps.
Definition stk.h:1295

◆ StateExit()

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
bool stk::Kernel< TMode, TSize, TStrategy, TPlatform >::StateExit ( KernelTask * now,
KernelTask * next,
Stack *& idle,
Stack *& active )
inlineprotected

Exits from scheduling.

Note
FSM state: stk::FSM_STATE_EXITING.
Exits only if stk::KERNEL_DYNAMIC mode is specified, otherwise ignored.
Parameters
[in]nowCurrently active kernel task (ignored).
[in]nextNext kernel task (ignored).
[out]idleStack of the task which must enter Idle state.
[out]activeStack of the task which must enter Active state (to which context will switch).

Definition at line 2259 of file stk.h.

2260 {
2261 STK_UNUSED(now);
2263
2265 {
2266 // dynamic tasks are not supported if main processes's stack memory is not provided in Start()
2267 STK_ASSERT(m_exit_trap[0].stack.SP != 0);
2268
2269 idle = nullptr;
2270 active = &m_exit_trap[0].stack;
2271
2272 m_task_now = nullptr;
2273
2274 m_platform.Stop();
2275 }
2276 else
2277 {
2280 }
2281
2282 return false;
2283 }

Referenced by stk::Kernel< stk::KERNEL_DYNAMIC|stk::KERNEL_SYNC|stk::KERNEL_TICKLESS, 16U, stk::SwitchStrategyFP32, stk::PlatformDefault >::UpdateFsmState().

Here is the caller graph for this function:

◆ StateSleep()

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
bool stk::Kernel< TMode, TSize, TStrategy, TPlatform >::StateSleep ( KernelTask * now,
KernelTask * next,
Stack *& idle,
Stack *& active )
inlineprotected

Enters into a sleeping mode.

Note
FSM state: stk::FSM_STATE_SLEEPING.
Parameters
[in]nowCurrently active kernel task.
[in]nextNext kernel task (ignored).
[out]idleStack of the task which must enter Idle state.
[out]activeStack of the task which must enter Active state (to which context will switch).

Definition at line 2224 of file stk.h.

2225 {
2227
2228 STK_ASSERT(now != nullptr);
2229 STK_ASSERT(m_sleep_trap[0].stack.SP != 0);
2230
2231 idle = now->GetUserStackPtr();
2232 active = &m_sleep_trap[0].stack;
2233
2234 m_task_now = static_cast<KernelTask *>(m_strategy.GetFirst());
2235
2236 #if STK_SEGGER_SYSVIEW
2237 SEGGER_SYSVIEW_OnTaskStopReady(now->GetUserStackPtr()->tid, TRACE_EVENT_SLEEP);
2238 #endif
2239
2241 {
2242 if (!now->IsPendingRemoval())
2243 {
2244 now->HrtOnSwitchedOut(&m_platform);
2245 }
2246 }
2247
2248 return true; // switch context
2249 }

Referenced by stk::Kernel< stk::KERNEL_DYNAMIC|stk::KERNEL_SYNC|stk::KERNEL_TICKLESS, 16U, stk::SwitchStrategyFP32, stk::PlatformDefault >::UpdateFsmState().

Here is the caller graph for this function:

◆ StateSwitch()

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
bool stk::Kernel< TMode, TSize, TStrategy, TPlatform >::StateSwitch ( KernelTask * now,
KernelTask * next,
Stack *& idle,
Stack *& active )
inlineprotected

Switches contexts.

Note
FSM state: stk::FSM_STATE_SWITCHING.
Parameters
[in]nowCurrently active kernel task.
[in]nextNext kernel task.
[out]idleStack of the task which must enter Idle state.
[out]activeStack of the task which must enter Active state (to which context will switch).

Definition at line 2140 of file stk.h.

2141 {
2142 STK_ASSERT(now != nullptr);
2143 STK_ASSERT(next != nullptr);
2144
2145 bool switch_context = false;
2146
2147 // if equal: do not switch context because task did not change
2148 if (next != now)
2149 {
2150 idle = now->GetUserStackPtr();
2151 active = next->GetUserStackPtr();
2152
2153 // if stack memory is exceeded these assertions will be hit
2154 if (now->IsBusy())
2155 {
2156 // current task could exit, thus we check it with IsBusy to avoid referencing nullptr returned by GetUserTask()
2157 STK_ASSERT(now->GetUserTask()->GetStack()[0] == STK_STACK_MEMORY_FILLER);
2158 }
2159 STK_ASSERT(next->GetUserTask()->GetStack()[0] == STK_STACK_MEMORY_FILLER);
2160
2161 m_task_now = next;
2162
2164 {
2165 if (now->m_hrt[0].done)
2166 {
2167 now->HrtOnSwitchedOut(&m_platform);
2168 next->HrtOnSwitchedIn();
2169 }
2170 }
2171
2172 #if STK_SEGGER_SYSVIEW
2173 SEGGER_SYSVIEW_OnTaskStopReady(now->GetUserStackPtr()->tid, TRACE_EVENT_SWITCH);
2174 SEGGER_SYSVIEW_OnTaskStartReady(next->GetUserStackPtr()->tid);
2175 #endif
2176
2177 switch_context = true;
2178 }
2179
2180 return switch_context;
2181 }

Referenced by stk::Kernel< stk::KERNEL_DYNAMIC|stk::KERNEL_SYNC|stk::KERNEL_TICKLESS, 16U, stk::SwitchStrategyFP32, stk::PlatformDefault >::UpdateFsmState().

Here is the caller graph for this function:

◆ StateWake()

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
bool stk::Kernel< TMode, TSize, TStrategy, TPlatform >::StateWake ( KernelTask * now,
KernelTask * next,
Stack *& idle,
Stack *& active )
inlineprotected

Wakes up after sleeping.

Note
FSM state: stk::FSM_STATE_WAKING.
Parameters
[in]nowCurrently active kernel task (ignored).
[in]nextNext kernel task.
[out]idleStack of the task which must enter Idle state.
[out]activeStack of the task which must enter Active state (to which context will switch).

Definition at line 2190 of file stk.h.

2191 {
2192 STK_UNUSED(now);
2193
2194 STK_ASSERT(next != nullptr);
2195
2196 idle = &m_sleep_trap[0].stack;
2197 active = next->GetUserStackPtr();
2198
2199 // if stack memory is exceeded these assertions will be hit
2201 STK_ASSERT(next->GetUserTask()->GetStack()[0] == STK_STACK_MEMORY_FILLER);
2202
2203 m_task_now = next;
2204
2205 #if STK_SEGGER_SYSVIEW
2206 SEGGER_SYSVIEW_OnTaskStartReady(next->GetUserStackPtr()->tid);
2207 #endif
2208
2210 {
2211 next->HrtOnSwitchedIn();
2212 }
2213
2214 return true; // switch context
2215 }

Referenced by stk::Kernel< stk::KERNEL_DYNAMIC|stk::KERNEL_SYNC|stk::KERNEL_TICKLESS, 16U, stk::SwitchStrategyFP32, stk::PlatformDefault >::UpdateFsmState().

Here is the caller graph for this function:

◆ SuspendTask()

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
void stk::Kernel< TMode, TSize, TStrategy, TPlatform >::SuspendTask ( ITask * user_task,
bool & suspended )
inlineoverridevirtual

Suspend task.

Parameters
[in]user_taskPointer to the user task to suspend.
[out]suspendedSet to true if task is suspended.
Note
hw::CriticalSection must not be active otherwise a deadlock will happen if task is suspending self.

Implements stk::IKernel.

Definition at line 1094 of file stk.h.

1095 {
1096 STK_ASSERT(user_task != nullptr);
1097
1098 bool self = false;
1099
1100 // avoid race with OnTick
1101 {
1103
1105 STK_ASSERT(task != nullptr);
1106
1107 // only suspend if the task is currently awake: if it is already sleeping
1108 // (e.g. blocked on a mutex or timed Sleep), do not overwrite m_time_sleep,
1109 // that would corrupt the original sleep state and, for sync-object waits,
1110 // would interfere with WaitObject::Tick()
1111 suspended = !task->IsSleeping();
1112 if (suspended == true)
1113 {
1114 task->ScheduleSleep(WAIT_INFINITE);
1115
1116 // check if suspending self
1117 self = (task == m_task_now);
1118 }
1119 }
1120
1121 // note: we do not spin long here, kernel will switch this task out from scheduling on the next tick
1122 if (self)
1123 {
1124 m_task_now->BusyWaitWhileSleeping();
1125 }
1126 }

◆ UpdateFsmState()

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
bool stk::Kernel< TMode, TSize, TStrategy, TPlatform >::UpdateFsmState ( Stack *& idle,
Stack *& active )
inlineprotected

Update FSM state.

Parameters
[out]idleStack of the task which must enter Idle state.
[out]activeStack of the task which must enter Active state (to which context will switch).
Returns
FSM state.

Definition at line 2097 of file stk.h.

2098 {
2099 KernelTask *const now = m_task_now, *next = nullptr;
2100 bool switch_context = false;
2101
2103
2104 switch (new_state)
2105 {
2109 break;
2110 case FSM_STATE_SLEEPING:
2113 break;
2114 case FSM_STATE_WAKING:
2117 break;
2118 case FSM_STATE_EXITING:
2121 break;
2122 case FSM_STATE_NONE:
2123 break; // valid intermittent non-persisting state: no-transition
2124 case FSM_STATE_MAX:
2125 default: // invalid state value
2127 break;
2128 }
2129
2130 return switch_context;
2131 }
EFsmState
Finite-state machine (FSM) state. Encodes what the kernel is currently doing between two consecutive ...
Definition stk.h:1255
bool StateWake(KernelTask *now, KernelTask *next, Stack *&idle, Stack *&active)
Wakes up after sleeping.
Definition stk.h:2190
bool StateExit(KernelTask *now, KernelTask *next, Stack *&idle, Stack *&active)
Exits from scheduling.
Definition stk.h:2259
bool StateSwitch(KernelTask *now, KernelTask *next, Stack *&idle, Stack *&active)
Switches contexts.
Definition stk.h:2140
bool StateSleep(KernelTask *now, KernelTask *next, Stack *&idle, Stack *&active)
Enters into a sleeping mode.
Definition stk.h:2224

Referenced by stk::Kernel< stk::KERNEL_DYNAMIC|stk::KERNEL_SYNC|stk::KERNEL_TICKLESS, 16U, stk::SwitchStrategyFP32, stk::PlatformDefault >::OnTick().

Here is the caller graph for this function:

◆ UpdateSyncObjects()

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
void stk::Kernel< TMode, TSize, TStrategy, TPlatform >::UpdateSyncObjects ( const Timeout elapsed_ticks)
inlineprotected

Update synchronization objects.

Definition at line 1994 of file stk.h.

1995 {
1997
1998 while (itr != nullptr)
1999 {
2000 ISyncObject::ListEntryType *const next = itr->GetNext();
2001
2002 if (!static_cast<ISyncObject *>(itr)->Tick(elapsed_ticks))
2003 {
2004 m_sync_list->Unlink(itr);
2005 }
2006
2007 itr = next;
2008 }
2009 }

Referenced by stk::Kernel< stk::KERNEL_DYNAMIC|stk::KERNEL_SYNC|stk::KERNEL_TICKLESS, 16U, stk::SwitchStrategyFP32, stk::PlatformDefault >::UpdateTasks().

Here is the caller graph for this function:

◆ UpdateTaskRequest()

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
void stk::Kernel< TMode, TSize, TStrategy, TPlatform >::UpdateTaskRequest ( )
inlineprotected

Update pending task requests.

Definition at line 2013 of file stk.h.

2014 {
2015 // process AddTask requests coming from tasks (KERNEL_DYNAMIC mode only, KERNEL_HRT is
2016 // excluded as we assume that HRT tasks must be known to the kernel before a Start())
2018 {
2019 // process serialized AddTask request made from another active task, requesting process
2020 // is currently waiting due to SwitchToNext()
2021 if ((m_request & REQ_ADD_TASK) != 0U)
2022 {
2024
2025 for (size_t i = 0U; i < TASKS_MAX; ++i)
2026 {
2027 KernelTask *const task = &m_task_storage[i];
2028
2029 if (task->m_srt[0].add_task_req != nullptr)
2030 {
2031 AllocateAndAddNewTask(task->m_srt[0].add_task_req->user_task);
2032
2033 task->m_srt[0].add_task_req = nullptr;
2035 }
2036 }
2037 }
2038 }
2039 }

Referenced by stk::Kernel< stk::KERNEL_DYNAMIC|stk::KERNEL_SYNC|stk::KERNEL_TICKLESS, 16U, stk::SwitchStrategyFP32, stk::PlatformDefault >::UpdateTasks().

Here is the caller graph for this function:

◆ UpdateTasks()

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
Timeout stk::Kernel< TMode, TSize, TStrategy, TPlatform >::UpdateTasks ( const Timeout elapsed_ticks)
inlineprotected

Update tasks (sleep, requests).

Definition at line 1874 of file stk.h.

1875 {
1876 // sync objects are updated before UpdateTaskRequest which may add a new object (newly added object must become 1 tick older)
1878 {
1880 }
1881
1882 if (m_request != REQ_NONE)
1883 {
1885 }
1886
1888 }
void UpdateTaskRequest()
Update pending task requests.
Definition stk.h:2013
Timeout UpdateTaskState(const Timeout elapsed_ticks)
Update task state: process removals, advance sleep timers, and track HRT durations.
Definition stk.h:1899
void UpdateSyncObjects(const Timeout elapsed_ticks)
Update synchronization objects.
Definition stk.h:1994

Referenced by stk::Kernel< stk::KERNEL_DYNAMIC|stk::KERNEL_SYNC|stk::KERNEL_TICKLESS, 16U, stk::SwitchStrategyFP32, stk::PlatformDefault >::OnTick().

Here is the caller graph for this function:

◆ UpdateTaskState()

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
Timeout stk::Kernel< TMode, TSize, TStrategy, TPlatform >::UpdateTaskState ( const Timeout elapsed_ticks)
inlineprotected

Update task state: process removals, advance sleep timers, and track HRT durations.

Parameters
[in]elapsed_ticksNumber of ticks elapsed since the previous call. Always 1 in non-tickless mode, may be >1 in tickless mode.
Returns
In non-tickless mode: always 1. In tickless mode (KERNEL_TICKLESS): the minimum remaining sleep ticks across all active tasks, clamped to [1, STK_TICKLESS_TICKS_MAX]. The platform driver uses this value to program the next timer wakeup interval, suppressing timer/tick ISR for that many ticks when the system would otherwise be idle.

Definition at line 1899 of file stk.h.

1900 {
1902
1903 for (size_t i = 0U; i < TASKS_MAX; ++i)
1904 {
1905 KernelTask *const task = &m_task_storage[i];
1906
1907 if (task->IsSleeping())
1908 {
1910 {
1911 // task is pending removal, wait until it is switched out
1912 if (task->IsPendingRemoval())
1913 {
1914 if ((task != m_task_now) ||
1915 ((m_strategy.GetSize() == 1U) && (m_fsm_state == FSM_STATE_SLEEPING)))
1916 {
1918 continue;
1919 }
1920 }
1921 }
1922
1923 // deliver sleep event to strategy
1924 // note: only currently scheduled task can be pending to sleep
1926 {
1927 if ((task->m_state & KernelTask::STATE_SLEEP_PENDING) != 0U)
1928 {
1930
1931 // notify strategy that task is sleeping
1932 m_strategy.OnTaskSleep(task);
1933 }
1934 }
1935
1936 // advance sleep time by a tick
1937 task->m_time_sleep += elapsed_ticks;
1938
1939 // deliver sleep event to strategy
1941 {
1942 // notify strategy that task woke up
1943 if (!task->IsSleeping())
1944 {
1945 m_strategy.OnTaskWake(task);
1946 }
1947 }
1948 }
1949 else
1950 {
1952 {
1953 // in HRT mode we trace how long task spent in active state (doing some work)
1954 if (task->IsBusy())
1955 {
1956 task->m_hrt[0].duration += elapsed_ticks;
1957
1958 // check if deadline is missed (HRT failure)
1959 if (task->HrtIsDeadlineMissed(task->m_hrt[0].duration))
1960 {
1961 // report deadline overrun to a strategy which supports overrun recovery
1963 {
1964 if (!m_strategy.OnTaskDeadlineMissed(task))
1965 {
1966 // report failure if it could not be recovered by the scheduling strategy
1967 task->HrtHardFailDeadline(&m_platform);
1968 }
1969 }
1970 else
1971 {
1972 task->HrtHardFailDeadline(&m_platform);
1973 }
1974 }
1975 }
1976 }
1977 }
1978
1979 // get the number ticks the driver has to keep CPU in Idle
1981 {
1982 if ((sleep_ticks > 1) && task->IsBusy())
1983 {
1984 sleep_ticks = task->GetSleepTicks(sleep_ticks);
1985 }
1986 }
1987 }
1988
1989 return sleep_ticks;
1990 }
Timeout GetInitialSleepTicks()
static constexpr bool IsTicklessMode()
Definition stk.h:2326

Referenced by stk::Kernel< stk::KERNEL_DYNAMIC|stk::KERNEL_SYNC|stk::KERNEL_TICKLESS, 16U, stk::SwitchStrategyFP32, stk::PlatformDefault >::UpdateTasks().

Here is the caller graph for this function:

Member Data Documentation

◆ m_exit_trap

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
ExitTrapStack stk::Kernel< TMode, TSize, TStrategy, TPlatform >::m_exit_trap[STK_ALLOCATE_COUNT< TMode, KERNEL_DYNAMIC, 1U, 0U >::Value]
protected

Exit trap: zero-size in KERNEL_STATIC mode; one entry in KERNEL_DYNAMIC mode.

Definition at line 2400 of file stk.h.

◆ m_fsm

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
const EFsmState stk::Kernel< TMode, TSize, TStrategy, TPlatform >::m_fsm[FSM_STATE_MAX][FSM_EVENT_MAX]
protected
Initial value:

Compile-time FSM transition table. Indexed as m_fsm[current_state][event] -> next_state. FSM_STATE_NONE as a next-state means "no transition": the FSM stays in the current state. Updated by UpdateFsmState() each tick via GetNewFsmState() -> FetchNextEvent().

Definition at line 2406 of file stk.h.

2406 {
2407 // FSM_EVENT_SWITCH FSM_EVENT_SLEEP FSM_EVENT_WAKE FSM_EVENT_EXIT
2412 };

◆ m_fsm_state

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
EFsmState stk::Kernel< TMode, TSize, TStrategy, TPlatform >::m_fsm_state
protected

Current FSM state. Drives context-switch decision on every tick.

Definition at line 2401 of file stk.h.

◆ m_kstate

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
volatile EKernelState stk::Kernel< TMode, TSize, TStrategy, TPlatform >::m_kstate
protected

Current kernel state.

Definition at line 2403 of file stk.h.

◆ m_platform

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
TPlatform stk::Kernel< TMode, TSize, TStrategy, TPlatform >::m_platform
protected

Platform driver (SysTick, PendSV, context switch implementation).

Definition at line 2395 of file stk.h.

◆ m_request

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
volatile uint8_t stk::Kernel< TMode, TSize, TStrategy, TPlatform >::m_request
protected

Bitmask of pending ERequest flags from running tasks. Written by tasks, read/cleared by UpdateTaskRequest() in tick context.

Definition at line 2402 of file stk.h.

◆ m_service

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
KernelService stk::Kernel< TMode, TSize, TStrategy, TPlatform >::m_service
protected

Kernel service singleton exposed to running tasks via IKernelService::GetInstance().

Definition at line 2394 of file stk.h.

◆ m_sleep_trap

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
SleepTrapStack stk::Kernel< TMode, TSize, TStrategy, TPlatform >::m_sleep_trap[1]
protected

Sleep trap (always present): executed when all tasks are sleeping.

Definition at line 2399 of file stk.h.

◆ m_strategy

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
TStrategy stk::Kernel< TMode, TSize, TStrategy, TPlatform >::m_strategy
protected

Task-switching strategy (determines which task runs next).

Definition at line 2396 of file stk.h.

◆ m_sync_list

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
SyncObjectList stk::Kernel< TMode, TSize, TStrategy, TPlatform >::m_sync_list[STK_ALLOCATE_COUNT< TMode, KERNEL_SYNC, 1U, 0U >::Value]
protected

List of active sync objects. Zero-size (no memory) if KERNEL_SYNC is not set.

Definition at line 2404 of file stk.h.

◆ m_task_now

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
KernelTask* stk::Kernel< TMode, TSize, TStrategy, TPlatform >::m_task_now
protected

Currently executing task, or nullptr before Start() or after all tasks exit.

Definition at line 2397 of file stk.h.

◆ m_task_storage

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
TaskStorageType stk::Kernel< TMode, TSize, TStrategy, TPlatform >::m_task_storage
protected

Static pool of TSize KernelTask slots (free slots have m_user == nullptr).

Definition at line 2398 of file stk.h.

◆ TASKS_MAX

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
size_t stk::Kernel< TMode, TSize, TStrategy, TPlatform >::TASKS_MAX = TSize
staticconstexpr

Maximum number of concurrently registered tasks. Fixed at compile time. Exceeding this limit in AddTask() triggers a compile-time assert (TASKS_MAX > 0) and a runtime STK_ASSERT.

Definition at line 898 of file stk.h.

◆ YIELD_TICKS

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
Timeout stk::Kernel< TMode, TSize, TStrategy, TPlatform >::YIELD_TICKS = 2
staticconstexprprotected

Ticks to yield.

Yield with 2 ticks: 1 will be incremented on the next OnTick call by UpdateTasks and remaining 1 will cause a context switch by UpdateFsmState when strategy detects it as a sleeping test.

Definition at line 1283 of file stk.h.


The documentation for this class was generated from the following file: