| 1 |
package sieve |
| 2 |
|
| 3 |
import ( |
| 4 |
"runtime" |
| 5 |
"sync" |
| 6 |
"sync/atomic" |
| 7 |
"testing" |
| 8 |
) |
| 9 |
|
| 10 |
func TestRWSpinlock_BasicReadLock(t *testing.T) { |
| 11 |
rw := newRWSpinlock(64) |
| 12 |
|
| 13 |
rw.RLock(0) |
| 14 |
rw.RUnlock(0) |
| 15 |
|
| 16 |
if rw.words[0] != 0 { |
| 17 |
t.Fatalf("slot not zero after RUnlock: %016x", rw.words[0]) |
| 18 |
} |
| 19 |
} |
| 20 |
|
| 21 |
func TestRWSpinlock_BasicWriteLock(t *testing.T) { |
| 22 |
rw := newRWSpinlock(64) |
| 23 |
|
| 24 |
rw.Lock(0) |
| 25 |
|
| 26 |
// Writer bit (bit 63) should be set, no readers |
| 27 |
if rw.words[0] != _WriterBit { |
| 28 |
t.Fatalf("expected writer bit 0x%016x, got 0x%016x", _WriterBit, rw.words[0]) |
| 29 |
} |
| 30 |
|
| 31 |
rw.Unlock(0) |
| 32 |
if rw.words[0] != 0 { |
| 33 |
t.Fatalf("slot not zero after Unlock: %016x", rw.words[0]) |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
func TestRWSpinlock_MultipleReaders(t *testing.T) { |
| 38 |
rw := newRWSpinlock(64) |
| 39 |
const idx = int32(5) |
| 40 |
const numReaders = 50 |
| 41 |
|
| 42 |
for i := 0; i < numReaders; i++ { |
| 43 |
rw.RLock(idx) |
| 44 |
} |
| 45 |
|
| 46 |
// Reader count is stored directly in bits 0-62 |
| 47 |
val := rw.words[idx] |
| 48 |
if val != uint64(numReaders) { |
| 49 |
t.Fatalf("expected reader count %d, got %d", numReaders, val) |
| 50 |
} |
| 51 |
|
| 52 |
for i := 0; i < numReaders; i++ { |
| 53 |
rw.RUnlock(idx) |
| 54 |
} |
| 55 |
|
| 56 |
if rw.words[idx] != 0 { |
| 57 |
t.Fatalf("expected 0 after all RUnlock, got %d", rw.words[idx]) |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
func TestRWSpinlock_WriterExcludesReaders(t *testing.T) { |
| 62 |
rw := newRWSpinlock(64) |
| 63 |
const idx = int32(3) |
| 64 |
|
| 65 |
rw.Lock(idx) |
| 66 |
|
| 67 |
acquired := make(chan struct{}) |
| 68 |
go func() { |
| 69 |
rw.RLock(idx) |
| 70 |
close(acquired) |
| 71 |
rw.RUnlock(idx) |
| 72 |
}() |
| 73 |
|
| 74 |
for i := 0; i < 100; i++ { |
| 75 |
runtime.Gosched() |
| 76 |
} |
| 77 |
|
| 78 |
select { |
| 79 |
case <-acquired: |
| 80 |
t.Fatal("reader acquired lock while writer held it") |
| 81 |
default: |
| 82 |
} |
| 83 |
|
| 84 |
rw.Unlock(idx) |
| 85 |
<-acquired |
| 86 |
} |
| 87 |
|
| 88 |
func TestRWSpinlock_ReaderExcludesWriter(t *testing.T) { |
| 89 |
rw := newRWSpinlock(64) |
| 90 |
const idx = int32(7) |
| 91 |
|
| 92 |
rw.RLock(idx) |
| 93 |
|
| 94 |
acquired := make(chan struct{}) |
| 95 |
go func() { |
| 96 |
rw.Lock(idx) |
| 97 |
close(acquired) |
| 98 |
rw.Unlock(idx) |
| 99 |
}() |
| 100 |
|
| 101 |
for i := 0; i < 100; i++ { |
| 102 |
runtime.Gosched() |
| 103 |
} |
| 104 |
|
| 105 |
select { |
| 106 |
case <-acquired: |
| 107 |
t.Fatal("writer acquired lock while reader held it") |
| 108 |
default: |
| 109 |
} |
| 110 |
|
| 111 |
rw.RUnlock(idx) |
| 112 |
<-acquired |
| 113 |
} |
| 114 |
|
| 115 |
func TestRWSpinlock_DifferentSlotsDontInterfere(t *testing.T) { |
| 116 |
rw := newRWSpinlock(64) |
| 117 |
|
| 118 |
// Lock slot 0 for writing |
| 119 |
rw.Lock(0) |
| 120 |
|
| 121 |
// Other slots are fully independent (separate uint64 words) |
| 122 |
rw.RLock(1) |
| 123 |
rw.RUnlock(1) |
| 124 |
|
| 125 |
rw.Lock(1) |
| 126 |
rw.Unlock(1) |
| 127 |
|
| 128 |
rw.RLock(7) |
| 129 |
rw.RUnlock(7) |
| 130 |
|
| 131 |
rw.RLock(8) |
| 132 |
rw.RUnlock(8) |
| 133 |
|
| 134 |
rw.Unlock(0) |
| 135 |
} |
| 136 |
|
| 137 |
func TestRWSpinlock_ConcurrentReaders(t *testing.T) { |
| 138 |
rw := newRWSpinlock(1024) |
| 139 |
const idx = int32(42) |
| 140 |
const numGoroutines = 100 |
| 141 |
const opsPerGoroutine = 10_000 |
| 142 |
|
| 143 |
var totalOps atomic.Int64 |
| 144 |
|
| 145 |
var wg sync.WaitGroup |
| 146 |
wg.Add(numGoroutines) |
| 147 |
|
| 148 |
for g := 0; g < numGoroutines; g++ { |
| 149 |
go func() { |
| 150 |
defer wg.Done() |
| 151 |
for i := 0; i < opsPerGoroutine; i++ { |
| 152 |
rw.RLock(idx) |
| 153 |
totalOps.Add(1) |
| 154 |
rw.RUnlock(idx) |
| 155 |
} |
| 156 |
}() |
| 157 |
} |
| 158 |
wg.Wait() |
| 159 |
|
| 160 |
expected := int64(numGoroutines) * int64(opsPerGoroutine) |
| 161 |
if totalOps.Load() != expected { |
| 162 |
t.Fatalf("expected %d ops, got %d", expected, totalOps.Load()) |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
func TestRWSpinlock_ConcurrentReadWrite(t *testing.T) { |
| 167 |
rw := newRWSpinlock(64) |
| 168 |
const idx = int32(0) |
| 169 |
const numReaders = 8 |
| 170 |
const numWriters = 2 |
| 171 |
const opsPerGoroutine = 50_000 |
| 172 |
|
| 173 |
var field1, field2 int64 |
| 174 |
|
| 175 |
var wg sync.WaitGroup |
| 176 |
var tornReads atomic.Int64 |
| 177 |
|
| 178 |
wg.Add(numReaders) |
| 179 |
for g := 0; g < numReaders; g++ { |
| 180 |
go func() { |
| 181 |
defer wg.Done() |
| 182 |
for i := 0; i < opsPerGoroutine; i++ { |
| 183 |
rw.RLock(idx) |
| 184 |
f1 := atomic.LoadInt64(&field1) |
| 185 |
f2 := atomic.LoadInt64(&field2) |
| 186 |
rw.RUnlock(idx) |
| 187 |
if f1 != f2 { |
| 188 |
tornReads.Add(1) |
| 189 |
} |
| 190 |
} |
| 191 |
}() |
| 192 |
} |
| 193 |
|
| 194 |
wg.Add(numWriters) |
| 195 |
for g := 0; g < numWriters; g++ { |
| 196 |
go func(id int) { |
| 197 |
defer wg.Done() |
| 198 |
for i := 0; i < opsPerGoroutine; i++ { |
| 199 |
val := int64(id*opsPerGoroutine + i) |
| 200 |
rw.Lock(idx) |
| 201 |
atomic.StoreInt64(&field1, val) |
| 202 |
atomic.StoreInt64(&field2, val) |
| 203 |
rw.Unlock(idx) |
| 204 |
} |
| 205 |
}(g) |
| 206 |
} |
| 207 |
|
| 208 |
wg.Wait() |
| 209 |
|
| 210 |
if torn := tornReads.Load(); torn != 0 { |
| 211 |
t.Fatalf("detected %d torn reads", torn) |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
func TestRWSpinlock_StressMultiSlot(t *testing.T) { |
| 216 |
rw := newRWSpinlock(64) |
| 217 |
const numSlots = 8 |
| 218 |
const numGoroutines = 4 |
| 219 |
const opsPerGoroutine = 20_000 |
| 220 |
|
| 221 |
var wg sync.WaitGroup |
| 222 |
|
| 223 |
for slot := int32(0); slot < numSlots; slot++ { |
| 224 |
wg.Add(numGoroutines) |
| 225 |
for g := 0; g < numGoroutines-1; g++ { |
| 226 |
go func(s int32) { |
| 227 |
defer wg.Done() |
| 228 |
for i := 0; i < opsPerGoroutine; i++ { |
| 229 |
rw.RLock(s) |
| 230 |
runtime.Gosched() |
| 231 |
rw.RUnlock(s) |
| 232 |
} |
| 233 |
}(slot) |
| 234 |
} |
| 235 |
go func(s int32) { |
| 236 |
defer wg.Done() |
| 237 |
for i := 0; i < opsPerGoroutine; i++ { |
| 238 |
rw.Lock(s) |
| 239 |
rw.Unlock(s) |
| 240 |
} |
| 241 |
}(slot) |
| 242 |
} |
| 243 |
|
| 244 |
wg.Wait() |
| 245 |
|
| 246 |
// Each slot is its own word now; check all 8 |
| 247 |
for i := int32(0); i < numSlots; i++ { |
| 248 |
if rw.words[i] != 0 { |
| 249 |
t.Fatalf("words[%d] not zero after stress: %016x", i, rw.words[i]) |
| 250 |
} |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
func TestRWSpinlock_ResetAll(t *testing.T) { |
| 255 |
rw := newRWSpinlock(64) |
| 256 |
|
| 257 |
rw.RLock(0) |
| 258 |
rw.RLock(1) |
| 259 |
rw.Lock(2) |
| 260 |
|
| 261 |
rw.ResetAll() |
| 262 |
|
| 263 |
for i, w := range rw.words { |
| 264 |
if w != 0 { |
| 265 |
t.Fatalf("word[%d] not zero after ResetAll: %016x", i, w) |
| 266 |
} |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
// --- Benchmarks --- |
| 271 |
|
| 272 |
func BenchmarkRWSpinlock_RLockUnlock_Uncontended(b *testing.B) { |
| 273 |
rw := newRWSpinlock(1024) |
| 274 |
b.ResetTimer() |
| 275 |
for i := 0; i < b.N; i++ { |
| 276 |
rw.RLock(0) |
| 277 |
rw.RUnlock(0) |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
func BenchmarkRWSpinlock_RLockUnlock_Parallel(b *testing.B) { |
| 282 |
rw := newRWSpinlock(1024) |
| 283 |
b.ResetTimer() |
| 284 |
b.RunParallel(func(pb *testing.PB) { |
| 285 |
for pb.Next() { |
| 286 |
rw.RLock(0) |
| 287 |
rw.RUnlock(0) |
| 288 |
} |
| 289 |
}) |
| 290 |
} |
| 291 |
|
| 292 |
func BenchmarkRWSpinlock_RLockUnlock_DifferentSlots(b *testing.B) { |
| 293 |
rw := newRWSpinlock(1024) |
| 294 |
b.ResetTimer() |
| 295 |
b.RunParallel(func(pb *testing.PB) { |
| 296 |
id := int32(runtime.GOMAXPROCS(0)) |
| 297 |
slot := int32(0) |
| 298 |
for pb.Next() { |
| 299 |
rw.RLock(slot % id) |
| 300 |
rw.RUnlock(slot % id) |
| 301 |
slot++ |
| 302 |
} |
| 303 |
}) |
| 304 |
} |
| 305 |
|
| 306 |
func BenchmarkRWSpinlock_LockUnlock_Uncontended(b *testing.B) { |
| 307 |
rw := newRWSpinlock(1024) |
| 308 |
b.ResetTimer() |
| 309 |
for i := 0; i < b.N; i++ { |
| 310 |
rw.Lock(0) |
| 311 |
rw.Unlock(0) |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
func BenchmarkRWSpinlock_Mixed_Parallel(b *testing.B) { |
| 316 |
rw := newRWSpinlock(1024) |
| 317 |
b.ResetTimer() |
| 318 |
b.RunParallel(func(pb *testing.PB) { |
| 319 |
i := 0 |
| 320 |
for pb.Next() { |
| 321 |
if i%5 == 0 { |
| 322 |
rw.Lock(0) |
| 323 |
rw.Unlock(0) |
| 324 |
} else { |
| 325 |
rw.RLock(0) |
| 326 |
rw.RUnlock(0) |
| 327 |
} |
| 328 |
i++ |
| 329 |
} |
| 330 |
}) |
| 331 |
} |