Skip to content

Commit b7e55a2

Browse files
committed
Removed DCL from LazyReference.
1 parent 0c05bce commit b7e55a2

File tree

2 files changed

+9
-43
lines changed

2 files changed

+9
-43
lines changed

lib/concurrent/lazy_reference.rb

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@ module Concurrent
1010
#
1111
# Because of its simplicity `LazyReference` is much faster than `Delay`:
1212
#
13-
# user system total real
14-
# Benchmarking Delay...
15-
# 0.730000 0.000000 0.730000 ( 0.738434)
16-
# Benchmarking LazyReference...
17-
# 0.040000 0.000000 0.040000 ( 0.042322)
13+
# Rehearsal -------------------------------------------------
14+
# Delay 0.200000 0.000000 0.200000 ( 0.201775)
15+
# LazyReference 0.150000 0.000000 0.150000 ( 0.151327)
16+
# ---------------------------------------- total: 0.350000sec
17+
#
18+
# user system total real
19+
# Delay 0.200000 0.000000 0.200000 ( 0.201151)
20+
# LazyReference 0.150000 0.000000 0.150000 ( 0.152647)
1821
#
1922
# @see Concurrent::Delay
2023
class LazyReference
@@ -35,16 +38,7 @@ def initialize(default = nil, &block)
3538
@fulfilled = false
3639
end
3740

38-
# The calculated value of the object or the default value if one
39-
# was given at construction. This first time this method is called
40-
# it will block indefinitely while the block is processed.
41-
# Subsequent calls will not block.
42-
#
43-
# @return [Object] the calculated value
4441
def value
45-
# double-checked locking is safe because we only update once
46-
return @value if @fulfilled
47-
4842
@mutex.synchronize do
4943
unless @fulfilled
5044
begin
@@ -55,8 +49,8 @@ def value
5549
@fulfilled = true
5650
end
5751
end
58-
return @value
5952
end
53+
return @value
6054
end
6155
end
6256
end

spec/concurrent/lazy_reference_spec.rb

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,6 @@ module Concurrent
3131
5.times{ lazy.value }
3232
end
3333

34-
it 'does not lock the mutex once the block has been called' do
35-
mutex = Mutex.new
36-
allow(Mutex).to receive(:new).and_return(mutex)
37-
38-
lazy = LazyReference.new(&task)
39-
lazy.value
40-
41-
expect(mutex).to_not receive(:synchronize).with(any_args)
42-
expect(mutex).to_not receive(:lock).with(any_args)
43-
expect(mutex).to_not receive(:try_lock).with(any_args)
44-
45-
5.times{ lazy.value }
46-
end
47-
4834
context 'on exception' do
4935

5036
it 'suppresses the error' do
@@ -62,20 +48,6 @@ module Concurrent
6248
lazy = LazyReference.new(100){ raise StandardError }
6349
expect(lazy.value).to eq 100
6450
end
65-
66-
it 'does not try to call the block again' do
67-
mutex = Mutex.new
68-
allow(Mutex).to receive(:new).and_return(mutex)
69-
70-
lazy = LazyReference.new{ raise StandardError }
71-
lazy.value
72-
73-
expect(mutex).to_not receive(:synchronize).with(any_args)
74-
expect(mutex).to_not receive(:lock).with(any_args)
75-
expect(mutex).to_not receive(:try_lock).with(any_args)
76-
77-
5.times{ lazy.value }
78-
end
7951
end
8052
end
8153
end

0 commit comments

Comments
 (0)