Skip to content

Commit 3a8aedc

Browse files
committed
Migrate CountDownLatch to SynchronizedObject
1 parent 52fdb3e commit 3a8aedc

File tree

2 files changed

+14
-23
lines changed

2 files changed

+14
-23
lines changed

lib/concurrent/atomic/count_down_latch.rb

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
require 'concurrent/atomic/condition'
1+
require 'concurrent/synchronized_object'
22

33
module Concurrent
44

@@ -11,7 +11,7 @@ module Concurrent
1111
# method. Each of the other threads calls `#count_down` when done with its work.
1212
# When the latch counter reaches zero the waiting thread is unblocked and continues
1313
# with its work. A `CountDownLatch` can be used only once. Its value cannot be reset.
14-
class MutexCountDownLatch
14+
class PureCountDownLatch < SynchronizedObject
1515

1616
# @!macro [attach] count_down_latch_method_initialize
1717
#
@@ -21,12 +21,11 @@ class MutexCountDownLatch
2121
#
2222
# @raise [ArgumentError] if `count` is not an integer or is less than zero
2323
def initialize(count = 1)
24+
super()
2425
unless count.is_a?(Fixnum) && count >= 0
2526
raise ArgumentError.new('count must be in integer greater than or equal zero')
2627
end
27-
@mutex = Mutex.new
28-
@condition = Condition.new
29-
@count = count
28+
synchronize { @count = count }
3029
end
3130

3231
# @!macro [attach] count_down_latch_method_wait
@@ -37,25 +36,17 @@ def initialize(count = 1)
3736
# to block indefinitely
3837
# @return [Boolean] `true` if the `count` reaches zero else false on `timeout`
3938
def wait(timeout = nil)
40-
@mutex.synchronize do
41-
42-
remaining = Condition::Result.new(timeout)
43-
while @count > 0 && remaining.can_wait?
44-
remaining = @condition.wait(@mutex, remaining.remaining_time)
45-
end
46-
47-
@count == 0
48-
end
39+
synchronize { ns_wait_until(timeout) { @count == 0 } }
4940
end
5041

5142
# @!macro [attach] count_down_latch_method_count_down
5243
#
5344
# Signal the latch to decrement the counter. Will signal all blocked threads when
5445
# the `count` reaches zero.
5546
def count_down
56-
@mutex.synchronize do
47+
synchronize do
5748
@count -= 1 if @count > 0
58-
@condition.broadcast if @count == 0
49+
ns_broadcast if @count == 0
5950
end
6051
end
6152

@@ -65,7 +56,7 @@ def count_down
6556
#
6657
# @return [Fixnum] the current value of the counter
6758
def count
68-
@mutex.synchronize { @count }
59+
synchronize { @count }
6960
end
7061
end
7162

@@ -110,7 +101,7 @@ class CountDownLatch < JavaCountDownLatch
110101
else
111102

112103
# @!macro count_down_latch
113-
class CountDownLatch < MutexCountDownLatch
104+
class CountDownLatch < PureCountDownLatch
114105
end
115106
end
116107
end

spec/concurrent/atomic/count_down_latch_spec.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@
8888

8989
module Concurrent
9090

91-
describe MutexCountDownLatch do
91+
describe PureCountDownLatch do
9292

9393
it_should_behave_like :count_down_latch
9494

@@ -98,9 +98,9 @@ module Concurrent
9898

9999
before(:each) do
100100
def subject.simulate_spurious_wake_up
101-
@mutex.synchronize do
102-
@condition.signal
103-
@condition.broadcast
101+
synchronize do
102+
signal
103+
broadcast
104104
end
105105
end
106106
end
@@ -147,7 +147,7 @@ def subject.simulate_spurious_wake_up
147147
end
148148
else
149149
it 'inherits from MutexCountDownLatch' do
150-
expect(CountDownLatch.ancestors).to include(MutexCountDownLatch)
150+
expect(CountDownLatch.ancestors).to include(PureCountDownLatch)
151151
end
152152
end
153153
end

0 commit comments

Comments
 (0)