Skip to content

Commit c1a08c0

Browse files
committed
CountDownLatch now inherits from MutexCountDownLatch or JavaCountDownLatch.
1 parent 98e685f commit c1a08c0

File tree

2 files changed

+58
-21
lines changed

2 files changed

+58
-21
lines changed

lib/concurrent/atomic/count_down_latch.rb

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,24 @@
22

33
module Concurrent
44

5-
# A synchronization object that allows one thread to wait on multiple other threads.
6-
# The thread that will wait creates a `CountDownLatch` and sets the initial value
7-
# (normally equal to the number of other threads). The initiating thread passes the
8-
# latch to the other threads then waits for the other threads by calling the `#wait`
9-
# method. Each of the other threads calls `#count_down` when done with its work.
10-
# When the latch counter reaches zero the waiting thread is unblocked and continues
11-
# with its work. A `CountDownLatch` can be used only once. Its value cannot be reset.
12-
class CountDownLatch
13-
14-
# Create a new `CountDownLatch` with the initial `count`.
5+
# @!macro [attach] count_down_latch
6+
#
7+
# A synchronization object that allows one thread to wait on multiple other threads.
8+
# The thread that will wait creates a `CountDownLatch` and sets the initial value
9+
# (normally equal to the number of other threads). The initiating thread passes the
10+
# latch to the other threads then waits for the other threads by calling the `#wait`
11+
# method. Each of the other threads calls `#count_down` when done with its work.
12+
# When the latch counter reaches zero the waiting thread is unblocked and continues
13+
# with its work. A `CountDownLatch` can be used only once. Its value cannot be reset.
14+
class MutexCountDownLatch
15+
16+
# @!macro [attach] count_down_latch_method_initialize
17+
#
18+
# Create a new `CountDownLatch` with the initial `count`.
1519
#
16-
# @param [Fixnum] count the initial count
20+
# @param [Fixnum] count the initial count
1721
#
18-
# @raise [ArgumentError] if `count` is not an integer or is less than zero
22+
# @raise [ArgumentError] if `count` is not an integer or is less than zero
1923
def initialize(count)
2024
unless count.is_a?(Fixnum) && count >= 0
2125
raise ArgumentError.new('count must be in integer greater than or equal zero')
@@ -25,11 +29,13 @@ def initialize(count)
2529
@count = count
2630
end
2731

28-
# Block on the latch until the counter reaches zero or until `timeout` is reached.
32+
# @!macro [attach] count_down_latch_method_wait
33+
#
34+
# Block on the latch until the counter reaches zero or until `timeout` is reached.
2935
#
30-
# @param [Fixnum] timeout the number of seconds to wait for the counter or `nil`
31-
# to block indefinitely
32-
# @return [Boolean] `true` if the `count` reaches zero else false on `timeout`
36+
# @param [Fixnum] timeout the number of seconds to wait for the counter or `nil`
37+
# to block indefinitely
38+
# @return [Boolean] `true` if the `count` reaches zero else false on `timeout`
3339
def wait(timeout = nil)
3440
@mutex.synchronize do
3541

@@ -42,34 +48,41 @@ def wait(timeout = nil)
4248
end
4349
end
4450

45-
# Signal the latch to decrement the counter. Will signal all blocked threads when
46-
# the `count` reaches zero.
51+
# @!macro [attach] count_down_latch_method_count_down
52+
#
53+
# Signal the latch to decrement the counter. Will signal all blocked threads when
54+
# the `count` reaches zero.
4755
def count_down
4856
@mutex.synchronize do
4957
@count -= 1 if @count > 0
5058
@condition.broadcast if @count == 0
5159
end
5260
end
5361

54-
# The current value of the counter.
62+
# @!macro [attach] count_down_latch_method_count
63+
#
64+
# The current value of the counter.
5565
#
56-
# @return [Fixnum] the current value of the counter
66+
# @return [Fixnum] the current value of the counter
5767
def count
5868
@mutex.synchronize { @count }
5969
end
6070
end
6171

6272
if RUBY_PLATFORM == 'java'
6373

74+
# @!macro count_down_latch
6475
class JavaCountDownLatch
6576

77+
# @!macro count_down_latch_method_initialize
6678
def initialize(count)
6779
unless count.is_a?(Fixnum) && count >= 0
6880
raise ArgumentError.new('count must be in integer greater than or equal zero')
6981
end
7082
@latch = java.util.concurrent.CountDownLatch.new(count)
7183
end
7284

85+
# @!macro count_down_latch_method_wait
7386
def wait(timeout = nil)
7487
if timeout.nil?
7588
@latch.await
@@ -79,13 +92,25 @@ def wait(timeout = nil)
7992
end
8093
end
8194

95+
# @!macro count_down_latch_method_count_down
8296
def count_down
8397
@latch.countDown
8498
end
8599

100+
# @!macro count_down_latch_method_count
86101
def count
87102
@latch.getCount
88103
end
89104
end
105+
106+
# @!macro count_down_latch
107+
class CountDownLatch < JavaCountDownLatch
108+
end
109+
110+
else
111+
112+
# @!macro count_down_latch
113+
class CountDownLatch < MutexCountDownLatch
114+
end
90115
end
91116
end

spec/concurrent/atomic/count_down_latch_spec.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@
8585

8686
module Concurrent
8787

88-
describe CountDownLatch do
88+
describe MutexCountDownLatch do
8989

9090
it_should_behave_like :count_down_latch
9191

@@ -136,4 +136,16 @@ def subject.simulate_spurious_wake_up
136136
it_should_behave_like :count_down_latch
137137
end
138138
end
139+
140+
describe CountDownLatch do
141+
if jruby?
142+
it 'inherits from JavaCountDownLatch' do
143+
CountDownLatch.ancestors.should include(JavaCountDownLatch)
144+
end
145+
else
146+
it 'inherits from MutexCountDownLatch' do
147+
CountDownLatch.ancestors.should include(MutexCountDownLatch)
148+
end
149+
end
150+
end
139151
end

0 commit comments

Comments
 (0)