/
reentrant_read_write_lock_spec.rb
466 lines (406 loc) · 17.2 KB
/
reentrant_read_write_lock_spec.rb
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
unless Concurrent.on_jruby?
# NOTE: These tests depend heavily on the private/undocumented
# `ThreadLocalVar#value_for` method. This method does not, and cannot work
# on JRuby at this time. Therefore, these tests cannot be run successfully on
# JRuby. The initial implementation used a variation of `ThreadLocalVar`
# which was pure-Ruby on all platforms and exhibited behavior that allowed
# these tests to work. Functionality on JRuby was verified at that time. So
# long as `ReentrantReadWriteLock` is not changed (with respect to
# `ThreadLocalVar`) we can safely assume proper functionality on JRuby.
require 'timeout'
module Concurrent
BaseMatcher = RSpec::Matchers::BuiltIn::BaseMatcher
RRWL = ReentrantReadWriteLock # no way I'm typing that 50 times
# ****************************************************************
# First some custom matchers to make our tests all nice and pretty
# ****************************************************************
class HoldLock
def initialize(lock)
@lock = lock
end
def for_read
HoldReadLock.new(@lock)
end
def for_write
HoldWriteLock.new(@lock)
end
def for_both
HoldBoth.new(@lock)
end
end
class HoldReadLock < BaseMatcher
def match(lock, thread)
((lock.instance_eval { @Counter.value } & RRWL::MAX_READERS) != 0) &&
((lock.instance_eval { @HeldCount.send(:value_for, thread) } & RRWL::READ_LOCK_MASK) > 0)
end
end
class HoldWriteLock < BaseMatcher
def match(lock, thread)
((lock.instance_eval { @Counter.value } & RRWL::RUNNING_WRITER) != 0) &&
((lock.instance_eval { @HeldCount.send(:value_for, thread) } & RRWL::WRITE_LOCK_MASK) > 0)
end
end
class HoldBoth < BaseMatcher
def match(lock, thread)
HoldReadLock.new(lock).matches?(thread) && HoldWriteLock.new(lock).matches?(thread)
end
end
class BeFree < BaseMatcher
MASK = RRWL::MAX_READERS + RRWL::RUNNING_WRITER
def matches?(lock)
(lock.instance_eval { @Counter.value } & MASK) == 0
end
def failure_message
"expected lock to be free"
end
end
# *******************************************************
RSpec.describe ReentrantReadWriteLock do
let(:lock) { RRWL.new }
def hold(lock)
HoldLock.new(lock)
end
def be_free
BeFree.new
end
def wait_up_to(secs, &condition)
_end = Time.now + secs
while !condition.call && Time.now < _end
sleep(0.001)
end
end
context "read lock" do
it "allows other read locks to be acquired at the same time" do
lock # stupid RSpec 'let' is not thread-safe!
Timeout.timeout(3) do
got_lock = 10.times.collect { CountDownLatch.new }
threads = 10.times.collect do |n|
Thread.new do
# Each thread takes the read lock and then waits for another one
# They will only finish if ALL of them get their read lock
expect(lock.acquire_read_lock).to be true
expect(Thread.current).to hold(lock).for_read
got_lock[n].count_down
got_lock[(n+1) % 10].wait
end
end
threads.each(&:join)
end
end
it "can be acquired more than once" do
Timeout.timeout(3) do
10.times { expect(lock.acquire_read_lock).to be true }
expect(Thread.current).to hold(lock).for_read
10.times { expect(lock.release_read_lock).to be true }
expect(Thread.current).not_to hold(lock).for_read
expect(lock).to be_free
end
end
it "can be acquired while holding a write lock" do
Timeout.timeout(3) do
expect(lock.acquire_write_lock).to be true
expect(Thread.current).to hold(lock).for_write
expect(lock.acquire_read_lock).to be true
expect(Thread.current).to hold(lock).for_both
expect(lock.release_read_lock).to be true
expect(Thread.current).to hold(lock).for_write
expect(Thread.current).not_to hold(lock).for_read
expect(lock.release_write_lock).to be true
expect(lock).to be_free
end
end
it "can be upgraded to a write lock" do
Timeout.timeout(3) do
expect(lock.acquire_read_lock).to be true
expect(Thread.current).to hold(lock).for_read
# now we want to upgrade...
expect(lock.acquire_write_lock).to be true
expect(lock.release_read_lock).to be true
expect(Thread.current).to hold(lock).for_write
expect(lock.release_write_lock).to be true
expect(lock).to be_free
end
end
it "cannot be released when not held" do
expect { lock.release_read_lock }.to raise_error(IllegalOperationError)
end
it "cannot be released more times than it was taken" do
Timeout.timeout(3) do
2.times { lock.acquire_read_lock }
2.times { lock.release_read_lock }
expect { lock.release_read_lock }.to raise_error(IllegalOperationError)
end
end
it "wakes up waiting writers when the last read lock is released" do
latch1,latch2 = CountDownLatch.new(3),CountDownLatch.new
good = AtomicBoolean.new(false)
threads = [
Thread.new { lock.acquire_read_lock; latch1.count_down; latch2.wait; lock.release_read_lock },
Thread.new { lock.acquire_read_lock; latch1.count_down; latch2.wait; lock.release_read_lock },
Thread.new { lock.acquire_read_lock; latch1.count_down; latch2.wait; lock.release_read_lock },
Thread.new { latch1.wait; lock.acquire_write_lock; good.value = true }
]
wait_up_to(0.2) { threads[3].status == 'sleep' }
# The last thread should be waiting to acquire a write lock now...
expect(threads[3].status).to eql "sleep"
expect(threads[3]).not_to hold(lock).for_write
expect(good.value).to be false
# Throw latch2 and the 3 readers will wake up and all release their read locks...
latch2.count_down
wait_up_to(0.2) { good.value }
expect(threads[3]).to hold(lock).for_write
expect(good.value).to be true
end
end
context "write lock" do
it "cannot be acquired when another thread holds a write lock" do
latch = CountDownLatch.new
threads = [
Thread.new { lock.acquire_write_lock; latch.count_down },
Thread.new { latch.wait; lock.acquire_write_lock }
]
expect { Timeout.timeout(1) { threads[0].join }}.not_to raise_error
expect(threads[0]).to hold(lock).for_write
expect(threads[1]).not_to hold(lock).for_write
wait_up_to(0.2) { threads[1].status == 'sleep' }
expect(threads[1].status).to eql "sleep"
end
it "cannot be acquired when another thread holds a read lock" do
latch = CountDownLatch.new
threads = [
Thread.new { lock.acquire_read_lock; latch.count_down },
Thread.new { latch.wait; lock.acquire_write_lock }
]
expect { Timeout.timeout(1) { threads[0].join }}.not_to raise_error
expect(threads[0]).to hold(lock).for_read
expect(threads[1]).not_to hold(lock).for_write
wait_up_to(0.2) { threads[1].status == 'sleep' }
expect(threads[1].status).to eql "sleep"
end
it "can be acquired more than once" do
Timeout.timeout(3) do
10.times { expect(lock.acquire_write_lock).to be true }
expect(Thread.current).to hold(lock).for_write
10.times { expect(lock.release_write_lock).to be true }
expect(Thread.current).not_to hold(lock).for_write
expect(lock).to be_free
end
end
it "can be acquired while holding a read lock" do
Timeout.timeout(3) do
expect(lock.acquire_read_lock).to be true
expect(Thread.current).to hold(lock).for_read
expect(lock.acquire_write_lock).to be true
expect(Thread.current).to hold(lock).for_both
expect(lock.release_write_lock).to be true
expect(Thread.current).to hold(lock).for_read
expect(Thread.current).not_to hold(lock).for_write
expect(lock.release_read_lock).to be true
expect(lock).to be_free
end
end
it "can be downgraded to a read lock" do
Timeout.timeout(3) do
expect(lock.acquire_write_lock).to be true
expect(Thread.current).to hold(lock).for_write
# now we want to downgrade...
expect(lock.acquire_read_lock).to be true
expect(lock.release_write_lock).to be true
expect(Thread.current).to hold(lock).for_read
expect(lock.release_read_lock).to be true
expect(lock).to be_free
end
end
it "cannot be released when not held" do
expect { lock.release_write_lock }.to raise_error(IllegalOperationError)
end
it "cannot be released more times than it was taken" do
Timeout.timeout(3) do
2.times { lock.acquire_write_lock }
2.times { lock.release_write_lock }
expect { lock.release_write_lock }.to raise_error(IllegalOperationError)
end
end
it "wakes up waiting readers when the write lock is released", buggy: true do
latch1,latch2 = CountDownLatch.new,CountDownLatch.new
good = AtomicFixnum.new(0)
threads = [
Thread.new { lock.acquire_write_lock; latch1.count_down; latch2.wait; lock.release_write_lock },
Thread.new { latch1.wait; lock.acquire_read_lock; good.update { |n| n+1 }},
Thread.new { latch1.wait; lock.acquire_read_lock; good.update { |n| n+1 }},
Thread.new { latch1.wait; lock.acquire_read_lock; good.update { |n| n+1 }}
]
wait_up_to(0.2) { threads[3].status == 'sleep' }
# The last 3 threads should be waiting to acquire read locks now...
# TODO (pitr-ch 15-Oct-2016): https://travis-ci.org/ruby-concurrency/concurrent-ruby/jobs/166777543
(1..3).each { |n| expect(threads[n].status).to eql "sleep" }
(1..3).each { |n| expect(threads[n]).not_to hold(lock).for_read }
# Throw latch2 and the writer will wake up and release its write lock...
latch2.count_down
wait_up_to(0.2) { good.value == 3 }
(1..3).each { |n| expect(threads[n]).to hold(lock).for_read }
end
it "wakes up waiting writers when the write lock is released" do
latch1,latch2 = CountDownLatch.new,CountDownLatch.new
good = AtomicBoolean.new(false)
threads = [
Thread.new { lock.acquire_write_lock; latch1.count_down; latch2.wait; lock.release_write_lock },
Thread.new { latch1.wait; lock.acquire_write_lock; good.value = true },
]
wait_up_to(0.2) { threads[1].status == 'sleep' }
# The last thread should be waiting to acquire a write lock now...
expect(threads[1].status).to eql "sleep"
expect(threads[1]).not_to hold(lock).for_write
# Throw latch2 and the writer will wake up and release its write lock...
latch2.count_down
wait_up_to(0.2) { good.value }
expect(threads[1]).to hold(lock).for_write
end
end
context "#with_read_lock" do
it "acquires read block before yielding, then releases it" do
expect(lock).to be_free
lock.with_read_lock { expect(Thread.current).to hold(lock).for_read }
expect(lock).to be_free
end
it "releases read lock if an exception is raised in block" do
expect {
lock.with_read_lock { raise "Bad" }
}.to raise_error
expect(lock).to be_free
expect(Thread.current).not_to hold(lock).for_read
end
end
context "#with_write_lock" do
it "acquires write block before yielding, then releases it" do
expect(lock).to be_free
lock.with_write_lock { expect(Thread.current).to hold(lock).for_write }
expect(lock).to be_free
end
it "releases write lock if an exception is raised in block" do
expect {
lock.with_write_lock { raise "Bad" }
}.to raise_error
expect(lock).to be_free
expect(Thread.current).not_to hold(lock).for_write
end
end
context "#try_read_lock" do
it "returns false immediately if read lock cannot be obtained" do
Timeout.timeout(3) do
latch = CountDownLatch.new
thread = Thread.new { lock.acquire_write_lock; latch.count_down }
latch.wait
expect {
Timeout.timeout(0.01) { expect(lock.try_read_lock).to be false }
}.not_to raise_error
expect(Thread.current).not_to hold(lock).for_read
end
end
it "acquires read lock and returns true if it can do so without blocking" do
Timeout.timeout(3) do
latch = CountDownLatch.new
thread = Thread.new { lock.acquire_read_lock; latch.count_down }
latch.wait
expect {
Timeout.timeout(0.01) { expect(lock.try_read_lock).to be true }
}.not_to raise_error
expect(lock).not_to be_free
expect(Thread.current).to hold(lock).for_read
end
end
it "can acquire a read lock if a read lock is already held" do
Timeout.timeout(3) do
expect(lock.acquire_read_lock).to be true
expect(lock.try_read_lock).to be true
expect(Thread.current).to hold(lock).for_read
expect(lock.release_read_lock).to be true
expect(lock.release_read_lock).to be true
expect(Thread.current).not_to hold(lock).for_read
expect(lock).to be_free
end
end
it "can acquire a read lock if a write lock is already held" do
Timeout.timeout(3) do
expect(lock.acquire_write_lock).to be true
expect(lock.try_read_lock).to be true
expect(Thread.current).to hold(lock).for_read
expect(lock.release_read_lock).to be true
expect(lock.release_write_lock).to be true
expect(Thread.current).not_to hold(lock).for_read
expect(lock).to be_free
end
end
end
context "#try_write_lock" do
it "returns false immediately if write lock cannot be obtained" do
Timeout.timeout(3) do
latch = CountDownLatch.new
thread = Thread.new { lock.acquire_write_lock; latch.count_down }
latch.wait
expect {
Timeout.timeout(0.02) { expect(lock.try_write_lock).to be false }
}.not_to raise_error
expect(Thread.current).not_to hold(lock).for_write
end
end
it "acquires write lock and returns true if it can do so without blocking" do
Timeout.timeout(3) do
expect {
Timeout.timeout(0.02) { expect(lock.try_write_lock).to be true }
}.not_to raise_error
expect(lock).not_to be_free
expect(Thread.current).to hold(lock).for_write
end
end
it "can acquire a write lock if a read lock is already held" do
Timeout.timeout(3) do
expect(lock.acquire_read_lock).to be true
expect(lock.try_write_lock).to be true
expect(Thread.current).to hold(lock).for_write
expect(lock.release_write_lock).to be true
expect(lock.release_read_lock).to be true
expect(Thread.current).not_to hold(lock).for_write
expect(lock).to be_free
end
end
it "can acquire a write lock if a write lock is already held" do
Timeout.timeout(3) do
expect(lock.acquire_write_lock).to be true
expect(lock.try_write_lock).to be true
expect(Thread.current).to hold(lock).for_write
expect(lock.release_write_lock).to be true
expect(lock.release_write_lock).to be true
expect(Thread.current).not_to hold(lock).for_write
expect(lock).to be_free
end
end
end
it "can survive a torture test" do
latch = CountDownLatch.new
count = 0
writers = 5.times.collect do
Thread.new do
500.times do
lock.with_write_lock do
value = (count += 1)
sleep(0.0001)
count = value+1
end
end
end
end
readers = 15.times.collect do
Thread.new do
500.times do
lock.with_read_lock { expect(count % 2).to eq 0 }
end
end
end
writers.each(&:join)
readers.each(&:join)
expect(count).to eq 5000
end
end
end
end