| 1 |
package sieve |
| 2 |
|
| 3 |
import ( |
| 4 |
"runtime" |
| 5 |
"sync" |
| 6 |
"sync/atomic" |
| 7 |
"testing" |
| 8 |
) |
| 9 |
|
| 10 |
func TestSpinlock_BasicLockUnlock(t *testing.T) { |
| 11 |
sl := newSpinlock(64) |
| 12 |
|
| 13 |
sl.Lock(0) |
| 14 |
if atomic.LoadUint64(&sl.slots[0]) != 1 { |
| 15 |
t.Fatal("slot not 1 after Lock") |
| 16 |
} |
| 17 |
|
| 18 |
sl.Unlock(0) |
| 19 |
if atomic.LoadUint64(&sl.slots[0]) != 0 { |
| 20 |
t.Fatal("slot not 0 after Unlock") |
| 21 |
} |
| 22 |
} |
| 23 |
|
| 24 |
func TestSpinlock_ExcludesSecondLocker(t *testing.T) { |
| 25 |
sl := newSpinlock(64) |
| 26 |
const idx = int32(3) |
| 27 |
|
| 28 |
sl.Lock(idx) |
| 29 |
|
| 30 |
acquired := make(chan struct{}) |
| 31 |
go func() { |
| 32 |
sl.Lock(idx) |
| 33 |
close(acquired) |
| 34 |
sl.Unlock(idx) |
| 35 |
}() |
| 36 |
|
| 37 |
for i := 0; i < 100; i++ { |
| 38 |
runtime.Gosched() |
| 39 |
} |
| 40 |
|
| 41 |
select { |
| 42 |
case <-acquired: |
| 43 |
t.Fatal("second locker acquired while first held it") |
| 44 |
default: |
| 45 |
} |
| 46 |
|
| 47 |
sl.Unlock(idx) |
| 48 |
<-acquired |
| 49 |
} |
| 50 |
|
| 51 |
func TestSpinlock_DifferentSlotsDontInterfere(t *testing.T) { |
| 52 |
sl := newSpinlock(128) |
| 53 |
|
| 54 |
sl.Lock(0) |
| 55 |
|
| 56 |
// Other slots independently lockable |
| 57 |
sl.Lock(1) |
| 58 |
sl.Unlock(1) |
| 59 |
|
| 60 |
sl.Lock(15) // same cache line |
| 61 |
sl.Unlock(15) |
| 62 |
|
| 63 |
sl.Lock(16) // different cache line |
| 64 |
sl.Unlock(16) |
| 65 |
|
| 66 |
sl.Unlock(0) |
| 67 |
} |
| 68 |
|
| 69 |
func TestSpinlock_AdjacentSlotIsolation(t *testing.T) { |
| 70 |
sl := newSpinlock(64) |
| 71 |
|
| 72 |
sl.Lock(0) |
| 73 |
sl.Lock(1) |
| 74 |
|
| 75 |
sl.Unlock(0) |
| 76 |
|
| 77 |
if atomic.LoadUint64(&sl.slots[0]) != 0 { |
| 78 |
t.Error("slot 0 still locked after Unlock") |
| 79 |
} |
| 80 |
if atomic.LoadUint64(&sl.slots[1]) != 1 { |
| 81 |
t.Error("slot 1 was cleared by slot 0's Unlock") |
| 82 |
} |
| 83 |
|
| 84 |
sl.Unlock(1) |
| 85 |
} |
| 86 |
|
| 87 |
func TestSpinlock_ConcurrentExclusion(t *testing.T) { |
| 88 |
sl := newSpinlock(64) |
| 89 |
const idx = int32(0) |
| 90 |
const numGoroutines = 50 |
| 91 |
const opsPerGoroutine = 10_000 |
| 92 |
|
| 93 |
var counter int64 |
| 94 |
|
| 95 |
var wg sync.WaitGroup |
| 96 |
wg.Add(numGoroutines) |
| 97 |
|
| 98 |
for g := 0; g < numGoroutines; g++ { |
| 99 |
go func() { |
| 100 |
defer wg.Done() |
| 101 |
for i := 0; i < opsPerGoroutine; i++ { |
| 102 |
sl.Lock(idx) |
| 103 |
counter++ |
| 104 |
sl.Unlock(idx) |
| 105 |
} |
| 106 |
}() |
| 107 |
} |
| 108 |
wg.Wait() |
| 109 |
|
| 110 |
expected := int64(numGoroutines) * int64(opsPerGoroutine) |
| 111 |
if counter != expected { |
| 112 |
t.Fatalf("expected %d, got %d", expected, counter) |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
func TestSpinlock_ConcurrentTornReadDetection(t *testing.T) { |
| 117 |
sl := newSpinlock(64) |
| 118 |
const idx = int32(0) |
| 119 |
const numReaders = 8 |
| 120 |
const numWriters = 2 |
| 121 |
const opsPerGoroutine = 50_000 |
| 122 |
|
| 123 |
var field1, field2 int64 |
| 124 |
var tornReads atomic.Int64 |
| 125 |
|
| 126 |
var wg sync.WaitGroup |
| 127 |
|
| 128 |
wg.Add(numReaders) |
| 129 |
for g := 0; g < numReaders; g++ { |
| 130 |
go func() { |
| 131 |
defer wg.Done() |
| 132 |
for i := 0; i < opsPerGoroutine; i++ { |
| 133 |
sl.Lock(idx) |
| 134 |
f1 := field1 |
| 135 |
f2 := field2 |
| 136 |
sl.Unlock(idx) |
| 137 |
if f1 != f2 { |
| 138 |
tornReads.Add(1) |
| 139 |
} |
| 140 |
} |
| 141 |
}() |
| 142 |
} |
| 143 |
|
| 144 |
wg.Add(numWriters) |
| 145 |
for g := 0; g < numWriters; g++ { |
| 146 |
go func(id int) { |
| 147 |
defer wg.Done() |
| 148 |
for i := 0; i < opsPerGoroutine; i++ { |
| 149 |
val := int64(id*opsPerGoroutine + i) |
| 150 |
sl.Lock(idx) |
| 151 |
field1 = val |
| 152 |
field2 = val |
| 153 |
sl.Unlock(idx) |
| 154 |
} |
| 155 |
}(g) |
| 156 |
} |
| 157 |
|
| 158 |
wg.Wait() |
| 159 |
|
| 160 |
if torn := tornReads.Load(); torn != 0 { |
| 161 |
t.Fatalf("detected %d torn reads", torn) |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
func TestSpinlock_StressMultiSlot(t *testing.T) { |
| 166 |
sl := newSpinlock(64) |
| 167 |
const numSlots = 16 // one cache line worth |
| 168 |
const numGoroutines = 4 |
| 169 |
const opsPerGoroutine = 20_000 |
| 170 |
|
| 171 |
var wg sync.WaitGroup |
| 172 |
|
| 173 |
for slot := int32(0); slot < numSlots; slot++ { |
| 174 |
wg.Add(numGoroutines) |
| 175 |
for g := 0; g < numGoroutines; g++ { |
| 176 |
go func(s int32) { |
| 177 |
defer wg.Done() |
| 178 |
for i := 0; i < opsPerGoroutine; i++ { |
| 179 |
sl.Lock(s) |
| 180 |
runtime.Gosched() |
| 181 |
sl.Unlock(s) |
| 182 |
} |
| 183 |
}(slot) |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
wg.Wait() |
| 188 |
|
| 189 |
for i := int32(0); i < numSlots; i++ { |
| 190 |
if atomic.LoadUint64(&sl.slots[i]) != 0 { |
| 191 |
t.Fatalf("slot[%d] not zero after stress", i) |
| 192 |
} |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
func TestSpinlock_ResetAll(t *testing.T) { |
| 197 |
sl := newSpinlock(128) |
| 198 |
|
| 199 |
sl.Lock(0) |
| 200 |
sl.Lock(1) |
| 201 |
sl.Lock(64) |
| 202 |
|
| 203 |
sl.ResetAll() |
| 204 |
|
| 205 |
for i, s := range sl.slots { |
| 206 |
if s != 0 { |
| 207 |
t.Fatalf("slot[%d] not zero after ResetAll: %d", i, s) |
| 208 |
} |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
// --- Benchmarks --- |
| 213 |
|
| 214 |
func BenchmarkSpinlock_LockUnlock_Uncontended(b *testing.B) { |
| 215 |
sl := newSpinlock(1024) |
| 216 |
b.ResetTimer() |
| 217 |
for i := 0; i < b.N; i++ { |
| 218 |
sl.Lock(0) |
| 219 |
sl.Unlock(0) |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
func BenchmarkSpinlock_LockUnlock_Parallel(b *testing.B) { |
| 224 |
sl := newSpinlock(1024) |
| 225 |
b.ResetTimer() |
| 226 |
b.RunParallel(func(pb *testing.PB) { |
| 227 |
for pb.Next() { |
| 228 |
sl.Lock(0) |
| 229 |
sl.Unlock(0) |
| 230 |
} |
| 231 |
}) |
| 232 |
} |
| 233 |
|
| 234 |
func BenchmarkSpinlock_LockUnlock_DifferentSlots(b *testing.B) { |
| 235 |
sl := newSpinlock(1024) |
| 236 |
b.ResetTimer() |
| 237 |
b.RunParallel(func(pb *testing.PB) { |
| 238 |
id := int32(runtime.GOMAXPROCS(0)) |
| 239 |
slot := int32(0) |
| 240 |
for pb.Next() { |
| 241 |
sl.Lock(slot % id) |
| 242 |
sl.Unlock(slot % id) |
| 243 |
slot++ |
| 244 |
} |
| 245 |
}) |
| 246 |
} |