Skip to content

Commit af2c37d

Browse files
committed
added circular condition
1 parent b9a2a3c commit af2c37d

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

lib/concurrent/channel/ring_buffer.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def put(value)
2121
@mutex.synchronize do
2222
wait_while_full
2323
@buffer[@last] = value
24-
@last += 1
24+
@last = (@last + 1) % @buffer.size
2525
@count += 1
2626
@condition.signal
2727
end
@@ -32,7 +32,7 @@ def take
3232
wait_while_empty
3333
result = @buffer[@first]
3434
@buffer[@first] = nil
35-
@first += 1
35+
@first = (@first + 1) % @buffer.size
3636
@count -= 1
3737
@condition.signal
3838
result

spec/concurrent/channel/ring_buffer_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,5 +85,17 @@ module Concurrent
8585
end
8686
end
8787

88+
context 'circular condition' do
89+
it 'can filled many times' do
90+
capacity.times { buffer.put 3 }
91+
capacity.times { buffer.take }
92+
93+
buffer.put 'hi'
94+
95+
buffer.take.should eq 'hi'
96+
buffer.capacity.should eq capacity
97+
end
98+
end
99+
88100
end
89101
end

0 commit comments

Comments
 (0)