C12 · Lesson 37 · Superloops and cooperative scheduling
Several jobs on one processor, each taking turns
A real device does several things at once: blinks a status light, samples a sensor, answers a serial port, runs a control loop. A single processor does one thing at a time, so the program has to share it out. The simplest way, and the one most small firmware uses, is the superloop: an endless loop that offers each job, a task, a turn, where every task does a little work and returns quickly. As long as no task hogs its turn, they all appear to run together. It only takes one task that waits inside itself to break the illusion for everyone.
- Three tasks share the loop: P0 toggles every 250 ms, P1 every 100 ms, and a status line goes to the terminal every 500 ms. Each task runs only when its next due time has come, then returns.
- On the timeline, P0 and P1 keep perfect time, interleaved.
- Set
BLOCKINGto 1: the status task now also does 150 ms of slow work in its turn. Run it and look at P1: whenever the status task runs, P1's toggles are delayed, and their spacing goes ragged.
Predict
In a superloop, one task can take up to 20 ms per turn and never gives the processor up early. How late can another task's turn be?
Up to about 20 ms: in a cooperative system no task can take the processor back from another, so a task whose turn comes while the slow one is running waits until it returns. The worst-case lateness of every task is set by the longest single turn of any other.
Name it
The superloop
while (1) {
task_a();
task_b();
task_c();
}
Each task does a short piece of work and returns, keeping whatever it needs to remember between turns in its own state (often a state machine, lesson 36). Time-triggered versions, like the play's, run each task only when its period has passed, keeping a next due time per task; comparing with a signed difference, (int32_t)(now - next) >= 0, survives the clock wrapping.
Rules
- Never block: no delay loops, no waiting for a slow device. Start the operation, return, and check on it next turn.
- The worst-case delay for any task is about the longest single turn of all the others, plus one pass of the loop.
- Urgent events go to interrupts, which pre-empt the loop; the ISR records the event and the task handles it.
Utilization
A task taking every uses of the processor; the total
must stay below 1, with margin for interrupts and lateness.
Beyond the superloop
A real-time operating system (RTOS) gives each task its own stack and switches between them pre-emptively, by priority, on a timer interrupt: a slow low-priority task can no longer delay an urgent one. The price is memory for each task's stack, context-switch time, and new hazards, such as two tasks sharing data, which need mutexes.
Worked example
Three tasks run 0.5 ms every 10 ms, 2 ms every 50 ms and 20 ms every 500 ms. What's the utilization?
Show the solution
- Add each task's share. 13% of the processor, leaving plenty of room.
- Check the long task. The 20 ms task can delay the 10 ms task's turn by 20 ms, two whole periods, so it should be split into 1 ms pieces.
Answer 13%, though the 20 ms task should be split into 1 ms pieces.
Worked example
How can the 150 ms of slow work in the play be done without blocking?
Show the solution
- Split the work. Each turn of
statusdoes 1 ms of work and returns. - Remember progress. A counter (a tiny state machine) records how far it got between turns.
- The result. The work still takes 150 ms in total, spread over 150 turns, and the other tasks run between the pieces.
Answer do it in 1 ms pieces, one per turn of status, with a counter recording how far it got.
Worked example
A superloop runs three tasks in turn, taking at most 0.5 ms, 2 ms and 20 ms. What is the worst-case wait for a task to get its next turn?
Show the solution
- The worst case. The task has just finished and must wait for the other two at their longest.
- For the first task.
- What it means. One long task sets the response time of all the others, which is why long work is split into slices or moved to an interrupt.
Answer 22 ms for the first task.
Check
Task 1 takes 0.2 ms every 5 ms; task 2 takes 10 ms every 250 ms. What's the processor's utilization?
Check
In a superloop, task A's turns take up to 0.5 ms and task B's up to 8 ms. How late can each task's turn start, at worst, because of the other? Give the larger of the two.
Write units the way you normally would: 1.5 ms, 1500 µs and 0.0015 s are the same answer.
Check
A task runs every 25 ms. How many times does it run in 1 seconds?
Check
A task's 60 ms of work is split into pieces of 1 ms, one per turn. How many turns does it take?
Check
A task's next due time is 4294967290 ms on a 32-bit clock, and its period is 10 ms. After next += period, what does next hold?
Check
A superloop task needs to wait 50 ms for a sensor to finish a measurement. What should it do?
Check
Toggle P0 every A ms (125) and P1 every B ms (30) from one superloop, without blocking either.
C on the course's RISC-V board (10 MHz, no floating point). #include "board.h" for the registers; print(x) shows a number. Tab indents; press Esc and then Tab to move on.
Check
The same in MicroPython: toggle P0 every 100 ms and P1 every 40 ms from one loop.
MicroPython on the course's board: machine (Pin, PWM, ADC, DAC, UART, I2C, SPI, Timer, WDT) and time. Each call into the hardware takes about 40 µs. Tab indents; press Esc and then Tab to move on.
Check
Three tasks run 0.5 ms every 5 ms, 4 ms every 50 ms and 5 ms every 100 ms.
(a)
What is the processor's utilization?
(b)
In a round-robin superloop, what's the longest the first task can wait for its turn?
Write units the way you normally would: 1.5 ms, 1500 µs and 0.0015 s are the same answer.
Check
150 ms of slow work is split into slices of 5 ms so other tasks can run between them.
(a)
How many slices?
(b)
What is the longest it now holds the processor at once?
Write units the way you normally would: 1.5 ms, 1500 µs and 0.0015 s are the same answer.
Explain why
Why does one blocking task ruin the timing of every other task in a superloop?
In a cooperative superloop, a task gives up the processor only by returning. While a task sits in a delay loop or waits for a slow device, the loop can't move on, so every other task's due time passes unnoticed and they all run late, by up to the blocking task's whole wait. Their lateness also varies with where in the blocking task's turn their due time fell, so their timing goes ragged. Splitting slow work into short pieces, or starting an operation and checking on it in later turns, keeps every turn short and bounds everyone's lateness to the longest short turn.
Practice
Keep practicing
12 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.