Conceptual
Login

Using a SpinLock Where Sleeping Is Forbidden

Inside an interrupt handler the code may not sleep, so a mutex is out of the question, yet the data still needs protecting. A spinlock is the answer: a waiting processor simply loops until the lock frees, which is cheap when the critical section is a few instructions and disastrous when it is long. In kernel Rust it has the same shape as the mutex, data inside, guard out, so the choice between them is a one-word edit driven purely by which context the lock is taken in. Taking a mutex from an interrupt handler, or holding a spinlock while allocating with the sleeping flag, is a classic machine freeze.

Questions this Concept answers

  • Why must an interrupt handler use a spinlock rather than a mutex for state it shares with the rest of the driver?