![]() |
SuperTinyKernel™ RTOS 1.05.3
Lightweight, high-performance, deterministic, bare-metal C++ RTOS for resource-constrained embedded systems. MIT Open Source License.
|
Fixed-priority preemptive scheduling strategy with round-robin arbitration within each priority level. More...
#include <stk_strategy_fpriority.h>
Public Types | |
| enum | EConfig { WEIGHT_API = 1 , SLEEP_EVENT_API = 1 , DEADLINE_MISSED_API = 0 } |
| Compile-time capability flags reported to the kernel. More... | |
| enum | EPriority { PRIORITY_HIGHEST = MAX_PRIORITIES - 1 , PRIORITY_NORMAL = MAX_PRIORITIES / 2 , 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. | |
| ~SwitchStrategyFixedPriority () | |
| Destructor. | |
| void | AddTask (IKernelTask *task) |
| Add task to the runnable set at its fixed priority level. | |
| void | RemoveTask (IKernelTask *task) |
| Remove task from whichever list it currently occupies. | |
| IKernelTask * | GetNext () |
| Select and return the next task to run. | |
| IKernelTask * | GetFirst () const |
| Get the first task in the managed set (used by the kernel for initial scheduling). | |
| size_t | GetSize () const |
| Get the total number of tasks managed by this strategy. | |
| void | OnTaskSleep (IKernelTask *task) |
| Notification that a task has entered the sleeping state. | |
| void | OnTaskWake (IKernelTask *task) |
| Notification that a task has become runnable again. | |
| bool | OnTaskDeadlineMissed (IKernelTask *) |
| Not supported, asserts unconditionally. | |
Protected Types | |
| typedef uint8_t | Priority |
| Priority 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) |
| Remove a task from its priority level's runnable list and update the bitmap/cursor. | |
Static Protected Member Functions | |
| static __stk_forceinline Priority | GetTaskPriority (IKernelTask *task) |
| Get priority from the task.. | |
| static __stk_forceinline Priority | GetHighestReadyPriority (uint32_t 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(). | |
| uint32_t | 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. | |
Fixed-priority preemptive scheduling strategy with round-robin arbitration within each priority level.
| MAX_PRIORITIES | Number of distinct priority levels. Must be in the range [1, 32] (enforced by a compile-time assertion). Bit i of the 32-bit m_ready_bitmap represents priority level i. The concrete alias SwitchStrategyFP32 uses MAX_PRIORITIES = 32. |
ITask::GetWeight(), cast to uint8_t, and must be in the range [0, MAX_PRIORITIES - 1]. Tasks do not use the dynamic current-weight mechanism, the static weight value is the priority. Configure a task's priority by overriding ITask::GetWeight() to return the desired level.N will never run while any task at priority > N is runnable.m_prev[prio]).m_ready_bitmap is a 32-bit bitmask where bit i is set whenever priority level i has at least one runnable task. GetNext() and GetFirst() locate the highest set bit in O(1) via __builtin_clz (GCC/Clang) or a portable 32->0 scan fallback. The bitmap is kept consistent by AddActive() (bit set) and RemoveActive() (bit cleared on last removal).GetWeight() is interpreted as the task's fixed priority level. The dynamic weight functions (SetCurrentWeight / GetCurrentWeight) are not used by this strategy. m_ready_bitmap accurate. Definition at line 58 of file stk_strategy_fpriority.h.
|
protected |
Priority type.
Definition at line 250 of file stk_strategy_fpriority.h.
| enum stk::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 |
| DEADLINE_MISSED_API | This strategy does not use OnTaskDeadlineMissed() events. |
Definition at line 64 of file stk_strategy_fpriority.h.
| enum stk::SwitchStrategyFixedPriority::EPriority |
Symbolic priority level constants for common use cases.
Definition at line 76 of file stk_strategy_fpriority.h.
|
inlineexplicit |
Construct an empty strategy with all priority levels empty, a clear bitmap, and null cursors.
m_ready_bitmap. Instantiating with MAX_PRIORITIES > 32 is a compile error. Definition at line 87 of file stk_strategy_fpriority.h.
|
inline |
Destructor.
Definition at line 96 of file stk_strategy_fpriority.h.
|
inlineprotected |
Append a task to its priority level's runnable list and update the bitmap.
| [in] | task | Task to make runnable. |
m_tasks[prio]. m_ready_bitmap bit prio is set and m_prev[prio] is initialised 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 261 of file stk_strategy_fpriority.h.
Referenced by stk::SwitchStrategyFixedPriority< 32 >::AddTask(), and stk::SwitchStrategyFixedPriority< 32 >::OnTaskWake().
|
inlinevirtual |
Add task to the runnable set at its fixed priority level.
| [in] | task | Task to add. Must not be NULL and must not already be in any list. |
GetWeight() to uint8_t. The result must be less than MAX_PRIORITIES (asserted); passing a task whose GetWeight() is out of range is a programming error. m_tasks[prio], sets m_ready_bitmap bit prio if this is the first task at that level, and initialises m_prev[prio]. 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 111 of file stk_strategy_fpriority.h.
|
inlinevirtual |
Get the first task in the managed set (used by the kernel for initial scheduling).
m_sleep. Asserts if the combined set is empty (GetSize() == 0). 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 178 of file stk_strategy_fpriority.h.
Referenced by stk::SwitchStrategyFixedPriority< 32 >::GetFirst(), and stk::test::TEST().
|
inlinestaticprotected |
Find the index of the highest set bit in bitmap.
| [in] | bitmap | Bitmask of ready priority levels. Must not be 0 (behaviour is undefined for __builtin_clz(0); callers must check before calling). |
__builtin_clz for an O(1) hardware instruction (BSR on x86, CLZ on ARM). On other compilers: falls back to a portable O(32) linear scan from bit 31 down to 0. Definition at line 327 of file stk_strategy_fpriority.h.
Referenced by stk::SwitchStrategyFixedPriority< 32 >::GetFirst(), and stk::SwitchStrategyFixedPriority< 32 >::GetNext().
|
inlinevirtual |
Select and return the next task to run.
nullptr if no runnable tasks exist (m_ready_bitmap == 0, kernel sleeps). 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. Implements stk::ITaskSwitchStrategy.
Definition at line 157 of file stk_strategy_fpriority.h.
|
inlinevirtual |
Get the total number of tasks managed by this strategy.
m_tasks plus tasks in m_sleep. Implements stk::ITaskSwitchStrategy.
Definition at line 194 of file stk_strategy_fpriority.h.
Referenced by stk::SwitchStrategyFixedPriority< 32 >::AddActive(), stk::SwitchStrategyFixedPriority< 32 >::GetFirst(), stk::SwitchStrategyFixedPriority< 32 >::GetSize(), and stk::SwitchStrategyFixedPriority< 32 >::RemoveTask().
|
inlinestaticprotected |
Get priority from the task..
| [in] | task | Pointer to the task. Priority is read from GetWeight(). |
Definition at line 313 of file stk_strategy_fpriority.h.
Referenced by stk::SwitchStrategyFixedPriority< 32 >::AddActive(), stk::SwitchStrategyFixedPriority< 32 >::AddTask(), and stk::SwitchStrategyFixedPriority< 32 >::RemoveActive().
|
inlinevirtual |
Not supported, asserts unconditionally.
Implements stk::ITaskSwitchStrategy.
Definition at line 241 of file stk_strategy_fpriority.h.
|
inlinevirtual |
Notification that a task has entered the sleeping state.
| [in] | task | The task that is now sleeping. Must be in m_tasks[priority] (asserted). |
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 209 of file stk_strategy_fpriority.h.
|
inlinevirtual |
Notification that a task has become runnable again.
| [in] | task | The task that woke up. Must be in m_sleep (asserted). |
m_sleep and delegates to AddActive() which appends it to m_tasks[priority] and sets the bitmap bit if the level was empty. Implements stk::ITaskSwitchStrategy.
Definition at line 228 of file stk_strategy_fpriority.h.
|
inlineprotected |
Remove a task from its priority level's runnable list and update the bitmap/cursor.
| [in] | task | Runnable task to remove. |
next = task->GetNext() before unlinking.next != task (other tasks remain at this level): set m_prev[prio] = next->GetPrev() so GetNext() returns next without skipping it.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 288 of file stk_strategy_fpriority.h.
Referenced by stk::SwitchStrategyFixedPriority< 32 >::OnTaskSleep(), and stk::SwitchStrategyFixedPriority< 32 >::RemoveTask().
|
inlinevirtual |
Remove task from whichever list it currently occupies.
| [in] | task | Task to remove. Must not be NULL and must belong to either m_tasks[priority] or m_sleep (asserted). |
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 136 of file stk_strategy_fpriority.h.
|
private |
|
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 347 of file stk_strategy_fpriority.h.
|
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 346 of file stk_strategy_fpriority.h.
|
private |
Sleeping (blocked) tasks. Priority is retained in GetWeight() and restored on OnTaskWake().
Definition at line 345 of file stk_strategy_fpriority.h.
|
private |
Per-priority runnable task lists. m_tasks[i] holds all runnable tasks at priority level i.
Definition at line 344 of file stk_strategy_fpriority.h.