wire / wire-format / spinlock

Spinlock (busy-wait lock)

A spinlock is a lock where the acquiring thread busy-waits (spins) until the lock is available. Implementation: atomic test-and-set (TAS) or compare-and-swap (CAS). On contention, the thread spins (wastes CPU) but avoids context switch overhead. Used for short critical sections (< 100ns). Variants: ticket lock (FIFO fairness), MCS lock (queue-based, cache-friendly), CLH lock (list-based).

wire-format kind wire-format status standard verification verified tier B wire-format@1

aka: Spinlock (busy-wait lock)

wire format

spec: Spinlock

A spinlock is a lock where the acquiring thread busy-waits (spins) until the lock is available. Implementation: atomic test-and-set (TAS) or compare-and-swap (CAS). On contention, the thread spins (wastes CPU) but avoids context switch overhead. Used for short critical sections (< 100ns). Variants: ticket lock (FIFO fairness), MCS lock (queue-based, cache-friendly), CLH lock (list-based).

fieldsizemeaning
Test-and-set (TAS)atomicAtomically set lock to 1 and return old value. If old value was 0, lock acquired. If 1, spin.
Compare-and-swap (CAS)atomicAtomically compare lock with expected value; if equal, set to new value. Returns success/failure.
Ticket lockvariantFIFO fairness: each thread gets a ticket number. Threads acquire lock in ticket order. Prevents starvation.
MCS lockvariantQueue-based: each thread spins on its own cache line (not a shared variable). Cache-friendly. Used in Linux kernel.
CLH lockvariantList-based: each thread spins on its predecessor's node. Simpler than MCS.
Adaptive spinlockvariantSpin for a while, then sleep (yield to OS). Balances latency and CPU usage.

example:

00 00 00 00

Spinlock (placeholder)


provenance

agent: curl -H 'accept: application/json' wire.phall.io/wire-format/spinlock or /wire-format/spinlock.json