Technical mental model
PendSV is commonly used for deferred context switching because it can run at low priority and avoid disrupting urgent interrupts. Correctness depends on precise save/restore sequencing.
Key low-level points:
- Hardware-stacked and software-saved context portions must be integrated correctly.
- Task pointer updates must be atomic relative to saved context ownership.
- Exception return path must match stack frame format exactly.
Equations and constraints that drive decisions
Switch-time decomposition:
Where:
- : scheduler next-task selection cost
Implementation walkthrough
Save current task context into its TCB before any next-task pointer mutation.
Run deterministic task selection with bounded execution time.
Restore next task context in reverse order of save contract.
Trace one full switch in debugger and verify register parity.
Validation and debugging checklist
Incorrect register order is a high-probability hard fault source.
Scheduler pointer races produce intermittent corruption symptoms.
FPU-enabled builds require additional context policy decisions.
ISR nesting assumptions must match mask/priority configuration.
A robust PendSV path is one where every saved field has one unambiguous restore point.