Skip to content

Commit f2e29bf

Browse files
committed
MVar: full?
1 parent edc1e1d commit f2e29bf

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

lib/concurrent/mvar.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def take(timeout = nil)
2020
@full_condition.wait(@mutex, timeout) if empty?
2121

2222
# If we timed out we'll still be empty
23-
if not empty?
23+
if full?
2424
value = @value
2525
@value = EMPTY
2626
@empty_condition.signal
@@ -36,7 +36,7 @@ def take(timeout = nil)
3636
def put(value, timeout = nil)
3737
@mutex.synchronize do
3838
# Unless the value is empty, wait for empty to be signalled
39-
@empty_condition.wait(@mutex, timeout) unless empty?
39+
@empty_condition.wait(@mutex, timeout) if full?
4040

4141
# If we timed out we won't be empty
4242
if empty?
@@ -53,6 +53,10 @@ def empty?
5353
@value == EMPTY
5454
end
5555

56+
def full?
57+
not empty?
58+
end
59+
5660
end
5761

5862
end

spec/concurrent/mvar_spec.rb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ module Concurrent
9696

9797
end
9898

99-
context '#empty' do
99+
context '#empty?' do
100100

101101
it 'returns true on an empty MVar' do
102102
m = MVar.new
@@ -110,6 +110,20 @@ module Concurrent
110110

111111
end
112112

113+
context '#full?' do
114+
115+
it 'returns false on an empty MVar' do
116+
m = MVar.new
117+
m.should_not be_full
118+
end
119+
120+
it 'returns true on a full MVar' do
121+
m = MVar.new(14)
122+
m.should be_full
123+
end
124+
125+
end
126+
113127
end
114128

115129
end

0 commit comments

Comments
 (0)