SuperTinyKernel™ RTOS 1.05.3
Lightweight, high-performance, deterministic, bare-metal C++ RTOS for resource-constrained embedded systems. MIT Open Source License.
Loading...
Searching...
No Matches
stk_strategy_rrobin.h
Go to the documentation of this file.
1/*
2 * SuperTinyKernel™ (STK): Lightweight High-Performance Deterministic C++ RTOS for Embedded Systems.
3 *
4 * Source: https://github.com/SuperTinyKernel-RTOS
5 *
6 * Copyright (c) 2022-2026 Neutron Code Limited <stk@neutroncode.com>. All Rights Reserved.
7 * License: MIT License, see LICENSE for a full text.
8 */
9
10#ifndef STK_STRATEGY_RROBIN_H_
11#define STK_STRATEGY_RROBIN_H_
12
16
17#include "stk_common.h"
18
19namespace stk {
20
42{
43public:
53
58
64
72 void AddTask(IKernelTask *task)
73 {
74 STK_ASSERT(task != nullptr);
75 STK_ASSERT(task->GetHead() == nullptr);
76
77 bool tail = (m_prev == m_tasks.GetLast());
78
79 m_tasks.LinkBack(task);
80
81 // if pointer was pointing to the tail, become a tail
82 if (tail)
83 m_prev = task;
84 }
85
93 {
94 STK_ASSERT(task != nullptr);
95 STK_ASSERT(GetSize() != 0);
96 STK_ASSERT((task->GetHead() == &m_tasks) || (task->GetHead() == &m_sleep));
97
98 if (task->GetHead() == &m_tasks)
99 RemoveActive(task);
100 else
101 m_sleep.Unlink(task);
102 }
103
114 {
115 IKernelTask *ret = m_prev;
116
117 if (ret != nullptr)
118 {
119 ret = (*ret->GetNext());
120 m_prev = ret;
121 }
122
123 return ret;
124 }
125
133 {
134 STK_ASSERT(GetSize() != 0U);
135
136 if (!m_tasks.IsEmpty())
137 return (*m_tasks.GetFirst());
138 else
139 return (*m_sleep.GetFirst());
140 }
141
145 size_t GetSize() const
146 {
147 return m_tasks.GetSize() + m_sleep.GetSize();
148 }
149
156 {
157 STK_ASSERT(task != nullptr);
158 STK_ASSERT(task->IsSleeping());
159 STK_ASSERT(task->GetHead() == &m_tasks);
160
161 RemoveActive(task);
162 m_sleep.LinkBack(task);
163 }
164
172 {
173 STK_ASSERT(task != nullptr);
174 STK_ASSERT(!task->IsSleeping());
175 STK_ASSERT(task->GetHead() == &m_sleep);
176
177 m_sleep.Unlink(task);
178 AddActive(task);
179 }
180
185 {
186 // Budget Overrun API unsupported
187 STK_ASSERT(false);
188 return false;
189 }
190
191protected:
193
201 {
202 m_tasks.LinkBack(task);
203
204 // update pointer: if all tasks were sleeping, this task will change state
205 // of the kernel to active
206 if (m_prev == nullptr)
207 m_prev = task;
208 }
209
223 {
224 IKernelTask *next = (*task->GetNext());
225
226 m_tasks.Unlink(task);
227
228 // update pointer: set to previous task so that GetNext() could return next,
229 // if there are no tasks left GetNext() will return nullptr causing a sleep
230 // state for the kernel
231 if (next != task)
232 m_prev = (*next->GetPrev());
233 else
234 m_prev = nullptr;
235 }
236
240};
241
247
248} // namespace stk
249
250#endif /* STK_STRATEGY_RROBIN_H_ */
Contains interface definitions of the library.
#define STK_ASSERT(e)
Runtime assertion. Halts execution if the expression e evaluates to false.
Definition stk_defs.h:330
Namespace of STK package.
SwitchStrategyRoundRobin SwitchStrategyRR
Shorthand alias for SwitchStrategyRoundRobin.
Scheduling-strategy-facing interface for a kernel task slot.
Definition stk_common.h:493
DLHeadType ListHeadType
List head type for IKernelTask elements.
Definition stk_common.h:498
virtual bool IsSleeping() const =0
Check whether the task is currently sleeping.
Interface for a task switching strategy implementation.
Definition stk_common.h:782
DLEntryType * GetPrev() const
Get the previous entry in the list.
DLHeadType * GetHead() const
Get the list head this entry currently belongs to.
DLEntryType * GetNext() const
Get the next entry in the list.
Round-Robin task-switching strategy: each runnable task receives one time slice (one tick interval) i...
IKernelTask::ListHeadType m_tasks
Runnable tasks eligible for scheduling.
void RemoveActive(IKernelTask *task)
Remove a task from m_tasks and update the cursor.
size_t GetSize() const
Get total number of tasks managed by this strategy.
IKernelTask::ListHeadType m_sleep
Sleeping (blocked) tasks not eligible for scheduling.
void AddActive(IKernelTask *task)
Append a task to m_tasks and restore the cursor if necessary.
SwitchStrategyRoundRobin()
Construct an empty strategy with no tasks and a null cursor.
EConfig
Compile-time capability flags reported to the kernel.
@ 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.
@ WEIGHT_API
This strategy does not use per-task weights; all tasks are treated equally.
void OnTaskWake(IKernelTask *task)
Notification that a task has become runnable again.
IKernelTask * GetFirst() const
Get first task in the managed set (used by the kernel for initial scheduling).
IKernelTask * GetNext()
Advance cursor and return the next runnable task.
void AddTask(IKernelTask *task)
Add task to the runnable set.
void RemoveTask(IKernelTask *task)
Remove task from whichever list it currently occupies.
void OnTaskSleep(IKernelTask *task)
Notification that a task has entered the sleeping state.
STK_NONCOPYABLE_CLASS(SwitchStrategyRoundRobin)
IKernelTask * m_prev
Iterator cursor: the most recently scheduled task, or nullptr when no runnable tasks exist....
bool OnTaskDeadlineMissed(IKernelTask *)
Not supported, asserts unconditionally.