|
SuperTinyKernel™ RTOS 1.08.x
Lightweight, high-performance, deterministic, bare-metal C++ RTOS for resource-constrained embedded systems. MIT Open Source License.
|
Fixed-priority scheduling with Preemption-Threshold (PTS), modelled on Azure RTOS / ThreadX. Each task has a priority P and a threshold T with T >= P. While a task is running it can be preempted only by tasks whose priority is strictly greater than T. With T == P the task behaves like a normal fixed-priority task.
More...
#include <stk_strategy_pthreshold.h>
Public Types | |
| enum | EConfig { WEIGHT_API = 1 , SLEEP_EVENT_API = 1 , DEADLINE_MISSED_API = 0 , PRIORITY_INHERITANCE_API = 1 , NOSLEEP_YIELD_API = 1 , TIMESLICE_API = (TIMESLICE ? 1 : 0) } |
| Compile-time capability flags reported to the kernel. More... | |
Public Member Functions | |
| SwitchStrategyPreemptionThreshold () | |
| Construct an empty strategy. | |
| STK_VIRT_DTOR | ~SwitchStrategyPreemptionThreshold ()=default |
| Destructor. | |
| void | AddTask (IKernelTask *task) override |
| Add task to the runnable set at its priority level. | |
| void | RemoveTask (IKernelTask *task) override |
| Remove task from whichever list it occupies (drops its hold if any). | |
| IKernelTask * | GetNext () override |
| Select the next task to run. | |
| IKernelTask * | GetFirst () override |
| First task in the managed set (highest ready level, else first sleeping). | |
| size_t | GetSize () const override |
| Total number of tasks (runnable + sleeping). | |
| void | OnTaskSleep (IKernelTask *task) override |
| Task went to sleep: leaves the runnable set and releases its hold. | |
| void | OnTaskWake (IKernelTask *task) override |
| Task became runnable again; re-enters its priority level (not held). | |
| void | OnTaskWeightChange (IKernelTask *task, Weight old_weight) override |
| Task weight changed (priority inheritance / restore): move to the new level. | |
| bool | OnTaskYield (IKernelTask *task) override |
| Yield: hand the CPU to the next task of the same priority. | |
| bool | OnTaskTimeSlice (IKernelTask *task) override |
| Time slice of the running task expired: rotate to the next task of the same priority. | |
| virtual bool | OnTaskDeadlineMissed (IKernelTask *task) |
| Notification that a task has exceeded its HRT deadline; returns whether the strategy can recover without a hard fault. | |
Static Public Member Functions | |
| static constexpr Weight | MakeWeight (uint8_t priority, uint8_t threshold) |
| Build a task weight from an STK priority and threshold. | |
| static constexpr Weight | FromThreadX (uint8_t tx_priority, uint8_t tx_threshold) |
| Build a task weight from ThreadX numbers (0 = highest priority). | |
| static uint8_t | ChangeThreshold (IKernelService &service, TId tid, uint8_t priority, uint8_t threshold) |
| Change the preemption-threshold of a task at run-time (tx_thread_preemption_change). | |
Protected Types | |
| typedef uint8_t | Priority |
| Priority type. | |
Protected Member Functions | |
| void | AddActive (IKernelTask *task) |
| void | RemoveActive (IKernelTask *task, const Priority prio) |
| void | UpdateHold (IKernelTask *task, const Priority prio) |
| Apply a threshold change to a runnable task that stays on the same level. | |
Static Protected Member Functions | |
| static __stk_forceinline Priority | DecodePriority (Weight weight) |
| static __stk_forceinline Priority | GetTaskPriority (IKernelTask *task) |
| static __stk_forceinline Priority | DecodeThreshold (Weight weight) |
| static __stk_forceinline Priority | GetRawThreshold (IKernelTask *task) |
| Configured threshold from the task's base weight (unaffected by priority inheritance). | |
| static __stk_forceinline Priority | GetTaskThreshold (IKernelTask *task) |
| static __stk_forceinline Priority | GetHighestReadyPriority (uint32_t bitmap) |
Private Member Functions | |
| STK_NONCOPYABLE_CLASS (SwitchStrategyPreemptionThreshold) | |
Private Attributes | |
| IKernelTask::ListHeadType | m_tasks [MAX_PRIORITIES] |
| Runnable tasks per priority level. | |
| IKernelTask::ListHeadType | m_sleep |
| Sleeping tasks. | |
| uint32_t | m_ready_bitmap |
| Bit i set when m_tasks[i] is non-empty. | |
| uint32_t | m_held_bitmap |
| Bit i set when m_held[i] is a task holding its preemption-threshold. | |
| uint32_t | m_advance_bitmap |
| Bit i set: next GetNext() at level i advances the cursor (TIMESLICE only). | |
| IKernelTask * | m_running |
| Task returned by the last GetNext() (nullptr if none or it left the runnable set). | |
| IKernelTask * | m_prev [MAX_PRIORITIES] |
| Per-level round-robin cursor: last task returned by GetNext() (sticky mode: the selected task, kept until a rotation is requested). | |
| IKernelTask * | m_held [MAX_PRIORITIES] |
| Per-level task currently holding its threshold (at most one per level, as in ThreadX). | |
Fixed-priority scheduling with Preemption-Threshold (PTS), modelled on Azure RTOS / ThreadX. Each task has a priority P and a threshold T with T >= P. While a task is running it can be preempted only by tasks whose priority is strictly greater than T. With T == P the task behaves like a normal fixed-priority task.
| MAX_PRIORITIES | Number of priority levels, [1, 32]. |
| TIMESLICE | true to enable time-slicing with per-task quanta for use with stk::KERNEL_TIMESLICE: GetNext() becomes sticky (the selected task of a level keeps running) and the level is rotated only by OnTaskTimeSlice(), OnTaskYield() or when the running task leaves the runnable set. false (default): fixed 1-tick round-robin on every GetNext(). |
T > P. The hold lasts until the task sleeps, is removed, yields (see OnTaskYield()), or its threshold is lowered to its priority; if its priority changes (priority inheritance) the hold moves to the new level when that level's hold slot is free. A held task that is preempted by a task above its threshold remains held and is resumed as soon as no ready task is above its threshold (this mirrors ThreadX's behaviour where a preempted threshold-thread is resumed before ready tasks inside its protected band).true. MakeWeight(p, p) for tasks which must be. Definition at line 83 of file stk_strategy_pthreshold.h.
|
protected |
Priority type.
Definition at line 437 of file stk_strategy_pthreshold.h.
| enum stk::SwitchStrategyPreemptionThreshold::EConfig |
Compile-time capability flags reported to the kernel.
| Enumerator | |
|---|---|
| WEIGHT_API | GetWeight() carries the encoded priority and preemption-threshold. |
| SLEEP_EVENT_API | Requires OnTaskSleep() / OnTaskWake() to maintain per-priority lists. |
| DEADLINE_MISSED_API | Not used. |
| PRIORITY_INHERITANCE_API | OnTaskWeightChange() events are handled. |
| NOSLEEP_YIELD_API | OnTaskYield() events are handled. |
| TIMESLICE_API | Sticky GetNext() and OnTaskTimeSlice() (see TIMESLICE template parameter). |
Definition at line 89 of file stk_strategy_pthreshold.h.
|
inlineexplicit |
Construct an empty strategy.
Definition at line 120 of file stk_strategy_pthreshold.h.
|
default |
Destructor.
|
inlineprotected |
Definition at line 439 of file stk_strategy_pthreshold.h.
Referenced by stk::SwitchStrategyPreemptionThreshold< 32 >::AddTask(), stk::SwitchStrategyPreemptionThreshold< 32 >::OnTaskWake(), and stk::SwitchStrategyPreemptionThreshold< 32 >::OnTaskWeightChange().
|
inlineoverridevirtual |
Add task to the runnable set at its priority level.
| [in] | task | Task to add. Its weight must be valid (priority < MAX_PRIORITIES, priority <= threshold < MAX_PRIORITIES). |
Implements stk::ITaskSwitchStrategy.
Definition at line 134 of file stk_strategy_pthreshold.h.
|
inlinestatic |
Change the preemption-threshold of a task at run-time (tx_thread_preemption_change).
| [in] | service | Kernel service (IKernelService::GetInstance()). |
| [in] | tid | Task id (IKernelService::GetTid() for the calling task). |
| [in] | priority | The task's configured STK priority (unchanged by this call). |
| [in] | threshold | New threshold, [priority, MAX_PRIORITIES - 1]. |
Definition at line 427 of file stk_strategy_pthreshold.h.
|
inlinestaticprotected |
Definition at line 519 of file stk_strategy_pthreshold.h.
Referenced by stk::SwitchStrategyPreemptionThreshold< 32 >::GetTaskPriority(), and stk::SwitchStrategyPreemptionThreshold< 32 >::OnTaskWeightChange().
|
inlinestaticprotected |
Definition at line 529 of file stk_strategy_pthreshold.h.
Referenced by stk::SwitchStrategyPreemptionThreshold< 32 >::ChangeThreshold(), and stk::SwitchStrategyPreemptionThreshold< 32 >::GetRawThreshold().
|
inlinestaticconstexpr |
Build a task weight from ThreadX numbers (0 = highest priority).
| [in] | tx_priority | ThreadX priority, [0, MAX_PRIORITIES - 1]. |
| [in] | tx_threshold | ThreadX preemption-threshold, [0, tx_priority]. |
Definition at line 112 of file stk_strategy_pthreshold.h.
|
inlineoverridevirtual |
First task in the managed set (highest ready level, else first sleeping).
Implements stk::ITaskSwitchStrategy.
Definition at line 242 of file stk_strategy_pthreshold.h.
|
inlinestaticprotected |
Definition at line 551 of file stk_strategy_pthreshold.h.
Referenced by stk::SwitchStrategyPreemptionThreshold< 32 >::GetFirst(), and stk::SwitchStrategyPreemptionThreshold< 32 >::GetNext().
|
inlineoverridevirtual |
Select the next task to run.
nullptr if nothing is runnable. hi. 2) If a task is held (the highest-priority holder H) and hi <= threshold(H), H keeps/resumes the CPU. 3) Otherwise round-robin inside level hi; if the chosen task has threshold > priority it becomes held. false) the cursor of the level advances on every call. In sticky mode (TIMESLICE = true) the cursor advances only if a rotation was requested for the level (OnTaskYield(), OnTaskTimeSlice() or the removal of the task the cursor was on), otherwise the same task is returned. Implements stk::ITaskSwitchStrategy.
Definition at line 187 of file stk_strategy_pthreshold.h.
|
inlinestaticprotected |
Configured threshold from the task's base weight (unaffected by priority inheritance).
Definition at line 535 of file stk_strategy_pthreshold.h.
Referenced by stk::SwitchStrategyPreemptionThreshold< 32 >::AddTask(), and stk::SwitchStrategyPreemptionThreshold< 32 >::GetTaskThreshold().
|
inlineoverridevirtual |
Total number of tasks (runnable + sleeping).
Implements stk::ITaskSwitchStrategy.
Definition at line 262 of file stk_strategy_pthreshold.h.
Referenced by stk::SwitchStrategyPreemptionThreshold< 32 >::AddActive(), stk::SwitchStrategyPreemptionThreshold< 32 >::GetFirst(), and stk::SwitchStrategyPreemptionThreshold< 32 >::RemoveTask().
|
inlinestaticprotected |
Definition at line 524 of file stk_strategy_pthreshold.h.
Referenced by stk::SwitchStrategyPreemptionThreshold< 32 >::AddActive(), stk::SwitchStrategyPreemptionThreshold< 32 >::AddTask(), stk::SwitchStrategyPreemptionThreshold< 32 >::GetTaskThreshold(), stk::SwitchStrategyPreemptionThreshold< 32 >::OnTaskSleep(), stk::SwitchStrategyPreemptionThreshold< 32 >::OnTaskTimeSlice(), stk::SwitchStrategyPreemptionThreshold< 32 >::OnTaskWeightChange(), stk::SwitchStrategyPreemptionThreshold< 32 >::OnTaskYield(), and stk::SwitchStrategyPreemptionThreshold< 32 >::RemoveTask().
|
inlinestaticprotected |
Effective threshold = max(configured threshold, current priority). While the task inherits a higher priority its protected band is raised to at least that priority but never loses the configured threshold.
Definition at line 543 of file stk_strategy_pthreshold.h.
Referenced by stk::SwitchStrategyPreemptionThreshold< 32 >::GetNext(), stk::SwitchStrategyPreemptionThreshold< 32 >::OnTaskWeightChange(), and stk::SwitchStrategyPreemptionThreshold< 32 >::UpdateHold().
|
inlinestaticconstexpr |
Build a task weight from an STK priority and threshold.
| [in] | priority | Task priority, [0, MAX_PRIORITIES - 1], higher = more important. |
| [in] | threshold | Preemption-threshold, [priority, MAX_PRIORITIES - 1]. |
Definition at line 103 of file stk_strategy_pthreshold.h.
Referenced by stk::SwitchStrategyPreemptionThreshold< 32 >::FromThreadX().
|
inlinevirtualinherited |
Notification that a task has exceeded its HRT deadline; returns whether the strategy can recover without a hard fault.
| [in] | task | The task whose deadline was missed. Must not be nullptr. |
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. 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(). 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." false (unrecoverable), which is the correct default for strategies that do not implement overrun recovery. Definition at line 1487 of file stk_common.h.
References STK_UNUSED.
|
inlineoverridevirtual |
Task went to sleep: leaves the runnable set and releases its hold.
Implements stk::ITaskSwitchStrategy.
Definition at line 277 of file stk_strategy_pthreshold.h.
|
inlineoverridevirtual |
Time slice of the running task expired: rotate to the next task of the same priority.
| [in] | task | The running task. |
true if the level was rotated; false if the task has no same-priority peer, is held by its preemption-threshold (held tasks are never time-sliced, as in ThreadX), is not in a runnable list, or the strategy is not a TIMESLICE one. false is returned the task simply keeps running, the kernel has already reloaded its slice. Reimplemented from stk::ITaskSwitchStrategy.
Definition at line 390 of file stk_strategy_pthreshold.h.
|
inlineoverridevirtual |
Task became runnable again; re-enters its priority level (not held).
Implements stk::ITaskSwitchStrategy.
Definition at line 291 of file stk_strategy_pthreshold.h.
|
inlineoverridevirtual |
Task weight changed (priority inheritance / restore): move to the new level.
Reimplemented from stk::ITaskSwitchStrategy.
Definition at line 305 of file stk_strategy_pthreshold.h.
|
inlineoverridevirtual |
Yield: hand the CPU to the next task of the same priority.
| [in] | task | The yielding (running) task. |
true: the task stays runnable and is not put to sleep by the kernel; false if the task is not in a runnable list (kernel falls back to a sleep-yield). Reimplemented from stk::ITaskSwitchStrategy.
Definition at line 354 of file stk_strategy_pthreshold.h.
|
inlineprotected |
Definition at line 452 of file stk_strategy_pthreshold.h.
Referenced by stk::SwitchStrategyPreemptionThreshold< 32 >::OnTaskSleep(), stk::SwitchStrategyPreemptionThreshold< 32 >::OnTaskWeightChange(), and stk::SwitchStrategyPreemptionThreshold< 32 >::RemoveTask().
|
inlineoverridevirtual |
Remove task from whichever list it occupies (drops its hold if any).
Implements stk::ITaskSwitchStrategy.
Definition at line 159 of file stk_strategy_pthreshold.h.
|
private |
|
inlineprotected |
Apply a threshold change to a runnable task that stays on the same level.
Definition at line 498 of file stk_strategy_pthreshold.h.
Referenced by stk::SwitchStrategyPreemptionThreshold< 32 >::OnTaskWeightChange().
|
private |
Bit i set: next GetNext() at level i advances the cursor (TIMESLICE only).
Definition at line 563 of file stk_strategy_pthreshold.h.
|
private |
Per-level task currently holding its threshold (at most one per level, as in ThreadX).
Definition at line 566 of file stk_strategy_pthreshold.h.
|
private |
Bit i set when m_held[i] is a task holding its preemption-threshold.
Definition at line 562 of file stk_strategy_pthreshold.h.
|
private |
Per-level round-robin cursor: last task returned by GetNext() (sticky mode: the selected task, kept until a rotation is requested).
Definition at line 565 of file stk_strategy_pthreshold.h.
|
private |
Bit i set when m_tasks[i] is non-empty.
Definition at line 561 of file stk_strategy_pthreshold.h.
|
private |
Task returned by the last GetNext() (nullptr if none or it left the runnable set).
Definition at line 564 of file stk_strategy_pthreshold.h.
|
private |
Sleeping tasks.
Definition at line 560 of file stk_strategy_pthreshold.h.
|
private |
Runnable tasks per priority level.
Definition at line 559 of file stk_strategy_pthreshold.h.