File tree Expand file tree Collapse file tree 2 files changed +19
-5
lines changed Expand file tree Collapse file tree 2 files changed +19
-5
lines changed Original file line number Diff line number Diff line change @@ -11,30 +11,38 @@ def initialize(capacity)
11
11
@condition = Condition . new
12
12
end
13
13
14
+ # @return [Integer] the capacity of the buffer
14
15
def capacity
15
16
@mutex . synchronize { @buffer . capacity }
16
17
end
17
18
19
+ # @return [Integer] the number of elements currently in the buffer
18
20
def count
19
21
@mutex . synchronize { @buffer . count }
20
22
end
21
23
22
- def full?
23
- @mutex . synchronize { @buffer . full? }
24
- end
25
-
24
+ # @return [Boolean] true if buffer is empty, false otherwise
26
25
def empty?
27
26
@mutex . synchronize { @buffer . empty? }
28
27
end
29
28
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
30
36
def put ( value )
31
37
@mutex . synchronize do
32
38
wait_while_full
33
39
@buffer . offer ( value )
34
40
@condition . signal
41
+ true
35
42
end
36
43
end
37
44
45
+ # @return [Object] the first available value and removes it from the buffer. If buffer is empty it blocks until an element is available
38
46
def take
39
47
@mutex . synchronize do
40
48
wait_while_empty
@@ -44,6 +52,7 @@ def take
44
52
end
45
53
end
46
54
55
+ # @return [Object] the first available value and without removing it from the buffer. If buffer is empty returns nil
47
56
def peek
48
57
@mutex . synchronize { @buffer . peek }
49
58
end
Original file line number Diff line number Diff line change 1
1
module Concurrent
2
2
3
- # not thread safe buffer
3
+ # non- thread safe buffer
4
4
class RingBuffer
5
5
6
6
def initialize ( capacity )
@@ -9,18 +9,23 @@ def initialize(capacity)
9
9
@count = 0
10
10
end
11
11
12
+
13
+ # @return [Integer] the capacity of the buffer
12
14
def capacity
13
15
@buffer . size
14
16
end
15
17
18
+ # @return [Integer] the number of elements currently in the buffer
16
19
def count
17
20
@count
18
21
end
19
22
23
+ # @return [Boolean] true if buffer is empty, false otherwise
20
24
def empty?
21
25
@count == 0
22
26
end
23
27
28
+ # @return [Boolean] true if buffer is full, false otherwise
24
29
def full?
25
30
@count == capacity
26
31
end
You can’t perform that action at this time.
0 commit comments