SuperTinyKernel™ RTOS 1.08.x
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(TM) RTOS: 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
17
18#include "stk_common.h"
19
20namespace stk {
21namespace sched {
22
47template <bool NoSleepYield> class SwitchStrategyRoundRobinT final : public ITaskSwitchStrategy
48{
49public:
62
67
72
80 void AddTask(IKernelTask *task) override
81 {
82 STK_ASSERT(task != nullptr);
83 STK_ASSERT(task->GetHead() == nullptr);
84
85 const bool tail = (m_prev == m_tasks.GetLast());
86
87 m_tasks.LinkBack(task);
88
89 // if pointer was pointing to the tail, become a tail
90 if (tail)
91 {
92 m_prev = task;
93 }
94 }
95
102 void RemoveTask(IKernelTask *task) override
103 {
104 STK_ASSERT(task != nullptr);
105 STK_ASSERT(GetSize() != 0);
106 STK_ASSERT((task->GetHead() == &m_tasks) || (task->GetHead() == &m_sleep));
107
108 if (task->GetHead() == &m_tasks)
109 {
110 RemoveActive(task);
111 }
112 else
113 {
114 m_sleep.Unlink(task);
115 }
116 }
117
128 {
129 IKernelTask *next = m_prev;
130
131 if (next != nullptr)
132 {
133 next = (*next->GetNext());
134 m_prev = next;
135 }
136
137 return next;
138 }
139
147 {
148 STK_ASSERT(GetSize() != 0U);
149
150 return (*(!m_tasks.IsEmpty() ? m_tasks.GetFirst() : m_sleep.GetFirst()));
151 }
152
156 size_t GetSize() const override
157 {
158 return m_tasks.GetSize() + m_sleep.GetSize();
159 }
160
166 void OnTaskSleep(IKernelTask *task) override
167 {
168 STK_ASSERT(task != nullptr);
169 STK_ASSERT(task->IsSleeping());
170 STK_ASSERT(task->GetHead() == &m_tasks);
171
172 RemoveActive(task);
173 m_sleep.LinkBack(task);
174 }
175
182 void OnTaskWake(IKernelTask *task) override
183 {
184 STK_ASSERT(task != nullptr);
185 STK_ASSERT(!task->IsSleeping());
186 STK_ASSERT(task->GetHead() == &m_sleep);
187
188 m_sleep.Unlink(task);
189 AddActive(task);
190 }
191
199 bool OnTaskYield(IKernelTask *task) override
200 {
201 if __stk_constexpr_cpp17 (NoSleepYield)
202 {
203 STK_ASSERT(task != nullptr);
204
205 bool handled = false;
206
207 if (task->GetHead() == &m_tasks)
208 {
209 m_prev = task; // GetNext() will return task's successor (or task itself if alone)
210 handled = true;
211 }
212
213 return handled;
214 }
215 else
216 {
217 STK_UNUSED(task);
218 return false;
219 }
220 }
221
222protected:
224
232 {
233 m_tasks.LinkBack(task);
234
235 // update pointer: if all tasks were sleeping, this task will change state
236 // of the kernel to active
237 if (m_prev == nullptr)
238 {
239 m_prev = task;
240 }
241 }
242
260 {
261 IKernelTask *const next = (*task->GetNext());
262
263 m_tasks.Unlink(task);
264
265 // update pointer: if there are no tasks left GetNext() will return nullptr causing a sleep
266 // state for the kernel
267 if (next == task)
268 {
269 m_prev = nullptr;
270 }
271 else if (m_prev == task)
272 {
273 // cursor was on the removed task: step back so that GetNext() returns its successor
274 m_prev = (*next->GetPrev());
275 }
276 else
277 {
278 // cursor is on another task: leave the rotation untouched
279 }
280 }
281
285};
286
292
298
304
310
311} // namespace sched
312
313// ----------------------------------------------------------------------------
314// Backward compatibility: the strategies live in stk::sched, the names below keep existing
315// code that refers to them as stk::<Name> compiling. Migrate to stk::sched::<Name>.
316// ----------------------------------------------------------------------------
317
319template <bool NoSleepYield>
321
323typedef sched::SwitchStrategyRoundRobin SwitchStrategyRoundRobin __stk_attr_deprecated_type("Use stk::sched::SwitchStrategyRoundRobin");
325typedef sched::SwitchStrategyRR SwitchStrategyRR __stk_attr_deprecated_type("Use stk::sched::SwitchStrategyRR");
327typedef sched::SwitchStrategyRoundRobinNoSleepYield SwitchStrategyRoundRobinNoSleepYield __stk_attr_deprecated_type("Use stk::sched::SwitchStrategyRoundRobinNoSleepYield");
329typedef sched::SwitchStrategyRR_NSY SwitchStrategyRR_NSY __stk_attr_deprecated_type("Use stk::sched::SwitchStrategyRR_NSY");
330
331} // namespace stk
332
333#endif /* STK_STRATEGY_RROBIN_H_ */
Contains interface definitions of the library.
#define STK_UNUSED(X)
Explicitly marks a variable as unused to suppress compiler warnings.
Definition stk_defs.h:715
#define STK_ASSERT(e)
Runtime assertion. Halts execution if the expression e evaluates to false.
Definition stk_defs.h:532
#define __stk_constexpr_cpp17
constexpr definition for C++17 and above.
Definition stk_defs.h:505
#define STK_VIRT_DTOR
Expands to nothing if STK_STRICT_COMPLIANCY=0 (non-virtual destructors, smaller objects),...
Definition stk_defs.h:254
Namespace of STK package.
sched::SwitchStrategyEDF SwitchStrategyEDF __stk_attr_deprecated_type("Use stk::sched::SwitchStrategyEDF")
Task-switching strategies (stk::sched::SwitchStrategy*) and their helpers (priority bitmaps,...
SwitchStrategyRoundRobinT< true > SwitchStrategyRoundRobinNoSleepYield
Round-Robin strategy where Yield() keeps the task runnable (NOSLEEP_YIELD_API = 1).
SwitchStrategyRoundRobinT< false > SwitchStrategyRoundRobin
Shorthand alias for SwitchStrategyRoundRobinT<>.
SwitchStrategyRoundRobinT< false > SwitchStrategyRR
Shorthand alias for SwitchStrategyRoundRobin.
SwitchStrategyRoundRobinT< true > SwitchStrategyRR_NSY
Shorthand for SwitchStrategyRoundRobinNoSleepYield.
Scheduling-strategy-facing interface for a kernel task slot.
DLHeadType ListHeadType
List head type for IKernelTask elements.
virtual bool IsSleeping() const =0
Check whether the task is currently sleeping.
Interface for a task switching strategy implementation.
DLHeadType * GetHead()
Get the list head this entry currently belongs to.
DLEntryType * GetPrev()
Get the previous entry in the list.
DLEntryType * GetNext()
Get the next entry in the list.
Round-Robin task-switching strategy: each runnable task receives one time slice (one tick interval) i...
bool OnTaskYield(IKernelTask *task) override
Notification that the running task called Yield() (non-HRT kernel modes only).
void OnTaskSleep(IKernelTask *task) override
Notification that a task has entered the sleeping state.
IKernelTask * GetFirst() override
Get first task in the managed set (reference task for the kernel, used when it starts and when all ta...
STK_NONCOPYABLE_CLASS(SwitchStrategyRoundRobinT)
void AddTask(IKernelTask *task) override
Add task to the runnable set.
size_t GetSize() const override
Get total number of tasks managed by this strategy.
SwitchStrategyRoundRobinT()
Construct an empty strategy with no tasks and a null cursor.
void RemoveActive(IKernelTask *task)
Remove a task from m_tasks and update the cursor.
void RemoveTask(IKernelTask *task) override
Remove task from whichever list it currently occupies.
void OnTaskWake(IKernelTask *task) override
Notification that a task has become runnable again.
IKernelTask * GetNext() override
Advance cursor and return the next runnable task.
STK_VIRT_DTOR ~SwitchStrategyRoundRobinT()=default
Destructor.
EConfig
Compile-time capability flags reported to the kernel.
void AddActive(IKernelTask *task)
Append a task to m_tasks and restore the cursor if necessary.