Using the spinlock
In the Determining which lock to use – in practice section, you learned – practically speaking – when to use the spinlock instead of the mutex lock and vice versa. For convenience, we have reproduced the key statements we provided previously here:
- Is the critical section running in an atomic (for example, interrupt) context, or in process context where it cannot sleep? Use the spinlock.
- Is the critical section running in process context and is sleep or blocking I/O in the critical section possible? Use the mutex lock.
In this section, we shall consider that you’ve now decided to use the spinlock.
Spinlock – simple usage
For all the spinlock APIs, you must include the relevant header file, that is, #include <linux/spinlock.h>
.
Similar to the mutex lock, you must declare and initialize the spinlock to the unlocked state before use. The spinlock is an “object” that’s...