spinlock
also: spin lock
A spinlock is a synchronization primitive that causes a process to wait in a tight loop, repeatedly checking a condition, rather than sleeping until a resource becomes available. Commonly used in kernel code where context switching overhead would be too high.
A spinlock is a mutual exclusion mechanism used to protect shared resources in multithreaded or multiprocessor code. When a thread attempts to acquire a spinlock that is already held, instead of being put to sleep (context switched out), it continues to loop and repeatedly check if the lock is available.
Spinlocks are primarily used in Linux kernel code rather than user-space applications, because the busy-waiting approach wastes CPU cycles. However, this waste is acceptable in the kernel when the critical section is very short and the overhead of putting a process to sleep would be greater than the time spent spinning.
Example: when two CPUs need to access the same kernel data structure, a spinlock ensures only one CPU modifies it at a time. If CPU 1 holds the lock, CPU 2 will spin in a loop testing the lock repeatedly until CPU 1 releases it.
The counterpart is a mutex or semaphore, which puts waiting processes to sleep, making them better suited for user-space applications and longer critical sections.