|
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 , NOSLEEP_YIELD_API = NoSleepYield , TIMESLICE_API = 0 } |
| Compile-time capability flags reported to the kernel. More... | |
Public Member Functions | |
| SwitchStrategyRoundRobinT () | |
| Construct an empty strategy with no tasks and a null cursor. | |
| STK_VIRT_DTOR | ~SwitchStrategyRoundRobinT ()=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 (reference task for the kernel, used when it starts and when all tasks sleep). | |
| 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. | |
| 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. | |
Protected Member Functions | |
| STK_NONCOPYABLE_CLASS (SwitchStrategyRoundRobinT) | |
| 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.
| NoSleepYield | If true, Yield() keeps the task in the runnable set and the strategy rotates the cursor (see OnTaskYield). If false, the kernel performs the legacy yield: the task sleeps for YIELD_TICKS. |
Definition at line 47 of file stk_strategy_rrobin.h.
| enum stk::sched::SwitchStrategyRoundRobinT::EConfig |
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 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 53 of file stk_strategy_rrobin.h.
|
inline |
Construct an empty strategy with no tasks and a null cursor.
Definition at line 65 of file stk_strategy_rrobin.h.
|
default |
Destructor.
|
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 231 of file stk_strategy_rrobin.h.
Referenced by stk::sched::SwitchStrategyRoundRobinT< false >::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 pointing at the tail before insertion (or the list was empty), it is moved to the new tail. GetNext() then continues with the head and the new task waits for its turn at the end of the current rotation. Implements stk::ITaskSwitchStrategy.
Definition at line 80 of file stk_strategy_rrobin.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 146 of file stk_strategy_rrobin.h.
|
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 127 of file stk_strategy_rrobin.h.
|
inlineoverridevirtual |
Get total number of tasks managed by this strategy.
m_tasks (runnable) and m_sleep (sleeping). Implements stk::ITaskSwitchStrategy.
Definition at line 156 of file stk_strategy_rrobin.h.
Referenced by stk::sched::SwitchStrategyRoundRobinT< false >::GetFirst(), and stk::sched::SwitchStrategyRoundRobinT< false >::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 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 updates the cursor so GetNext() continues correctly. Implements stk::ITaskSwitchStrategy.
Definition at line 166 of file stk_strategy_rrobin.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_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 182 of file stk_strategy_rrobin.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 strategy must arrange for the following GetNext() to return another task according to its own policy; false (default) if the kernel shall perform the legacy yield, i.e. sleep the task for YIELD_TICKS. Reimplemented from stk::ITaskSwitchStrategy.
Definition at line 199 of file stk_strategy_rrobin.h.
|
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) and the cursor was on the removed task, 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 259 of file stk_strategy_rrobin.h.
Referenced by stk::sched::SwitchStrategyRoundRobinT< false >::OnTaskSleep(), and stk::sched::SwitchStrategyRoundRobinT< false >::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 102 of file stk_strategy_rrobin.h.
|
protected |
|
protected |
Iterator cursor: the most recently scheduled task, or nullptr when no runnable tasks exist. GetNext() advances from this position.
Definition at line 284 of file stk_strategy_rrobin.h.
|
protected |
Sleeping (blocked) tasks not eligible for scheduling.
Definition at line 283 of file stk_strategy_rrobin.h.
|
protected |
Runnable tasks eligible for scheduling.
Definition at line 282 of file stk_strategy_rrobin.h.