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::sched::SwitchStrategySmoothWeightedRoundRobinT< NoSleepYield > Class Template Referencefinal

Smooth Weighted Round-Robin (SWRR) task-switching strategy: distributes CPU time proportionally to per-task weights while avoiding execution bursts by spreading selections evenly over time. More...

#include <stk_strategy_swrrobin.h>

Inheritance diagram for stk::sched::SwitchStrategySmoothWeightedRoundRobinT< NoSleepYield >:
Collaboration diagram for stk::sched::SwitchStrategySmoothWeightedRoundRobinT< NoSleepYield >:

Public Types

enum  EConfig {
  WEIGHT_API = 1 ,
  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

 SwitchStrategySmoothWeightedRoundRobinT ()
 Construct an empty strategy with no tasks and a zero total weight.
STK_VIRT_DTOR ~SwitchStrategySmoothWeightedRoundRobinT ()=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
 Select and return the next task to run, applying one step of the SWRR algorithm.
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 the 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.

Private Member Functions

 STK_NONCOPYABLE_CLASS (SwitchStrategySmoothWeightedRoundRobinT)
void AddActive (IKernelTask *task)
 Append task to m_tasks and update the total weight.
void RemoveActive (IKernelTask *task)
 Remove task from m_tasks and update the total weight.

Private Attributes

IKernelTask::ListHeadType m_tasks
 Runnable tasks eligible for scheduling.
IKernelTask::ListHeadType m_sleep
 Sleeping (blocked) tasks not eligible for scheduling.
IKernelTask * m_yielded [STK_ALLOCATE_COUNT< NoSleepYield, 1U, 1U, 0U >::Value]
 Task that just yielded, skipped by the next GetNext() only. Zero-size without NoSleepYield on GCC/Clang, one entry on MSVC/IAR.
int32_t m_total_weight
 Sum of static weights (GetWeight()) of all tasks currently in m_tasks. Sleeping tasks are excluded. Updated on every AddActive() / RemoveActive() call. Used as the post-selection deduction amount in GetNext() and as the wake-up boost value in OnTaskWake().

Detailed Description

template<bool NoSleepYield>
class stk::sched::SwitchStrategySmoothWeightedRoundRobinT< NoSleepYield >

Smooth Weighted Round-Robin (SWRR) task-switching strategy: distributes CPU time proportionally to per-task weights while avoiding execution bursts by spreading selections evenly over time.

This is an improved variant of standard Weighted Round-Robin.

See also
https://en.wikipedia.org/wiki/Weighted_round_robin
Algorithm
Each task carries two weight values:
  • Static weight (GetWeight()): configured by the application via ITask::GetWeight(). Defines the task's desired CPU share relative to other tasks. Must be in the range 1 .. 0x7FFFFF (keeps the accumulated weights within int32_t). A task with weight 3 receives three times as much CPU time as one with weight 1.
  • Dynamic (current) weight (GetCurrentWeight() / SetCurrentWeight()): modified each scheduling step by the algorithm below. Reset to NO_WEIGHT (-1) by AddTask(), the same value for every task, so all tasks start from an equal position.

On every call to GetNext() (once per kernel tick):

  1. For every runnable task: current_weight += static_weight
  2. Select the task with the highest current_weight as the next to run.
  3. For the selected task only: current_weight -= total_weight_sum (where total_weight_sum is the sum of static weights of all runnable tasks).

Over time this produces fair, proportional CPU distribution without consecutive bursts.

No-sleep yield
When NoSleepYield is true, Yield() keeps the calling task runnable (it is NOT put to sleep). OnTaskYield() remembers the yielding task and the next GetNext() excludes it from the selection (one-shot). The yielding task still accrues its static weight in that step, so the proportional accounting is preserved, but it cannot win the selection unless it is the only runnable task. If NoSleepYield is false the kernel performs the legacy yield: the task sleeps for YIELD_TICKS.
Wake-up priority boost
When a sleeping task is woken (OnTaskWake()), its current_weight is set to total_weight_sum so it is selected on the very next tick. This prevents starvation of tasks that had been blocking on I/O or synchronization objects.
Template Parameters
NoSleepYieldIf true, Yield() keeps the task in the runnable set and the strategy skips the yielding task in the next selection (see OnTaskYield). If false, the kernel performs the legacy yield: the task sleeps for YIELD_TICKS.
Note
Requires the Weight API (WEIGHT_API = 1): the kernel must provide GetWeight(), GetCurrentWeight(), and SetCurrentWeight() on each kernel task.
Requires the kernel Sleep API (SLEEP_EVENT_API = 1): the kernel must call OnTaskSleep() and OnTaskWake() to keep m_total_weight consistent as tasks enter and leave the runnable set.
GetNext() iterates over all runnable tasks — O(n) per tick. For large task counts the constant factor is small (integer arithmetic only), but this should be considered when sizing TASKS_MAX on severely constrained targets.
See also
SwitchStrategySWRR, SwitchStrategySWRR_NSY, ITaskSwitchStrategy, ITask::GetWeight, IKernelTask::GetWeight

Definition at line 76 of file stk_strategy_swrrobin.h.

Member Enumeration Documentation

◆ EConfig

Compile-time capability flags reported to the kernel.

Enumerator
WEIGHT_API 

This strategy uses per-task static and dynamic weights; the kernel must expose the Weight API on each IKernelTask.

SLEEP_EVENT_API 

This strategy requires OnTaskSleep() / OnTaskWake() events to keep m_total_weight accurate as tasks move between the runnable and sleeping sets.

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 82 of file stk_strategy_swrrobin.h.

83 {
84 WEIGHT_API = 1,
85 SLEEP_EVENT_API = 1,
89 TIMESLICE_API = 0
90 };
Smooth Weighted Round-Robin (SWRR) task-switching strategy: distributes CPU time proportionally to pe...
@ PRIORITY_INHERITANCE_API
This strategy does not require Priority Inheritance and OnTaskWeightChange() events.
@ DEADLINE_MISSED_API
This strategy does not use OnTaskDeadlineMissed() events.
@ SLEEP_EVENT_API
This strategy requires OnTaskSleep() / OnTaskWake() events to keep m_total_weight accurate as tasks m...
@ NOSLEEP_YIELD_API
(1) enables OnTaskYield(): yielding task stays runnable instead of sleeping.
@ TIMESLICE_API
This strategy does not support KERNEL_TIMESLICE mode.
@ WEIGHT_API
This strategy uses per-task static and dynamic weights; the kernel must expose the Weight API on each...

Constructor & Destructor Documentation

◆ SwitchStrategySmoothWeightedRoundRobinT()

template<bool NoSleepYield>
stk::sched::SwitchStrategySmoothWeightedRoundRobinT< NoSleepYield >::SwitchStrategySmoothWeightedRoundRobinT ( )
inlineexplicit

Construct an empty strategy with no tasks and a zero total weight.

Definition at line 94 of file stk_strategy_swrrobin.h.

95 {}
IKernelTask::ListHeadType m_sleep
Sleeping (blocked) tasks not eligible for scheduling.
int32_t m_total_weight
Sum of static weights (GetWeight()) of all tasks currently in m_tasks. Sleeping tasks are excluded....
IKernelTask * m_yielded[STK_ALLOCATE_COUNT< NoSleepYield, 1U, 1U, 0U >::Value]
Task that just yielded, skipped by the next GetNext() only. Zero-size without NoSleepYield on GCC/Cla...
IKernelTask::ListHeadType m_tasks
Runnable tasks eligible for scheduling.

◆ ~SwitchStrategySmoothWeightedRoundRobinT()

Destructor.

Note
MISRA deviation: [STK-DEV-005] Rule 10-3-2.

Member Function Documentation

◆ AddActive()

template<bool NoSleepYield>
void stk::sched::SwitchStrategySmoothWeightedRoundRobinT< NoSleepYield >::AddActive ( IKernelTask * task)
inlineprivate

Append task to m_tasks and update the total weight.

Parameters
[in]taskTask to make runnable.
Note
Increments m_total_weight by the task's static weight so that the post-selection deduction in GetNext() remains correct.

Definition at line 332 of file stk_strategy_swrrobin.h.

333 {
334 m_tasks.LinkBack(task);
335 m_total_weight += task->GetWeight();
336 }
int32_t m_total_weight
Sum of static weights (GetWeight()) of all tasks currently in m_tasks. Sleeping tasks are excluded....
IKernelTask::ListHeadType m_tasks
Runnable tasks eligible for scheduling.

Referenced by stk::sched::SwitchStrategySmoothWeightedRoundRobinT< false >::AddTask(), and stk::sched::SwitchStrategySmoothWeightedRoundRobinT< false >::OnTaskWake().

Here is the caller graph for this function:

◆ AddTask()

template<bool NoSleepYield>
void stk::sched::SwitchStrategySmoothWeightedRoundRobinT< NoSleepYield >::AddTask ( IKernelTask * task)
inlineoverridevirtual

Add task to the runnable set.

Parameters
[in]taskTask to add. Must not be nullptr.
Note
The task's static weight (GetWeight()) is validated: must be in [1, 0x7FFFFF]. Negative or zero weights would break the algorithm invariant. Values above 0x7FFFFF would risk int32_t overflow in the accumulated current-weight arithmetic.
The dynamic weight (current weight) is reset to NO_WEIGHT before the task is added to the runnable set. This gives all newly-added tasks an equal starting position in the selection cycle.
Delegates to AddActive() which also increments m_total_weight.

Implements stk::ITaskSwitchStrategy.

Definition at line 112 of file stk_strategy_swrrobin.h.

113 {
114 STK_ASSERT(task != nullptr);
115 STK_ASSERT((task->GetWeight() > 0) && (task->GetWeight() <= 0x7FFFFF)); // must not be negative, max 24-bit number
116
117 task->SetCurrentWeight(NO_WEIGHT);
118
120 }
#define STK_ASSERT(e)
Runtime assertion. Halts execution if the expression e evaluates to false.
Definition stk_defs.h:532
void AddActive(IKernelTask *task)
Append task to m_tasks and update the total weight.

◆ GetFirst()

template<bool NoSleepYield>
IKernelTask * stk::sched::SwitchStrategySmoothWeightedRoundRobinT< NoSleepYield >::GetFirst ( )
inlineoverridevirtual

Get first task in the managed set (reference task for the kernel, used when it starts and when all tasks sleep).

Returns
The first task in m_tasks if any task is runnable, otherwise the first task in m_sleep. Asserts if the combined set is empty (GetSize() == 0).
Note
Preference is given to runnable tasks. The sleep fallback allows the kernel to identify any task even when all are currently sleeping.

Implements stk::ITaskSwitchStrategy.

Definition at line 234 of file stk_strategy_swrrobin.h.

235 {
236 STK_ASSERT(GetSize() != 0U);
237
238 return (*(!m_tasks.IsEmpty() ? m_tasks.GetFirst() : m_sleep.GetFirst()));
239 }
size_t GetSize() const override
Get the total number of tasks managed by this strategy.
IKernelTask::ListHeadType m_sleep
Sleeping (blocked) tasks not eligible for scheduling.

◆ GetNext()

template<bool NoSleepYield>
IKernelTask * stk::sched::SwitchStrategySmoothWeightedRoundRobinT< NoSleepYield >::GetNext ( )
inlineoverridevirtual

Select and return the next task to run, applying one step of the SWRR algorithm.

Returns
The task with the highest current weight after the update step, or NULL if m_tasks is empty (no runnable tasks - kernel will sleep).
Note
Algorithm applied per call (O(n) over runnable tasks):
  1. For every task in m_tasks: current_weight += static_weight.
  2. Scan for the task with the maximum current_weight (initial sentinel: INT32_MIN - never equals a valid weight).
  3. Deduct the total runnable weight from the winner: selected->current_weight -= m_total_weight.
With NoSleepYield the task recorded by OnTaskYield() is excluded from step 2 (one-shot, it still receives the step 1 increment). If it is the only runnable task it is selected anyway.
The assertion next != NULL guards against a logic error where m_tasks is non-empty but no task was picked (should never occur).

Implements stk::ITaskSwitchStrategy.

Definition at line 160 of file stk_strategy_swrrobin.h.

161 {
162 IKernelTask *next = nullptr;
163
164 if (!m_tasks.IsEmpty())
165 {
166 IKernelTask *skip = nullptr;
167
169 {
170 skip = m_yielded[0]; // one-shot: excluded from this selection only
171 m_yielded[0] = nullptr;
172 }
173 else
174 {
176 }
177
178 IKernelTask *itr = (*m_tasks.GetFirst());
179 IKernelTask *const start = itr;
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
188 {
189 if ((itr != skip) && (candidate_weight > max_weight))
190 {
192 next = itr;
193 }
194 }
195 else
196 {
198 {
200 next = itr;
201 }
202 }
203
204 itr = (*itr->GetNext());
205 }
206 while (itr != start);
207
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 }
#define STK_UNUSED(X)
Explicitly marks a variable as unused to suppress compiler warnings.
Definition stk_defs.h:715
#define __stk_constexpr_cpp17
constexpr definition for C++17 and above.
Definition stk_defs.h:505
IKernelTask * GetNext() override
Select and return the next task to run, applying one step of the SWRR algorithm.
IKernelTask * m_yielded[STK_ALLOCATE_COUNT< NoSleepYield, 1U, 1U, 0U >::Value]
Task that just yielded, skipped by the next GetNext() only. Zero-size without NoSleepYield on GCC/Cla...

◆ GetSize()

template<bool NoSleepYield>
size_t stk::sched::SwitchStrategySmoothWeightedRoundRobinT< NoSleepYield >::GetSize ( ) const
inlineoverridevirtual

Get the total number of tasks managed by this strategy.

Returns
Sum of tasks in m_tasks (runnable) and m_sleep (sleeping).

Implements stk::ITaskSwitchStrategy.

Definition at line 244 of file stk_strategy_swrrobin.h.

245 {
246 return m_tasks.GetSize() + m_sleep.GetSize();
247 }

Referenced by stk::sched::SwitchStrategySmoothWeightedRoundRobinT< false >::GetFirst().

Here is the caller graph for this function:

◆ OnTaskDeadlineMissed()

virtual bool stk::ITaskSwitchStrategy::OnTaskDeadlineMissed ( IKernelTask * task)
inlinevirtualinherited

Notification that a task has exceeded its HRT deadline; returns whether the strategy can recover without a hard fault.

Parameters
[in]taskThe task whose deadline was missed. Must not be nullptr.
Returns
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.
Note
Budget Overrun API. Called by the kernel from UpdateTaskState() within a tick, after GetNext() has already been called for that tick. Only invoked when 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().
Returning 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."
The base implementation returns false (unrecoverable), which is the correct default for strategies that do not implement overrun recovery.

Definition at line 1501 of file stk_common.h.

1502 {
1503 STK_UNUSED(task);
1504 return false;
1505 }

References STK_UNUSED.

◆ OnTaskSleep()

template<bool NoSleepYield>
void stk::sched::SwitchStrategySmoothWeightedRoundRobinT< NoSleepYield >::OnTaskSleep ( IKernelTask * task)
inlineoverridevirtual

Notification that a task has entered the sleeping state.

Parameters
[in]taskThe task that is now sleeping. Must be in m_tasks (asserted).
Note
Moves the task from m_tasks to m_sleep via RemoveActive(), which also decrements m_total_weight by the task's static weight. Sleeping tasks do not participate in weight distribution until they wake.

Implements stk::ITaskSwitchStrategy.

Definition at line 255 of file stk_strategy_swrrobin.h.

256 {
257 STK_ASSERT(task != nullptr);
258 STK_ASSERT(task->IsSleeping());
259 STK_ASSERT(task->GetHead() == &m_tasks);
260
262 m_sleep.LinkBack(task);
263 }
void RemoveActive(IKernelTask *task)
Remove task from m_tasks and update the total weight.

◆ OnTaskTimeSlice()

virtual bool stk::ITaskSwitchStrategy::OnTaskTimeSlice ( IKernelTask * task)
inlinevirtualinherited

Notification that the running task's time slice expired (KERNEL_TIMESLICE only).

Parameters
[in]taskPointer to the running task, currently in the runnable set.
Returns
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).
Note
Unlike OnTaskYield() this must not release preemption-threshold style holds.
Called from the tick handler. Only invoked when TIMESLICE_API == 1.
See also
SwitchStrategyPreemptionThreshold::OnTaskTimeSlice

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.

1476 {
1477 STK_UNUSED(task);
1478 return false;
1479 }

References STK_UNUSED.

◆ OnTaskWake()

template<bool NoSleepYield>
void stk::sched::SwitchStrategySmoothWeightedRoundRobinT< NoSleepYield >::OnTaskWake ( IKernelTask * task)
inlineoverridevirtual

Notification that a task has become runnable again.

Parameters
[in]taskThe task that woke up. Must be in m_sleep (asserted).
Note
Applies a priority boost before re-inserting into m_tasks: the task's current weight is set to m_total_weight (the sum of all runnable tasks' static weights). On the next GetNext() call every runnable task increases its current weight by its static weight, but the waking task starts from m_total_weight, giving it the highest initial value and guaranteeing it is selected first. This prevents starvation of tasks that had been blocking on I/O or synchronization objects, and mimics the fairness behaviour of plain Round-Robin for equal-weight tasks.
After the boost, delegates to AddActive() which increments m_total_weight.

Implements stk::ITaskSwitchStrategy.

Definition at line 277 of file stk_strategy_swrrobin.h.

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
287 task->SetCurrentWeight(m_total_weight);
288
290 }

◆ OnTaskWeightChange()

virtual void stk::ITaskSwitchStrategy::OnTaskWeightChange ( IKernelTask * task,
Weight old_weight )
inlinevirtualinherited

Notification that a runnable task's scheduling weight has changed.

Parameters
[in]taskThe task whose weight was just updated via SetWeight().
[in]old_weightThe previous weight of this task (required to remove it from the priority list belonging to that weight).
Note
Called for any bound task, including a sleeping one (e.g. SetWeight() on it), so the strategy must not assume the task is in its runnable set. The strategy must reflect the new weight in its data structures. For strategies with WEIGHT_API = 0 this is a no-op. For SwitchStrategyFixedPriority it moves a runnable task from its old priority-level list to the new one and updates m_ready_bitmap.
Called from within a critical section (see hw::CriticalSection).

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.

1519 {
1520 STK_UNUSED(task);
1521 STK_UNUSED(old_weight);
1522 }

References STK_UNUSED.

◆ OnTaskYield()

template<bool NoSleepYield>
bool stk::sched::SwitchStrategySmoothWeightedRoundRobinT< NoSleepYield >::OnTaskYield ( IKernelTask * task)
inlineoverridevirtual

Notification that the running task called Yield() (non-HRT kernel modes only).

Parameters
[in]taskPointer to the yielding task, currently in the runnable set.
Returns
true if the strategy handled the yield itself: the task stays runnable (it is NOT put to sleep) and the following GetNext() will not select it (unless it is the only runnable task); false if the kernel shall perform the legacy yield, i.e. sleep the task for YIELD_TICKS.
Note
The exclusion is one-shot and is dropped if the task leaves the runnable set before the next GetNext() (see RemoveActive()).

Reimplemented from stk::ITaskSwitchStrategy.

Definition at line 301 of file stk_strategy_swrrobin.h.

302 {
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 {
320 return false;
321 }
322 }

◆ RemoveActive()

template<bool NoSleepYield>
void stk::sched::SwitchStrategySmoothWeightedRoundRobinT< NoSleepYield >::RemoveActive ( IKernelTask * task)
inlineprivate

Remove task from m_tasks and update the total weight.

Parameters
[in]taskRunnable task to remove.
Note
Decrements m_total_weight by the task's static weight so that the post-selection deduction in GetNext() and the wake-up boost in OnTaskWake() remain proportionally correct for the remaining tasks. With NoSleepYield also drops a pending yield exclusion of this task.

Definition at line 345 of file stk_strategy_swrrobin.h.

346 {
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 }

Referenced by stk::sched::SwitchStrategySmoothWeightedRoundRobinT< false >::OnTaskSleep(), and stk::sched::SwitchStrategySmoothWeightedRoundRobinT< false >::RemoveTask().

Here is the caller graph for this function:

◆ RemoveTask()

template<bool NoSleepYield>
void stk::sched::SwitchStrategySmoothWeightedRoundRobinT< NoSleepYield >::RemoveTask ( IKernelTask * task)
inlineoverridevirtual

Remove task from whichever list it currently occupies.

Parameters
[in]taskTask to remove. Must not be NULL and must belong to either m_tasks or m_sleep (asserted).
Note
If the task is in m_tasks, delegates to RemoveActive() which also decrements m_total_weight. If the task is in m_sleep, simply unlinks it (m_total_weight is not adjusted since sleeping tasks are already excluded from the weight sum).

Implements stk::ITaskSwitchStrategy.

Definition at line 130 of file stk_strategy_swrrobin.h.

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 {
138 }
139 else
140 {
141 m_sleep.Unlink(task);
142 }
143 }

◆ STK_NONCOPYABLE_CLASS()

template<bool NoSleepYield>
stk::sched::SwitchStrategySmoothWeightedRoundRobinT< NoSleepYield >::STK_NONCOPYABLE_CLASS ( SwitchStrategySmoothWeightedRoundRobinT< NoSleepYield > )
private

Member Data Documentation

◆ m_sleep

template<bool NoSleepYield>
IKernelTask::ListHeadType stk::sched::SwitchStrategySmoothWeightedRoundRobinT< NoSleepYield >::m_sleep
private

Sleeping (blocked) tasks not eligible for scheduling.

Definition at line 361 of file stk_strategy_swrrobin.h.

◆ m_tasks

template<bool NoSleepYield>
IKernelTask::ListHeadType stk::sched::SwitchStrategySmoothWeightedRoundRobinT< NoSleepYield >::m_tasks
private

Runnable tasks eligible for scheduling.

Definition at line 360 of file stk_strategy_swrrobin.h.

◆ m_total_weight

template<bool NoSleepYield>
int32_t stk::sched::SwitchStrategySmoothWeightedRoundRobinT< NoSleepYield >::m_total_weight
private

Sum of static weights (GetWeight()) of all tasks currently in m_tasks. Sleeping tasks are excluded. Updated on every AddActive() / RemoveActive() call. Used as the post-selection deduction amount in GetNext() and as the wake-up boost value in OnTaskWake().

Definition at line 363 of file stk_strategy_swrrobin.h.

◆ m_yielded

template<bool NoSleepYield>
IKernelTask* stk::sched::SwitchStrategySmoothWeightedRoundRobinT< NoSleepYield >::m_yielded[STK_ALLOCATE_COUNT< NoSleepYield, 1U, 1U, 0U >::Value]
private

Task that just yielded, skipped by the next GetNext() only. Zero-size without NoSleepYield on GCC/Clang, one entry on MSVC/IAR.

Definition at line 362 of file stk_strategy_swrrobin.h.


The documentation for this class was generated from the following file: