| 1 |
// rwspinlock.go - high-performance reader-writer spinlock |
| 2 |
// |
| 3 |
// One uint64 per lock eliminates intra-word CAS contention that plagued |
| 4 |
// the packed 8-slot design (8 independent locks serialized on the same |
| 5 |
// atomic address). With 1M entries: 8 MB vs 1 MB — negligible compared |
| 6 |
// to cached data. |
| 7 |
// |
| 8 |
// Bit layout per uint64: |
| 9 |
// Bit 63: writer exclusive flag |
| 10 |
// Bits 0-62: reader count (max ~4.6×10¹⁸ — overflow impossible) |
| 11 |
// |
| 12 |
// Design: |
| 13 |
// RLock: optimistic atomic Add — single atomic op on uncontended path. |
| 14 |
// If writer present: undo Add, spin, retry. |
| 15 |
// RUnlock: unconditional atomic Add (subtract 1). One op, no loop. |
| 16 |
// Lock: two-phase acquire: |
| 17 |
// (1) atomic.OrUint64 to claim writer bit (one op, no CAS loop) |
| 18 |
// (2) spin until reader count drains to zero |
| 19 |
// New readers see writer bit and back off → no writer starvation. |
| 20 |
// Unlock: atomic.AndUint64 to clear writer bit. One op. |
| 21 |
// |
| 22 |
// Spin strategy (matches Go runtime sync.Mutex tuning): |
| 23 |
// First 4 iterations: runtime.procyield(30) — PAUSE on x86, YIELD on arm64 |
| 24 |
// Then: runtime.Gosched() — deschedule goroutine |
| 25 |
// For nanosecond critical sections (value update in cache slot), the lock |
| 26 |
// holder almost always releases during the PAUSE phase. |
| 27 |
|
| 28 |
package sieve |
| 29 |
|
| 30 |
import ( |
| 31 |
"sync/atomic" |
| 32 |
) |
| 33 |
|
| 34 |
const ( |
| 35 |
_WriterBit = uint64(1) << 63 |
| 36 |
_ReaderMask = _WriterBit - 1 // bits 0-62 |
| 37 |
) |
| 38 |
|
| 39 |
// rwSpinlock is an array of reader-writer spinlocks, one uint64 per slot. |
| 40 |
type rwSpinlock struct { |
| 41 |
words []uint64 |
| 42 |
} |
| 43 |
|
| 44 |
// newRWSpinlock creates an RW spinlock array for the given capacity. |
| 45 |
func newRWSpinlock(capacity int) *rwSpinlock { |
| 46 |
return &rwSpinlock{ |
| 47 |
words: make([]uint64, capacity), |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
// RLock acquires a shared read lock on slot idx. |
| 52 |
// |
| 53 |
// Uncontended fast path: one atomic.AddUint64 — no Load+CAS loop. |
| 54 |
// The Add cannot overflow into bit 63 (would require 2⁶³ concurrent readers). |
| 55 |
func (rw *rwSpinlock) RLock(idx int32) { |
| 56 |
word := &rw.words[idx] |
| 57 |
for i := 0; ; i++ { |
| 58 |
// Optimistic: bump reader count. |
| 59 |
nv := atomic.AddUint64(word, 1) |
| 60 |
if nv&_WriterBit == 0 { |
| 61 |
return // no writer — lock acquired |
| 62 |
} |
| 63 |
// Writer present: undo our increment and spin. |
| 64 |
// The writer will see readers drain and complete quickly for |
| 65 |
// nanosecond critical sections. |
| 66 |
atomic.AddUint64(word, ^uint64(0)) // subtract 1 |
| 67 |
pause(i) |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
// RUnlock releases a shared read lock on slot idx. |
| 72 |
// Single atomic op, no loop. |
| 73 |
func (rw *rwSpinlock) RUnlock(idx int32) { |
| 74 |
// Subtract 1 from reader count. Two's complement: ^uint64(0) == -1. |
| 75 |
// Safe: caller holds lock so reader count > 0; no underflow into adjacent |
| 76 |
// bits. Writer bit (63) is unaffected because no borrow propagates from |
| 77 |
// bit 62 when count > 0. |
| 78 |
atomic.AddUint64(&rw.words[idx], ^uint64(0)) |
| 79 |
} |
| 80 |
|
| 81 |
// Lock acquires an exclusive write lock on slot idx. |
| 82 |
// |
| 83 |
// Two-phase design: |
| 84 |
// |
| 85 |
// Phase 1: Claim writer bit via atomic.OrUint64 (returns old value). |
| 86 |
// One atomic op on uncontended path — no Load+CAS loop. |
| 87 |
// Phase 2: Wait for pre-existing readers to drain. |
| 88 |
// New readers see writer bit and back off, so only in-flight |
| 89 |
// readers must finish — bounded by critical section duration. |
| 90 |
func (rw *rwSpinlock) Lock(idx int32) { |
| 91 |
word := &rw.words[idx] |
| 92 |
|
| 93 |
// Phase 1: claim the writer bit. |
| 94 |
for i := 0; ; i++ { |
| 95 |
old := atomic.OrUint64(word, _WriterBit) |
| 96 |
if old&_WriterBit == 0 { |
| 97 |
break // we transitioned the bit 0→1 |
| 98 |
} |
| 99 |
// Another writer holds it. Our Or was a no-op (bit already set). |
| 100 |
pause(i) |
| 101 |
} |
| 102 |
|
| 103 |
// Phase 2: drain pre-existing readers. |
| 104 |
for i := 0; atomic.LoadUint64(word)&_ReaderMask != 0; i++ { |
| 105 |
pause(i) |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
// Unlock releases an exclusive write lock on slot idx. |
| 110 |
// Single atomic op. |
| 111 |
func (rw *rwSpinlock) Unlock(idx int32) { |
| 112 |
atomic.AndUint64(&rw.words[idx], ^_WriterBit) |
| 113 |
} |
| 114 |
|
| 115 |
// ResetAll clears all lock state. Must be called with external synchronization |
| 116 |
// (e.g., the caller holds s.mu in Purge). |
| 117 |
func (rw *rwSpinlock) ResetAll() { |
| 118 |
for i := range rw.words { |
| 119 |
atomic.StoreUint64(&rw.words[i], 0) |
| 120 |
} |
| 121 |
} |