Class: Concurrent::Channel::BlockingRingBuffer
- Inherits:
-
Synchronization::Object
- Object
- Synchronization::Object
- Concurrent::Channel::BlockingRingBuffer
- Defined in:
- lib/concurrent/channel/blocking_ring_buffer.rb
Instance Method Summary collapse
-
#capacity ⇒ Integer
The capacity of the buffer.
-
#count ⇒ Integer
The number of elements currently in the buffer.
-
#empty? ⇒ Boolean
True if buffer is empty, false otherwise.
-
#full? ⇒ Boolean
True if buffer is full, false otherwise.
-
#initialize(capacity) ⇒ BlockingRingBuffer
constructor
A new instance of BlockingRingBuffer.
-
#peek ⇒ Object
The first available value and without removing it from the buffer.
-
#put(value) ⇒ Boolean
True if value has been inserted, false otherwise.
-
#take ⇒ Object
The first available value and removes it from the buffer.
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
#capacity ⇒ Integer
Returns the capacity of the buffer.
16 17 18 |
# File 'lib/concurrent/channel/blocking_ring_buffer.rb', line 16 def capacity synchronize { @buffer.capacity } end |
#count ⇒ Integer
Returns 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.
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.
31 32 33 |
# File 'lib/concurrent/channel/blocking_ring_buffer.rb', line 31 def full? synchronize { @buffer.full? } end |
#peek ⇒ Object
Returns 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.
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 |
#take ⇒ Object
Returns 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 |