wire / wire-format / 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).
aka: Spinlock (busy-wait lock)
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).
| field | size | meaning |
|---|---|---|
| Test-and-set (TAS) | atomic | Atomically set lock to 1 and return old value. If old value was 0, lock acquired. If 1, spin. |
| Compare-and-swap (CAS) | atomic | Atomically compare lock with expected value; if equal, set to new value. Returns success/failure. |
| Ticket lock | variant | FIFO fairness: each thread gets a ticket number. Threads acquire lock in ticket order. Prevents starvation. |
| MCS lock | variant | Queue-based: each thread spins on its own cache line (not a shared variable). Cache-friendly. Used in Linux kernel. |
| CLH lock | variant | List-based: each thread spins on its predecessor's node. Simpler than MCS. |
| Adaptive spinlock | variant | Spin for a while, then sleep (yield to OS). Balances latency and CPU usage. |
example:
00 00 00 00
Spinlock (placeholder)
agent: curl -H 'accept: application/json' wire.phall.io/wire-format/spinlock
or /wire-format/spinlock.json