Skip to content

Commit 38382b0

Browse files
committed
rename #take to #borrow in case a block is given
1 parent f01086d commit 38382b0

File tree

2 files changed

+42
-28
lines changed

2 files changed

+42
-28
lines changed

lib/concurrent/mvar.rb

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,27 @@ def take(timeout = nil)
6868

6969
# If we timed out we'll still be empty
7070
if unlocked_full?
71-
if block_given?
72-
yield @value
73-
else
74-
value = @value
75-
@value = EMPTY
76-
@empty_condition.signal
77-
apply_deref_options(value)
78-
end
71+
value = @value
72+
@value = EMPTY
73+
@empty_condition.signal
74+
apply_deref_options(value)
75+
else
76+
TIMEOUT
77+
end
78+
end
79+
end
80+
81+
# acquires lock on the from an `MVAR`, yields the value to provided block,
82+
# and release lock. A timeout can be set to limit the time spent blocked,
83+
# in which case it returns `TIMEOUT` if the time is exceeded.
84+
# @return [Object] the value returned by the block, or `TIMEOUT`
85+
def borrow(timeout = nil)
86+
@mutex.synchronize do
87+
wait_for_full(timeout)
88+
89+
# if we timeoud out we'll still be empty
90+
if unlocked_full?
91+
yield @value
7992
else
8093
TIMEOUT
8194
end

spec/concurrent/mvar_spec.rb

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -68,35 +68,36 @@ def dereferenceable_subject(value, opts = {})
6868
expect(m.take(0.1)).to eq MVar::TIMEOUT
6969
end
7070

71-
context 'when a block is given' do
71+
end
7272

73-
it 'yields current value to the block and puts back value' do
74-
m = MVar.new(14)
75-
expect { |b| m.take(&b) }.to yield_with_args(14)
76-
expect(m.take).to eq(14)
77-
end
73+
describe '#borrow' do
7874

79-
it 'puts back value even if an exception is raised' do
80-
m = MVar.new(14)
81-
expect { m.take { fail 'boom!' } }.to raise_error('boom!')
82-
expect(m.take).to eq(14)
83-
end
75+
it 'yields current value to the block and puts back value' do
76+
m = MVar.new(14)
77+
expect { |b| m.borrow(&b) }.to yield_with_args(14)
78+
expect(m.take).to eq(14)
79+
end
8480

85-
it 'returns the returned value of the block' do
86-
m = MVar.new(14)
87-
expect(m.take{2}).to eq(2)
88-
expect(m.take).to eq(14)
89-
end
81+
it 'puts back value even if an exception is raised' do
82+
m = MVar.new(14)
83+
expect { m.borrow { fail 'boom!' } }.to raise_error('boom!')
84+
expect(m.take).to eq(14)
85+
end
9086

91-
it 'returns TIMEOUT on timeout on an empty MVar' do
92-
m = MVar.new
93-
expect(m.take(0.1){}).to eq MVar::TIMEOUT
94-
end
87+
it 'returns the returned value of the block' do
88+
m = MVar.new(14)
89+
expect(m.borrow{2}).to eq(2)
90+
expect(m.take).to eq(14)
91+
end
9592

93+
it 'returns TIMEOUT on timeout on an empty MVar' do
94+
m = MVar.new
95+
expect(m.borrow(0.1){}).to eq MVar::TIMEOUT
9696
end
9797

9898
end
9999

100+
100101
describe '#put' do
101102

102103
it 'sets the MVar to be empty' do

0 commit comments

Comments
 (0)