-
Notifications
You must be signed in to change notification settings - Fork 3k
/
ReadWriteLockTests.swift
executable file
·219 lines (192 loc) · 6.96 KB
/
ReadWriteLockTests.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
//
// ReadWriteLockTests.swift
// ReadWriteLockTests
//
// Created by John Gallagher on 7/19/14.
// Copyright (c) 2014 Big Nerd Ranch. All rights reserved.
//
import XCTest
import Deferred
func timeIntervalSleep(duration: NSTimeInterval) {
usleep(useconds_t(duration * NSTimeInterval(USEC_PER_SEC)))
}
class PerfTestThread: NSThread {
let iters: Int
var lock: ReadWriteLock
let joinLock = NSConditionLock(condition: 0)
init(lock: ReadWriteLock, iters: Int) {
self.lock = lock
self.iters = iters
super.init()
}
override func main() {
joinLock.lock()
let doNothing: () -> () = {}
for i in 0 ..< iters {
if (i % 10) == 0 {
lock.withWriteLock(doNothing)
} else {
lock.withReadLock(doNothing)
}
}
joinLock.unlockWithCondition(1)
}
func join() {
joinLock.lockWhenCondition(1)
joinLock.unlock()
}
}
class ReadWriteLockTests: XCTestCase {
var gcdLock: GCDReadWriteLock!
var spinLock: SpinLock!
var casSpinLock: CASSpinLock!
var queue: dispatch_queue_t!
override func setUp() {
super.setUp()
gcdLock = GCDReadWriteLock()
spinLock = SpinLock()
casSpinLock = CASSpinLock()
queue = dispatch_queue_create("ReadWriteLockTests", DISPATCH_QUEUE_CONCURRENT)
}
override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
super.tearDown()
}
func testMultipleConcurrentReaders() {
// do not test spinLock, as it does not allow concurrent reading
let locks: [ReadWriteLock] = [gcdLock, casSpinLock]
for (var lock) in locks {
// start up 32 readers that block for 0.1 seconds each...
for i in 0 ..< 32 {
let expectation = expectationWithDescription("read \(lock)")
dispatch_async(queue) {
lock.withReadLock {
timeIntervalSleep(0.1)
expectation.fulfill()
}
}
}
// and make sure all 32 complete in < 3 second. If the readers
// did not run concurrently, they would take >= 3.2 seconds
waitForExpectationsWithTimeout(3, handler: nil)
}
}
func testMultipleConcurrentWriters() {
// all three lock types ensure writes happen exclusively
let locks: [ReadWriteLock] = [gcdLock, casSpinLock, spinLock]
for (var lock) in locks {
var x: Int32 = 0
// spin up 5 writers concurrently...
for i in 0 ..< 5 {
let expectation = expectationWithDescription("write \(lock) #\(i)")
dispatch_async(queue) {
lock.withWriteLock {
// ... and make sure each runs in order by checking that
// no two blocks increment x at the same time
XCTAssertEqual(OSAtomicIncrement32Barrier(&x), 1)
timeIntervalSleep(0.05)
XCTAssertEqual(OSAtomicDecrement32Barrier(&x), 0)
expectation.fulfill()
}
}
}
waitForExpectationsWithTimeout(5, handler: nil)
}
}
func testSimultaneousReadersAndWriters() {
// all three lock types ensure reads cannot run while writes do
let locks: [ReadWriteLock] = [gcdLock, casSpinLock, spinLock]
for (var lock) in locks {
var x: Int32 = 0
let startReader: (Int) -> () = { i in
let expectation = self.expectationWithDescription("reader \(i)")
dispatch_async(self.queue) {
lock.withReadLock {
// make sure we get the value of x either before or after
// the writer runs, never a partway-through value
XCTAssertTrue(x == 0 || x == 5)
expectation.fulfill()
}
}
}
// spin up 32 readers before a writer
for i in 0 ..< 32 {
startReader(i)
}
// spin up a writer that (slowly) increments x from 0 to 5
let expectation = expectationWithDescription("writer")
dispatch_async(queue) {
lock.withWriteLock {
for i in 0 ..< 5 {
OSAtomicIncrement32Barrier(&x)
timeIntervalSleep(0.1)
}
expectation.fulfill()
}
}
// and spin up 32 more readers after
for i in 32 ..< 64 {
startReader(i)
}
waitForExpectationsWithTimeout(5, handler: nil)
}
}
/*
func measureReadLockSingleThread(var lock: ReadWriteLock, iters: Int) {
let doNothing: () -> () = {}
self.measureBlock {
for i in 0 ..< iters {
lock.withReadLock(doNothing)
}
}
}
func measureWriteLockSingleThread(var lock: ReadWriteLock, iters: Int) {
let doNothing: () -> () = {}
self.measureBlock {
for i in 0 ..< iters {
lock.withWriteLock(doNothing)
}
}
}
func measureLock90PercentReadsNThreads(var lock: ReadWriteLock, iters: Int, nthreads: Int) {
self.measureBlock {
var threads: [PerfTestThread] = []
for i in 0 ..< nthreads {
let t = PerfTestThread(lock: lock, iters: iters)
t.start()
threads.append(t)
}
for t in threads {
t.join()
}
}
}
func testSingleThreadPerformanceGCDLockRead() {
measureReadLockSingleThread(gcdLock, iters: 200_000)
}
func testSingleThreadPerformanceGCDLockWrite() {
measureWriteLockSingleThread(gcdLock, iters: 200_000)
}
func testSingleThreadPerformanceSpinLockRead() {
measureReadLockSingleThread(spinLock, iters: 1_000_000)
}
func testSingleThreadPerformanceSpinLockWrite() {
measureWriteLockSingleThread(spinLock, iters: 1_000_000)
}
func testSingleThreadPerformanceCASSpinLockRead() {
measureReadLockSingleThread(casSpinLock, iters: 1_000_000)
}
func testSingleThreadPerformanceCASSpinLockWrite() {
measureWriteLockSingleThread(casSpinLock, iters: 1_000_000)
}
func test90PercentReads4ThreadsGCDLock() {
measureLock90PercentReadsNThreads(gcdLock, iters: 2_500, nthreads: 4)
}
func test90PercentReads4ThreadsSpinLock() {
measureLock90PercentReadsNThreads(spinLock, iters: 250_000, nthreads: 4)
}
func test90PercentReads4ThreadsCASSpinLock() {
measureLock90PercentReadsNThreads(casSpinLock, iters: 250_000, nthreads: 4)
}
*/
}