![]() |
SuperTinyKernel™ RTOS 1.06.x
Lightweight, high-performance, deterministic, bare-metal C++ RTOS for resource-constrained embedded systems. MIT Open Source License.
|
Smooth Weighted Round-Robin (SWRR) task-switching strategy: distributes CPU time proportionally to per-task weights while avoiding execution bursts by spreading selections evenly over time. More...
#include <stk_strategy_swrrobin.h>
Public Types | |
| enum | EConfig { WEIGHT_API = 1 , SLEEP_EVENT_API = 1 , DEADLINE_MISSED_API = 0 , PRIORITY_INHERITANCE_API = 0 } |
| Compile-time capability flags reported to the kernel. More... | |
Public Member Functions | |
| SwitchStrategySmoothWeightedRoundRobin () | |
| Construct an empty strategy with no tasks and a zero total weight. | |
| STK_VIRT_DTOR | ~SwitchStrategySmoothWeightedRoundRobin ()=default |
| Destructor. | |
| void | AddTask (IKernelTask *task) override |
| Add task to the runnable set. | |
| void | RemoveTask (IKernelTask *task) override |
| Remove task from whichever list it currently occupies. | |
| IKernelTask * | GetNext () override |
| Select and return the next task to run, applying one step of the SWRR algorithm. | |
| IKernelTask * | GetFirst () override |
| Get 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. | |
| virtual bool | OnTaskDeadlineMissed (IKernelTask *task) |
| Notification that a task has exceeded its HRT deadline; returns whether the strategy can recover without a hard fault. | |
| virtual void | OnTaskWeightChange (IKernelTask *task, Weight old_weight) |
| Notification that a runnable task's scheduling weight has changed. | |
Private Member Functions | |
| STK_NONCOPYABLE_CLASS (SwitchStrategySmoothWeightedRoundRobin) | |
| void | AddActive (IKernelTask *task) |
Append task to m_tasks and update the total weight. | |
| void | RemoveActive (IKernelTask *task) |
Remove task from m_tasks and update the total weight. | |
Private Attributes | |
| IKernelTask::ListHeadType | m_tasks |
| Runnable tasks eligible for scheduling. | |
| IKernelTask::ListHeadType | m_sleep |
| Sleeping (blocked) tasks not eligible for scheduling. | |
| int32_t | m_total_weight |
Sum of static weights (GetWeight()) of all tasks currently in m_tasks. Sleeping tasks are excluded. Updated on every AddActive() / RemoveActive() call. Used as the post-selection deduction amount in GetNext() and as the wake-up boost value in OnTaskWake(). | |
Smooth Weighted Round-Robin (SWRR) task-switching strategy: distributes CPU time proportionally to per-task weights while avoiding execution bursts by spreading selections evenly over time.
This is an improved variant of standard Weighted Round-Robin.
On every call to GetNext() (once per kernel tick):
Over time this produces fair, proportional CPU distribution without consecutive bursts.
m_total_weight consistent as tasks enter and leave the runnable set. Definition at line 62 of file stk_strategy_swrrobin.h.
Compile-time capability flags reported to the kernel.
| Enumerator | |
|---|---|
| WEIGHT_API | This strategy uses per-task static and dynamic weights; the kernel must expose the Weight API on each IKernelTask. |
| SLEEP_EVENT_API | This strategy requires OnTaskSleep() / OnTaskWake() events to keep |
| DEADLINE_MISSED_API | This strategy does not use OnTaskDeadlineMissed() events. |
| PRIORITY_INHERITANCE_API | This strategy does not require Priority Inheritance and OnTaskPriorityChange() events. |
Definition at line 68 of file stk_strategy_swrrobin.h.
|
inlineexplicit |
Construct an empty strategy with no tasks and a zero total weight.
Definition at line 78 of file stk_strategy_swrrobin.h.
References m_sleep, m_tasks, and m_total_weight.
Referenced by STK_NONCOPYABLE_CLASS().
|
default |
|
inlineprivate |
Append task to m_tasks and update the total weight.
| [in] | task | Task to make runnable. |
m_total_weight by the task's static weight so that the post-selection deduction in GetNext() remains correct. Definition at line 246 of file stk_strategy_swrrobin.h.
References stk::IKernelTask::GetWeight(), m_tasks, and m_total_weight.
Referenced by AddTask(), and OnTaskWake().
|
inlineoverridevirtual |
Add task to the runnable set.
| [in] | task | Task to add. Must not be nullptr. |
m_total_weight. Implements stk::ITaskSwitchStrategy.
Definition at line 97 of file stk_strategy_swrrobin.h.
References AddActive(), stk::IKernelTask::GetWeight(), stk::NO_WEIGHT, stk::IKernelTask::SetCurrentWeight(), and STK_ASSERT.
|
inlineoverridevirtual |
Get first task in the managed set (used by the kernel for initial scheduling).
m_tasks if any task is runnable, otherwise the first task in m_sleep. Asserts if the combined set is empty (GetSize() == 0). Implements stk::ITaskSwitchStrategy.
Definition at line 180 of file stk_strategy_swrrobin.h.
References GetSize(), m_sleep, m_tasks, and STK_ASSERT.
|
inlineoverridevirtual |
Select and return the next task to run, applying one step of the SWRR algorithm.
NULL if m_tasks is empty (no runnable tasks - kernel will sleep). m_tasks: current_weight += static_weight.m_tasks is non-empty but no task was picked (should never occur). Implements stk::ITaskSwitchStrategy.
Definition at line 142 of file stk_strategy_swrrobin.h.
References stk::IKernelTask::GetCurrentWeight(), stk::util::DListEntry< T, TClosedLoop >::GetNext(), stk::IKernelTask::GetWeight(), m_tasks, m_total_weight, stk::IKernelTask::SetCurrentWeight(), and STK_ASSERT.
|
inlineoverridevirtual |
Get the total number of tasks managed by this strategy.
m_tasks (runnable) and m_sleep (sleeping). Implements stk::ITaskSwitchStrategy.
Definition at line 190 of file stk_strategy_swrrobin.h.
References m_sleep, and m_tasks.
Referenced by GetFirst().
|
inlinevirtualinherited |
Notification that a task has exceeded its HRT deadline; returns whether the strategy can recover without a hard fault.
| [in] | task | The task whose deadline was missed. Must not be nullptr. |
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. 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(). 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." false (unrecoverable), which is the correct default for strategies that do not implement overrun recovery. Definition at line 1167 of file stk_common.h.
References STK_UNUSED.
|
inlineoverridevirtual |
Notification that a task has entered the sleeping state.
| [in] | task | The task that is now sleeping. Must be in m_tasks (asserted). |
m_tasks to m_sleep via RemoveActive(), which also decrements m_total_weight by the task's static weight. Sleeping tasks do not participate in weight distribution until they wake. Implements stk::ITaskSwitchStrategy.
Definition at line 201 of file stk_strategy_swrrobin.h.
References stk::util::DListEntry< T, TClosedLoop >::GetHead(), stk::IKernelTask::IsSleeping(), m_sleep, m_tasks, RemoveActive(), and STK_ASSERT.
|
inlineoverridevirtual |
Notification that a task has become runnable again.
| [in] | task | The task that woke up. Must be in m_sleep (asserted). |
m_tasks: the task's current weight is set to m_total_weight (the sum of all runnable tasks' static weights). On the next GetNext() call every runnable task increases its current weight by its static weight, but the waking task starts from m_total_weight, giving it the highest initial value and guaranteeing it is selected first. This prevents starvation of tasks that had been blocking on I/O or synchronization objects, and mimics the fairness behaviour of plain Round-Robin for equal-weight tasks. m_total_weight. Implements stk::ITaskSwitchStrategy.
Definition at line 223 of file stk_strategy_swrrobin.h.
References AddActive(), stk::util::DListEntry< T, TClosedLoop >::GetHead(), stk::IKernelTask::IsSleeping(), m_sleep, m_total_weight, stk::IKernelTask::SetCurrentWeight(), and STK_ASSERT.
|
inlinevirtualinherited |
Notification that a runnable task's scheduling weight has changed.
| [in] | task | The task whose weight was just updated via SetWeight(). |
| [in] | old_weight | The previous weight of this task (required to remove it from the priority list belonging to that weight). |
Reimplemented in stk::SwitchStrategyFixedPriority< MAX_PRIORITIES >, and stk::SwitchStrategyFixedPriority< 32 >.
Definition at line 1185 of file stk_common.h.
References STK_UNUSED.
|
inlineprivate |
Remove task from m_tasks and update the total weight.
| [in] | task | Runnable task to remove. |
m_total_weight by the task's static weight so that the post-selection deduction in GetNext() and the wake-up boost in OnTaskWake() remain proportionally correct for the remaining tasks. Definition at line 258 of file stk_strategy_swrrobin.h.
References stk::IKernelTask::GetWeight(), m_tasks, and m_total_weight.
Referenced by OnTaskSleep(), and RemoveTask().
|
inlineoverridevirtual |
Remove task from whichever list it currently occupies.
| [in] | task | Task to remove. Must not be NULL and must belong to either m_tasks or m_sleep (asserted). |
m_tasks, delegates to RemoveActive() which also decrements m_total_weight. If the task is in m_sleep, simply unlinks it (m_total_weight is not adjusted since sleeping tasks are already excluded from the weight sum). Implements stk::ITaskSwitchStrategy.
Definition at line 115 of file stk_strategy_swrrobin.h.
References stk::util::DListEntry< T, TClosedLoop >::GetHead(), m_sleep, m_tasks, RemoveActive(), and STK_ASSERT.
|
private |
|
private |
Sleeping (blocked) tasks not eligible for scheduling.
Definition at line 265 of file stk_strategy_swrrobin.h.
Referenced by GetFirst(), GetSize(), OnTaskSleep(), OnTaskWake(), RemoveTask(), and SwitchStrategySmoothWeightedRoundRobin().
|
private |
Runnable tasks eligible for scheduling.
Definition at line 264 of file stk_strategy_swrrobin.h.
Referenced by AddActive(), GetFirst(), GetNext(), GetSize(), OnTaskSleep(), RemoveActive(), RemoveTask(), and SwitchStrategySmoothWeightedRoundRobin().
|
private |
Sum of static weights (GetWeight()) of all tasks currently in m_tasks. Sleeping tasks are excluded. Updated on every AddActive() / RemoveActive() call. Used as the post-selection deduction amount in GetNext() and as the wake-up boost value in OnTaskWake().
Definition at line 266 of file stk_strategy_swrrobin.h.
Referenced by AddActive(), GetNext(), OnTaskWake(), RemoveActive(), and SwitchStrategySmoothWeightedRoundRobin().