![]() |
SuperTinyKernel™ RTOS 1.08.x
Lightweight, high-performance, deterministic, bare-metal C++ RTOS for resource-constrained embedded systems. MIT Open Source License.
|
Round-Robin task-switching strategy: each runnable task receives one time slice (one tick interval) in turn before the kernel moves to the next task. More...
#include <stk_strategy_rrobin.h>
Public Types | |
| enum | EConfig { WEIGHT_API = 0 , SLEEP_EVENT_API = 1 , DEADLINE_MISSED_API = 0 , PRIORITY_INHERITANCE_API = 0 } |
| Compile-time capability flags reported to the kernel. More... | |
Public Member Functions | |
| SwitchStrategyRoundRobin () | |
| Construct an empty strategy with no tasks and a null cursor. | |
| STK_VIRT_DTOR | ~SwitchStrategyRoundRobin ()=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 |
| Advance cursor and return the next runnable task. | |
| IKernelTask * | GetFirst () override |
| Get first task in the managed set (used by the kernel for initial scheduling). | |
| size_t | GetSize () const override |
| Get 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. | |
Protected Member Functions | |
| STK_NONCOPYABLE_CLASS (SwitchStrategyRoundRobin) | |
| void | AddActive (IKernelTask *task) |
Append a task to m_tasks and restore the cursor if necessary. | |
| void | RemoveActive (IKernelTask *task) |
Remove a task from m_tasks and update the cursor. | |
Protected Attributes | |
| IKernelTask::ListHeadType | m_tasks |
| Runnable tasks eligible for scheduling. | |
| IKernelTask::ListHeadType | m_sleep |
| Sleeping (blocked) tasks not eligible for scheduling. | |
| IKernelTask * | m_prev |
Iterator cursor: the most recently scheduled task, or nullptr when no runnable tasks exist. GetNext() advances from this position. | |
Round-Robin task-switching strategy: each runnable task receives one time slice (one tick interval) in turn before the kernel moves to the next task.
Internally maintains two intrusive lists:
m_tasks — tasks currently eligible for scheduling (runnable).m_sleep — tasks that called Sleep() or are otherwise blocked.The iterator cursor (m_prev) points to the most recently scheduled task. On each call to GetNext() the cursor advances by one position in m_tasks, wrapping around at the end (closed-loop list). When m_tasks is empty, GetNext() returns nullptr and the kernel transitions to the sleep trap.
Definition at line 41 of file stk_strategy_rrobin.h.
Compile-time capability flags reported to the kernel.
| Enumerator | |
|---|---|
| WEIGHT_API | This strategy does not use per-task weights; all tasks are treated equally. |
| SLEEP_EVENT_API | This strategy requires OnTaskSleep() / OnTaskWake() events to maintain the active/sleep list split. |
| 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 47 of file stk_strategy_rrobin.h.
|
inline |
Construct an empty strategy with no tasks and a null cursor.
Definition at line 57 of file stk_strategy_rrobin.h.
References m_prev, m_sleep, and m_tasks.
Referenced by STK_NONCOPYABLE_CLASS().
|
default |
|
inlineprotected |
Append a task to m_tasks and restore the cursor if necessary.
| [in] | task | Task to make runnable. |
m_prev is nullptr (all tasks were previously sleeping), it is set to the newly added task so GetNext() immediately returns a valid task on the next call rather than returning nullptr and causing a spurious sleep cycle. Definition at line 193 of file stk_strategy_rrobin.h.
References m_prev, and m_tasks.
Referenced by OnTaskWake().
|
inlineoverridevirtual |
Add task to the runnable set.
| [in] | task | Task to add. Must not be nullptr and must not already be in any list. |
m_tasks. m_prev was already pointing at the tail before insertion, it is advanced to the new tail so that GetNext() will return the new task on its next iteration rather than skipping it. Implements stk::ITaskSwitchStrategy.
Definition at line 72 of file stk_strategy_rrobin.h.
References stk::util::DListEntry< T, TClosedLoop >::GetHead(), m_prev, m_tasks, 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 138 of file stk_strategy_rrobin.h.
References GetSize(), m_sleep, m_tasks, and STK_ASSERT.
|
inlineoverridevirtual |
Advance cursor and return the next runnable task.
m_tasks after the cursor position, or nullptr if m_tasks is empty (no runnable tasks — kernel will sleep). m_prev) is updated to the returned task on each call. Because m_tasks is a closed-loop list the cursor wraps automatically from the last task back to the first, producing continuous round-robin rotation. nullptr (all tasks were sleeping and none have woken), the method returns nullptr immediately without touching m_prev. Implements stk::ITaskSwitchStrategy.
Definition at line 119 of file stk_strategy_rrobin.h.
References stk::util::DListEntry< T, TClosedLoop >::GetNext(), and m_prev.
|
inlineoverridevirtual |
Get total number of tasks managed by this strategy.
m_tasks (runnable) and m_sleep (sleeping). Implements stk::ITaskSwitchStrategy.
Definition at line 148 of file stk_strategy_rrobin.h.
References m_sleep, and m_tasks.
Referenced by GetFirst(), and RemoveTask().
|
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 1371 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 updates the cursor so GetNext() continues correctly. Implements stk::ITaskSwitchStrategy.
Definition at line 158 of file stk_strategy_rrobin.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_sleep to m_tasks via AddActive(), which also restores the cursor if it was null (i.e. this is the first runnable task after a period where all tasks were sleeping). Implements stk::ITaskSwitchStrategy.
Definition at line 174 of file stk_strategy_rrobin.h.
References AddActive(), stk::util::DListEntry< T, TClosedLoop >::GetHead(), stk::IKernelTask::IsSleeping(), m_sleep, 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 1389 of file stk_common.h.
References STK_UNUSED.
|
inlineprotected |
Remove a task from m_tasks and update the cursor.
| [in] | task | Runnable task to remove. |
next = task->GetNext() (the successor in the closed-loop list) before unlinking, while the list links are still valid.next != task (i.e. other tasks remain), set m_prev = next->GetPrev(). GetNext() will then advance from m_prev to next, preserving the round-robin sequence without skipping a task.next == task the removed task was the only element; set m_prev to nullptr so GetNext() returns nullptr and the kernel sleeps. Definition at line 217 of file stk_strategy_rrobin.h.
References stk::util::DListEntry< T, TClosedLoop >::GetNext(), stk::util::DListEntry< T, TClosedLoop >::GetPrev(), m_prev, and m_tasks.
Referenced by OnTaskSleep(), and RemoveTask().
|
inlineoverridevirtual |
Remove task from whichever list it currently occupies.
| [in] | task | Task to remove. Must not be nullptr and must belong to either m_tasks or m_sleep (asserted). |
m_tasks, delegates to RemoveActive() which also updates the cursor. If the task is in m_sleep, simply unlinks it. Implements stk::ITaskSwitchStrategy.
Definition at line 94 of file stk_strategy_rrobin.h.
References stk::util::DListEntry< T, TClosedLoop >::GetHead(), GetSize(), m_sleep, m_tasks, RemoveActive(), and STK_ASSERT.
|
protected |
|
protected |
Iterator cursor: the most recently scheduled task, or nullptr when no runnable tasks exist. GetNext() advances from this position.
Definition at line 238 of file stk_strategy_rrobin.h.
Referenced by AddActive(), AddTask(), GetNext(), RemoveActive(), and SwitchStrategyRoundRobin().
|
protected |
Sleeping (blocked) tasks not eligible for scheduling.
Definition at line 237 of file stk_strategy_rrobin.h.
Referenced by GetFirst(), GetSize(), OnTaskSleep(), OnTaskWake(), RemoveTask(), and SwitchStrategyRoundRobin().
|
protected |
Runnable tasks eligible for scheduling.
Definition at line 236 of file stk_strategy_rrobin.h.
Referenced by AddActive(), AddTask(), GetFirst(), GetSize(), OnTaskSleep(), RemoveActive(), RemoveTask(), and SwitchStrategyRoundRobin().