class Concurrent::ReadWriteLock

def acquire_read_lock

Raises:
  • (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