11require 'thread'
2- require 'concurrent/atomic/atomic_reference '
2+ require 'concurrent/atomic/atomic_fixnum '
33require 'concurrent/errors'
44require 'concurrent/synchronization'
55
@@ -33,7 +33,7 @@ class ReadWriteLock < Synchronization::Object
3333 WAITING_WRITER = 1 << 15
3434
3535 # @!visibility private
36- RUNNING_WRITER = 1 << 30
36+ RUNNING_WRITER = 1 << 29
3737
3838 # @!visibility private
3939 MAX_READERS = WAITING_WRITER - 1
@@ -50,11 +50,11 @@ class ReadWriteLock < Synchronization::Object
5050 # Each reader increments the counter by 1 when acquiring a read lock
5151 # (and decrements by 1 when releasing the read lock)
5252 # The counter is increased by (1 << 15) for each writer waiting to acquire the
53- # write lock, and by (1 << 30 ) if the write lock is taken
53+ # write lock, and by (1 << 29 ) if the write lock is taken
5454
5555 # Create a new `ReadWriteLock` in the unlocked state.
5656 def initialize
57- @Counter = AtomicReference . new ( 0 ) # single integer which represents lock state
57+ @Counter = AtomicFixnum . new ( 0 ) # single integer which represents lock state
5858 @ReadLock = Synchronization ::Lock . new
5959 @WriteLock = Synchronization ::Lock . new
6060 ensure_ivar_visibility!
@@ -122,11 +122,11 @@ def acquire_read_lock
122122 if running_writer? ( c )
123123 @ReadLock . wait_until { !running_writer? }
124124 else
125- return if @Counter . compare_and_swap ( c , c +1 )
125+ return if @Counter . compare_and_set ( c , c +1 )
126126 end
127127 end
128128 else
129- break if @Counter . compare_and_swap ( c , c +1 )
129+ break if @Counter . compare_and_set ( c , c +1 )
130130 end
131131 end
132132 true
@@ -138,7 +138,7 @@ def acquire_read_lock
138138 def release_read_lock
139139 while true
140140 c = @Counter . value
141- if @Counter . compare_and_swap ( c , c -1 )
141+ if @Counter . compare_and_set ( c , c -1 )
142142 # If one or more writers were waiting, and we were the last reader, wake a writer up
143143 if waiting_writer? ( c ) && running_readers ( c ) == 1
144144 @WriteLock . signal
@@ -162,8 +162,8 @@ def acquire_write_lock
162162
163163 if c == 0 # no readers OR writers running
164164 # if we successfully swap the RUNNING_WRITER bit on, then we can go ahead
165- break if @Counter . compare_and_swap ( 0 , RUNNING_WRITER )
166- elsif @Counter . compare_and_swap ( c , c +WAITING_WRITER )
165+ break if @Counter . compare_and_set ( 0 , RUNNING_WRITER )
166+ elsif @Counter . compare_and_set ( c , c +WAITING_WRITER )
167167 while true
168168 # Now we have successfully incremented, so no more readers will be able to increment
169169 # (they will wait instead)
@@ -180,7 +180,7 @@ def acquire_write_lock
180180 # Then we are OK to stop waiting and go ahead
181181 # Otherwise go back and wait again
182182 c = @Counter . value
183- break if !running_writer? ( c ) && !running_readers? ( c ) && @Counter . compare_and_swap ( c , c +RUNNING_WRITER -WAITING_WRITER )
183+ break if !running_writer? ( c ) && !running_readers? ( c ) && @Counter . compare_and_set ( c , c +RUNNING_WRITER -WAITING_WRITER )
184184 end
185185 break
186186 end
0 commit comments