| 1 |
package sieve |
| 2 |
|
| 3 |
import ( |
| 4 |
"runtime" |
| 5 |
"sync" |
| 6 |
"sync/atomic" |
| 7 |
"testing" |
| 8 |
) |
| 9 |
|
| 10 |
func TestPackedSpinlock_BasicLockUnlock(t *testing.T) { |
| 11 |
sl := newPackedSpinlock(64) |
| 12 |
|
| 13 |
sl.Lock(0) |
| 14 |
// Verify bit is set |
| 15 |
w, mask := packedSlotPos(0) |
| 16 |
if sl.words[w]&mask == 0 { |
| 17 |
t.Fatal("bit not set after Lock") |
| 18 |
} |
| 19 |
|
| 20 |
sl.Unlock(0) |
| 21 |
if sl.words[w]&mask != 0 { |
| 22 |
t.Fatal("bit not cleared after Unlock") |
| 23 |
} |
| 24 |
} |
| 25 |
|
| 26 |
func TestPackedSpinlock_ExcludesSecondLocker(t *testing.T) { |
| 27 |
sl := newPackedSpinlock(64) |
| 28 |
const idx = int32(3) |
| 29 |
|
| 30 |
sl.Lock(idx) |
| 31 |
|
| 32 |
acquired := make(chan struct{}) |
| 33 |
go func() { |
| 34 |
sl.Lock(idx) |
| 35 |
close(acquired) |
| 36 |
sl.Unlock(idx) |
| 37 |
}() |
| 38 |
|
| 39 |
// Give the goroutine time to spin |
| 40 |
for i := 0; i < 100; i++ { |
| 41 |
runtime.Gosched() |
| 42 |
} |
| 43 |
|
| 44 |
select { |
| 45 |
case <-acquired: |
| 46 |
t.Fatal("second locker acquired while first held it") |
| 47 |
default: |
| 48 |
// Expected: spinning |
| 49 |
} |
| 50 |
|
| 51 |
sl.Unlock(idx) |
| 52 |
<-acquired |
| 53 |
} |
| 54 |
|
| 55 |
func TestPackedSpinlock_DifferentSlotsDontInterfere(t *testing.T) { |
| 56 |
sl := newPackedSpinlock(128) |
| 57 |
|
| 58 |
// Lock slot 0 |
| 59 |
sl.Lock(0) |
| 60 |
|
| 61 |
// Slot 1 (same word) should be independently lockable |
| 62 |
sl.Lock(1) |
| 63 |
sl.Unlock(1) |
| 64 |
|
| 65 |
// Slot 63 (same word, last bit) |
| 66 |
sl.Lock(63) |
| 67 |
sl.Unlock(63) |
| 68 |
|
| 69 |
// Slot 64 (different word) |
| 70 |
sl.Lock(64) |
| 71 |
sl.Unlock(64) |
| 72 |
|
| 73 |
sl.Unlock(0) |
| 74 |
} |
| 75 |
|
| 76 |
func TestPackedSpinlock_AdjacentBitIsolation(t *testing.T) { |
| 77 |
sl := newPackedSpinlock(64) |
| 78 |
|
| 79 |
// Lock slots 0 and 1 |
| 80 |
sl.Lock(0) |
| 81 |
sl.Lock(1) |
| 82 |
|
| 83 |
// Unlock slot 0 — slot 1 must remain locked |
| 84 |
sl.Unlock(0) |
| 85 |
|
| 86 |
w0, mask0 := packedSlotPos(0) |
| 87 |
w1, mask1 := packedSlotPos(1) |
| 88 |
|
| 89 |
if sl.words[w0]&mask0 != 0 { |
| 90 |
t.Error("slot 0 still locked after Unlock") |
| 91 |
} |
| 92 |
if sl.words[w1]&mask1 == 0 { |
| 93 |
t.Error("slot 1 was cleared by slot 0's Unlock") |
| 94 |
} |
| 95 |
|
| 96 |
sl.Unlock(1) |
| 97 |
} |
| 98 |
|
| 99 |
func TestPackedSpinlock_AllBitsInWord(t *testing.T) { |
| 100 |
sl := newPackedSpinlock(64) |
| 101 |
|
| 102 |
// Lock all 64 bits in word 0 |
| 103 |
for i := int32(0); i < 64; i++ { |
| 104 |
sl.Lock(i) |
| 105 |
} |
| 106 |
|
| 107 |
// Word should be all 1s |
| 108 |
if sl.words[0] != ^uint64(0) { |
| 109 |
t.Fatalf("expected all bits set, got %016x", sl.words[0]) |
| 110 |
} |
| 111 |
|
| 112 |
// Unlock all |
| 113 |
for i := int32(0); i < 64; i++ { |
| 114 |
sl.Unlock(i) |
| 115 |
} |
| 116 |
|
| 117 |
if sl.words[0] != 0 { |
| 118 |
t.Fatalf("expected 0, got %016x", sl.words[0]) |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
func TestPackedSpinlock_ConcurrentExclusion(t *testing.T) { |
| 123 |
sl := newPackedSpinlock(64) |
| 124 |
const idx = int32(0) |
| 125 |
const numGoroutines = 50 |
| 126 |
const opsPerGoroutine = 10_000 |
| 127 |
|
| 128 |
var counter int64 |
| 129 |
|
| 130 |
var wg sync.WaitGroup |
| 131 |
wg.Add(numGoroutines) |
| 132 |
|
| 133 |
for g := 0; g < numGoroutines; g++ { |
| 134 |
go func() { |
| 135 |
defer wg.Done() |
| 136 |
for i := 0; i < opsPerGoroutine; i++ { |
| 137 |
sl.Lock(idx) |
| 138 |
counter++ |
| 139 |
sl.Unlock(idx) |
| 140 |
} |
| 141 |
}() |
| 142 |
} |
| 143 |
wg.Wait() |
| 144 |
|
| 145 |
expected := int64(numGoroutines) * int64(opsPerGoroutine) |
| 146 |
if counter != expected { |
| 147 |
t.Fatalf("expected %d, got %d", expected, counter) |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
func TestPackedSpinlock_ConcurrentTornReadDetection(t *testing.T) { |
| 152 |
sl := newPackedSpinlock(64) |
| 153 |
const idx = int32(0) |
| 154 |
const numReaders = 8 |
| 155 |
const numWriters = 2 |
| 156 |
const opsPerGoroutine = 50_000 |
| 157 |
|
| 158 |
var field1, field2 int64 |
| 159 |
var tornReads atomic.Int64 |
| 160 |
|
| 161 |
var wg sync.WaitGroup |
| 162 |
|
| 163 |
// Readers |
| 164 |
wg.Add(numReaders) |
| 165 |
for g := 0; g < numReaders; g++ { |
| 166 |
go func() { |
| 167 |
defer wg.Done() |
| 168 |
for i := 0; i < opsPerGoroutine; i++ { |
| 169 |
sl.Lock(idx) |
| 170 |
f1 := field1 |
| 171 |
f2 := field2 |
| 172 |
sl.Unlock(idx) |
| 173 |
if f1 != f2 { |
| 174 |
tornReads.Add(1) |
| 175 |
} |
| 176 |
} |
| 177 |
}() |
| 178 |
} |
| 179 |
|
| 180 |
// Writers |
| 181 |
wg.Add(numWriters) |
| 182 |
for g := 0; g < numWriters; g++ { |
| 183 |
go func(id int) { |
| 184 |
defer wg.Done() |
| 185 |
for i := 0; i < opsPerGoroutine; i++ { |
| 186 |
val := int64(id*opsPerGoroutine + i) |
| 187 |
sl.Lock(idx) |
| 188 |
field1 = val |
| 189 |
field2 = val |
| 190 |
sl.Unlock(idx) |
| 191 |
} |
| 192 |
}(g) |
| 193 |
} |
| 194 |
|
| 195 |
wg.Wait() |
| 196 |
|
| 197 |
if torn := tornReads.Load(); torn != 0 { |
| 198 |
t.Fatalf("detected %d torn reads", torn) |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
func TestPackedSpinlock_StressMultiSlot(t *testing.T) { |
| 203 |
sl := newPackedSpinlock(64) |
| 204 |
const numSlots = 64 // all in same uint64 word |
| 205 |
const numGoroutines = 4 |
| 206 |
const opsPerGoroutine = 20_000 |
| 207 |
|
| 208 |
var wg sync.WaitGroup |
| 209 |
|
| 210 |
for slot := int32(0); slot < numSlots; slot++ { |
| 211 |
wg.Add(numGoroutines) |
| 212 |
for g := 0; g < numGoroutines; g++ { |
| 213 |
go func(s int32) { |
| 214 |
defer wg.Done() |
| 215 |
for i := 0; i < opsPerGoroutine; i++ { |
| 216 |
sl.Lock(s) |
| 217 |
runtime.Gosched() |
| 218 |
sl.Unlock(s) |
| 219 |
} |
| 220 |
}(slot) |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
wg.Wait() |
| 225 |
|
| 226 |
if sl.words[0] != 0 { |
| 227 |
t.Fatalf("word not zero after stress: %016x", sl.words[0]) |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
func TestPackedSpinlock_ResetAll(t *testing.T) { |
| 232 |
sl := newPackedSpinlock(128) |
| 233 |
|
| 234 |
sl.Lock(0) |
| 235 |
sl.Lock(1) |
| 236 |
sl.Lock(64) |
| 237 |
|
| 238 |
sl.ResetAll() |
| 239 |
|
| 240 |
for i, w := range sl.words { |
| 241 |
if w != 0 { |
| 242 |
t.Fatalf("word[%d] not zero after ResetAll: %016x", i, w) |
| 243 |
} |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
// --- Benchmarks --- |
| 248 |
|
| 249 |
func BenchmarkPackedSpinlock_LockUnlock_Uncontended(b *testing.B) { |
| 250 |
sl := newPackedSpinlock(1024) |
| 251 |
b.ResetTimer() |
| 252 |
for i := 0; i < b.N; i++ { |
| 253 |
sl.Lock(0) |
| 254 |
sl.Unlock(0) |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
func BenchmarkPackedSpinlock_LockUnlock_Parallel(b *testing.B) { |
| 259 |
sl := newPackedSpinlock(1024) |
| 260 |
b.ResetTimer() |
| 261 |
b.RunParallel(func(pb *testing.PB) { |
| 262 |
for pb.Next() { |
| 263 |
sl.Lock(0) |
| 264 |
sl.Unlock(0) |
| 265 |
} |
| 266 |
}) |
| 267 |
} |
| 268 |
|
| 269 |
func BenchmarkPackedSpinlock_LockUnlock_DifferentSlots(b *testing.B) { |
| 270 |
sl := newPackedSpinlock(1024) |
| 271 |
b.ResetTimer() |
| 272 |
b.RunParallel(func(pb *testing.PB) { |
| 273 |
id := int32(runtime.GOMAXPROCS(0)) |
| 274 |
slot := int32(0) |
| 275 |
for pb.Next() { |
| 276 |
sl.Lock(slot % id) |
| 277 |
sl.Unlock(slot % id) |
| 278 |
slot++ |
| 279 |
} |
| 280 |
}) |
| 281 |
} |