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_edf.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_EDF_H_
11#define STK_STRATEGY_EDF_H_
12
16
17#include "stk_common.h"
18
19namespace stk {
20
66{
67public:
79
83 {}
84
89
99 void AddTask(IKernelTask *task) override
100 {
101 STK_ASSERT(task != nullptr);
102 STK_ASSERT(task->GetHead() == nullptr);
103
104 m_tasks.LinkBack(task);
105 }
106
113 void RemoveTask(IKernelTask *task) override
114 {
115 STK_ASSERT(task != nullptr);
116 STK_ASSERT(GetSize() != 0U);
117 STK_ASSERT((task->GetHead() == &m_tasks) || (task->GetHead() == &m_sleep));
118
119 if (task->GetHead() == &m_tasks)
120 {
121 m_tasks.Unlink(task);
122 }
123 else
124 {
125 m_sleep.Unlink(task);
126 }
127 }
128
145 {
146 IKernelTask *next = nullptr;
147
148 if (!m_tasks.IsEmpty())
149 {
150 IKernelTask *const start = (*m_tasks.GetFirst());
151
152 next = start; // initialize earliest found task to the first one
153
154 Timeout earliest = next->GetHrtRelativeDeadline();
155 IKernelTask *itr = (*start->GetNext());
156
157 while (itr != start)
158 {
159 const Timeout deadline = itr->GetHrtRelativeDeadline();
160
161 if (deadline < earliest)
162 {
163 earliest = deadline;
164 next = itr;
165 }
166
167 itr = (*itr->GetNext());
168 }
169 }
170
171 return next;
172 }
173
182 {
183 STK_ASSERT(GetSize() != 0U);
184
185 return (*(!m_tasks.IsEmpty() ? m_tasks.GetFirst() : m_sleep.GetFirst()));
186 }
187
191 size_t GetSize() const override
192 {
193 return m_tasks.GetSize() + m_sleep.GetSize();
194 }
195
202 void OnTaskSleep(IKernelTask *task) override
203 {
204 STK_ASSERT(task != nullptr);
205 STK_ASSERT(task->IsSleeping());
206 STK_ASSERT(task->GetHead() == &m_tasks);
207
208 m_tasks.Unlink(task);
209 m_sleep.LinkBack(task);
210 }
211
220 void OnTaskWake(IKernelTask *task) override
221 {
222 STK_ASSERT(task != nullptr);
223 STK_ASSERT(!task->IsSleeping());
224 STK_ASSERT(task->GetHead() == &m_sleep);
225
226 m_sleep.Unlink(task);
227 m_tasks.LinkBack(task);
228 }
229
230protected:
232
235};
236
237} // namespace stk
238
239#endif /* STK_STRATEGY_EDF_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:516
#define STK_VIRT_DTOR
Makes destructors virtual and compliant to strict rules if STK_STRICT_COMPLIANCY=0.
Definition stk_defs.h:261
Namespace of STK package.
int32_t Timeout
Timeout time (ticks).
Definition stk_common.h:154
Scheduling-strategy-facing interface for a kernel task slot.
Definition stk_common.h:902
virtual Timeout GetHrtRelativeDeadline() const =0
Get HRT task's relative deadline.
DLHeadType ListHeadType
List head type for IKernelTask elements.
Definition stk_common.h:907
virtual bool IsSleeping() const =0
Check whether the task is currently sleeping.
Interface for a task switching strategy implementation.
DLEntryType * GetNext()
Get the next entry in the list.
DLHeadType * GetHead()
Get the list head this entry currently belongs to.
SwitchStrategyEDF()
Construct an empty strategy with no tasks.
IKernelTask * GetFirst() override
Get first task in the managed set (used by the kernel for initial scheduling).
STK_VIRT_DTOR ~SwitchStrategyEDF()=default
Destructor.
IKernelTask::ListHeadType m_sleep
Sleeping (blocked) tasks not eligible for scheduling. Deadline tracking continues in the kernel while...
STK_NONCOPYABLE_CLASS(SwitchStrategyEDF)
EConfig
Compile-time capability flags reported to the kernel.
@ NOSLEEP_YIELD_API
This strategy does not handle Yield() itself (OnTaskYield() is not used); the kernel puts the yieldin...
@ PRIORITY_INHERITANCE_API
This strategy does not require Priority Inheritance and OnTaskPriorityChange() events.
@ DEADLINE_MISSED_API
This strategy does not use OnTaskDeadlineMissed() events.
@ SLEEP_EVENT_API
This strategy requires OnTaskSleep() / OnTaskWake() events to move tasks between the runnable and sle...
@ WEIGHT_API
This strategy does not use per-task weights. Deadline tracking is handled by the kernel in KERNEL_HRT...
size_t GetSize() const override
Get total number of tasks managed by this strategy.
IKernelTask::ListHeadType m_tasks
Runnable tasks eligible for scheduling. Scanned in full by GetNext() each tick to find the minimum re...
void AddTask(IKernelTask *task) override
Add task to the runnable set.
void OnTaskWake(IKernelTask *task) override
Notification that a task has become runnable again.
void OnTaskSleep(IKernelTask *task) override
Notification that a task has entered the sleeping state.
void RemoveTask(IKernelTask *task) override
Remove task from whichever list it currently occupies.
IKernelTask * GetNext() override
Select and return the task with the earliest (minimum) relative deadline.