Skip to content

Commit 860b42c

Browse files
committed
added YARD to ring buffer and blocking ring buffer
1 parent fd397db commit 860b42c

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

lib/concurrent/channel/blocking_ring_buffer.rb

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,38 @@ def initialize(capacity)
1111
@condition = Condition.new
1212
end
1313

14+
# @return [Integer] the capacity of the buffer
1415
def capacity
1516
@mutex.synchronize { @buffer.capacity }
1617
end
1718

19+
# @return [Integer] the number of elements currently in the buffer
1820
def count
1921
@mutex.synchronize { @buffer.count }
2022
end
2123

22-
def full?
23-
@mutex.synchronize { @buffer.full? }
24-
end
25-
24+
# @return [Boolean] true if buffer is empty, false otherwise
2625
def empty?
2726
@mutex.synchronize { @buffer.empty? }
2827
end
2928

29+
# @return [Boolean] true if buffer is full, false otherwise
30+
def full?
31+
@mutex.synchronize { @buffer.full? }
32+
end
33+
34+
# @param [Object] value. This methods blocks until an empty slot is available
35+
# @return [Boolean] true if value has been inserted, false otherwise
3036
def put(value)
3137
@mutex.synchronize do
3238
wait_while_full
3339
@buffer.offer(value)
3440
@condition.signal
41+
true
3542
end
3643
end
3744

45+
# @return [Object] the first available value and removes it from the buffer. If buffer is empty it blocks until an element is available
3846
def take
3947
@mutex.synchronize do
4048
wait_while_empty
@@ -44,6 +52,7 @@ def take
4452
end
4553
end
4654

55+
# @return [Object] the first available value and without removing it from the buffer. If buffer is empty returns nil
4756
def peek
4857
@mutex.synchronize { @buffer.peek }
4958
end

lib/concurrent/channel/ring_buffer.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module Concurrent
22

3-
# not thread safe buffer
3+
# non-thread safe buffer
44
class RingBuffer
55

66
def initialize(capacity)
@@ -9,18 +9,23 @@ def initialize(capacity)
99
@count = 0
1010
end
1111

12+
13+
# @return [Integer] the capacity of the buffer
1214
def capacity
1315
@buffer.size
1416
end
1517

18+
# @return [Integer] the number of elements currently in the buffer
1619
def count
1720
@count
1821
end
1922

23+
# @return [Boolean] true if buffer is empty, false otherwise
2024
def empty?
2125
@count == 0
2226
end
2327

28+
# @return [Boolean] true if buffer is full, false otherwise
2429
def full?
2530
@count == capacity
2631
end

0 commit comments

Comments
 (0)