class Concurrent::BlockingRingBuffer

def capacity

Returns:
  • (Integer) - the capacity of the buffer
def capacity
  @mutex.synchronize { @buffer.capacity }
end

def count

Returns:
  • (Integer) - the number of elements currently in the buffer
def count
  @mutex.synchronize { @buffer.count }
end

def empty?

Returns:
  • (Boolean) - true if buffer is empty, false otherwise
def empty?
  @mutex.synchronize { @buffer.empty? }
end

def full?

Returns:
  • (Boolean) - true if buffer is full, false otherwise
def full?
  @mutex.synchronize { @buffer.full? }
end

def initialize(capacity)

def initialize(capacity)
  @buffer = RingBuffer.new(capacity)
  @first = @last = 0
  @count = 0
  @mutex = Mutex.new
  @condition = Condition.new
end

def peek

Returns:
  • (Object) - the first available value and without removing it from the buffer. If buffer is empty returns nil
def peek
  @mutex.synchronize { @buffer.peek }
end

def put(value)

Returns:
  • (Boolean) - true if value has been inserted, false otherwise

Parameters:
  • value. (Object) -- This methods blocks until an empty slot is available
def put(value)
  @mutex.synchronize do
    wait_while_full
    @buffer.offer(value)
    @condition.signal
    true
  end
end

def take

Returns:
  • (Object) - the first available value and removes it from the buffer. If buffer is empty it blocks until an element is available
def take
  @mutex.synchronize do
    wait_while_empty
    result = @buffer.poll
    @condition.signal
    result
  end
end

def wait_while_empty

def wait_while_empty
  @condition.wait(@mutex) while @buffer.empty?
end

def wait_while_full

def wait_while_full
  @condition.wait(@mutex) while @buffer.full?
end