SuperTinyKernel™ RTOS 1.06.0
Lightweight, high-performance, deterministic, bare-metal C++ RTOS for resource-constrained embedded systems. MIT Open Source License.
Loading...
Searching...
No Matches
stk::SwitchStrategyMonotonic< _Type > Class Template Reference

Monotonic scheduling strategy: Rate-Monotonic (RM) or Deadline-Monotonic (DM), selected at compile time by the _Type template parameter. More...

#include <stk_strategy_monotonic.h>

Inheritance diagram for stk::SwitchStrategyMonotonic< _Type >:
Collaboration diagram for stk::SwitchStrategyMonotonic< _Type >:

Public Types

enum  EConfig {
  WEIGHT_API = 0 ,
  SLEEP_EVENT_API = 0 ,
  DEADLINE_MISSED_API = 0
}
 Compile-time capability flags reported to the kernel. More...

Public Member Functions

 SwitchStrategyMonotonic ()
 Construct an empty strategy with no tasks.
 ~SwitchStrategyMonotonic ()
 Destructor.
void AddTask (IKernelTask *task)
 Add a task to the priority-sorted runnable list.
void RemoveTask (IKernelTask *task)
 Remove a task from the list.
IKernelTaskGetNext ()
 Select and return the highest-priority non-sleeping task.
IKernelTaskGetFirst () const
 Get the first task in the managed set (used by the kernel for initial scheduling).
size_t GetSize () const
 Get the total number of tasks managed by this strategy.
void OnTaskSleep (IKernelTask *)
 Not supported, asserts unconditionally.
void OnTaskWake (IKernelTask *)
 Not supported, asserts unconditionally.
bool OnTaskDeadlineMissed (IKernelTask *)
 Not supported, asserts unconditionally.

Private Member Functions

 STK_NONCOPYABLE_CLASS (SwitchStrategyMonotonic)

Private Attributes

IKernelTask::ListHeadType m_tasks
 All tasks (runnable and sleeping) in priority order. GetNext() skips sleeping tasks via IsSleeping(). AddTask() maintains sort order.

Detailed Description

template<EMonotonicSwitchStrategyType _Type>
class stk::SwitchStrategyMonotonic< _Type >

Monotonic scheduling strategy: Rate-Monotonic (RM) or Deadline-Monotonic (DM), selected at compile time by the _Type template parameter.

Template Parameters
_TypePolicy selector (EMonotonicSwitchStrategyType):
  • MSS_TYPE_RATE - Rate-Monotonic: tasks with a shorter scheduling period receive higher priority. The most frequently activating task always runs first.
  • MSS_TYPE_DEADLINE - Deadline-Monotonic: tasks with a shorter execution deadline receive higher priority.
Sorted-list design
m_tasks is maintained as a priority-sorted intrusive list. AddTask() inserts each new task at the correct sorted position (O(n) insertion sort) so that GetNext() need only scan from the front: the first non-sleeping task it encounters is always the highest-priority runnable task. No re-sorting occurs at runtime after a task is added.
Sleep handling - no separate sleep list
Unlike RR, SWRR, EDF, and FP strategies, this strategy does not maintain a separate sleep list (SLEEP_EVENT_API = 0). Sleeping tasks remain in m_tasks in their sorted position and are skipped by GetNext() via IKernelTask::IsSleeping(). OnTaskSleep() and OnTaskWake() assert unconditionally, the kernel must not be configured to deliver these events to this strategy.
HRT mode requirement
This strategy reads GetHrtPeriodicity() and GetHrtDeadline() from each task during AddTask() to determine its priority. These values are only populated in KERNEL_HRT mode; using this strategy without KERNEL_HRT produces undefined priority ordering.
Note
Does not use per-task weights (WEIGHT_API = 0). Priority is derived entirely from HRT timing parameters set via IKernel::AddTask(periodicity_tc, deadline_tc, ...).
For schedulability analysis of the configured task set use SchedulabilityCheck::IsSchedulableWCRT().
See also
SwitchStrategyRM, SwitchStrategyDM, SchedulabilityCheck, ITaskSwitchStrategy

Definition at line 69 of file stk_strategy_monotonic.h.

Member Enumeration Documentation

◆ EConfig

Compile-time capability flags reported to the kernel.

Enumerator
WEIGHT_API 

This strategy does not use per-task weights. Priority is derived from HRT timing parameters (GetHrtPeriodicity() for RM, GetHrtDeadline() for DM) at AddTask() time.

SLEEP_EVENT_API 

This strategy does not use OnTaskSleep() / OnTaskWake() events. Sleeping tasks remain in m_tasks and are skipped by GetNext() via IKernelTask::IsSleeping(). Delivering these events will trigger an assertion.

DEADLINE_MISSED_API 

This strategy does not use OnTaskDeadlineMissed() events.

Definition at line 75 of file stk_strategy_monotonic.h.

76 {
77 WEIGHT_API = 0,
78 SLEEP_EVENT_API = 0,
80 };
@ DEADLINE_MISSED_API
This strategy does not use OnTaskDeadlineMissed() events.
@ WEIGHT_API
This strategy does not use per-task weights. Priority is derived from HRT timing parameters (GetHrtPe...
@ SLEEP_EVENT_API
This strategy does not use OnTaskSleep() / OnTaskWake() events. Sleeping tasks remain in m_tasks and ...

Constructor & Destructor Documentation

◆ SwitchStrategyMonotonic()

stk::SwitchStrategyMonotonic< _Type >::SwitchStrategyMonotonic ( )
inlineexplicit

Construct an empty strategy with no tasks.

Definition at line 84 of file stk_strategy_monotonic.h.

84 : m_tasks()
85 {}
IKernelTask::ListHeadType m_tasks
All tasks (runnable and sleeping) in priority order. GetNext() skips sleeping tasks via IsSleeping()....

◆ ~SwitchStrategyMonotonic()

Destructor.

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

Definition at line 90 of file stk_strategy_monotonic.h.

91 {}

Member Function Documentation

◆ AddTask()

void stk::SwitchStrategyMonotonic< _Type >::AddTask ( IKernelTask * task)
inlinevirtual

Add a task to the priority-sorted runnable list.

Parameters
[in]taskTask to add. Must not be NULL and must not already be in any list.
Note
Performs an O(n) insertion sort into m_tasks so that higher-priority tasks appear closer to the head. The sort key depends on _Type:
  • MSS_TYPE_RATE: key = GetHrtPeriodicity() (smaller -> nearer to head).
  • MSS_TYPE_DEADLINE: key = GetHrtDeadline() (smaller -> nearer to head).
Three insertion cases handled:
  1. Empty list: insert at front (LinkFront).
  2. New task has a strictly smaller key than the current head: insert at front (LinkFront), becoming the new highest-priority task.
  3. Otherwise: scan forward until a task with a larger-or-equal key is found, then insert immediately before it (Link), or append at back (LinkBack) if the scan reaches the end of the list.
Tasks with equal keys are ordered by registration time: later-added tasks go behind earlier-added tasks with the same key.

Implements stk::ITaskSwitchStrategy.

Definition at line 109 of file stk_strategy_monotonic.h.

110 {
111 STK_ASSERT(task != nullptr);
112 STK_ASSERT(task->GetHead() == nullptr);
113
114 if (m_tasks.IsEmpty())
115 {
116 m_tasks.LinkFront(task);
117 }
118 else
119 {
120 IKernelTask *itr = (*m_tasks.GetFirst()), * const start = itr;
121
122 for (;;)
123 {
124 bool higher_priority;
125 switch (_Type)
126 {
127 case MSS_TYPE_RATE:
128 higher_priority = (task->GetHrtPeriodicity() < itr->GetHrtPeriodicity());
129 break;
131 higher_priority = (task->GetHrtDeadline() < itr->GetHrtDeadline());
132 break;
133 default:
134 STK_ASSERT(false);
135 break;
136 }
137
138 if (higher_priority)
139 {
140 if (itr == start)
141 {
142 m_tasks.LinkFront(task);
143 break;
144 }
145
146 m_tasks.Link(task, itr, itr->GetPrev());
147 break;
148 }
149
150 // end of the list
151 if ((itr = (*itr->GetNext())) == start)
152 {
153 m_tasks.LinkBack(task);
154 break;
155 }
156 }
157 }
158 }
#define STK_ASSERT(e)
Runtime assertion. Halts execution if the expression e evaluates to false.
Definition stk_defs.h:330
Monotonic scheduling strategy: Rate-Monotonic (RM) or Deadline-Monotonic (DM), selected at compile ti...
IKernelTask * GetNext()
Select and return the highest-priority non-sleeping task.

◆ GetFirst()

IKernelTask * stk::SwitchStrategyMonotonic< _Type >::GetFirst ( ) const
inlinevirtual

Get the first task in the managed set (used by the kernel for initial scheduling).

Returns
The first task in m_tasks (highest priority due to sort order). Asserts if m_tasks is empty.
Note
Because all tasks (runnable and sleeping) reside in m_tasks there is no sleep-list fallback path, unlike RR, SWRR, EDF, and FP strategies. The returned task may be sleeping; the kernel uses it only to seed the initial context before the first GetNext() call.

Implements stk::ITaskSwitchStrategy.

Definition at line 217 of file stk_strategy_monotonic.h.

218 {
219 STK_ASSERT(m_tasks.GetSize() != 0U);
220
221 return (*m_tasks.GetFirst());
222 }

◆ GetNext()

IKernelTask * stk::SwitchStrategyMonotonic< _Type >::GetNext ( )
inlinevirtual

Select and return the highest-priority non-sleeping task.

Returns
The first non-sleeping task in m_tasks, or NULL if every task in the list is currently sleeping (kernel will sleep until the next activation).
Note
Because m_tasks is sorted in descending priority order by AddTask(), a linear scan from the head is sufficient: the first task for which IsSleeping() returns false is always the highest-priority runnable task. Complexity is O(k) where k is the number of sleeping tasks at the front of the list (i.e. higher-priority tasks currently between activations).
Asserts if m_tasks is empty; at least one task must be registered before GetNext() is called.

Implements stk::ITaskSwitchStrategy.

Definition at line 186 of file stk_strategy_monotonic.h.

187 {
188 STK_ASSERT(!m_tasks.IsEmpty());
189
190 IKernelTask *itr = (*m_tasks.GetFirst()), * const start = itr;
191 IKernelTask *next = nullptr;
192
193 // Scan from head (highest priority). Skip tasks that are sleeping between
194 // periodic activations; return the first task ready to run.
195 do
196 {
197 if (!itr->IsSleeping())
198 {
199 next = itr;
200 break; // list is sorted by priority; no need to continue
201 }
202 }
203 while ((itr = (*itr->GetNext())) != start);
204
205 // nullptr means all tasks are sleeping, return idle signal to kernel
206 return next;
207 }

◆ GetSize()

size_t stk::SwitchStrategyMonotonic< _Type >::GetSize ( ) const
inlinevirtual

Get the total number of tasks managed by this strategy.

Returns
Number of tasks in m_tasks. Includes both runnable and sleeping tasks since this strategy does not maintain a separate sleep list.

Implements stk::ITaskSwitchStrategy.

Definition at line 228 of file stk_strategy_monotonic.h.

229 {
230 return m_tasks.GetSize();
231 }

Referenced by stk::SwitchStrategyMonotonic< MSS_TYPE_RATE >::RemoveTask().

Here is the caller graph for this function:

◆ OnTaskDeadlineMissed()

bool stk::SwitchStrategyMonotonic< _Type >::OnTaskDeadlineMissed ( IKernelTask * )
inlinevirtual

Not supported, asserts unconditionally.

Note
This strategy uses DEADLINE_MISSED_API = 0. See OnTaskDeadlineMissed() for rationale.

Implements stk::ITaskSwitchStrategy.

Definition at line 256 of file stk_strategy_monotonic.h.

257 {
258 // Budget Overrun API unsupported
259 STK_ASSERT(false);
260 return false;
261 }

◆ OnTaskSleep()

void stk::SwitchStrategyMonotonic< _Type >::OnTaskSleep ( IKernelTask * )
inlinevirtual

Not supported, asserts unconditionally.

Note
This strategy uses SLEEP_EVENT_API = 0. Sleeping tasks remain in m_tasks in their sorted position and are detected by IsSleeping() in GetNext(). Calling this method indicates a kernel/strategy configuration mismatch.

Implements stk::ITaskSwitchStrategy.

Definition at line 238 of file stk_strategy_monotonic.h.

239 {
240 // Sleep API unsupported: RM/DM keeps a single sorted non-volatile list.
241 STK_ASSERT(false);
242 }

◆ OnTaskWake()

void stk::SwitchStrategyMonotonic< _Type >::OnTaskWake ( IKernelTask * )
inlinevirtual

Not supported, asserts unconditionally.

Note
This strategy uses SLEEP_EVENT_API = 0. See OnTaskSleep() for rationale.

Implements stk::ITaskSwitchStrategy.

Definition at line 247 of file stk_strategy_monotonic.h.

248 {
249 // Sleep API unsupported: RM/DM keeps a single sorted non-volatile list.
250 STK_ASSERT(false);
251 }

◆ RemoveTask()

void stk::SwitchStrategyMonotonic< _Type >::RemoveTask ( IKernelTask * task)
inlinevirtual

Remove a task from the list.

Parameters
[in]taskTask to remove. Must not be NULL. Must be in m_tasks (asserted).
Note
Unlike RR, SWRR, EDF, and FP strategies there is no separate sleep list: all tasks, runnable and sleeping, reside in m_tasks, so this method simply unlinks the task unconditionally without a list-membership check.

Implements stk::ITaskSwitchStrategy.

Definition at line 166 of file stk_strategy_monotonic.h.

167 {
168 STK_ASSERT(task != nullptr);
169 STK_ASSERT(GetSize() != 0U);
170 STK_ASSERT(task->GetHead() == &m_tasks);
171
172 m_tasks.Unlink(task);
173 }
size_t GetSize() const
Get the total number of tasks managed by this strategy.

◆ STK_NONCOPYABLE_CLASS()

stk::SwitchStrategyMonotonic< _Type >::STK_NONCOPYABLE_CLASS ( SwitchStrategyMonotonic< _Type > )
private

Member Data Documentation

◆ m_tasks

All tasks (runnable and sleeping) in priority order. GetNext() skips sleeping tasks via IsSleeping(). AddTask() maintains sort order.

Definition at line 266 of file stk_strategy_monotonic.h.


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