Class: Concurrent::Channel::BlockingRingBuffer

Inherits:
Synchronization::Object
  • Object
show all
Defined in:
lib/concurrent/channel/blocking_ring_buffer.rb

Instance Method Summary collapse

Constructor Details

#initialize(capacity) ⇒ BlockingRingBuffer

Returns a new instance of BlockingRingBuffer.



10
11
12
13
# File 'lib/concurrent/channel/blocking_ring_buffer.rb', line 10

def initialize(capacity)
  super()
  synchronize { ns_initialize capacity}
end

Instance Method Details

#capacityInteger

Returns the capacity of the buffer.

Returns:

  • (Integer)

    the capacity of the buffer



16
17
18
# File 'lib/concurrent/channel/blocking_ring_buffer.rb', line 16

def capacity
  synchronize { @buffer.capacity }
end

#countInteger

Returns the number of elements currently in the buffer.

Returns:

  • (Integer)

    the number of elements currently in the buffer



21
22
23
# File 'lib/concurrent/channel/blocking_ring_buffer.rb', line 21

def count
  synchronize { @buffer.count }
end

#empty?Boolean

Returns true if buffer is empty, false otherwise.

Returns:

  • (Boolean)

    true if buffer is empty, false otherwise



26
27
28
# File 'lib/concurrent/channel/blocking_ring_buffer.rb', line 26

def empty?
  synchronize { @buffer.empty? }
end

#full?Boolean

Returns true if buffer is full, false otherwise.

Returns:

  • (Boolean)

    true if buffer is full, false otherwise



31
32
33
# File 'lib/concurrent/channel/blocking_ring_buffer.rb', line 31

def full?
  synchronize { @buffer.full? }
end

#peekObject

Returns the first available value and without removing it from the buffer. If buffer is empty returns nil.

Returns:

  • (Object)

    the first available value and without removing it from the buffer. If buffer is empty returns nil



59
60
61
# File 'lib/concurrent/channel/blocking_ring_buffer.rb', line 59

def peek
  synchronize { @buffer.peek }
end

#put(value) ⇒ Boolean

Returns true if value has been inserted, false otherwise.

Parameters:

  • value (Object)

    the value to be inserted

Returns:

  • (Boolean)

    true if value has been inserted, false otherwise



37
38
39
40
41
42
43
44
# File 'lib/concurrent/channel/blocking_ring_buffer.rb', line 37

def put(value)
  synchronize do
    wait_while_full
    @buffer.offer(value)
    ns_signal
    true
  end
end

#takeObject

Returns the first available value and removes it from the buffer. If buffer is empty it blocks until an element is available.

Returns:

  • (Object)

    the first available value and removes it from the buffer. If buffer is empty it blocks until an element is available



48
49
50
51
52
53
54
55
# File 'lib/concurrent/channel/blocking_ring_buffer.rb', line 48

def take
  synchronize do
    wait_while_empty
    result = @buffer.poll
    ns_signal
    result
  end
end