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_swrrobin.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_SWRROBIN_H_
11#define STK_STRATEGY_SWRROBIN_H_
12
18
19#include "stk_common.h"
20
21namespace stk {
22namespace sched {
23
76template <bool NoSleepYield> class SwitchStrategySmoothWeightedRoundRobinT final : public ITaskSwitchStrategy
77{
78public:
91
96
101
112 void AddTask(IKernelTask *task) override
113 {
114 STK_ASSERT(task != nullptr);
115 STK_ASSERT((task->GetWeight() > 0) && (task->GetWeight() <= 0x7FFFFF)); // must not be negative, max 24-bit number
116
118
119 AddActive(task);
120 }
121
130 void RemoveTask(IKernelTask *task) override
131 {
132 STK_ASSERT(task != nullptr);
133 STK_ASSERT((task->GetHead() == &m_tasks) || (task->GetHead() == &m_sleep));
134
135 if (task->GetHead() == &m_tasks)
136 {
137 RemoveActive(task);
138 }
139 else
140 {
141 m_sleep.Unlink(task);
142 }
143 }
144
161 {
162 IKernelTask *next = nullptr;
163
164 if (!m_tasks.IsEmpty())
165 {
166 IKernelTask *skip = nullptr;
167
168 if __stk_constexpr_cpp17 (NoSleepYield)
169 {
170 skip = m_yielded[0]; // one-shot: excluded from this selection only
171 m_yielded[0] = nullptr;
172 }
173 else
174 {
175 STK_UNUSED(skip);
176 }
177
178 IKernelTask *itr = (*m_tasks.GetFirst());
179 IKernelTask *const start = itr;
180 int32_t max_weight = INT32_MIN;
181
182 do
183 {
184 const int32_t candidate_weight = itr->GetCurrentWeight() + itr->GetWeight();
185 itr->SetCurrentWeight(candidate_weight); // yielding task still accrues weight
186
187 if __stk_constexpr_cpp17 (NoSleepYield)
188 {
189 if ((itr != skip) && (candidate_weight > max_weight))
190 {
191 max_weight = candidate_weight;
192 next = itr;
193 }
194 }
195 else
196 {
197 if (candidate_weight > max_weight)
198 {
199 max_weight = candidate_weight;
200 next = itr;
201 }
202 }
203
204 itr = (*itr->GetNext());
205 }
206 while (itr != start);
207
208 if __stk_constexpr_cpp17 (NoSleepYield)
209 {
210 // yielding task is the only runnable task: nobody to hand over to
211 if (next == nullptr)
212 {
213 STK_ASSERT(skip != nullptr);
214
215 next = skip;
216 max_weight = skip->GetCurrentWeight();
217 }
218 }
219
220 STK_ASSERT(next != nullptr);
221
222 next->SetCurrentWeight(max_weight - m_total_weight);
223 }
224
225 return next;
226 }
227
235 {
236 STK_ASSERT(GetSize() != 0U);
237
238 return (*(!m_tasks.IsEmpty() ? m_tasks.GetFirst() : m_sleep.GetFirst()));
239 }
240
244 size_t GetSize() const override
245 {
246 return m_tasks.GetSize() + m_sleep.GetSize();
247 }
248
255 void OnTaskSleep(IKernelTask *task) override
256 {
257 STK_ASSERT(task != nullptr);
258 STK_ASSERT(task->IsSleeping());
259 STK_ASSERT(task->GetHead() == &m_tasks);
260
261 RemoveActive(task);
262 m_sleep.LinkBack(task);
263 }
264
277 void OnTaskWake(IKernelTask *task) override
278 {
279 STK_ASSERT(task != nullptr);
280 STK_ASSERT(!task->IsSleeping());
281 STK_ASSERT(task->GetHead() == &m_sleep);
282
283 m_sleep.Unlink(task);
284
285 // boost priority of the previously sleeping task, this resembles to a RR pattern
286 // with tasks having equal weights
288
289 AddActive(task);
290 }
291
301 bool OnTaskYield(IKernelTask *task) override
302 {
303 if __stk_constexpr_cpp17 (NoSleepYield)
304 {
305 STK_ASSERT(task != nullptr);
306
307 bool handled = false;
308
309 if (task->GetHead() == &m_tasks)
310 {
311 m_yielded[0] = task; // excluded from the next selection only
312 handled = true;
313 }
314
315 return handled;
316 }
317 else
318 {
319 STK_UNUSED(task);
320 return false;
321 }
322 }
323
324private:
326
333 {
334 m_tasks.LinkBack(task);
335 m_total_weight += task->GetWeight();
336 }
337
346 {
347 if __stk_constexpr_cpp17 (NoSleepYield)
348 {
349 // do not leave a dangling pointer to a task that is leaving the runnable set
350 if (m_yielded[0] == task)
351 {
352 m_yielded[0] = nullptr;
353 }
354 }
355
356 m_tasks.Unlink(task);
357 m_total_weight -= task->GetWeight();
358 }
359
364};
365
371
377
383
389
390} // namespace sched
391
392// ----------------------------------------------------------------------------
393// Backward compatibility: the strategies live in stk::sched, the names below keep existing
394// code that refers to them as stk::<Name> compiling. Migrate to stk::sched::<Name>.
395// ----------------------------------------------------------------------------
396
398template <bool NoSleepYield>
400
402typedef sched::SwitchStrategySmoothWeightedRoundRobin SwitchStrategySmoothWeightedRoundRobin __stk_attr_deprecated_type("Use stk::sched::SwitchStrategySmoothWeightedRoundRobin");
404typedef sched::SwitchStrategySWRR SwitchStrategySWRR __stk_attr_deprecated_type("Use stk::sched::SwitchStrategySWRR");
406typedef sched::SwitchStrategySmoothWeightedRoundRobinNoSleepYield SwitchStrategySmoothWeightedRoundRobinNoSleepYield __stk_attr_deprecated_type("Use stk::sched::SwitchStrategySmoothWeightedRoundRobinNoSleepYield");
408typedef sched::SwitchStrategySWRR_NSY SwitchStrategySWRR_NSY __stk_attr_deprecated_type("Use stk::sched::SwitchStrategySWRR_NSY");
409
410} // namespace stk
411
412#endif /* STK_STRATEGY_SWRROBIN_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.
static constexpr Weight NO_WEIGHT
Weight value: weight is not set.
Definition stk_common.h:285
sched::SwitchStrategyEDF SwitchStrategyEDF __stk_attr_deprecated_type("Use stk::sched::SwitchStrategyEDF")
Task-switching strategies (stk::sched::SwitchStrategy*) and their helpers (priority bitmaps,...
SwitchStrategySmoothWeightedRoundRobinT< false > SwitchStrategySmoothWeightedRoundRobin
Shorthand alias for SwitchStrategySmoothWeightedRoundRobinT<false>.
SwitchStrategySmoothWeightedRoundRobinT< true > SwitchStrategySWRR_NSY
Shorthand for SwitchStrategySmoothWeightedRoundRobinNoSleepYield.
SwitchStrategySmoothWeightedRoundRobinT< true > SwitchStrategySmoothWeightedRoundRobinNoSleepYield
Smooth Weighted Round-Robin strategy where Yield() keeps the task runnable (NOSLEEP_YIELD_API = 1).
SwitchStrategySmoothWeightedRoundRobinT< false > SwitchStrategySWRR
Shorthand alias for SwitchStrategySmoothWeightedRoundRobin.
Scheduling-strategy-facing interface for a kernel task slot.
virtual Weight GetCurrentWeight() const =0
Get the current dynamic weight value of this task.
virtual Weight GetWeight() const =0
Get the effective weight of the task.
DLHeadType ListHeadType
List head type for IKernelTask elements.
virtual bool IsSleeping() const =0
Check whether the task is currently sleeping.
virtual void SetCurrentWeight(Weight weight)=0
Set the current dynamic weight value used by the scheduling strategy.
Interface for a task switching strategy implementation.
static constexpr size_t Value
Definition stk_defs.h:681
DLHeadType * GetHead()
Get the list head this entry currently belongs to.
DLEntryType * GetNext()
Get the next entry in the list.
Smooth Weighted Round-Robin (SWRR) task-switching strategy: distributes CPU time proportionally to pe...
SwitchStrategySmoothWeightedRoundRobinT()
Construct an empty strategy with no tasks and a zero total weight.
size_t GetSize() const override
Get the total number of tasks managed by this strategy.
void RemoveActive(IKernelTask *task)
Remove task from m_tasks and update the total weight.
IKernelTask * GetNext() override
Select and return the next task to run, applying one step of the SWRR algorithm.
void AddTask(IKernelTask *task) override
Add task to the runnable set.
IKernelTask * GetFirst() override
Get first task in the managed set (reference task for the kernel, used when it starts and when all ta...
void OnTaskWake(IKernelTask *task) override
Notification that a task has become runnable again.
STK_NONCOPYABLE_CLASS(SwitchStrategySmoothWeightedRoundRobinT)
STK_VIRT_DTOR ~SwitchStrategySmoothWeightedRoundRobinT()=default
Destructor.
IKernelTask * m_yielded[STK_ALLOCATE_COUNT< NoSleepYield, 1U, 1U, 0U >::Value]
void RemoveTask(IKernelTask *task) override
Remove task from whichever list it currently occupies.
void AddActive(IKernelTask *task)
Append task to m_tasks and update the total weight.
void OnTaskSleep(IKernelTask *task) override
Notification that a task has entered the sleeping state.
EConfig
Compile-time capability flags reported to the kernel.
bool OnTaskYield(IKernelTask *task) override
Notification that the running task called Yield() (non-HRT kernel modes only).