An interrupt handler is a routine that runs when a hardware or software interrupt occurs. The event is delivered through a vector table; the processor saves a small register context, transfers control to the handler, then restores the context so interrupted code can resume. section 1.2.4 section 1.4.4
Handlers can interrupt the main program at unpredictable times. When both contexts access the same data or peripheral, race conditions and data corruption can result. Rust makes sharing explicit and can restrict access to an appropriate synchronization mechanism. section 1.4.4 section 4.6
Embedded Rust uses several approaches to control shared access:
- For simple cases, a short critical section disables interrupts around an access; its permission to access shared state lasts only while the section is active. section 1.4.4
- RTIC assigns task priorities and derives permitted references for compiler checking. Embassy coordinates async tasks and shared devices through nonblocking drivers with exclusive-access APIs. section 1.4.4
Figure 4.9 Race condition with shared variables
The figure illustrates how a race condition can arise when an interrupt handler and other code share variables. section 4.6

