|
SuperTinyKernel™ RTOS 1.08.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 , NOSLEEP_YIELD_API = NoSleepYield , TIMESLICE_API = 0 } |
| Compile-time capability flags reported to the kernel. More... | |
Public Member Functions | |
| SwitchStrategySmoothWeightedRoundRobinT () | |
| Construct an empty strategy with no tasks and a zero total weight. | |
| STK_VIRT_DTOR | ~SwitchStrategySmoothWeightedRoundRobinT ()=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 (reference task for the kernel, used when it starts and when all tasks sleep). | |
| 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. | |
| 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. | |
| virtual void | OnTaskWeightChange (IKernelTask *task, Weight old_weight) |
| Notification that a runnable task's scheduling weight has changed. | |
Private Member Functions | |
| STK_NONCOPYABLE_CLASS (SwitchStrategySmoothWeightedRoundRobinT) | |
| 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. | |
| IKernelTask * | m_yielded [STK_ALLOCATE_COUNT< NoSleepYield, 1U, 1U, 0U >::Value] |
| Task that just yielded, skipped by the next GetNext() only. Zero-size without NoSleepYield on GCC/Clang, one entry on MSVC/IAR. | |
| 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.
NoSleepYield is true, Yield() keeps the calling task runnable (it is NOT put to sleep). OnTaskYield() remembers the yielding task and the next GetNext() excludes it from the selection (one-shot). The yielding task still accrues its static weight in that step, so the proportional accounting is preserved, but it cannot win the selection unless it is the only runnable task. If NoSleepYield is false the kernel performs the legacy yield: the task sleeps for YIELD_TICKS.| NoSleepYield | If true, Yield() keeps the task in the runnable set and the strategy skips the yielding task in the next selection (see OnTaskYield). If false, the kernel performs the legacy yield: the task sleeps for YIELD_TICKS. |
m_total_weight consistent as tasks enter and leave the runnable set. Definition at line 76 of file stk_strategy_swrrobin.h.
| enum stk::sched::SwitchStrategySmoothWeightedRoundRobinT::EConfig |
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 OnTaskWeightChange() events. |
| NOSLEEP_YIELD_API | (1) enables OnTaskYield(): yielding task stays runnable instead of sleeping. |
| TIMESLICE_API | This strategy does not support KERNEL_TIMESLICE mode. |
Definition at line 82 of file stk_strategy_swrrobin.h.
|
inlineexplicit |
Construct an empty strategy with no tasks and a zero total weight.
Definition at line 94 of file stk_strategy_swrrobin.h.
|
default |
Destructor.
|
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 332 of file stk_strategy_swrrobin.h.
Referenced by stk::sched::SwitchStrategySmoothWeightedRoundRobinT< false >::AddTask(), and stk::sched::SwitchStrategySmoothWeightedRoundRobinT< false >::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 112 of file stk_strategy_swrrobin.h.
|
inlineoverridevirtual |
Get first task in the managed set (reference task for the kernel, used when it starts and when all tasks sleep).
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 234 of file stk_strategy_swrrobin.h.
|
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.NoSleepYield the task recorded by OnTaskYield() is excluded from step 2 (one-shot, it still receives the step 1 increment). If it is the only runnable task it is selected anyway. m_tasks is non-empty but no task was picked (should never occur). Implements stk::ITaskSwitchStrategy.
Definition at line 160 of file stk_strategy_swrrobin.h.
|
inlineoverridevirtual |
Get the total number of tasks managed by this strategy.
m_tasks (runnable) and m_sleep (sleeping). Implements stk::ITaskSwitchStrategy.
Definition at line 244 of file stk_strategy_swrrobin.h.
Referenced by stk::sched::SwitchStrategySmoothWeightedRoundRobinT< false >::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 1501 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 255 of file stk_strategy_swrrobin.h.
|
inlinevirtualinherited |
Notification that the running task's time slice expired (KERNEL_TIMESLICE only).
| [in] | task | Pointer to the running task, currently in the runnable set. |
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). TIMESLICE_API == 1. 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.
References STK_UNUSED.
|
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 277 of file stk_strategy_swrrobin.h.
|
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::sched::SwitchStrategyFixedPriority< MAX_PRIORITIES, NoSleepYield, ReadyBitmap >, stk::sched::SwitchStrategyFixedPriority< 32 >, stk::sched::SwitchStrategyFixedPriority< 32 >, stk::sched::SwitchStrategyFixedPriority< 32, true >, stk::sched::SwitchStrategyFixedPriority< 32, true >, stk::sched::SwitchStrategyFixedPriority< 64 >, stk::sched::SwitchStrategyFixedPriority< 64 >, stk::sched::SwitchStrategyFixedPriority< 64, true >, stk::sched::SwitchStrategyFixedPriority< 64, true >, 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 >, stk::SwitchStrategyFixedPriority< MAX_PRIORITIES, NoSleepYield, ReadyBitmap >, and stk::SwitchStrategyPreemptionThreshold< MAX_PRIORITIES, TIMESLICE, ReadyBitmap >.
Definition at line 1518 of file stk_common.h.
References STK_UNUSED.
|
inlineoverridevirtual |
Notification that the running task called Yield() (non-HRT kernel modes only).
| [in] | task | Pointer to the yielding task, currently in the runnable set. |
true if the strategy handled the yield itself: the task stays runnable (it is NOT put to sleep) and the following GetNext() will not select it (unless it is the only runnable task); false if the kernel shall perform the legacy yield, i.e. sleep the task for YIELD_TICKS. Reimplemented from stk::ITaskSwitchStrategy.
Definition at line 301 of file stk_strategy_swrrobin.h.
|
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. With NoSleepYield also drops a pending yield exclusion of this task. Definition at line 345 of file stk_strategy_swrrobin.h.
Referenced by stk::sched::SwitchStrategySmoothWeightedRoundRobinT< false >::OnTaskSleep(), and stk::sched::SwitchStrategySmoothWeightedRoundRobinT< false >::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 130 of file stk_strategy_swrrobin.h.
|
private |
|
private |
Sleeping (blocked) tasks not eligible for scheduling.
Definition at line 361 of file stk_strategy_swrrobin.h.
|
private |
Runnable tasks eligible for scheduling.
Definition at line 360 of file stk_strategy_swrrobin.h.
|
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 363 of file stk_strategy_swrrobin.h.
|
private |
Task that just yielded, skipped by the next GetNext() only. Zero-size without NoSleepYield on GCC/Clang, one entry on MSVC/IAR.
Definition at line 362 of file stk_strategy_swrrobin.h.