Interrupts and deferred handling
Why an interrupt handler should only signal a task, how the task runs the moment the interrupt returns, and what happens to interrupts that arrive while it is busy.
Simulated kernel: FreeRTOS V11.3.3 and Zephyr v4.5.0. This scenario and every other one are in the scheduler playground. The simulator and its data files are not covered by the site's CC BY-SA license; see their license notices.
Hardware does not wait for the scheduler. A UART receives a byte, a timer expires, a button is pressed, and the CPU stops whatever it was doing to run an interrupt service routine (ISR). The interrupted task does not choose this and cannot delay it; the ISR runs before any task, however urgent.
That is exactly why an ISR should do as little as possible. While it runs, no task runs, and other interrupts of the same or lower priority wait. The usual design is deferred interrupt handling: the ISR does only what cannot wait (read the byte, clear the interrupt), signals a task, and returns. The task does the real work, at a priority the designer chose, where it can block, take mutexes and be preempted like any other task.
The scenario
Handler(most urgent) loops for ever: take the semaphoreS, then work for two ticks.Mainis a less urgent task with fourteen ticks of work.- The
UARTinterrupt fires at ticks 3, 8, 9 and 15. Its handler givesSand nothing else.
S is a binary semaphore that starts empty. The interrupt lane under the tasks
on the timeline marks each interrupt.
The simulator makes two simplifications. An interrupt arrives right after a tick, before the context switch that tick may ask for, never in the middle of a tick or of a kernel call. And an ISR takes no time. Real interrupts arrive at any moment and take a few microseconds; neither changes what the kernel does with the signal.
What happens
- Tick 0:
Handlerruns first and blocks on the emptyS.Maingets the CPU. - Tick 3: the interrupt fires while
Mainis running. The handler givesS.Handlerwas waiting for it, so the kernel makes it ready. It is more urgent thanMain, so the kernel switches to it as soon as the interrupt returns:Handlerruns during tick 3. The delay between the interrupt and the start of the real work is the interrupt-to-task latency, here zero ticks. Handlerworks for two ticks, loops, and at tick 5 takesSagain. It is empty, soHandlerblocks andMaincontinues.- Tick 8: the same again.
- Tick 9: the interrupt fires while
Handleris still busy with the previous one. Nobody waits forS, so the give only sets it to 1. Nothing is lost: whenHandlerloops at tick 10 it takesSat once, without blocking, and handles the second event. - Tick 15: one more event, handled at once.
Mainfinishes at tick 22.
Main was interrupted four times and preempted three times, and nothing in its code knows about
it. That is the point of an RTOS: the urgent work is organised by priority, not by inserting checks
into the main loop.
Leaving the interrupt: who switches tasks?
An ISR runs in the middle of whatever task it interrupted (on a Cortex-M, on a separate interrupt stack). It must not switch tasks in the middle of itself; the switch happens on the way out. The two kernels arrange this differently, and you can see it in the Kernel code tab at tick 3.
- FreeRTOS has a separate set of ISR calls.
xSemaphoreGiveFromISR()never switches tasks. It reports, inxHigherPriorityTaskWoken, whether it woke a task more urgent than the interrupted one, and the handler ends withportYIELD_FROM_ISR( xHigherPriorityTaskWoken ). On a Cortex-M that sets the PendSV exception pending, and the switch happens after the interrupt returns. Leaving out that last line is allowed, and the parameter Yield from ISR does it. - Zephyr uses the same
k_sem_give()in threads and ISRs. Inside an ISR the kernel never switches; instead, the interrupt exit code always checks whether the thread to run has changed. There is nothing to forget, and no parameter to turn off.
The walkthroughs show the code: FreeRTOS and Zephyr.
What to try
- On FreeRTOS, turn Yield from ISR off. At tick 3 the interrupt still makes
Handlerready, but nobody asks for a switch, soMaingoes on running until the next tick: the latency grows to one tick. The kernel has noted the switch inxYieldPending(watch the variables), and the tick interrupt honours it. Worse, at tick 8Handleris woken but has not run by tick 9, soSis still given and the tick-9 interrupt is lost: a binary semaphore cannot count two events.Handlerhandles three of the four interrupts. - Set Handler urgency to 1, below
Main. Now the interrupts still arrive on time, butHandlercannot run untilMainhas finished at tick 14. By then the interrupts at 8 and 9 have foundSalready given and have been lost. Deferring work to a task only works if the task is urgent enough. - Now set S is a to counting semaphore and try both again. A counting semaphore remembers how
many interrupts are pending (up to its maximum, 4 here), so with Yield from ISR off
Handlerhandles all four interrupts, though none of them in the tick it arrived. With Handler urgency at 1 it handles all four too, one after another onceMainhas finished.
Lost events are the weakness of a binary semaphore as a signal. A counting semaphore keeps count of them, and a queue keeps the data that comes with each one.