123456789_123456789_123456789_123456789_123456789_

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.new
lock.acquire_write_lock
lock.acquire_read_lock
lock.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.
lock = Concurrent::ReentrantReadWriteLock.new
lock.with_read_lock  { data.retrieve }
lock.with_write_lock { data.modify! }

See Also:

Constant Summary

Class Attribute Summary

Class Method Summary

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 does.

.attr_volatile

Creates methods for reading and writing (as attr_accessor does) to a instance variable with volatile (Java) semantic.

.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 Method Summary

Constructor Details

.newReentrantReadWriteLock

Create a new ReentrantReadWriteLock in the unlocked state.

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/atomic/reentrant_read_write_lock.rb', line 109

def initialize
  super()
  @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  = LockLocalVar.new(0) # indicates # of R & W locks held by this thread
end

Instance Method Details

#acquire_read_lockBoolean

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

Returns:

  • (Boolean)

    true if the lock is successfully acquired

Raises:

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/atomic/reentrant_read_write_lock.rb', line 162

def acquire_read_lock
  if (held = @HeldCount.value) > 0
    # If we already have a lock, there's no need to wait
    if held & 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 + 1
    return true
  end

  while true
    c = @Counter.value
    raise ResourceLimitError.new('Too many reader threads') if max_readers?(c)

    # If a writer is waiting OR running when we first queue up, we need to wait
    if waiting_or_running_writer?(c)
      # Before going to sleep, check again with the ReadQueue mutex held
      @ReadQueue.synchronize do
        @ReadQueue.ns_wait if waiting_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)
      while true
        c = @Counter.value
        if running_writer?(c)
          @ReadQueue.synchronize do
            @ReadQueue.ns_wait if running_writer?
          end
        elsif @Counter.compare_and_set(c, c+1)
          @HeldCount.value = held + 1
          return true
        end
      end
    elsif @Counter.compare_and_set(c, c+1)
      @HeldCount.value = held + 1
      return true
    end
  end
end

#acquire_write_lockBoolean

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

Returns:

  • (Boolean)

    true if the lock is successfully acquired

Raises:

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/atomic/reentrant_read_write_lock.rb', line 257

def acquire_write_lock
  if (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_HELD
    return true
  end

  while true
    c = @Counter.value
    raise ResourceLimitError.new('Too many writer threads') if max_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
    if held > 0 && @Counter.compare_and_set(1, c+RUNNING_WRITER)
      # If we are the only one reader and successfully swap the RUNNING_WRITER bit on, then we can go ahead
      @HeldCount.value = held + WRITE_LOCK_HELD
      return true
    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
        @WriteQueue.synchronize do
          # 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_wait if running_writer?(c) || running_readers(c) != held
        end
        # 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.value
        if !running_writer?(c) &&
           running_readers(c) == held &&
           @Counter.compare_and_set(c, c+RUNNING_WRITER-WAITING_WRITER)
          @HeldCount.value = held + WRITE_LOCK_HELD
          return true
        end
      end
    end
  end
end

#max_readers?(c = @Counter.value) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/atomic/reentrant_read_write_lock.rb', line 370

def max_readers?(c = @Counter.value)
  (c & MAX_READERS) == MAX_READERS
end

#max_writers?(c = @Counter.value) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/atomic/reentrant_read_write_lock.rb', line 375

def max_writers?(c = @Counter.value)
  (c & MAX_WRITERS) == MAX_WRITERS
end

#release_read_lockBoolean

Release a previously acquired read lock.

Returns:

  • (Boolean)

    true if the lock is successfully released

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/atomic/reentrant_read_write_lock.rb', line 236

def release_read_lock
  held = @HeldCount.value = @HeldCount.value - 1
  rlocks_held = held & READ_LOCK_MASK
  if rlocks_held == 0
    c = @Counter.update { |counter| counter - 1 }
    # If one or more writers were waiting, and we were the last reader, wake a writer up
    if waiting_or_running_writer?(c) && running_readers(c) == 0
      @WriteQueue.signal
    end
  elsif rlocks_held == READ_LOCK_MASK
    raise IllegalOperationError, "Cannot release a read lock which is not held"
  end
  true
end

#release_write_lockBoolean

Release a previously acquired write lock.

Returns:

  • (Boolean)

    true if the lock is successfully released

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/atomic/reentrant_read_write_lock.rb', line 329

def release_write_lock
  held = @HeldCount.value = @HeldCount.value - WRITE_LOCK_HELD
  wlocks_held = held & WRITE_LOCK_MASK
  if wlocks_held == 0
    c = @Counter.update { |counter| counter - RUNNING_WRITER }
    @ReadQueue.broadcast
    @WriteQueue.signal if waiting_writers(c) > 0
  elsif wlocks_held == WRITE_LOCK_MASK
    raise IllegalOperationError, "Cannot release a write lock which is not held"
  end
  true
end

#running_readers(c = @Counter.value) (private)

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/atomic/reentrant_read_write_lock.rb', line 345

def running_readers(c = @Counter.value)
  c & MAX_READERS
end

#running_readers?(c = @Counter.value) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/atomic/reentrant_read_write_lock.rb', line 350

def running_readers?(c = @Counter.value)
  (c & MAX_READERS) > 0
end

#running_writer?(c = @Counter.value) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/atomic/reentrant_read_write_lock.rb', line 355

def running_writer?(c = @Counter.value)
  c >= RUNNING_WRITER
end

#try_read_lockBoolean

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

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/atomic/reentrant_read_write_lock.rb', line 215

def try_read_lock
  if (held = @HeldCount.value) > 0
    if held & READ_LOCK_MASK == 0
      # If we hold a write lock, but not a read lock...
      @Counter.update { |c| c + 1 }
    end
    @HeldCount.value = held + 1
    return true
  else
    c = @Counter.value
    if !waiting_or_running_writer?(c) && @Counter.compare_and_set(c, c+1)
      @HeldCount.value = held + 1
      return true
    end
  end
  false
end

#try_write_lockBoolean

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

Returns:

  • (Boolean)

    true if the lock is successfully acquired

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/atomic/reentrant_read_write_lock.rb', line 310

def try_write_lock
  if (held = @HeldCount.value) >= WRITE_LOCK_HELD
    @HeldCount.value = held + WRITE_LOCK_HELD
    return true
  else
    c = @Counter.value
    if !waiting_or_running_writer?(c) &&
       running_readers(c) == held &&
       @Counter.compare_and_set(c, c+RUNNING_WRITER)
       @HeldCount.value = held + WRITE_LOCK_HELD
      return true
    end
  end
  false
end

#waiting_or_running_writer?(c = @Counter.value) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/atomic/reentrant_read_write_lock.rb', line 365

def waiting_or_running_writer?(c = @Counter.value)
  c >= WAITING_WRITER
end

#waiting_writers(c = @Counter.value) (private)

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/atomic/reentrant_read_write_lock.rb', line 360

def waiting_writers(c = @Counter.value)
  (c & MAX_WRITERS) >> READER_BITS
end

#with_read_lock { ... } ⇒ Object

Execute a block operation within a read lock.

Yields:

  • the task to be performed within the lock.

Returns:

  • (Object)

    the result of the block operation.

Raises:

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/atomic/reentrant_read_write_lock.rb', line 126

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.

Yields:

  • the task to be performed within the lock.

Returns:

  • (Object)

    the result of the block operation.

Raises:

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/atomic/reentrant_read_write_lock.rb', line 145

def with_write_lock
  raise ArgumentError.new('no block given') unless block_given?
  acquire_write_lock
  begin
    yield
  ensure
    release_write_lock
  end
end