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:
80
84 {}
85
90
100 void AddTask(IKernelTask *task) override
101 {
102 STK_ASSERT(task != nullptr);
103 STK_ASSERT(task->GetHead() == nullptr);
104
105 m_tasks.LinkBack(task);
106 }
107
114 void RemoveTask(IKernelTask *task) override
115 {
116 STK_ASSERT(task != nullptr);
117 STK_ASSERT(GetSize() != 0U);
118 STK_ASSERT((task->GetHead() == &m_tasks) || (task->GetHead() == &m_sleep));
119
120 if (task->GetHead() == &m_tasks)
121 {
122 m_tasks.Unlink(task);
123 }
124 else
125 {
126 m_sleep.Unlink(task);
127 }
128 }
129
146 {
147 IKernelTask *next = nullptr;
148
149 if (!m_tasks.IsEmpty())
150 {
151 IKernelTask *const start = (*m_tasks.GetFirst());
152
153 next = start; // initialize earliest found task to the first one
154
155 Timeout earliest = next->GetHrtRelativeDeadline();
156 IKernelTask *itr = (*start->GetNext());
157
158 while (itr != start)
159 {
160 const Timeout deadline = itr->GetHrtRelativeDeadline();
161
162 if (deadline < earliest)
163 {
164 earliest = deadline;
165 next = itr;
166 }
167
168 itr = (*itr->GetNext());
169 }
170 }
171
172 return next;
173 }
174
183 {
184 STK_ASSERT(GetSize() != 0U);
185
186 return (*(!m_tasks.IsEmpty() ? m_tasks.GetFirst() : m_sleep.GetFirst()));
187 }
188
192 size_t GetSize() const override
193 {
194 return m_tasks.GetSize() + m_sleep.GetSize();
195 }
196
203 void OnTaskSleep(IKernelTask *task) override
204 {
205 STK_ASSERT(task != nullptr);
206 STK_ASSERT(task->IsSleeping());
207 STK_ASSERT(task->GetHead() == &m_tasks);
208
209 m_tasks.Unlink(task);
210 m_sleep.LinkBack(task);
211 }
212
221 void OnTaskWake(IKernelTask *task) override
222 {
223 STK_ASSERT(task != nullptr);
224 STK_ASSERT(!task->IsSleeping());
225 STK_ASSERT(task->GetHead() == &m_sleep);
226
227 m_sleep.Unlink(task);
228 m_tasks.LinkBack(task);
229 }
230
231protected:
233
236};
237
238} // namespace stk
239
240#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:170
Scheduling-strategy-facing interface for a kernel task slot.
Definition stk_common.h:963
virtual Timeout GetHrtRelativeDeadline() const =0
Get HRT task's relative deadline.
DLHeadType ListHeadType
List head type for IKernelTask elements.
Definition stk_common.h:968
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.
@ TIMESLICE_API
This strategy does not support KERNEL_TIMESLICE mode.
@ 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.