![]() |
SuperTinyKernel™ RTOS 1.05.3
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 } |
| Compile-time capability flags reported to the kernel. More... | |
Public Member Functions | |
| SwitchStrategyRoundRobin () | |
| Construct an empty strategy with no tasks and a null cursor. | |
| ~SwitchStrategyRoundRobin () | |
| Destructor. | |
| void | AddTask (IKernelTask *task) |
| Add task to the runnable set. | |
| void | RemoveTask (IKernelTask *task) |
| Remove task from whichever list it currently occupies. | |
| IKernelTask * | GetNext () |
| Advance cursor and return the next runnable task. | |
| IKernelTask * | GetFirst () const |
| Get first task in the managed set (used by the kernel for initial scheduling). | |
| size_t | GetSize () const |
| Get 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 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. |
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 56 of file stk_strategy_rrobin.h.
References m_prev, m_sleep, and m_tasks.
Referenced by STK_NONCOPYABLE_CLASS().
|
inline |
Destructor.
Definition at line 62 of file stk_strategy_rrobin.h.
|
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 200 of file stk_strategy_rrobin.h.
References m_prev, and m_tasks.
Referenced by OnTaskWake().
|
inlinevirtual |
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, _ClosedLoop >::GetHead(), m_prev, m_tasks, and STK_ASSERT.
|
inlinevirtual |
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 132 of file stk_strategy_rrobin.h.
References GetSize(), m_sleep, m_tasks, and STK_ASSERT.
Referenced by stk::test::TEST().
|
inlinevirtual |
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 113 of file stk_strategy_rrobin.h.
References stk::util::DListEntry< T, _ClosedLoop >::GetNext(), and m_prev.
|
inlinevirtual |
Get total number of tasks managed by this strategy.
m_tasks (runnable) and m_sleep (sleeping). Implements stk::ITaskSwitchStrategy.
Definition at line 145 of file stk_strategy_rrobin.h.
References m_sleep, and m_tasks.
Referenced by GetFirst(), and RemoveTask().
|
inlinevirtual |
Not supported, asserts unconditionally.
Implements stk::ITaskSwitchStrategy.
Definition at line 184 of file stk_strategy_rrobin.h.
References STK_ASSERT.
|
inlinevirtual |
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 155 of file stk_strategy_rrobin.h.
References stk::util::DListEntry< T, _ClosedLoop >::GetHead(), stk::IKernelTask::IsSleeping(), m_sleep, m_tasks, RemoveActive(), and STK_ASSERT.
|
inlinevirtual |
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 171 of file stk_strategy_rrobin.h.
References AddActive(), stk::util::DListEntry< T, _ClosedLoop >::GetHead(), stk::IKernelTask::IsSleeping(), m_sleep, and STK_ASSERT.
|
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 222 of file stk_strategy_rrobin.h.
References stk::util::DListEntry< T, _ClosedLoop >::GetNext(), stk::util::DListEntry< T, _ClosedLoop >::GetPrev(), m_prev, and m_tasks.
Referenced by OnTaskSleep(), and RemoveTask().
|
inlinevirtual |
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 92 of file stk_strategy_rrobin.h.
References stk::util::DListEntry< T, _ClosedLoop >::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 239 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 238 of file stk_strategy_rrobin.h.
Referenced by GetFirst(), GetSize(), OnTaskSleep(), OnTaskWake(), RemoveTask(), and SwitchStrategyRoundRobin().
|
protected |
Runnable tasks eligible for scheduling.
Definition at line 237 of file stk_strategy_rrobin.h.
Referenced by AddActive(), AddTask(), GetFirst(), GetSize(), OnTaskSleep(), RemoveActive(), RemoveTask(), and SwitchStrategyRoundRobin().