Class: Concurrent::ReentrantReadWriteLock

Overview

Re-entrant read-write lock implementation

Allows any number of concurrent readers, but only one concurrent writer
(And while the "write" lock is taken, no read locks can be obtained either.
Hence, the write lock can also be called an "exclusive" lock.)

If another thread has taken a read lock, any thread which wants a write lock
will block until all the readers release their locks. However, once a thread
starts waiting to obtain a write lock, any additional readers that come along
will also wait (so writers are not starved).

A thread can acquire both a read and write lock at the same time. A thread can
also acquire a read lock OR a write lock more than once. Only when the read (or
write) lock is released as many times as it was acquired, will the thread
actually let it go, allowing other threads which might have been waiting
to proceed. Therefore the lock can be upgraded by first acquiring
read lock and then write lock and that the lock can be downgraded by first
having both read and write lock a releasing just the write lock.

If both read and write locks are acquired by the same thread, it is not strictly
necessary to release them in the same order they were acquired. In other words,
the following code is legal:

This implementation was inspired by java.util.concurrent.ReentrantReadWriteLock.

Examples:

lock=Concurrent::ReentrantReadWriteLock.newlock.acquire_write_locklock.acquire_read_locklock.release_write_lock# At this point, the current thread is holding only a read lock, not a write
# lock. So other threads can take read locks, but not a write lock.
lock.release_read_lock# Now the current thread is not holding either a read or write lock, so
# another thread could potentially acquire a write lock.

Constructor Details

# File 'lib/concurrent/atomic/reentrant_read_write_lock.rb', line 107definitializesuper()@Counter=AtomicFixnum.new(0)# single integer which represents lock state
@ReadQueue=Synchronization::Lock.new# used to queue waiting readers
@WriteQueue=Synchronization::Lock.new# used to queue waiting writers
@HeldCount=ThreadLocalVar.new(0)# indicates # of R & W locks held by this thread
end

Instance Method Details

#acquire_read_lock ⇒ Boolean

Acquire a read lock. If a write lock is held by another thread, will block
until it is released.

# File 'lib/concurrent/atomic/reentrant_read_write_lock.rb', line 160defacquire_read_lockif(held=@HeldCount.value)>0# If we already have a lock, there's no need to wait
ifheld&READ_LOCK_MASK==0# But we do need to update the counter, if we were holding a write
# lock but not a read lock
@Counter.update{|c|c+1}end@HeldCount.value=held+1returntrueendwhiletruec=@Counter.valueraiseResourceLimitError.new('Too many reader threads')ifmax_readers?(c)# If a writer is waiting OR running when we first queue up, we need to wait
ifwaiting_or_running_writer?(c)# Before going to sleep, check again with the ReadQueue mutex held
@ReadQueue.synchronizedo@ReadQueue.ns_waitifwaiting_or_running_writer?end# Note: the above 'synchronize' block could have used #wait_until,
# but that waits repeatedly in a loop, checking the wait condition
# each time it wakes up (to protect against spurious wakeups)
# But we are already in a loop, which is only broken when we successfully
# acquire the lock! So we don't care about spurious wakeups, and would
# rather not pay the extra overhead of using #wait_until
# After a reader has waited once, they are allowed to "barge" ahead of waiting writers
# But if a writer is *running*, the reader still needs to wait (naturally)
whiletruec=@Counter.valueifrunning_writer?(c)@ReadQueue.synchronizedo@ReadQueue.ns_waitifrunning_writer?endelsif@Counter.compare_and_set(c,c+1)@HeldCount.value=held+1returntrueendendelsif@Counter.compare_and_set(c,c+1)@HeldCount.value=held+1returntrueendendend

#acquire_write_lock ⇒ Boolean

Acquire a write lock. Will block and wait for all active readers and writers.

# File 'lib/concurrent/atomic/reentrant_read_write_lock.rb', line 255defacquire_write_lockif(held=@HeldCount.value)>=WRITE_LOCK_HELD# if we already have a write (exclusive) lock, there's no need to wait
@HeldCount.value=held+WRITE_LOCK_HELDreturntrueendwhiletruec=@Counter.valueraiseResourceLimitError.new('Too many writer threads')ifmax_writers?(c)# To go ahead and take the lock without waiting, there must be no writer
# running right now, AND no writers who came before us still waiting to
# acquire the lock
# Additionally, if any read locks have been taken, we must hold all of them
ifc==held# If we successfully swap the RUNNING_WRITER bit on, then we can go ahead
if@Counter.compare_and_set(c,c+RUNNING_WRITER)@HeldCount.value=held+WRITE_LOCK_HELDreturntrueendelsif@Counter.compare_and_set(c,c+WAITING_WRITER)whiletrue# Now we have successfully incremented, so no more readers will be able to increment
# (they will wait instead)
# However, readers OR writers could decrement right here
@WriteQueue.synchronizedo# So we have to do another check inside the synchronized section
# If a writer OR another reader is running, then go to sleep
c=@Counter.value@WriteQueue.ns_waitifrunning_writer?(c)||running_readers(c)!=heldend# Note: if you are thinking of replacing the above 'synchronize' block
# with #wait_until, read the comment in #acquire_read_lock first!
# We just came out of a wait
# If we successfully turn the RUNNING_WRITER bit on with an atomic swap,
# then we are OK to stop waiting and go ahead
# Otherwise go back and wait again
c=@Counter.valueif!running_writer?(c)&&running_readers(c)==held&&@Counter.compare_and_set(c,c+RUNNING_WRITER-WAITING_WRITER)@HeldCount.value=held+WRITE_LOCK_HELDreturntrueendendendendend

#release_read_lock ⇒ Boolean

Release a previously acquired read lock.

Returns:

(Boolean)
—

true if the lock is successfully released

234
235
236
237
238
239
240
241
242
243
244
245
246
247

# File 'lib/concurrent/atomic/reentrant_read_write_lock.rb', line 234defrelease_read_lockheld=@HeldCount.value=@HeldCount.value-1rlocks_held=held&READ_LOCK_MASKifrlocks_held==0c=@Counter.update{|counter|counter-1}# If one or more writers were waiting, and we were the last reader, wake a writer up
ifwaiting_or_running_writer?(c)&&running_readers(c)==0@WriteQueue.signalendelsifrlocks_held==READ_LOCK_MASKraiseIllegalOperationError,"Cannot release a read lock which is not held"endtrueend

#release_write_lock ⇒ Boolean

Release a previously acquired write lock.

Returns:

(Boolean)
—

true if the lock is successfully released

329
330
331
332
333
334
335
336
337
338
339
340

# File 'lib/concurrent/atomic/reentrant_read_write_lock.rb', line 329defrelease_write_lockheld=@HeldCount.value=@HeldCount.value-WRITE_LOCK_HELDwlocks_held=held&WRITE_LOCK_MASKifwlocks_held==0c=@Counter.update{|counter|counter-RUNNING_WRITER}@ReadQueue.broadcast@WriteQueue.signalifwaiting_writers(c)>0elsifwlocks_held==WRITE_LOCK_MASKraiseIllegalOperationError,"Cannot release a write lock which is not held"endtrueend

#try_read_lock ⇒ Boolean

Try to acquire a read lock and return true if we succeed. If it cannot be
acquired immediately, return false.

Returns:

(Boolean)
—

true if the lock is successfully acquired

213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229

# File 'lib/concurrent/atomic/reentrant_read_write_lock.rb', line 213deftry_read_lockif(held=@HeldCount.value)>0ifheld&READ_LOCK_MASK==0# If we hold a write lock, but not a read lock...
@Counter.update{|c|c+1}end@HeldCount.value=held+1returntrueelsec=@Counter.valueif!waiting_or_running_writer?(c)&&@Counter.compare_and_set(c,c+1)@HeldCount.value=held+1returntrueendendfalseend

#try_write_lock ⇒ Boolean

Try to acquire a write lock and return true if we succeed. If it cannot be
acquired immediately, return false.