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::sched::SwitchStrategyFixedPriority< MAX_PRIORITIES, NoSleepYield, ReadyBitmap > Class Template Referencefinal

Fixed-priority preemptive scheduling strategy with round-robin arbitration within each priority level. More...

#include <stk_strategy_fpriority.h>

Inheritance diagram for stk::sched::SwitchStrategyFixedPriority< MAX_PRIORITIES, NoSleepYield, ReadyBitmap >:
Collaboration diagram for stk::sched::SwitchStrategyFixedPriority< MAX_PRIORITIES, NoSleepYield, ReadyBitmap >:

Public Types

enum  EConfig {
  WEIGHT_API = 1 ,
  SLEEP_EVENT_API = 1 ,
  DEADLINE_MISSED_API = 0 ,
  PRIORITY_INHERITANCE_API = 1 ,
  NOSLEEP_YIELD_API = NoSleepYield ,
  TIMESLICE_API = 0
}
 Compile-time capability flags reported to the kernel. More...
enum  EPriority {
  PRIORITY_HIGHEST = (MAX_PRIORITIES - 1) ,
  PRIORITY_HIGH = ((MAX_PRIORITIES / 2) + (MAX_PRIORITIES - 1)) / 2 ,
  PRIORITY_ABOVE_NORMAL = (MAX_PRIORITIES / 2) + 1 ,
  PRIORITY_NORMAL = (MAX_PRIORITIES / 2) ,
  PRIORITY_BELOW_NORMAL = (MAX_PRIORITIES / 2) - 1 ,
  PRIORITY_LOWER = (MAX_PRIORITIES / 4) ,
  PRIORITY_ABOVE_LOWEST = (0 + 1) ,
  PRIORITY_LOWEST = 0
}
 Symbolic priority level constants for common use cases. More...

Public Member Functions

 SwitchStrategyFixedPriority ()
 Construct an empty strategy with all priority levels empty, a clear bitmap, and null cursors.
STK_VIRT_DTOR ~SwitchStrategyFixedPriority ()=default
 Destructor.
void AddTask (IKernelTask *task) override
 Add task to the runnable set at its fixed priority level.
void RemoveTask (IKernelTask *task) override
 Remove task from whichever list it currently occupies.
IKernelTask * GetNext () override
 Select and return the next task to run.
IKernelTask * GetFirst () override
 Get the first task in the managed set (used by the kernel for initial scheduling).
size_t GetSize () const override
 Get the total number of tasks managed by this strategy.
void OnTaskSleep (IKernelTask *task) override
 Notification that a task has entered the sleeping state.
void OnTaskWake (IKernelTask *task) override
 Notification that a task has become runnable again.
void OnTaskWeightChange (IKernelTask *task, Weight old_weight) override
 Move a runnable task to a new priority level after its weight changed.
bool OnTaskYield (IKernelTask *task) override
 Notification that the running task called Yield() (non-HRT kernel modes only).
virtual bool OnTaskTimeSlice (IKernelTask *task)
 Notification that the running task's time slice expired (KERNEL_TIMESLICE only).
virtual bool OnTaskDeadlineMissed (IKernelTask *task)
 Notification that a task has exceeded its HRT deadline; returns whether the strategy can recover without a hard fault.

Protected Types

typedef uint16_t Priority
 Priority type, wide enough for the 1024 levels of the largest PriorityBitmap and for custom wider bitmaps.
typedef PriorityBitmapTraits< ReadyBitmap > BitmapTraits
 Bitmap operations (set/clear/empty/highest-set) for the ReadyBitmap type.

Protected Member Functions

void AddActive (IKernelTask *task)
 Append a task to its priority level's runnable list and update the bitmap.
void RemoveActive (IKernelTask *task, const Priority prio)
 Remove a task from its priority level's runnable list and update the bitmap/cursor.

Static Protected Member Functions

static __stk_forceinline bool IsWeightValid (int32_t weight)
 Check that a weight is a valid priority level, [0, MAX_PRIORITIES).
static __stk_forceinline Priority GetTaskPriorityFromWeight (int32_t weight)
 Get priority from the weight.
static __stk_forceinline Priority GetTaskPriority (IKernelTask *task)
 Get priority from the task.
static __stk_forceinline Priority GetHighestReadyPriority (const ReadyBitmap &bitmap)
 Find the index of the highest set bit in bitmap.

Private Member Functions

 STK_NONCOPYABLE_CLASS (SwitchStrategyFixedPriority)

Private Attributes

IKernelTask::ListHeadType m_tasks [MAX_PRIORITIES]
 Per-priority runnable task lists. m_tasks[i] holds all runnable tasks at priority level i.
IKernelTask::ListHeadType m_sleep
 Sleeping (blocked) tasks. Priority is retained in GetWeight() and restored on OnTaskWake().
ReadyBitmap m_ready_bitmap
 Bitmask of non-empty priority levels: bit i is set when m_tasks[i] has at least one runnable task. Updated by AddActive() (bit set) and RemoveActive() (bit cleared on last removal).
IKernelTask * m_prev [MAX_PRIORITIES]
 Per-priority round-robin cursor. m_prev[i] is the most recently scheduled task at level i, or nullptr when level i is empty. GetNext() advances from this position.

Detailed Description

template<uint16_t MAX_PRIORITIES, bool NoSleepYield = false, class ReadyBitmap = typename ReadyBitmapSelect<MAX_PRIORITIES>::Type>
class stk::sched::SwitchStrategyFixedPriority< MAX_PRIORITIES, NoSleepYield, ReadyBitmap >

Fixed-priority preemptive scheduling strategy with round-robin arbitration within each priority level.

Template Parameters
MAX_PRIORITIESNumber of distinct priority levels. Must be in the range [1, PriorityBitmapTraits<ReadyBitmap>::CAPACITY] (enforced by a compile-time assertion) and not exceed 32768 (priority is stored as uint16_t, the same ceiling as SwitchStrategyPreemptionThreshold; the default PriorityBitmap<> tops out at 1024). Bit i of m_ready_bitmap represents priority level i. The concrete aliases SwitchStrategyFP32 and SwitchStrategyFP64 use MAX_PRIORITIES = 32 and 64 respectively.
NoSleepYieldIf true, Yield() keeps the task in the runnable set and hands the CPU to the next task at the same priority level (see OnTaskYield). If false (default), the kernel performs the legacy yield: the task sleeps for YIELD_TICKS, which also lets lower-priority tasks run during that tick.
ReadyBitmapType of the ready-priorities bitmap (m_ready_bitmap). Defaults to uint32_t for MAX_PRIORITIES <= 32, to uint64_t for <= 64 and to PriorityBitmap<> (multi-word, 96..1024 bits) above that. Can be any unsigned integer type, PriorityBitmap<BITS>, or a user-defined class for which a PriorityBitmapTraits specialization is provided.
Priority assignment
Each task's priority is its weight (IKernelTask::GetWeight()), narrowed to uint16_t, and must be in the range [0, MAX_PRIORITIES - 1] (checked on the raw weight). Configure a task's priority by overriding ITask::GetWeight() to return the desired level. While a task inherits a higher weight (priority inheritance) its priority is the inherited one, the strategy is notified by OnTaskWeightChange().
Scheduling rules
  • The highest runnable priority level is always selected. A task at priority N will never run while any task at priority > N is runnable.
  • Tasks at the same priority level are scheduled in round-robin order (one tick slice each), using a per-level cursor (m_prev[prio]).
  • Higher numeric value = higher priority. 0 is the lowest, MAX_PRIORITIES - 1 is highest.
No-sleep yield
When NoSleepYield is true, OnTaskYield() moves the yielding task's per-level cursor onto the task itself, so the following GetNext() returns its successor at the same priority level. If the task is alone at its level, it is selected again (no context switch): a yield never hands the CPU to a lower priority level, unlike the legacy sleep-based yield.
Priority detection — O(1) bitmap
m_ready_bitmap is a bitmask (of type ReadyBitmap) where bit i is set whenever priority level i has at least one runnable task. GetNext() and GetFirst() locate the highest set bit via PriorityBitmapTraits<ReadyBitmap>::HighestSet(): O(1) for native 32-bit and 64-bit bitmaps (a 64-bit scan is two 32-bit scans) and for PriorityBitmap<> (summary word + one word scan). The bitmap is kept consistent by AddActive() (bit set) and RemoveActive() (bit cleared on last removal). All bitmap access goes through PriorityBitmapTraits, so the bitmap type can be replaced without touching the scheduling logic.
Note
Uses the Weight API (WEIGHT_API = 1): GetWeight() is interpreted as the task's priority level. The strategy itself never calls SetCurrentWeight() / GetCurrentWeight() (the kernel uses them to keep the inherited weight).
Requires the kernel Sleep API (SLEEP_EVENT_API = 1): OnTaskSleep() and OnTaskWake() maintain the per-priority runnable lists and keep m_ready_bitmap accurate.
See also
SwitchStrategyFP32, SwitchStrategyFP32_NSY, ITaskSwitchStrategy, ITask::GetWeight

Definition at line 81 of file stk_strategy_fpriority.h.

Member Typedef Documentation

◆ BitmapTraits

template<uint16_t MAX_PRIORITIES, bool NoSleepYield = false, class ReadyBitmap = typename ReadyBitmapSelect<MAX_PRIORITIES>::Type>
typedef PriorityBitmapTraits<ReadyBitmap> stk::sched::SwitchStrategyFixedPriority< MAX_PRIORITIES, NoSleepYield, ReadyBitmap >::BitmapTraits
protected

Bitmap operations (set/clear/empty/highest-set) for the ReadyBitmap type.

Definition at line 361 of file stk_strategy_fpriority.h.

◆ Priority

template<uint16_t MAX_PRIORITIES, bool NoSleepYield = false, class ReadyBitmap = typename ReadyBitmapSelect<MAX_PRIORITIES>::Type>
typedef uint16_t stk::sched::SwitchStrategyFixedPriority< MAX_PRIORITIES, NoSleepYield, ReadyBitmap >::Priority
protected

Priority type, wide enough for the 1024 levels of the largest PriorityBitmap and for custom wider bitmaps.

Definition at line 358 of file stk_strategy_fpriority.h.

Member Enumeration Documentation

◆ EConfig

template<uint16_t MAX_PRIORITIES, bool NoSleepYield = false, class ReadyBitmap = typename ReadyBitmapSelect<MAX_PRIORITIES>::Type>
enum stk::sched::SwitchStrategyFixedPriority::EConfig

Compile-time capability flags reported to the kernel.

Enumerator
WEIGHT_API 

This strategy interprets GetWeight() as the task's fixed priority level (0 .. MAX_PRIORITIES - 1). Dynamic weight functions are not used.

SLEEP_EVENT_API 

This strategy requires OnTaskSleep() / OnTaskWake() events to maintain per-priority runnable lists and keep m_ready_bitmap accurate.

DEADLINE_MISSED_API 

This strategy does not use OnTaskDeadlineMissed() events.

PRIORITY_INHERITANCE_API 

This strategy handles OnTaskWeightChange() events to support priority inheritance.

NOSLEEP_YIELD_API 

(1) enables OnTaskYield(): yielding task stays runnable and hands the CPU to same-priority peers.

TIMESLICE_API 

This strategy does not support KERNEL_TIMESLICE mode.

Definition at line 87 of file stk_strategy_fpriority.h.

88 {
89 WEIGHT_API = 1,
90 SLEEP_EVENT_API = 1,
94 TIMESLICE_API = 0
95 };
Fixed-priority preemptive scheduling strategy with round-robin arbitration within each priority level...
@ DEADLINE_MISSED_API
This strategy does not use OnTaskDeadlineMissed() events.
@ WEIGHT_API
This strategy interprets GetWeight() as the task's fixed priority level (0 .. MAX_PRIORITIES - 1)....
@ NOSLEEP_YIELD_API
(1) enables OnTaskYield(): yielding task stays runnable and hands the CPU to same-priority peers.
@ TIMESLICE_API
This strategy does not support KERNEL_TIMESLICE mode.
@ PRIORITY_INHERITANCE_API
This strategy handles OnTaskWeightChange() events to support priority inheritance.
@ SLEEP_EVENT_API
This strategy requires OnTaskSleep() / OnTaskWake() events to maintain per-priority runnable lists an...

◆ EPriority

template<uint16_t MAX_PRIORITIES, bool NoSleepYield = false, class ReadyBitmap = typename ReadyBitmapSelect<MAX_PRIORITIES>::Type>
enum stk::sched::SwitchStrategyFixedPriority::EPriority

Symbolic priority level constants for common use cases.

Note
Concrete values depend on MAX_PRIORITIES. For SwitchStrategyFP32 (MAX_PRIORITIES = 32): PRIORITY_HIGHEST = 31, PRIORITY_HIGH = 23, PRIORITY_ABOVE_NORMAL = 17, PRIORITY_NORMAL = 16, PRIORITY_BELOW_NORMAL = 15, PRIORITY_LOWER = 8, PRIORITY_ABOVE_LOWEST = 1, PRIORITY_LOWEST = 0. For SwitchStrategyFP64 (MAX_PRIORITIES = 64): PRIORITY_HIGHEST = 63, PRIORITY_HIGH = 47, PRIORITY_ABOVE_NORMAL = 33, PRIORITY_NORMAL = 32, PRIORITY_BELOW_NORMAL = 31, PRIORITY_LOWER = 16, PRIORITY_ABOVE_LOWEST = 1, PRIORITY_LOWEST = 0.
Enumerator
PRIORITY_HIGHEST 

Highest available priority level.

PRIORITY_HIGH 

High priority; mid-point between NORMAL and HIGHEST.

PRIORITY_ABOVE_NORMAL 

Slightly elevated priority; one step above NORMAL.

PRIORITY_NORMAL 

Mid-range priority level (MAX_PRIORITIES / 2).

PRIORITY_BELOW_NORMAL 

Slightly reduced priority; one step below NORMAL.

PRIORITY_LOWER 

Lower priority; mid-point between LOWEST and NORMAL.

PRIORITY_ABOVE_LOWEST 

Slightly elevated low priority; one step above LOWEST.

PRIORITY_LOWEST 

Lowest priority level; tasks only run when idle.

Definition at line 107 of file stk_strategy_fpriority.h.

108 {
110 PRIORITY_HIGH = ((MAX_PRIORITIES / 2) + (MAX_PRIORITIES - 1)) / 2,
115 PRIORITY_ABOVE_LOWEST = (0 + 1),
116 PRIORITY_LOWEST = 0
117 };
@ PRIORITY_LOWER
Lower priority; mid-point between LOWEST and NORMAL.
@ PRIORITY_BELOW_NORMAL
Slightly reduced priority; one step below NORMAL.
@ PRIORITY_NORMAL
Mid-range priority level (MAX_PRIORITIES / 2).
@ PRIORITY_HIGHEST
Highest available priority level.
@ PRIORITY_LOWEST
Lowest priority level; tasks only run when idle.
@ PRIORITY_ABOVE_NORMAL
Slightly elevated priority; one step above NORMAL.
@ PRIORITY_HIGH
High priority; mid-point between NORMAL and HIGHEST.
@ PRIORITY_ABOVE_LOWEST
Slightly elevated low priority; one step above LOWEST.

Constructor & Destructor Documentation

◆ SwitchStrategyFixedPriority()

template<uint16_t MAX_PRIORITIES, bool NoSleepYield = false, class ReadyBitmap = typename ReadyBitmapSelect<MAX_PRIORITIES>::Type>
stk::sched::SwitchStrategyFixedPriority< MAX_PRIORITIES, NoSleepYield, ReadyBitmap >::SwitchStrategyFixedPriority ( )
inlineexplicit

Construct an empty strategy with all priority levels empty, a clear bitmap, and null cursors.

Note
A compile-time assertion enforces 1 <= MAX_PRIORITIES <= BitmapTraits::CAPACITY, i.e. the number of bits the ReadyBitmap type can hold. Instantiating with more priorities is a compile error.
m_ready_bitmap is value-initialized, so a user-defined ReadyBitmap must be default-constructible into the empty state.

Definition at line 125 of file stk_strategy_fpriority.h.

126 {
127 STK_STATIC_ASSERT_DESC_N(MinPrio, MAX_PRIORITIES >= 1U, "MAX_PRIORITIES must be at least 1");
128 STK_STATIC_ASSERT_DESC_N(MaxPrio, MAX_PRIORITIES <= static_cast<uint32_t>(BitmapTraits::CAPACITY), "MAX_PRIORITIES exceeds ReadyBitmap width");
129 STK_STATIC_ASSERT_DESC_N(MaxIdx, MAX_PRIORITIES <= 32768U, "MAX_PRIORITIES exceeds 32768 (priority is uint16_t)");
130 }
#define STK_STATIC_ASSERT_DESC_N(NAME, X, DESC)
Compile-time assertion with a name tag and a custom error description.
Definition stk_defs.h:543
IKernelTask::ListHeadType m_sleep
Sleeping (blocked) tasks. Priority is retained in GetWeight() and restored on OnTaskWake().
ReadyBitmap m_ready_bitmap
Bitmask of non-empty priority levels: bit i is set when m_tasks[i] has at least one runnable task....
IKernelTask * m_prev[MAX_PRIORITIES]
Per-priority round-robin cursor. m_prev[i] is the most recently scheduled task at level i,...
IKernelTask::ListHeadType m_tasks[MAX_PRIORITIES]
Per-priority runnable task lists. m_tasks[i] holds all runnable tasks at priority level i.

◆ ~SwitchStrategyFixedPriority()

template<uint16_t MAX_PRIORITIES, bool NoSleepYield = false, class ReadyBitmap = typename ReadyBitmapSelect<MAX_PRIORITIES>::Type>
STK_VIRT_DTOR stk::sched::SwitchStrategyFixedPriority< MAX_PRIORITIES, NoSleepYield, ReadyBitmap >::~SwitchStrategyFixedPriority ( )
default

Destructor.

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

Member Function Documentation

◆ AddActive()

template<uint16_t MAX_PRIORITIES, bool NoSleepYield = false, class ReadyBitmap = typename ReadyBitmapSelect<MAX_PRIORITIES>::Type>
void stk::sched::SwitchStrategyFixedPriority< MAX_PRIORITIES, NoSleepYield, ReadyBitmap >::AddActive ( IKernelTask * task)
inlineprotected

Append a task to its priority level's runnable list and update the bitmap.

Parameters
[in]taskTask to make runnable.
Note
Appends to the back of m_tasks[prio].
Bitmap and cursor are only updated when the level was previously empty (GetSize() == 1 after LinkBack): m_ready_bitmap bit prio is set and m_prev[prio] is initialized to task. Subsequent additions at the same level leave the cursor untouched (the tail-adjustment in AddTask() handles the cursor for non-first additions).

Definition at line 372 of file stk_strategy_fpriority.h.

373 {
375
376 m_tasks[prio].LinkBack(task);
377
378 // init pointer
379 if (m_tasks[prio].GetSize() == 1U)
380 {
381 m_prev[prio] = task;
382
384 }
385 }
static __stk_forceinline void Set(ReadyBitmap &bitmap, uint32_t bit)
size_t GetSize() const override
Get the total number of tasks managed by this strategy.
static __stk_forceinline Priority GetTaskPriority(IKernelTask *task)
Get priority from the task.
ReadyBitmap m_ready_bitmap
Bitmask of non-empty priority levels: bit i is set when m_tasks[i] has at least one runnable task....
uint16_t Priority
Priority type, wide enough for the 1024 levels of the largest PriorityBitmap and for custom wider bit...
IKernelTask * m_prev[MAX_PRIORITIES]
Per-priority round-robin cursor. m_prev[i] is the most recently scheduled task at level i,...
IKernelTask::ListHeadType m_tasks[MAX_PRIORITIES]
Per-priority runnable task lists. m_tasks[i] holds all runnable tasks at priority level i.

Referenced by stk::sched::SwitchStrategyFixedPriority< 32 >::AddTask(), stk::sched::SwitchStrategyFixedPriority< 32 >::OnTaskWake(), and stk::sched::SwitchStrategyFixedPriority< 32 >::OnTaskWeightChange().

Here is the caller graph for this function:

◆ AddTask()

template<uint16_t MAX_PRIORITIES, bool NoSleepYield = false, class ReadyBitmap = typename ReadyBitmapSelect<MAX_PRIORITIES>::Type>
void stk::sched::SwitchStrategyFixedPriority< MAX_PRIORITIES, NoSleepYield, ReadyBitmap >::AddTask ( IKernelTask * task)
inlineoverridevirtual

Add task to the runnable set at its fixed priority level.

Parameters
[in]taskTask to add. Must not be NULL and must not already be in any list.
Note
Priority is extracted from GetWeight(). The weight must be in [0, MAX_PRIORITIES) (asserted before it is narrowed to the Priority type); passing a task whose GetWeight() is out of range is a programming error.
Delegates to AddActive() which appends the task to m_tasks[prio], sets m_ready_bitmap bit prio if this is the first task at that level, and initializes m_prev[prio].
Tail-cursor invariant (same as RR): if m_prev[prio] was already pointing at the tail before insertion, it is advanced to the new tail so GetNext() will include the new task in the very next rotation at this priority level.

Implements stk::ITaskSwitchStrategy.

Definition at line 149 of file stk_strategy_fpriority.h.

150 {
151 STK_ASSERT(task != nullptr);
152 STK_ASSERT(task->GetHead() == nullptr);
153 STK_ASSERT(IsWeightValid(task->GetWeight()));
154
156 const bool is_tail = (m_prev[prio] == m_tasks[prio].GetLast());
157
159
160 // if pointer was pointing to the tail, become a tail
161 if (is_tail)
162 {
163 m_prev[prio] = task;
164 }
165 }
#define STK_ASSERT(e)
Runtime assertion. Halts execution if the expression e evaluates to false.
Definition stk_defs.h:532
static __stk_forceinline bool IsWeightValid(int32_t weight)
Check that a weight is a valid priority level, [0, MAX_PRIORITIES).
void AddActive(IKernelTask *task)
Append a task to its priority level's runnable list and update the bitmap.

◆ GetFirst()

template<uint16_t MAX_PRIORITIES, bool NoSleepYield = false, class ReadyBitmap = typename ReadyBitmapSelect<MAX_PRIORITIES>::Type>
IKernelTask * stk::sched::SwitchStrategyFixedPriority< MAX_PRIORITIES, NoSleepYield, ReadyBitmap >::GetFirst ( )
inlineoverridevirtual

Get the first task in the managed set (used by the kernel for initial scheduling).

Returns
The first task in the highest runnable priority level if any task is runnable (bitmap non-empty); otherwise the first task in m_sleep. Asserts if the combined set is empty (GetSize() == 0).
Note
Uses m_ready_bitmap to determine whether any runnable task exists, consistent with GetNext(). The sleep fallback allows the kernel to identify any task even when all are currently sleeping.

Implements stk::ITaskSwitchStrategy.

Definition at line 223 of file stk_strategy_fpriority.h.

224 {
225 STK_ASSERT(GetSize() != 0U);
226
227 IKernelTask *first_task = nullptr;
228
230 {
231 first_task = (*m_sleep.GetFirst());
232 }
233 else
234 {
236 first_task = (*m_tasks[prio].GetFirst());
237 }
238
239 return first_task;
240 }
static __stk_forceinline Priority GetHighestReadyPriority(const ReadyBitmap &bitmap)
Find the index of the highest set bit in bitmap.
IKernelTask::ListHeadType m_sleep
Sleeping (blocked) tasks. Priority is retained in GetWeight() and restored on OnTaskWake().

◆ GetHighestReadyPriority()

template<uint16_t MAX_PRIORITIES, bool NoSleepYield = false, class ReadyBitmap = typename ReadyBitmapSelect<MAX_PRIORITIES>::Type>
__stk_forceinline Priority stk::sched::SwitchStrategyFixedPriority< MAX_PRIORITIES, NoSleepYield, ReadyBitmap >::GetHighestReadyPriority ( const ReadyBitmap & bitmap)
inlinestaticprotected

Find the index of the highest set bit in bitmap.

Parameters
[in]bitmapBitmask of ready priority levels. Must not be empty (behaviour is undefined; callers must check before calling).
Returns
Index of the most-significant set bit (0-based), which is the highest runnable priority level.
Note
Delegates to PriorityBitmapTraits<ReadyBitmap>::HighestSet(). For the native integer bitmaps this is built on CountLeadingZeros() (a 64-bit bitmap is scanned as two 32-bit halves, high half first).

Definition at line 464 of file stk_strategy_fpriority.h.

465 {
466 return static_cast<Priority>(BitmapTraits::HighestSet(bitmap));
467 }
static __stk_forceinline uint32_t HighestSet(const ReadyBitmap &bitmap)

Referenced by stk::sched::SwitchStrategyFixedPriority< 32 >::GetFirst(), and stk::sched::SwitchStrategyFixedPriority< 32 >::GetNext().

Here is the caller graph for this function:

◆ GetNext()

template<uint16_t MAX_PRIORITIES, bool NoSleepYield = false, class ReadyBitmap = typename ReadyBitmapSelect<MAX_PRIORITIES>::Type>
IKernelTask * stk::sched::SwitchStrategyFixedPriority< MAX_PRIORITIES, NoSleepYield, ReadyBitmap >::GetNext ( )
inlineoverridevirtual

Select and return the next task to run.

Returns
The next task in the highest runnable priority level's round-robin rotation, or nullptr if no runnable tasks exist (m_ready_bitmap is empty, kernel sleeps).
Note
Priority selection is O(1) via GetHighestReadyPriority() on m_ready_bitmap. Within the selected level the cursor (m_prev[prio]) is advanced by one position in the closed-loop list, implementing round-robin within the level.
Tasks at a lower priority are never returned while any higher-priority task has its bitmap bit set — preemption is enforced entirely by the bitmap lookup.

Implements stk::ITaskSwitchStrategy.

Definition at line 200 of file stk_strategy_fpriority.h.

201 {
202 IKernelTask *next = nullptr;
203
205 {
207
208 next = (*m_prev[prio]->GetNext());
209 m_prev[prio] = next;
210 }
211
212 return next;
213 }

◆ GetSize()

template<uint16_t MAX_PRIORITIES, bool NoSleepYield = false, class ReadyBitmap = typename ReadyBitmapSelect<MAX_PRIORITIES>::Type>
size_t stk::sched::SwitchStrategyFixedPriority< MAX_PRIORITIES, NoSleepYield, ReadyBitmap >::GetSize ( ) const
inlineoverridevirtual

Get the total number of tasks managed by this strategy.

Returns
Sum of tasks across all priority levels in m_tasks plus tasks in m_sleep.
Note
O(MAX_PRIORITIES): iterates all priority levels to accumulate the count. Unlike the RR and SWRR strategies this is not O(1).

Implements stk::ITaskSwitchStrategy.

Definition at line 247 of file stk_strategy_fpriority.h.

248 {
249 size_t total = m_sleep.GetSize();
250 for (uint16_t i = 0U; i < MAX_PRIORITIES; ++i)
251 {
252 total += m_tasks[i].GetSize();
253 }
254
255 return total;
256 }

Referenced by stk::sched::SwitchStrategyFixedPriority< 32 >::AddActive(), stk::sched::SwitchStrategyFixedPriority< 32 >::GetFirst(), and stk::sched::SwitchStrategyFixedPriority< 32 >::RemoveTask().

Here is the caller graph for this function:

◆ GetTaskPriority()

template<uint16_t MAX_PRIORITIES, bool NoSleepYield = false, class ReadyBitmap = typename ReadyBitmapSelect<MAX_PRIORITIES>::Type>
__stk_forceinline Priority stk::sched::SwitchStrategyFixedPriority< MAX_PRIORITIES, NoSleepYield, ReadyBitmap >::GetTaskPriority ( IKernelTask * task)
inlinestaticprotected

Get priority from the task.

Parameters
[in]taskPointer to the task. Priority is read from GetWeight().
Returns
Priority level.

Definition at line 450 of file stk_strategy_fpriority.h.

451 {
452 return GetTaskPriorityFromWeight(task->GetWeight());
453 }
static __stk_forceinline Priority GetTaskPriorityFromWeight(int32_t weight)
Get priority from the weight.

Referenced by stk::sched::SwitchStrategyFixedPriority< 32 >::AddActive(), stk::sched::SwitchStrategyFixedPriority< 32 >::AddTask(), stk::sched::SwitchStrategyFixedPriority< 32 >::OnTaskSleep(), stk::sched::SwitchStrategyFixedPriority< 32 >::OnTaskYield(), and stk::sched::SwitchStrategyFixedPriority< 32 >::RemoveTask().

Here is the caller graph for this function:

◆ GetTaskPriorityFromWeight()

template<uint16_t MAX_PRIORITIES, bool NoSleepYield = false, class ReadyBitmap = typename ReadyBitmapSelect<MAX_PRIORITIES>::Type>
__stk_forceinline Priority stk::sched::SwitchStrategyFixedPriority< MAX_PRIORITIES, NoSleepYield, ReadyBitmap >::GetTaskPriorityFromWeight ( int32_t weight)
inlinestaticprotected

Get priority from the weight.

Parameters
[in]weightWeight of the task read from GetWeight().
Returns
Priority level.

Definition at line 441 of file stk_strategy_fpriority.h.

442 {
443 return static_cast<Priority>(weight);
444 }

Referenced by stk::sched::SwitchStrategyFixedPriority< 32 >::GetTaskPriority(), and stk::sched::SwitchStrategyFixedPriority< 32 >::OnTaskWeightChange().

Here is the caller graph for this function:

◆ IsWeightValid()

template<uint16_t MAX_PRIORITIES, bool NoSleepYield = false, class ReadyBitmap = typename ReadyBitmapSelect<MAX_PRIORITIES>::Type>
__stk_forceinline bool stk::sched::SwitchStrategyFixedPriority< MAX_PRIORITIES, NoSleepYield, ReadyBitmap >::IsWeightValid ( int32_t weight)
inlinestaticprotected

Check that a weight is a valid priority level, [0, MAX_PRIORITIES).

Parameters
[in]weightWeight of the task read from GetWeight().
Note
Checked on the raw weight: narrowing to Priority first would alias negative or too large weights onto valid levels.

Definition at line 432 of file stk_strategy_fpriority.h.

433 {
434 return ((weight >= 0) && (weight < static_cast<int32_t>(MAX_PRIORITIES)));
435 }

Referenced by stk::sched::SwitchStrategyFixedPriority< 32 >::AddTask(), and stk::sched::SwitchStrategyFixedPriority< 32 >::OnTaskWeightChange().

Here is the caller graph for this function:

◆ OnTaskDeadlineMissed()

virtual bool stk::ITaskSwitchStrategy::OnTaskDeadlineMissed ( IKernelTask * task)
inlinevirtualinherited

Notification that a task has exceeded its HRT deadline; returns whether the strategy can recover without a hard fault.

Parameters
[in]taskThe task whose deadline was missed. Must not be nullptr.
Returns
true — the strategy has absorbed the overrun (e.g. by escalating its scheduling mode): the kernel must not call HrtHardFailDeadline() for this tick. false — the strategy cannot recover: the kernel must call HrtHardFailDeadline() as normal.
Note
Budget Overrun API. Called by the kernel from UpdateTaskState() within a tick, after GetNext() has already been called for that tick. Only invoked when DEADLINE_MISSED_API == 1 in the concrete strategy's EConfig. Strategies that set DEADLINE_MISSED_API = 0 do not need to implement this method; the kernel will not call it and will proceed directly to HrtHardFailDeadline().
Returning true carries no implicit side-effects on task sleep state or duration counters — normal tick-driven scheduling remains responsible for those. This call only communicates "do not hard-fault this tick."
The base implementation returns false (unrecoverable), which is the correct default for strategies that do not implement overrun recovery.

Definition at line 1501 of file stk_common.h.

1502 {
1503 STK_UNUSED(task);
1504 return false;
1505 }
#define STK_UNUSED(X)
Explicitly marks a variable as unused to suppress compiler warnings.
Definition stk_defs.h:715

References STK_UNUSED.

◆ OnTaskSleep()

template<uint16_t MAX_PRIORITIES, bool NoSleepYield = false, class ReadyBitmap = typename ReadyBitmapSelect<MAX_PRIORITIES>::Type>
void stk::sched::SwitchStrategyFixedPriority< MAX_PRIORITIES, NoSleepYield, ReadyBitmap >::OnTaskSleep ( IKernelTask * task)
inlineoverridevirtual

Notification that a task has entered the sleeping state.

Parameters
[in]taskThe task that is now sleeping. Must be in m_tasks[priority] (asserted).
Note
Delegates to RemoveActive() which unlinks the task from its priority level list, updates the per-level cursor, and clears bit prio in m_ready_bitmap if this was the last runnable task at that level. Then appends the task to m_sleep.

Implements stk::ITaskSwitchStrategy.

Definition at line 264 of file stk_strategy_fpriority.h.

265 {
266 STK_ASSERT(task != nullptr);
267 STK_ASSERT(task->IsSleeping());
268 STK_ASSERT(task->GetHead() == &m_tasks[GetTaskPriority(task)]);
269
271 m_sleep.LinkBack(task);
272 }
void RemoveActive(IKernelTask *task, const Priority prio)
Remove a task from its priority level's runnable list and update the bitmap/cursor.
static __stk_forceinline Priority GetTaskPriority(IKernelTask *task)
Get priority from the task.

◆ OnTaskTimeSlice()

virtual bool stk::ITaskSwitchStrategy::OnTaskTimeSlice ( IKernelTask * task)
inlinevirtualinherited

Notification that the running task's time slice expired (KERNEL_TIMESLICE only).

Parameters
[in]taskPointer to the running task, currently in the runnable set.
Returns
true if the strategy rotated to a same-priority peer (the following GetNext() returns it); false to keep running the task (no peer, or the task is not eligible for time-slicing).
Note
Unlike OnTaskYield() this must not release preemption-threshold style holds.
Called from the tick handler. Only invoked when TIMESLICE_API == 1.
See also
SwitchStrategyPreemptionThreshold::OnTaskTimeSlice

Reimplemented in stk::sched::SwitchStrategyPreemptionThreshold< MAX_PRIORITIES, TIMESLICE, ReadyBitmap >, stk::sched::SwitchStrategyPreemptionThreshold< 32 >, stk::sched::SwitchStrategyPreemptionThreshold< 32 >, stk::sched::SwitchStrategyPreemptionThreshold< 32, true >, stk::sched::SwitchStrategyPreemptionThreshold< 32, true >, stk::sched::SwitchStrategyPreemptionThreshold< 64 >, stk::sched::SwitchStrategyPreemptionThreshold< 64 >, stk::sched::SwitchStrategyPreemptionThreshold< 64, true >, stk::sched::SwitchStrategyPreemptionThreshold< 64, true >, and stk::SwitchStrategyPreemptionThreshold< MAX_PRIORITIES, TIMESLICE, ReadyBitmap >.

Definition at line 1475 of file stk_common.h.

1476 {
1477 STK_UNUSED(task);
1478 return false;
1479 }

References STK_UNUSED.

◆ OnTaskWake()

template<uint16_t MAX_PRIORITIES, bool NoSleepYield = false, class ReadyBitmap = typename ReadyBitmapSelect<MAX_PRIORITIES>::Type>
void stk::sched::SwitchStrategyFixedPriority< MAX_PRIORITIES, NoSleepYield, ReadyBitmap >::OnTaskWake ( IKernelTask * task)
inlineoverridevirtual

Notification that a task has become runnable again.

Parameters
[in]taskThe task that woke up. Must be in m_sleep (asserted).
Note
Unlinks the task from m_sleep and delegates to AddActive() which appends it to m_tasks[priority] and sets the bitmap bit if the level was empty.
Unlike SwitchStrategySmoothWeightedRoundRobin, no priority boost is applied. The waking task re-enters its natural priority level and waits its turn in the round-robin rotation at that level. Its preemption guarantee is provided solely by its fixed priority relative to other runnable levels.

Implements stk::ITaskSwitchStrategy.

Definition at line 283 of file stk_strategy_fpriority.h.

284 {
285 STK_ASSERT(task != nullptr);
286 STK_ASSERT(!task->IsSleeping());
287 STK_ASSERT(task->GetHead() == &m_sleep);
288
289 m_sleep.Unlink(task);
291 }

◆ OnTaskWeightChange()

template<uint16_t MAX_PRIORITIES, bool NoSleepYield = false, class ReadyBitmap = typename ReadyBitmapSelect<MAX_PRIORITIES>::Type>
void stk::sched::SwitchStrategyFixedPriority< MAX_PRIORITIES, NoSleepYield, ReadyBitmap >::OnTaskWeightChange ( IKernelTask * task,
Weight old_weight )
inlineoverridevirtual

Move a runnable task to a new priority level after its weight changed.

Parameters
[in]taskTask whose weight was just updated.
[in]old_weightPriority level the task occupied before the change.
Note
Removes from the old-priority list (using old_weight to locate it), then re-inserts via AddActive() which reads the new GetWeight(). m_ready_bitmap is updated by both RemoveActive and AddActive.
No-op if old and new priority are identical.
Called from within a hw::CriticalSection.

Reimplemented from stk::ITaskSwitchStrategy.

Definition at line 302 of file stk_strategy_fpriority.h.

303 {
305
306 STK_ASSERT(IsWeightValid(task->GetWeight()));
308
309 if (task->GetHead() != &m_sleep)
310 {
311 STK_ASSERT(task->GetHead() == &m_tasks[old_prio]);
312
313 // remove from the old priority list
315
316 // re-add to the current priority list
318 }
319 }

◆ OnTaskYield()

template<uint16_t MAX_PRIORITIES, bool NoSleepYield = false, class ReadyBitmap = typename ReadyBitmapSelect<MAX_PRIORITIES>::Type>
bool stk::sched::SwitchStrategyFixedPriority< MAX_PRIORITIES, NoSleepYield, ReadyBitmap >::OnTaskYield ( IKernelTask * task)
inlineoverridevirtual

Notification that the running task called Yield() (non-HRT kernel modes only).

Parameters
[in]taskPointer to the yielding task, currently in the runnable set.
Returns
true if the strategy handled the yield itself: the task stays runnable (it is NOT put to sleep) and the following GetNext() returns its successor at the same priority level, or the task itself if it is alone at its level (a yield never selects a lower priority level); false if the kernel shall perform the legacy yield, i.e. sleep the task for YIELD_TICKS.
Note
If a higher priority level became ready in the meantime it is selected as usual; the yielding task then only loses its place in its own level's rotation.
Called from within a hw::CriticalSection.

Reimplemented from stk::ITaskSwitchStrategy.

Definition at line 332 of file stk_strategy_fpriority.h.

333 {
335 {
336 STK_ASSERT(task != nullptr);
337
338 bool handled = false;
340
341 if (task->GetHead() == &m_tasks[prio])
342 {
343 m_prev[prio] = task; // GetNext() will return task's successor at this level (or task itself if alone)
344 handled = true;
345 }
346
347 return handled;
348 }
349 else
350 {
352 return false;
353 }
354 }
#define __stk_constexpr_cpp17
constexpr definition for C++17 and above.
Definition stk_defs.h:505

◆ RemoveActive()

template<uint16_t MAX_PRIORITIES, bool NoSleepYield = false, class ReadyBitmap = typename ReadyBitmapSelect<MAX_PRIORITIES>::Type>
void stk::sched::SwitchStrategyFixedPriority< MAX_PRIORITIES, NoSleepYield, ReadyBitmap >::RemoveActive ( IKernelTask * task,
const Priority prio )
inlineprotected

Remove a task from its priority level's runnable list and update the bitmap/cursor.

Parameters
[in]taskRunnable task to remove.
[in]prioPriority of the task with which it was registered with AddActive.
Note
Cursor update algorithm (same as SwitchStrategyRoundRobin::RemoveActive, applied per priority level):
  • Capture next = task->GetNext() before unlinking.
  • If next != task (other tasks remain at this level) and the cursor m_prev[prio] pointed at the removed task: set m_prev[prio] = next->GetPrev() so GetNext() returns next without skipping it. If the cursor pointed at another task it is left untouched so that the rotation is not disturbed.
  • If next == task (last task at this level): set m_prev[prio] = NULL and clear bit prio in m_ready_bitmap. GetNext() will then select the next highest set bit, falling through to a lower priority level.

Definition at line 402 of file stk_strategy_fpriority.h.

403 {
404 IKernelTask *next = (*task->GetNext());
405
406 m_tasks[prio].Unlink(task);
407
408 // update pointer
409 if (next == task)
410 {
411 m_prev[prio] = nullptr;
412
413 // this will cause a switch to a lower priority task list
415 }
416 else if (m_prev[prio] == task)
417 {
418 // cursor was on the removed task: step back so that GetNext() returns its successor
419 m_prev[prio] = (*next->GetPrev());
420 }
421 else
422 {
423 // cursor is on another task: leave the rotation untouched
424 }
425 }
static __stk_forceinline void Clear(ReadyBitmap &bitmap, uint32_t bit)
IKernelTask * GetNext() override
Select and return the next task to run.

Referenced by stk::sched::SwitchStrategyFixedPriority< 32 >::OnTaskSleep(), stk::sched::SwitchStrategyFixedPriority< 32 >::OnTaskWeightChange(), and stk::sched::SwitchStrategyFixedPriority< 32 >::RemoveTask().

Here is the caller graph for this function:

◆ RemoveTask()

template<uint16_t MAX_PRIORITIES, bool NoSleepYield = false, class ReadyBitmap = typename ReadyBitmapSelect<MAX_PRIORITIES>::Type>
void stk::sched::SwitchStrategyFixedPriority< MAX_PRIORITIES, NoSleepYield, ReadyBitmap >::RemoveTask ( IKernelTask * task)
inlineoverridevirtual

Remove task from whichever list it currently occupies.

Parameters
[in]taskTask to remove. Must not be NULL and must belong to either m_tasks[priority] or m_sleep (asserted).
Note
Dispatch checks m_sleep first (reversed from the RR/SWRR pattern). If sleeping, simply unlinks from m_sleep. If runnable, delegates to RemoveActive() which also updates the cursor and clears the bitmap bit if the priority level becomes empty.

Implements stk::ITaskSwitchStrategy.

Definition at line 175 of file stk_strategy_fpriority.h.

176 {
177 STK_ASSERT(task != nullptr);
178 STK_ASSERT(GetSize() != 0U);
179 STK_ASSERT((task->GetHead() == &m_tasks[GetTaskPriority(task)]) || (task->GetHead() == &m_sleep));
180
181 if (task->GetHead() == &m_sleep)
182 {
183 m_sleep.Unlink(task);
184 }
185 else
186 {
188 }
189 }

◆ STK_NONCOPYABLE_CLASS()

template<uint16_t MAX_PRIORITIES, bool NoSleepYield = false, class ReadyBitmap = typename ReadyBitmapSelect<MAX_PRIORITIES>::Type>
stk::sched::SwitchStrategyFixedPriority< MAX_PRIORITIES, NoSleepYield, ReadyBitmap >::STK_NONCOPYABLE_CLASS ( SwitchStrategyFixedPriority< MAX_PRIORITIES, NoSleepYield, ReadyBitmap > )
private

Member Data Documentation

◆ m_prev

template<uint16_t MAX_PRIORITIES, bool NoSleepYield = false, class ReadyBitmap = typename ReadyBitmapSelect<MAX_PRIORITIES>::Type>
IKernelTask* stk::sched::SwitchStrategyFixedPriority< MAX_PRIORITIES, NoSleepYield, ReadyBitmap >::m_prev[MAX_PRIORITIES]
private

Per-priority round-robin cursor. m_prev[i] is the most recently scheduled task at level i, or nullptr when level i is empty. GetNext() advances from this position.

Definition at line 475 of file stk_strategy_fpriority.h.

◆ m_ready_bitmap

template<uint16_t MAX_PRIORITIES, bool NoSleepYield = false, class ReadyBitmap = typename ReadyBitmapSelect<MAX_PRIORITIES>::Type>
ReadyBitmap stk::sched::SwitchStrategyFixedPriority< MAX_PRIORITIES, NoSleepYield, ReadyBitmap >::m_ready_bitmap
private

Bitmask of non-empty priority levels: bit i is set when m_tasks[i] has at least one runnable task. Updated by AddActive() (bit set) and RemoveActive() (bit cleared on last removal).

Definition at line 474 of file stk_strategy_fpriority.h.

◆ m_sleep

template<uint16_t MAX_PRIORITIES, bool NoSleepYield = false, class ReadyBitmap = typename ReadyBitmapSelect<MAX_PRIORITIES>::Type>
IKernelTask::ListHeadType stk::sched::SwitchStrategyFixedPriority< MAX_PRIORITIES, NoSleepYield, ReadyBitmap >::m_sleep
private

Sleeping (blocked) tasks. Priority is retained in GetWeight() and restored on OnTaskWake().

Definition at line 473 of file stk_strategy_fpriority.h.

◆ m_tasks

template<uint16_t MAX_PRIORITIES, bool NoSleepYield = false, class ReadyBitmap = typename ReadyBitmapSelect<MAX_PRIORITIES>::Type>
IKernelTask::ListHeadType stk::sched::SwitchStrategyFixedPriority< MAX_PRIORITIES, NoSleepYield, ReadyBitmap >::m_tasks[MAX_PRIORITIES]
private

Per-priority runnable task lists. m_tasks[i] holds all runnable tasks at priority level i.

Definition at line 472 of file stk_strategy_fpriority.h.


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