Repository navigation
spin v0.1.5 — TTAS slow path with adaptive backoff
Introduction
Package spin provides minimal spin primitives for performance-critical code paths: Lock (non-fair spinlock), Wait (adaptive spin-wait), Pause (architecture-specific CPU hint), and Yield (cooperative sleep). Designed for ultra-short critical sections where OS mutex overhead is prohibitive, with zero allocations on hot paths.
Lock acquisition uses FAA (Fetch-And-Add) for O(n) coherence traffic under contention, compared to O(n²) with CAS-based spinlocks. Architecture-specific pause instructions are provided for amd64, arm64, riscv64, loong64, ppc64le, s390x, and 386.
Usage
var lk spin.Lock
lk.Lock()
// critical section (< 1µs)
lk.Unlock()Highlights
Lock slow path rewritten to TTAS (test-and-test-and-set) with local-counter adaptive backoff, reducing cache-line invalidations under contention and guaranteeing forward progress on low-core-count machines.
What's Changed
Improvements
- Rewrite
Lockslow path to TTAS: spin on read-onlyLoad, attempt FAA only when lock appears free (#8)
Documentation
- Document FAA/TTAS design rationale in
Locktype and method comments (#8) - Document intentional FAA-on-failure behavior in
Try(#8)
Chore
- Bump Go to 1.26 (#8)
Compatibility
- Go: 1.26+
- Platforms: linux/amd64, linux/arm64, linux/riscv64, linux/loong64, linux/ppc64le, linux/s390x, linux/386, wasm