SuperTinyKernel™ RTOS 1.08.x
Lightweight, high-performance, deterministic, bare-metal C++ RTOS for resource-constrained embedded systems. MIT Open Source License.
Loading...
Searching...
No Matches
stk::Kernel< TMode, TSize, TStrategy, TPlatform >::KernelTask Class Referencefinal

Internal per-slot kernel descriptor that wraps a user ITask instance. More...

#include <stk.h>

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

Classes

class  AddTaskRequest
 Payload for an in-flight AddTask() request issued by a running task. More...
class  SrtInfo
 Per-task soft real-time (SRT) metadata. More...
class  HrtInfo
 Per-task Hard Real-Time (HRT) scheduling metadata. More...
class  WaitObject
 Concrete implementation of IWaitObject, embedded in each KernelTask slot. More...

Public Types

typedef DLHeadType ListHeadType
 List head type for IKernelTask elements.
typedef DLEntryType ListEntryType
 List entry type of IKernelTask elements.
enum  
 A tag for type-safe casts done by CastListEntryToParent. More...
typedef DListEntry< IKernelTask, TClosedLoop > DLEntryType
 Convenience alias for this entry type. Used to avoid repeating the full template spelling.
typedef DListHead< IKernelTask, TClosedLoop > DLHeadType
 Convenience alias for the corresponding list head type.

Public Member Functions

 KernelTask ()
 Construct a free (unbound) task slot. All fields set to zero/null.
ITaskGetUserTask () override
 Get bound user task.
Stack GetUserStack () const override
 Get stack descriptor for this task slot.
bool IsBusy () const
 Check whether this slot is bound to a user task.
bool IsSleeping () const override
 Check whether this task is currently sleeping (waiting for a tick or a wake event).
bool IsSleepInfinite () const
 Check whether this task is currently sleeping infinitely.
TId GetTid () const
 Get task identifier.
void Wake () override
 Wake this task on the next scheduling tick.
void SetCurrentWeight (Weight weight) override
 Update the run-time scheduling weight (weighted strategies only).
Weight GetWeight () const override
 Get static scheduling weight from the user task.
Weight GetCurrentWeight () const override
 Get current (run-time) scheduling weight.
Timeout GetHrtPeriodicity () const override
 Get HRT scheduling periodicity.
Timeout GetHrtDeadline () const override
 Get absolute HRT deadline (ticks elapsed since task was activated).
Timeout GetHrtRelativeDeadline () const override
 Get remaining HRT deadline (ticks left before the deadline expires).
Timeout GetSleepTicks (Timeout sleep_ticks)
DLHeadTypeGetHead ()
 Get the list head this entry currently belongs to.
DLEntryTypeGetNext ()
 Get the next entry in the list.
DLEntryTypeGetPrev ()
 Get the previous entry in the list.
bool IsLinked () const
 Check whether this entry is currently a member of any list.
 operator IKernelTask * ()
 Implicit conversion to a mutable pointer to the host object (T).
 operator const IKernelTask * () const
 Implicit conversion to a const pointer to the host object (T).

Protected Member Functions

 ~KernelTask ()=default
 Destructor.
void Bind (TPlatform *platform, ITask *user_task)
 Bind this slot to a user task: set access mode, task ID, and initialize the stack.
void Unbind ()
 Reset this slot to the free (unbound) state, clearing all scheduling metadata.
void ScheduleRemoval ()
 Schedule the removal of the task from the kernel on next tick.
bool IsPendingRemoval () const
 Check if task is pending removal.
bool IsMemoryOfSP (Word SP) const
 Check if Stack Pointer (SP) belongs to this task.
void HrtInit (Timeout periodicity_tc, Timeout deadline_tc, Timeout start_delay_tc)
 Initialize task with HRT info.
void HrtOnSwitchedIn ()
 Called when task is switched into the scheduling process.
void HrtOnSwitchedOut ()
 Called when task is switched out from the scheduling process.
void HrtHardFailDeadline (IPlatform *platform)
 Hard-fail HRT task when it missed its deadline.
void HrtOnWorkCompleted ()
 Called when task process called IKernelService::SwitchToNext to inform Kernel that work is completed.
bool HrtIsDeadlineMissed (Timeout duration) const
 Check if deadline missed.
void ScheduleSleep (Timeout ticks)
 Put the task into a sleeping state for the specified number of ticks.
void BusyWaitWhileSleeping (Kernel *kernel) const
 Block further execution of the task's context while in sleeping state.
StackGetUserStackPtr ()
 Get pointer to user Stack.

Protected Attributes

ITaskm_user
 Bound user task, or NULL when slot is free.
Stack m_stack
 Stack descriptor (SP register value + access mode + optional tid).
volatile uint32_t m_state
 Bitmask of EStateFlags. Written by task thread, read/cleared by kernel tick.
volatile Timeout m_time_sleep
 Sleep countdown: negative while sleeping (absolute value = ticks remaining), zero when awake.
SrtInfo m_srt [STK_ALLOCATE_COUNT< TMode, KERNEL_HRT, 0U, 1U >::Value]
 SRT metadata. Zero-size (no memory) in KERNEL_HRT mode.
HrtInfo m_hrt [STK_ALLOCATE_COUNT< TMode, KERNEL_HRT, 1U, 0U >::Value]
 HRT metadata. Zero-size (no memory) in non-HRT mode.
Weight m_rt_weight [STK_ALLOCATE_COUNT< TStrategy::WEIGHT_API, 1U, 1U, 0U >::Value]
 Run-time weight for weighted-round-robin scheduling. Zero-size for unweighted strategies.
WaitObject m_wait_obj [STK_ALLOCATE_COUNT< TMode, KERNEL_SYNC, 1U, 0U >::Value]
 Embedded wait object for synchronization. Zero-size (no memory) if KERNEL_SYNC is not set.

Private Types

enum  EStateFlags : uint32_t {
  STATE_NONE = 0 ,
  STATE_REMOVE_PENDING = (1 << 0) ,
  STATE_SLEEP_PENDING = (1 << 1) ,
  STATE_WAKE_PENDING = (1 << 2)
}
 Bitmask of transient state flags. Set by the task or the kernel and consumed (cleared) during UpdateTaskState() on the next tick. More...

Private Member Functions

void Link (DLHeadType *head, DLEntryType *next, DLEntryType *prev)
 Wire this entry into a list between prev and next.
void Unlink ()
 Remove this entry from its current list.

Private Attributes

DLHeadTypem_head
 Owning list head, or NULL when the entry is not linked.
DLEntryTypem_next
 Next entry in the list, or NULL (open list boundary) / first entry (closed loop).
DLEntryTypem_prev
 Previous entry in the list, or NULL (open list boundary) / last entry (closed loop).

Friends

class Kernel

Detailed Description

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

Internal per-slot kernel descriptor that wraps a user ITask instance.

Holds the kernel-side state for one task slot: the Stack descriptor, sleep timer, HRT or SRT scheduling metadata, optional wait object (KERNEL_SYNC), and optional weight (weighted strategies). The task-switching strategy operates on KernelTask pointers rather than ITask pointers directly.

A slot is "free" when m_user == NULL (IsBusy() == false). The Kernel pre-allocates TSize slots in m_task_storage; AddTask() finds a free slot and calls Bind().

Definition at line 125 of file stk.h.

Member Typedef Documentation

◆ DLEntryType

typedef DListEntry<IKernelTask, TClosedLoop> stk::util::DListEntry< IKernelTask, TClosedLoop >::DLEntryType
inherited

Convenience alias for this entry type. Used to avoid repeating the full template spelling.

Definition at line 75 of file stk_linked_list.h.

◆ DLHeadType

typedef DListHead<IKernelTask, TClosedLoop> stk::util::DListEntry< IKernelTask, TClosedLoop >::DLHeadType
inherited

Convenience alias for the corresponding list head type.

Definition at line 80 of file stk_linked_list.h.

◆ ListEntryType

List entry type of IKernelTask elements.

Definition at line 895 of file stk_common.h.

◆ ListHeadType

List head type for IKernelTask elements.

Definition at line 890 of file stk_common.h.

Member Enumeration Documentation

◆ anonymous enum

anonymous enum
inherited

A tag for type-safe casts done by CastListEntryToParent.

See also
CastListEntryToParent.

Definition at line 70 of file stk_linked_list.h.

70{ DLEntryTag = 1 };
Intrusive doubly-linked list node. Embed this as a base class in any object (T) that needs to partici...

◆ EStateFlags

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
enum stk::Kernel::KernelTask::EStateFlags : uint32_t
private

Bitmask of transient state flags. Set by the task or the kernel and consumed (cleared) during UpdateTaskState() on the next tick.

Enumerator
STATE_NONE 

No pending state flags.

STATE_REMOVE_PENDING 

Task returned from its Run function; slot will be freed on the next tick (KERNEL_DYNAMIC only).

STATE_SLEEP_PENDING 

Task called Sleep/SleepUntil/Yield; strategy's OnTaskSleep() will be invoked on the next tick (sleep-aware strategies only).

STATE_WAKE_PENDING 

Task wake is pending (see Wake).

Definition at line 133 of file stk.h.

134 {
135 STATE_NONE = 0,
136 STATE_REMOVE_PENDING = (1 << 0),
137 STATE_SLEEP_PENDING = (1 << 1),
138 STATE_WAKE_PENDING = (1 << 2),
139 };
@ STATE_REMOVE_PENDING
Task returned from its Run function; slot will be freed on the next tick (KERNEL_DYNAMIC only).
Definition stk.h:136
@ STATE_WAKE_PENDING
Task wake is pending (see Wake).
Definition stk.h:138
@ STATE_SLEEP_PENDING
Task called Sleep/SleepUntil/Yield; strategy's OnTaskSleep() will be invoked on the next tick (sleep-...
Definition stk.h:137
@ STATE_NONE
No pending state flags.
Definition stk.h:135

Constructor & Destructor Documentation

◆ KernelTask()

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

Construct a free (unbound) task slot. All fields set to zero/null.

Note
In KERNEL_SYNC mode the embedded WaitObject back-pointer is wired to this KernelTask at construction so the wait object can wake its owning task.

Definition at line 158 of file stk.h.

159 m_srt(), m_hrt(), m_rt_weight()
160 {
161 // bind to wait object
163 {
164 m_wait_obj->m_task = this;
165 }
166 }
#define __stk_constexpr_cpp17
constexpr definition for C++17 and above.
Definition stk_defs.h:489
Concrete implementation of IKernel.
Definition stk.h:90
static constexpr bool IsSyncMode()
Definition stk.h:2503
SrtInfo m_srt[STK_ALLOCATE_COUNT< TMode, KERNEL_HRT, 0U, 1U >::Value]
SRT metadata. Zero-size (no memory) in KERNEL_HRT mode.
Definition stk.h:786
Stack m_stack
Stack descriptor (SP register value + access mode + optional tid).
Definition stk.h:783
Weight m_rt_weight[STK_ALLOCATE_COUNT< TStrategy::WEIGHT_API, 1U, 1U, 0U >::Value]
Run-time weight for weighted-round-robin scheduling. Zero-size for unweighted strategies.
Definition stk.h:788
volatile uint32_t m_state
Bitmask of EStateFlags. Written by task thread, read/cleared by kernel tick.
Definition stk.h:784
ITask * m_user
Bound user task, or NULL when slot is free.
Definition stk.h:782
HrtInfo m_hrt[STK_ALLOCATE_COUNT< TMode, KERNEL_HRT, 1U, 0U >::Value]
HRT metadata. Zero-size (no memory) in non-HRT mode.
Definition stk.h:787
volatile Timeout m_time_sleep
Sleep countdown: negative while sleeping (absolute value = ticks remaining), zero when awake.
Definition stk.h:785
WaitObject m_wait_obj[STK_ALLOCATE_COUNT< TMode, KERNEL_SYNC, 1U, 0U >::Value]
Embedded wait object for synchronization. Zero-size (no memory) if KERNEL_SYNC is not set.
Definition stk.h:789

References __stk_constexpr_cpp17, stk::Kernel< TMode, TSize, TStrategy, TPlatform >::IsSyncMode(), m_hrt, m_rt_weight, m_srt, m_stack, m_state, m_time_sleep, m_user, m_wait_obj, stk::NO_WAIT, and STATE_NONE.

Here is the call graph for this function:

◆ ~KernelTask()

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

Destructor.

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

References STK_VIRT_DTOR.

Member Function Documentation

◆ Bind()

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

Bind this slot to a user task: set access mode, task ID, and initialize the stack.

Parameters
[in]platformPlatform driver used to initialize the stack frame.
[in]user_taskUser task to bind. Asserts that the stack is successfully initialized.

Definition at line 556 of file stk.h.

557 {
558 // bind user task (GetTid depends on m_user)
560
561 // set access mode for this stack
562 m_stack.access_mode = user_task->GetAccessMode();
563
564 // set task id for tracking purpose
565 #if STK_STACK_NEEDS_TASK_ID
566 m_stack.tid = GetTid();
568 #endif
569
570 // init stack of the user task
572
573 // initialize current weight to NO_WEIGHT for priority inheritance mechanism
575 {
577 }
578 }
#define STK_ASSERT(e)
Runtime assertion. Halts execution if the expression e evaluates to false.
Definition stk_defs.h:516
TId GetTid() const
Get task identifier.
Definition stk.h:196
void SetCurrentWeight(Weight weight) override
Update the run-time scheduling weight (weighted strategies only).
Definition stk.h:219

References __stk_constexpr_cpp17, stk::ITask::GetAccessMode(), GetTid(), m_stack, m_user, stk::NO_WEIGHT, SetCurrentWeight(), stk::STACK_USER_TASK, STK_ASSERT, and stk::TID_NONE.

Here is the call graph for this function:

◆ BusyWaitWhileSleeping()

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

Block further execution of the task's context while in sleeping state.

Definition at line 767 of file stk.h.

768 {
769 kernel->m_platform.ForceContextSwitch(GetTid());
770
771 while (IsSleeping())
772 {
774 }
775 }
TPlatform m_platform
Platform driver (SysTick, PendSV, context switch implementation).
Definition stk.h:2573
bool IsSleeping() const override
Check whether this task is currently sleeping (waiting for a tick or a wake event).
Definition stk.h:186

References GetTid(), IsSleeping(), Kernel, and stk::Kernel< TMode, TSize, TStrategy, TPlatform >::m_platform.

Here is the call graph for this function:

◆ GetCurrentWeight()

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
Weight stk::Kernel< TMode, TSize, TStrategy, TPlatform >::KernelTask::GetCurrentWeight ( ) const
inlineoverridevirtual

Get current (run-time) scheduling weight.

Returns
m_rt_weight[0] if WEIGHT_API is true; 1 otherwise.
Note
The run-time weight is decremented each tick by the weighted strategy and reset to GetWeight() when exhausted.

Implements stk::IKernelTask.

Definition at line 269 of file stk.h.

270 {
272
274 {
276 }
277 else
278 {
280 }
281
282 return cur_weight;
283 }

References __stk_constexpr_cpp17, stk::DEFAULT_WEIGHT, and m_rt_weight.

◆ GetHead()

DLHeadType * stk::util::DListEntry< IKernelTask, TClosedLoop >::GetHead ( )
inlineinherited

Get the list head this entry currently belongs to.

Returns
Pointer to the owning DListHead, or NULL if the entry is not linked.

Definition at line 85 of file stk_linked_list.h.

85{ return m_head; }

◆ GetHrtDeadline()

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
Timeout stk::Kernel< TMode, TSize, TStrategy, TPlatform >::KernelTask::GetHrtDeadline ( ) const
inlineoverridevirtual

Get absolute HRT deadline (ticks elapsed since task was activated).

Returns
Deadline in ticks. The task must complete its work within this many ticks of being switched in, or OnDeadlineMissed() is invoked.
Note
KERNEL_HRT mode only. Asserts if called outside HRT mode.

Implements stk::IKernelTask.

Definition at line 312 of file stk.h.

313 {
315
317
319 {
320 deadline = m_hrt[0].deadline;
321 }
322 else
323 {
324 deadline = 0;
325 }
326
327 return deadline;
328 }
static constexpr bool IsHrtMode()
Definition stk.h:2502

References __stk_constexpr_cpp17, stk::Kernel< TMode, TSize, TStrategy, TPlatform >::IsHrtMode(), m_hrt, and STK_ASSERT.

Here is the call graph for this function:

◆ GetHrtPeriodicity()

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
Timeout stk::Kernel< TMode, TSize, TStrategy, TPlatform >::KernelTask::GetHrtPeriodicity ( ) const
inlineoverridevirtual

Get HRT scheduling periodicity.

Returns
Period in ticks between successive activations of this task.
Note
KERNEL_HRT mode only. Asserts if called outside HRT mode.

Implements stk::IKernelTask.

Definition at line 289 of file stk.h.

290 {
292
293 Timeout to;
294
296 {
297 to = m_hrt[0].periodicity;
298 }
299 else
300 {
301 to = 0;
302 }
303
304 return to;
305 }

References __stk_constexpr_cpp17, stk::Kernel< TMode, TSize, TStrategy, TPlatform >::IsHrtMode(), m_hrt, and STK_ASSERT.

Here is the call graph for this function:

◆ GetHrtRelativeDeadline()

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
Timeout stk::Kernel< TMode, TSize, TStrategy, TPlatform >::KernelTask::GetHrtRelativeDeadline ( ) const
inlineoverridevirtual

Get remaining HRT deadline (ticks left before the deadline expires).

Returns
deadline - duration: ticks remaining before the task must call Yield(). Zero means this is the last tick still within the deadline; a negative value means the deadline has already been missed (see HrtIsDeadlineMissed(), which uses a strict duration > deadline comparison).
Note
KERNEL_HRT mode only. Asserts if called outside HRT mode or while sleeping.

Implements stk::IKernelTask.

Definition at line 337 of file stk.h.

338 {
341
343
345 {
346 relative_deadline = (m_hrt[0].deadline - m_hrt[0].duration);
347 }
348 else
349 {
351 }
352
353 return relative_deadline;
354 }

References __stk_constexpr_cpp17, stk::Kernel< TMode, TSize, TStrategy, TPlatform >::IsHrtMode(), IsSleeping(), m_hrt, and STK_ASSERT.

Here is the call graph for this function:

◆ GetNext()

DLEntryType * stk::util::DListEntry< IKernelTask, TClosedLoop >::GetNext ( )
inlineinherited

Get the next entry in the list.

Returns
Pointer to the next DListEntry, or NULL if this is the last entry (open list) or the first entry (closed loop, where next wraps to first).
Note
In a closed loop (TClosedLoop == true) this pointer is never NULL when the entry is linked.

Definition at line 98 of file stk_linked_list.h.

98{ return m_next; }

◆ GetPrev()

DLEntryType * stk::util::DListEntry< IKernelTask, TClosedLoop >::GetPrev ( )
inlineinherited

Get the previous entry in the list.

Returns
Pointer to the previous DListEntry, or NULL if this is the first entry (open list) or the last entry (closed loop, where prev wraps to last).
Note
In a closed loop (TClosedLoop == true) this pointer is never NULL when the entry is linked.

Definition at line 114 of file stk_linked_list.h.

114{ return m_prev; }

◆ GetSleepTicks()

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
Timeout stk::Kernel< TMode, TSize, TStrategy, TPlatform >::KernelTask::GetSleepTicks ( Timeout sleep_ticks)
inline

Definition at line 356 of file stk.h.

357 {
358 // note: task sleep time is negative
360
362 {
363 // likely task is sleeping during sync operation (see Wait)
364 if (m_wait_obj->IsWaiting())
365 {
366 // note: sync wait time is positive
367 task_sleep = m_wait_obj->m_time_wait;
368
369 // we shall account for only valid time (when task is waiting during sync operation)
370 if (task_sleep > NO_WAIT)
371 {
373 }
374 }
375 else
376 {
378 }
379 }
380 else
381 {
383 }
384
385 // clamp to [1, STK_TICKLESS_TICKS_MAX] range
386 return Max<Timeout>(1, sleep_ticks);
387 }
static constexpr T Max(T a, T b) noexcept
Compile-time maximum of two values.
Definition stk_defs.h:759
static constexpr T Min(T a, T b) noexcept
Compile-time minimum of two values.
Definition stk_defs.h:753

References __stk_constexpr_cpp17, stk::Kernel< TMode, TSize, TStrategy, TPlatform >::IsSyncMode(), m_time_sleep, m_wait_obj, stk::Max(), stk::Min(), and stk::NO_WAIT.

Here is the call graph for this function:

◆ GetTid()

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
TId stk::Kernel< TMode, TSize, TStrategy, TPlatform >::KernelTask::GetTid ( ) const
inline

Get task identifier.

Returns
TId derived from the bound ITask pointer address (unique per task instance).

Definition at line 196 of file stk.h.

196{ return GetTidFromUserTask(m_user); }
static constexpr TId GetTidFromUserTask(const ITask *task) noexcept
Get task identifier from ITask instance.
Definition stk_arch.h:716

References stk::GetTidFromUserTask(), and m_user.

Referenced by Bind(), and BusyWaitWhileSleeping().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetUserStack()

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
Stack stk::Kernel< TMode, TSize, TStrategy, TPlatform >::KernelTask::GetUserStack ( ) const
inlineoverridevirtual

Get stack descriptor for this task slot.

Returns
Stack info (SP register value and access mode flags).

Implements stk::IKernelTask.

Definition at line 176 of file stk.h.

176{ return m_stack;}

References m_stack.

◆ GetUserStackPtr()

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

Get pointer to user Stack.

Returns
Pointer to the Stack (SP register value and access mode flags).

Definition at line 780 of file stk.h.

780{ return &m_stack; }

References m_stack.

◆ GetUserTask()

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
ITask * stk::Kernel< TMode, TSize, TStrategy, TPlatform >::KernelTask::GetUserTask ( )
inlineoverridevirtual

Get bound user task.

Returns
Pointer to the ITask, or NULL if the slot is free (IsBusy() == false).

Implements stk::IKernelTask.

Definition at line 171 of file stk.h.

171{ return m_user; }

References m_user.

◆ GetWeight()

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
Weight stk::Kernel< TMode, TSize, TStrategy, TPlatform >::KernelTask::GetWeight ( ) const
inlineoverridevirtual

Get static scheduling weight from the user task.

Returns
ITask::GetWeight() if WEIGHT_API is true; 1 otherwise.

Implements stk::IKernelTask.

Definition at line 230 of file stk.h.

231 {
233
235 {
236 if (m_rt_weight[0] != NO_WEIGHT)
237 {
239 }
240 else
241 {
243 {
244 static_weight = m_user->GetWeight();
245 }
246 else
247 {
249 }
250 }
251 }
253 {
254 static_weight = m_user->GetWeight();
255 }
256 else
257 {
259 }
260
261 return static_weight;
262 }

References __stk_constexpr_cpp17, stk::DEFAULT_WEIGHT, m_rt_weight, m_user, and stk::NO_WEIGHT.

◆ HrtHardFailDeadline()

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

Hard-fail HRT task when it missed its deadline.

Note
Related to stk::KERNEL_HRT mode only.
Parameters
[in]platformPlatform driver instance.

Definition at line 713 of file stk.h.

714 {
715 const Timeout duration = m_hrt[0].duration;
716
717 STK_ASSERT(duration >= 0);
719
720 m_user->OnDeadlineMissed(duration);
721 platform->ProcessHardFault();
722 }
bool HrtIsDeadlineMissed(Timeout duration) const
Check if deadline missed.
Definition stk.h:736

References HrtIsDeadlineMissed(), m_hrt, m_user, stk::IPlatform::ProcessHardFault(), and STK_ASSERT.

Here is the call graph for this function:

◆ HrtInit()

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
void stk::Kernel< TMode, TSize, TStrategy, TPlatform >::KernelTask::HrtInit ( Timeout periodicity_tc,
Timeout deadline_tc,
Timeout start_delay_tc )
inlineprotected

Initialize task with HRT info.

Note
Related to stk::KERNEL_HRT mode only.
Parameters
[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 668 of file stk.h.

669 {
675
676 m_hrt[0].periodicity = periodicity_tc;
677 m_hrt[0].deadline = deadline_tc;
678
679 if (start_delay_tc > 0)
680 {
682 }
683 }
void ScheduleSleep(Timeout ticks)
Put the task into a sleeping state for the specified number of ticks.
Definition stk.h:750

References m_hrt, ScheduleSleep(), and STK_ASSERT.

Here is the call graph for this function:

◆ HrtIsDeadlineMissed()

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
bool stk::Kernel< TMode, TSize, TStrategy, TPlatform >::KernelTask::HrtIsDeadlineMissed ( Timeout duration) const
inlineprotected

Check if deadline missed.

Note
Related to stk::KERNEL_HRT mode only.

Definition at line 736 of file stk.h.

737 {
738 return (duration > m_hrt[0].deadline);
739 }

References m_hrt.

Referenced by HrtHardFailDeadline().

Here is the caller graph for this function:

◆ HrtOnSwitchedIn()

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

Called when task is switched into the scheduling process.

Note
Related to stk::KERNEL_HRT mode only.

Definition at line 688 of file stk.h.

688{}

◆ HrtOnSwitchedOut()

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

Called when task is switched out from the scheduling process.

Note
Related to stk::KERNEL_HRT mode only.

Definition at line 693 of file stk.h.

694 {
695 const Timeout duration = m_hrt[0].duration;
696
697 STK_ASSERT(duration >= 0);
698
699 const Timeout sleep = m_hrt[0].periodicity - duration;
700 if (sleep > 0)
701 {
703 }
704
705 m_hrt[0].duration = 0;
706 m_hrt[0].done = false;
707 }

References m_hrt, ScheduleSleep(), and STK_ASSERT.

Here is the call graph for this function:

◆ HrtOnWorkCompleted()

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

Called when task process called IKernelService::SwitchToNext to inform Kernel that work is completed.

Note
Related to stk::KERNEL_HRT mode only.

Definition at line 727 of file stk.h.

728 {
729 m_hrt[0].done = true;
731 }

References m_hrt.

Referenced by ScheduleRemoval().

Here is the caller graph for this function:

◆ IsBusy()

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
bool stk::Kernel< TMode, TSize, TStrategy, TPlatform >::KernelTask::IsBusy ( ) const
inline

Check whether this slot is bound to a user task.

Returns
true if a user task is assigned (m_user != NULL); false if the slot is free.

Definition at line 181 of file stk.h.

181{ return (m_user != nullptr); }

References m_user.

◆ IsLinked()

bool stk::util::DListEntry< IKernelTask, TClosedLoop >::IsLinked ( ) const
inlineinherited

Check whether this entry is currently a member of any list.

Returns
true if linked (m_head != NULL); false otherwise.

Definition at line 127 of file stk_linked_list.h.

127{ return (GetHead() != nullptr); }

◆ IsMemoryOfSP()

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

Check if Stack Pointer (SP) belongs to this task.

Parameters
[in]SPStack Pointer.

Definition at line 630 of file stk.h.

631 {
632 bool is_match = false;
633
634 const Word start = hw::PtrToWord(m_user->GetStack());
635 const Word end = start + (m_user->GetStackSize() * sizeof(Word));
636
637 if ((SP >= start) && (SP <= end))
638 {
639 is_match = true;
640 }
641 #if STK_TZ_SECURE // lookup Secure memory region too when on a Secure side
642 else
643 {
644 IStackMemory *const secure_mem = m_user->GetSecureStackMemory();
645
646 if (secure_mem != nullptr)
647 {
648 const Word s_start = hw::PtrToWord(secure_mem->GetStack());
649 const Word s_end = s_start + (secure_mem->GetStackSize() * sizeof(Word));
650
651 if ((SP >= s_start) && (SP <= s_end))
652 {
653 is_match = true;
654 }
655 }
656 }
657 #endif
658
659 return is_match;
660 }
static constexpr Word PtrToWord(T *const ptr) noexcept
Cast a pointer to a CPU register-width integer.
Definition stk_arch.h:214

References stk::IStackMemory::GetStack(), stk::IStackMemory::GetStackSize(), m_user, and stk::hw::PtrToWord().

Here is the call graph for this function:

◆ IsPendingRemoval()

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
bool stk::Kernel< TMode, TSize, TStrategy, TPlatform >::KernelTask::IsPendingRemoval ( ) const
inlineprotected

Check if task is pending removal.

Definition at line 625 of file stk.h.

625{ return ((m_state & STATE_REMOVE_PENDING) != 0U); }

References m_state, and STATE_REMOVE_PENDING.

◆ IsSleepInfinite()

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
bool stk::Kernel< TMode, TSize, TStrategy, TPlatform >::KernelTask::IsSleepInfinite ( ) const
inline

Check whether this task is currently sleeping infinitely.

Returns
true if m_time_sleep == -WAIT_INFINITE.

Definition at line 191 of file stk.h.

191{ return (m_time_sleep == -WAIT_INFINITE); }

References __stk_forceinline, m_time_sleep, and stk::WAIT_INFINITE.

◆ IsSleeping()

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
bool stk::Kernel< TMode, TSize, TStrategy, TPlatform >::KernelTask::IsSleeping ( ) const
inlineoverridevirtual

Check whether this task is currently sleeping (waiting for a tick or a wake event).

Returns
true if m_time_sleep < 0 (negative value encodes remaining sleep ticks).

Implements stk::IKernelTask.

Definition at line 186 of file stk.h.

186{ return (m_time_sleep < 0); }

References __stk_forceinline, and m_time_sleep.

Referenced by BusyWaitWhileSleeping(), GetHrtRelativeDeadline(), ScheduleSleep(), and Wake().

Here is the caller graph for this function:

◆ Link()

void stk::util::DListEntry< IKernelTask, TClosedLoop >::Link ( DLHeadType * head,
DLEntryType * next,
DLEntryType * prev )
inlineprivateinherited

Wire this entry into a list between prev and next.

Parameters
[in]headThe owning DListHead. Stored as a back-pointer for IsLinked() and ownership checks.
[in]nextThe entry that will follow this one, or NULL if this becomes the last entry.
[in]prevThe entry that will precede this one, or NULL if this becomes the first entry.
Note
Called exclusively by DListHead::Link(). Assumes the entry is not currently linked. Updates the neighbours' forward/back pointers to splice this entry in.

Definition at line 162 of file stk_linked_list.h.

163 {
164 m_head = head;
165 m_next = next;
166 m_prev = prev;
167
168 if (m_prev != nullptr)
169 {
170 m_prev->m_next = this;
171 }
172
173 if (m_next != nullptr)
174 {
175 m_next->m_prev = this;
176 }
177 }
DLEntryType * m_next
Next entry in the list, or NULL (open list boundary) / first entry (closed loop).
DLEntryType * m_prev
Previous entry in the list, or NULL (open list boundary) / last entry (closed loop).

◆ operator const IKernelTask *()

stk::util::DListEntry< IKernelTask, TClosedLoop >::operator const IKernelTask * ( ) const
inlineinherited

Implicit conversion to a const pointer to the host object (T).

Note
Safe because T must derive from DListEntry<T, TClosedLoop>. Eliminates the need for explicit static_cast at call sites.
MISRA deviation: [STK-DEV-004] Rule 5-2-x.

Definition at line 141 of file stk_linked_list.h.

141{ return static_cast<const T *>(this); }

◆ operator IKernelTask *()

stk::util::DListEntry< IKernelTask, TClosedLoop >::operator IKernelTask* ( )
inlineinherited

Implicit conversion to a mutable pointer to the host object (T).

Note
Safe because T must derive from DListEntry<T, TClosedLoop>. Eliminates the need for explicit static_cast at call sites.
MISRA deviation: [STK-DEV-004] Rule 5-2-x.

Definition at line 134 of file stk_linked_list.h.

134{ return static_cast<T *>(this); }

◆ ScheduleRemoval()

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

Schedule the removal of the task from the kernel on next tick.

Definition at line 608 of file stk.h.

609 {
610 // make this task sleeping to switch it out from scheduling process
612
613 // mark it as done HRT task
615 {
617 }
618
619 // mark it as pending for removal
621 }
void HrtOnWorkCompleted()
Called when task process called IKernelService::SwitchToNext to inform Kernel that work is completed.
Definition stk.h:727

References __stk_constexpr_cpp17, HrtOnWorkCompleted(), stk::Kernel< TMode, TSize, TStrategy, TPlatform >::IsHrtMode(), m_state, ScheduleSleep(), STATE_REMOVE_PENDING, and stk::WAIT_INFINITE.

Here is the call graph for this function:

◆ ScheduleSleep()

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
void stk::Kernel< TMode, TSize, TStrategy, TPlatform >::KernelTask::ScheduleSleep ( Timeout ticks)
inlineprotected

Put the task into a sleeping state for the specified number of ticks.

Parameters
[in]ticksNumber of ticks to sleep. Must be > 0.
Note
Stores -ticks in m_time_sleep (negative values indicate sleeping; UpdateTaskState() increments toward 0 each tick until the task wakes).
If the strategy uses SLEEP_EVENT_API and the task is not already sleeping, sets STATE_SLEEP_PENDING so OnTaskSleep() is delivered on the next tick.
A full memory fence is emitted after the assignment so that the ISR-side scheduler sees the updated value without delay.

Definition at line 750 of file stk.h.

751 {
752 STK_ASSERT(ticks > 0);
753
754 // set state first as kernel checks it when task IsSleeping
755 if (!IsSleeping())
756 {
758 }
759
761
763 }

References IsSleeping(), m_state, m_time_sleep, STATE_SLEEP_PENDING, and STK_ASSERT.

Referenced by HrtInit(), HrtOnSwitchedOut(), and ScheduleRemoval().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ SetCurrentWeight()

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
void stk::Kernel< TMode, TSize, TStrategy, TPlatform >::KernelTask::SetCurrentWeight ( Weight weight)
inlineoverridevirtual

Update the run-time scheduling weight (weighted strategies only).

Parameters
[in]weightNew current weight. Ignored unless TStrategy::WEIGHT_API is true.

Implements stk::IKernelTask.

Definition at line 219 of file stk.h.

220 {
222 {
223 m_rt_weight[0] = weight;
224 }
225 }

References __stk_constexpr_cpp17, and m_rt_weight.

Referenced by Bind().

Here is the caller graph for this function:

◆ Unbind()

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

Reset this slot to the free (unbound) state, clearing all scheduling metadata.

Note
Called by RemoveTask(). After Unbind() the slot is available for the next AddTask().

Definition at line 583 of file stk.h.

584 {
586 {
587 // should be freed from waiting on task exit
588 STK_ASSERT(!m_wait_obj->IsWaiting());
589 }
590
591 m_user = nullptr;
592 m_stack = {};
594 m_time_sleep = 0;
595
597 {
598 m_hrt[0].Clear();
599 }
600 else
601 {
602 m_srt->Clear();
603 }
604 }

References __stk_constexpr_cpp17, stk::Kernel< TMode, TSize, TStrategy, TPlatform >::IsHrtMode(), stk::Kernel< TMode, TSize, TStrategy, TPlatform >::IsSyncMode(), m_hrt, m_srt, m_stack, m_state, m_time_sleep, m_user, m_wait_obj, STATE_NONE, and STK_ASSERT.

Here is the call graph for this function:

◆ Unlink()

void stk::util::DListEntry< IKernelTask, TClosedLoop >::Unlink ( )
inlineprivateinherited

Remove this entry from its current list.

Note
Called exclusively by DListHead::Unlink(). Patches the neighbours' pointers to bridge over this entry, then clears m_head, m_next, and m_prev to NULL so the entry is in a clean unlinked state.
Does not update DListHead::m_count or m_first / m_last — those are the responsibility of the calling DListHead::Unlink().

Definition at line 186 of file stk_linked_list.h.

187 {
188 if (m_prev != nullptr)
189 {
191 }
192
193 if (m_next != nullptr)
194 {
196 }
197
198 m_head = nullptr;
199 m_next = nullptr;
200 m_prev = nullptr;
201 }

◆ Wake()

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

Wake this task on the next scheduling tick.

Note
Sets m_time_sleep to -1 (one tick remaining) so the task exits sleep state on the next UpdateTaskState() pass. Asserts that the task is currently sleeping.

Implements stk::IKernelTask.

Definition at line 202 of file stk.h.

203 {
205
206 // wakeup on a next cycle
207 m_time_sleep = -1;
208
209 // notify kernel that this task, even if was going to sleep will be woken on a next tick
210 if ((m_state & STATE_SLEEP_PENDING) != 0U)
211 {
213 }
214 }

References IsSleeping(), m_state, m_time_sleep, STATE_SLEEP_PENDING, STATE_WAKE_PENDING, and STK_ASSERT.

Here is the call graph for this function:

◆ Kernel

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

Definition at line 127 of file stk.h.

References Kernel.

Referenced by BusyWaitWhileSleeping(), and Kernel.

Member Data Documentation

◆ m_head

DLHeadType* stk::util::DListEntry< IKernelTask, TClosedLoop >::m_head
privateinherited

Owning list head, or NULL when the entry is not linked.

Definition at line 203 of file stk_linked_list.h.

◆ m_hrt

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
HrtInfo stk::Kernel< TMode, TSize, TStrategy, TPlatform >::KernelTask::m_hrt[STK_ALLOCATE_COUNT< TMode, KERNEL_HRT, 1U, 0U >::Value]
protected

HRT metadata. Zero-size (no memory) in non-HRT mode.

Definition at line 787 of file stk.h.

Referenced by GetHrtDeadline(), GetHrtPeriodicity(), GetHrtRelativeDeadline(), HrtHardFailDeadline(), HrtInit(), HrtIsDeadlineMissed(), HrtOnSwitchedOut(), HrtOnWorkCompleted(), KernelTask(), and Unbind().

◆ m_next

DLEntryType* stk::util::DListEntry< IKernelTask, TClosedLoop >::m_next
privateinherited

Next entry in the list, or NULL (open list boundary) / first entry (closed loop).

Definition at line 204 of file stk_linked_list.h.

◆ m_prev

DLEntryType* stk::util::DListEntry< IKernelTask, TClosedLoop >::m_prev
privateinherited

Previous entry in the list, or NULL (open list boundary) / last entry (closed loop).

Definition at line 205 of file stk_linked_list.h.

◆ m_rt_weight

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
Weight stk::Kernel< TMode, TSize, TStrategy, TPlatform >::KernelTask::m_rt_weight[STK_ALLOCATE_COUNT< TStrategy::WEIGHT_API, 1U, 1U, 0U >::Value]
protected

Run-time weight for weighted-round-robin scheduling. Zero-size for unweighted strategies.

Definition at line 788 of file stk.h.

Referenced by GetCurrentWeight(), GetWeight(), KernelTask(), and SetCurrentWeight().

◆ m_srt

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
SrtInfo stk::Kernel< TMode, TSize, TStrategy, TPlatform >::KernelTask::m_srt[STK_ALLOCATE_COUNT< TMode, KERNEL_HRT, 0U, 1U >::Value]
protected

SRT metadata. Zero-size (no memory) in KERNEL_HRT mode.

Definition at line 786 of file stk.h.

Referenced by KernelTask(), and Unbind().

◆ m_stack

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

Stack descriptor (SP register value + access mode + optional tid).

Definition at line 783 of file stk.h.

Referenced by Bind(), GetUserStack(), GetUserStackPtr(), KernelTask(), and Unbind().

◆ m_state

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

Bitmask of EStateFlags. Written by task thread, read/cleared by kernel tick.

Definition at line 784 of file stk.h.

Referenced by IsPendingRemoval(), KernelTask(), ScheduleRemoval(), ScheduleSleep(), Unbind(), and Wake().

◆ m_time_sleep

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

Sleep countdown: negative while sleeping (absolute value = ticks remaining), zero when awake.

Definition at line 785 of file stk.h.

Referenced by GetSleepTicks(), IsSleepInfinite(), IsSleeping(), KernelTask(), ScheduleSleep(), Unbind(), and Wake().

◆ m_user

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

Bound user task, or NULL when slot is free.

Definition at line 782 of file stk.h.

Referenced by Bind(), GetTid(), GetUserTask(), GetWeight(), HrtHardFailDeadline(), IsBusy(), IsMemoryOfSP(), KernelTask(), and Unbind().

◆ m_wait_obj

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

Embedded wait object for synchronization. Zero-size (no memory) if KERNEL_SYNC is not set.

Definition at line 789 of file stk.h.

Referenced by GetSleepTicks(), KernelTask(), and Unbind().


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