Skip to content

spin v0.1.5 — TTAS slow path with adaptive backoff

Choose a tag to compare

@hayabusa-cloud hayabusa-cloud released this 16 Feb 13:33
· 3 commits to main since this release
88e1258

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 Lock slow path to TTAS: spin on read-only Load, attempt FAA only when lock appears free (#8)

Documentation

  • Document FAA/TTAS design rationale in Lock type 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