1414 module Concurrent
1515
1616 BaseMatcher = RSpec ::Matchers ::BuiltIn ::BaseMatcher
17- RRWL = ReentrantReadWriteLock # no way I'm typing that 50 times
17+ # RRWL = ReentrantReadWriteLock # no way I'm typing that 50 times
18+
19+ class RRWL < ReentrantReadWriteLock
20+ def initialize
21+ super
22+
23+ @read_acquired = Concurrent ::Set . new
24+ @write_acquired = Concurrent ::Set . new
25+ end
26+
27+ attr :read_acquired
28+ attr :write_acquired
29+
30+ def try_read_lock
31+ if super
32+ @read_acquired . add ( Thread . current )
33+ true
34+ else
35+ false
36+ end
37+ end
38+
39+ def try_write_lock
40+ if super
41+ @write_acquired . add ( Thread . current )
42+ true
43+ else
44+ false
45+ end
46+ end
47+
48+ def acquire_read_lock
49+ if super
50+ @read_acquired . add ( Thread . current )
51+ true
52+ else
53+ false
54+ end
55+ end
56+
57+ def acquire_write_lock
58+ if super
59+ @write_acquired . add ( Thread . current )
60+ true
61+ else
62+ false
63+ end
64+ end
65+
66+ def release_read_lock
67+ super . tap do
68+ @read_acquired . delete ( Thread . current )
69+ end
70+ end
71+
72+ def release_write_lock
73+ super . tap do
74+ @write_acquired . delete ( Thread . current )
75+ end
76+ end
77+ end
1878
1979 # ****************************************************************
2080 # First some custom matchers to make our tests all nice and pretty
@@ -40,15 +100,13 @@ def for_both
40100
41101 class HoldReadLock < BaseMatcher
42102 def match ( lock , thread )
43- ( ( lock . instance_eval { @Counter . value } & RRWL ::MAX_READERS ) != 0 ) &&
44- ( ( lock . instance_eval { @HeldCount . send ( :value_for , thread ) } & RRWL ::READ_LOCK_MASK ) > 0 )
103+ ( ( lock . instance_eval { @Counter . value } & RRWL ::MAX_READERS ) != 0 ) && lock . read_acquired . include? ( thread )
45104 end
46105 end
47106
48107 class HoldWriteLock < BaseMatcher
49108 def match ( lock , thread )
50- ( ( lock . instance_eval { @Counter . value } & RRWL ::RUNNING_WRITER ) != 0 ) &&
51- ( ( lock . instance_eval { @HeldCount . send ( :value_for , thread ) } & RRWL ::WRITE_LOCK_MASK ) > 0 )
109+ ( ( lock . instance_eval { @Counter . value } & RRWL ::RUNNING_WRITER ) != 0 ) && lock . write_acquired . include? ( thread )
52110 end
53111 end
54112
0 commit comments