Class: Concurrent::ReadWriteLock
Relationships & Source Files | |
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
|
|
Instance Chain:
|
|
Inherits: |
Concurrent::Synchronization::Object
|
Defined in: | lib/concurrent-ruby/concurrent/atomic/read_write_lock.rb |
Overview
Do not try to acquire the write lock while already holding a read lock or try to acquire the write lock while you already have it. This will lead to deadlock
Ruby read-write lock implementation
Allows any number of concurrent readers, but only one concurrent writer (And if the “write” lock is taken, any readers who come along will have to wait)
If readers are already active when a writer comes along, the writer will wait for all the readers to finish before going ahead. Any additional readers that come when the writer is already waiting, will also wait (so writers are not starved).
This implementation is based on java.util.concurrent.ReentrantReadWriteLock
.
Constant Summary
-
MAX_READERS =
private
# File 'lib/concurrent-ruby/concurrent/atomic/read_write_lock.rb', line 40WAITING_WRITER - 1
-
MAX_WRITERS =
private
# File 'lib/concurrent-ruby/concurrent/atomic/read_write_lock.rb', line 43RUNNING_WRITER - MAX_READERS - 1
-
RUNNING_WRITER =
private
# File 'lib/concurrent-ruby/concurrent/atomic/read_write_lock.rb', line 371 << 29
-
WAITING_WRITER =
private
# File 'lib/concurrent-ruby/concurrent/atomic/read_write_lock.rb', line 341 << 15
Class Attribute Summary
Synchronization::Object
- Inherited
Class Method Summary
-
.new ⇒ ReadWriteLock
constructor
Create a new
ReadWriteLock
in the unlocked state.
Synchronization::Object
- Inherited
.atomic_attribute?, .atomic_attributes, | |
.attr_atomic | Creates methods for reading and writing to a instance variable with volatile (Java) semantic as |
.attr_volatile | Creates methods for reading and writing (as |
.ensure_safe_initialization_when_final_fields_are_present | For testing purposes, quite slow. |
.new | Has to be called by children. |
.safe_initialization!, .define_initialize_atomic_fields |
Synchronization::AbstractObject
- Inherited
Instance Attribute Summary
-
#has_waiters? ⇒ Boolean
readonly
Queries whether any threads are waiting to acquire the read or write lock.
-
#write_locked? ⇒ Boolean
readonly
Queries if the write lock is held by any thread.
Instance Method Summary
-
#acquire_read_lock ⇒ Boolean
Acquire a read lock.
-
#acquire_write_lock ⇒ Boolean
Acquire a write lock.
-
#release_read_lock ⇒ Boolean
Release a previously acquired read lock.
-
#release_write_lock ⇒ Boolean
Release a previously acquired write lock.
-
#with_read_lock { ... } ⇒ Object
Execute a block operation within a read lock.
-
#with_write_lock { ... } ⇒ Object
Execute a block operation within a write lock.
- #max_readers?(c = @Counter.value) ⇒ Boolean private
- #max_writers?(c = @Counter.value) ⇒ Boolean private
- #running_readers(c = @Counter.value) private
- #running_readers?(c = @Counter.value) ⇒ Boolean private
- #running_writer?(c = @Counter.value) ⇒ Boolean private
- #waiting_writer?(c = @Counter.value) ⇒ Boolean private
- #waiting_writers(c = @Counter.value) private
Synchronization::Object
- Inherited
Synchronization::Volatile
- Included
Synchronization::AbstractObject
- Inherited
Constructor Details
.new ⇒ ReadWriteLock
Create a new ReadWriteLock
in the unlocked state.
# File 'lib/concurrent-ruby/concurrent/atomic/read_write_lock.rb', line 59
def initialize super() @Counter = AtomicFixnum.new(0) # single integer which represents lock state @ReadLock = Synchronization::Lock.new @WriteLock = Synchronization::Lock.new end
Instance Attribute Details
#has_waiters? ⇒ Boolean
(readonly)
Queries whether any threads are waiting to acquire the read or write lock.
# File 'lib/concurrent-ruby/concurrent/atomic/read_write_lock.rb', line 214
def has_waiters? waiting_writer?(@Counter.value) end
#write_locked? ⇒ Boolean
(readonly)
Queries if the write lock is held by any thread.
# File 'lib/concurrent-ruby/concurrent/atomic/read_write_lock.rb', line 207
def write_locked? @Counter.value >= RUNNING_WRITER end
Instance Method Details
#acquire_read_lock ⇒ Boolean
Acquire a read lock. If a write lock has been acquired will block until it is released. Will not block if other read locks have been acquired.
# File 'lib/concurrent-ruby/concurrent/atomic/read_write_lock.rb', line 111
def acquire_read_lock while true c = @Counter.value raise ResourceLimitError.new('Too many reader threads') if max_readers?(c) # If a writer is waiting when we first queue up, we need to wait if waiting_writer?(c) @ReadLock.wait_until { !waiting_writer? } # 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) while true c = @Counter.value if running_writer?(c) @ReadLock.wait_until { !running_writer? } else return if @Counter.compare_and_set(c, c+1) end end else break if @Counter.compare_and_set(c, c+1) end end true end
#acquire_write_lock ⇒ Boolean
Acquire a write lock. Will block and wait for all active readers and writers.
# File 'lib/concurrent-ruby/concurrent/atomic/read_write_lock.rb', line 160
def acquire_write_lock while true c = @Counter.value raise ResourceLimitError.new('Too many writer threads') if max_writers?(c) if c == 0 # no readers OR writers running # if we successfully swap the RUNNING_WRITER bit on, then we can go ahead break if @Counter.compare_and_set(0, RUNNING_WRITER) elsif @Counter.compare_and_set(c, c+WAITING_WRITER) while true # 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, OR another writer could increment @WriteLock.wait_until do # So we have to do another check inside the synchronized section # If a writer OR reader is running, then go to sleep c = @Counter.value !running_writer?(c) && !running_readers?(c) end # 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.value break if !running_writer?(c) && !running_readers?(c) && @Counter.compare_and_set(c, c+RUNNING_WRITER-WAITING_WRITER) end break end end true end
#max_readers?(c = @Counter.value) ⇒ Boolean
(private)
# File 'lib/concurrent-ruby/concurrent/atomic/read_write_lock.rb', line 246
def max_readers?(c = @Counter.value) (c & MAX_READERS) == MAX_READERS end
#max_writers?(c = @Counter.value) ⇒ Boolean
(private)
# File 'lib/concurrent-ruby/concurrent/atomic/read_write_lock.rb', line 251
def max_writers?(c = @Counter.value) (c & MAX_WRITERS) == MAX_WRITERS end
#release_read_lock ⇒ Boolean
Release a previously acquired read lock.
# File 'lib/concurrent-ruby/concurrent/atomic/read_write_lock.rb', line 140
def release_read_lock while true c = @Counter.value if @Counter.compare_and_set(c, c-1) # If one or more writers were waiting, and we were the last reader, wake a writer up if waiting_writer?(c) && running_readers(c) == 1 @WriteLock.signal end break end end true end
#release_write_lock ⇒ Boolean
Release a previously acquired write lock.
# File 'lib/concurrent-ruby/concurrent/atomic/read_write_lock.rb', line 196
def release_write_lock return true unless running_writer? c = @Counter.update { |counter| counter - RUNNING_WRITER } @ReadLock.broadcast @WriteLock.signal if waiting_writers(c) > 0 true end
#running_readers(c = @Counter.value) (private)
[ GitHub ]# File 'lib/concurrent-ruby/concurrent/atomic/read_write_lock.rb', line 221
def running_readers(c = @Counter.value) c & MAX_READERS end
#running_readers?(c = @Counter.value) ⇒ Boolean
(private)
# File 'lib/concurrent-ruby/concurrent/atomic/read_write_lock.rb', line 226
def running_readers?(c = @Counter.value) (c & MAX_READERS) > 0 end
#running_writer?(c = @Counter.value) ⇒ Boolean
(private)
# File 'lib/concurrent-ruby/concurrent/atomic/read_write_lock.rb', line 231
def running_writer?(c = @Counter.value) c >= RUNNING_WRITER end
#waiting_writer?(c = @Counter.value) ⇒ Boolean
(private)
# File 'lib/concurrent-ruby/concurrent/atomic/read_write_lock.rb', line 241
def waiting_writer?(c = @Counter.value) c >= WAITING_WRITER end
#waiting_writers(c = @Counter.value) (private)
[ GitHub ]# File 'lib/concurrent-ruby/concurrent/atomic/read_write_lock.rb', line 236
def waiting_writers(c = @Counter.value) (c & MAX_WRITERS) / WAITING_WRITER end
#with_read_lock { ... } ⇒ Object
Execute a block operation within a read lock.
# File 'lib/concurrent-ruby/concurrent/atomic/read_write_lock.rb', line 75
def with_read_lock raise ArgumentError.new('no block given') unless block_given? acquire_read_lock begin yield ensure release_read_lock end end
#with_write_lock { ... } ⇒ Object
Execute a block operation within a write lock.
# File 'lib/concurrent-ruby/concurrent/atomic/read_write_lock.rb', line 94
def with_write_lock raise ArgumentError.new('no block given') unless block_given? acquire_write_lock begin yield ensure release_write_lock end end