Glossary term

Spinlock

Engineering definition of a spinlock covering busy waiting, hold-time bounds, contention cost, backoff, fairness and validation evidence.

Definition

concept

A spinlock is a mutual-exclusion lock in which a waiting task repeatedly checks the lock state instead of sleeping.

Spinlocks are used in kernels, low-level runtimes, interrupt-sensitive firmware and short multicore critical sections when the expected wait is shorter than the cost of sleeping and waking. A useful design states the protected invariant, maximum hold time, preemption and interrupt policy, atomic primitive, backoff rule, fairness behavior, cache effect, watchdog consequence and validation evidence.

A spinlock is a mutual-exclusion lock in which a waiting task repeatedly checks the lock state instead of sleeping. It can be faster than a blocking mutex when the critical section is extremely short, but it wastes CPU time while waiting.

Spinlocks appear in kernels, low-level runtimes, device drivers, embedded firmware and multicore synchronization paths. They are not a general replacement for mutexes. A spinlock is appropriate only when hold time is bounded, the owner will not sleep, and the cost of blocking and waking would be larger than the expected spin.

Busy-Wait Model

Let the lock hold time seen by a waiter be:

T_{hold}

and let lock acquisition polling cost per attempt be:

C_{poll}

If polling happens at rate:

f_{poll}

then the approximate CPU time spent spinning is:

T_{spin}\approx T_{hold}

and the number of polls is:

N_{poll}\approx f_{poll}T_{hold}

The waiting CPU is doing no useful work during that time. This is the core tradeoff.

Contention Cost

If there are:

N_w

waiters spinning for average time:

\bar{T}_{spin}

then aggregate busy-wait CPU demand is:

C_{cpu}=N_w\bar{T}_{spin}

per acquisition event. A single short spin may be acceptable. Many waiters spinning on the same cache line can create memory traffic, reduce useful throughput and delay unrelated work.

Hold-Time Bound

A spinlock should have a documented maximum hold time:

T_{hold,max}

For a task or interrupt path with response budget:

D

and other response terms:

T_{other}

the margin is:

M_D=D-T_{hold,max}-T_{other}

The design passes this screen only when:

M_D\geq0

If the lock owner can be preempted, page fault, allocate memory, wait for I/O or sleep while holding the spinlock, the bound can fail catastrophically.

Atomic Operation

Spinlocks are commonly built from atomic operations such as test-and-set, exchange or compare-and-swap. A simplified acquire loop is:

CAS(L,0,1)

where L=0 means unlocked and L=1 means locked. The lock release must publish protected data before making the lock available:

\text{store protected state before }L=0

That ordering may require release semantics or a memory barrier on weakly ordered architectures.

Backoff and Fairness

Simple test-and-set loops can create heavy cache-coherence traffic. Backoff inserts delay between attempts. If backoff interval is:

B_k

for attempt:

k

then increasing B_k can reduce bus traffic but also increase latency. Ticket locks and queue-based locks improve fairness by controlling acquisition order, but they can cost more per acquisition.

Fairness is a requirement, not a default property. A spinlock without fairness can starve a task under repeated reacquisition by other cores.

Worked Timing Check

A firmware kernel uses a spinlock around a per-core statistics update. The measured maximum hold time is:

T_{hold,max}=6\ \mu\text{s}

Other response terms for the monitored path are:

T_{other}=31\ \mu\text{s}

The response budget is:

D=50\ \mu\text{s}

The margin is:

M_D=50-6-31=13\ \mu\text{s}

The screen passes. If later logging expands the critical section to:

T_{hold,max}=28\ \mu\text{s}

then:

M_D=50-28-31=-9\ \mu\text{s}

The spinlock now violates the response budget even though the code still looks like a small protected section.

Boundary With Mutexes

A mutex normally blocks or sleeps a waiter. A spinlock keeps the waiter running. The spinlock is better only when the expected wait is shorter than the sleep-and-wakeup overhead and when the system can afford burning CPU during the wait. On a single-core system, spinning while the owner cannot run is usually a design error unless interrupts or special scheduler rules guarantee release.

Failure Modes

Common failure modes include spinning while the owner is preempted, holding the lock during I/O, cache-line bouncing, unfair reacquisition, priority inversion, interrupt masking that grows beyond its bound, missing memory barriers, livelock under excessive contention and watchdog resets caused by busy-wait loops.

Validation Evidence

Useful evidence includes maximum hold-time traces, spin duration distribution, waiter count, cache-coherence or bus counters, preemption state while held, interrupt-mask state, backoff behavior, fairness tests, watchdog behavior and regression rules for every protected section. A release claim should show that the worst spin path remains inside latency and deadline budgets under load.

REF

See also