class Concurrent::ReadWriteLock
@see docs.oracle.com/javase/7/docs/api/java/util/concurrent/locks/ReentrantReadWriteLock.html java.util.concurrent.ReentrantReadWriteLock
This will lead to deadlock
or try to acquire the write lock while you already have it.
@note Do not try to acquire the write lock while already holding a read lock
lock.with_write_lock { data.modify! }
lock.with_read_lock { data.retrieve }
lock = Concurrent::ReadWriteLock.new
@example
This implementation is based on ‘java.util.concurrent.ReentrantReadWriteLock`.
wait (so writers are not starved).
Any additional readers that come when the writer is already waiting, will also
all the readers to finish before going ahead.
If readers are already active when a writer comes along, the writer will wait for
(And if the “write” lock is taken, any readers who come along will have to wait)
Allows any number of concurrent readers, but only one concurrent writer
Ruby read-write lock implementation
def acquire_read_lock
-
(Concurrent::ResourceLimitError)
- if the maximum number of readers
Returns:
-
(Boolean)
- true if the lock is successfully acquired
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
def acquire_write_lock
-
(Concurrent::ResourceLimitError)
- if the maximum number of writers
Returns:
-
(Boolean)
- true if the lock is successfully acquired
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
def has_waiters?
-
(Boolean)
- true if any threads are waiting for a lock else false
def has_waiters? waiting_writer?(@Counter.value) end
def initialize
def initialize super() @Counter = AtomicFixnum.new(0) # single integer which represents lock state @ReadLock = Synchronization::Lock.new @WriteLock = Synchronization::Lock.new end
def max_readers?(c = @Counter.value)
def max_readers?(c = @Counter.value) (c & MAX_READERS) == MAX_READERS end
def max_writers?(c = @Counter.value)
def max_writers?(c = @Counter.value) (c & MAX_WRITERS) == MAX_WRITERS end
def release_read_lock
-
(Boolean)
- true if the lock is successfully released
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
def release_write_lock
-
(Boolean)
- true if the lock is successfully released
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
def running_readers(c = @Counter.value)
def running_readers(c = @Counter.value) c & MAX_READERS end
def running_readers?(c = @Counter.value)
def running_readers?(c = @Counter.value) (c & MAX_READERS) > 0 end
def running_writer?(c = @Counter.value)
def running_writer?(c = @Counter.value) c >= RUNNING_WRITER end
def waiting_writer?(c = @Counter.value)
def waiting_writer?(c = @Counter.value) c >= WAITING_WRITER end
def waiting_writers(c = @Counter.value)
def waiting_writers(c = @Counter.value) (c & MAX_WRITERS) / WAITING_WRITER end
def with_read_lock
-
(Concurrent::ResourceLimitError)
- if the maximum number of readers -
(ArgumentError)
- when no block is given.
Returns:
-
(Object)
- the result of the block operation.
Other tags:
- Yield: - the task to be performed within the lock.
def with_read_lock raise ArgumentError.new('no block given') unless block_given? acquire_read_lock begin yield ensure release_read_lock end end
def with_write_lock
-
(Concurrent::ResourceLimitError)
- if the maximum number of readers -
(ArgumentError)
- when no block is given.
Returns:
-
(Object)
- the result of the block operation.
Other tags:
- Yield: - the task to be performed within the lock.
def with_write_lock raise ArgumentError.new('no block given') unless block_given? acquire_write_lock begin yield ensure release_write_lock end end
def write_locked?
-
(Boolean)
- true if the write lock is held else false`
def write_locked? @Counter.value >= RUNNING_WRITER end