Skip to content

Commit 0df85fb

Browse files
committed
Moved count_down_latch classes into own files.
1 parent 502d5d1 commit 0df85fb

File tree

5 files changed

+146
-106
lines changed

5 files changed

+146
-106
lines changed

lib/concurrent/atomic/atomic_fixnum.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,5 @@ module Concurrent
127127
#
128128
# @!macro atomic_fixnum_public_api
129129
class AtomicFixnum < AtomicFixnumImplementation
130-
131130
end
132131
end
Lines changed: 62 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -1,118 +1,77 @@
1-
require 'concurrent/synchronization'
1+
require 'concurrent/atomic/mutex_count_down_latch'
2+
require 'concurrent/atomic/java_count_down_latch'
3+
require 'concurrent/utility/engine'
24

35
module Concurrent
46

5-
# @!macro [attach] count_down_latch
7+
###################################################################
8+
9+
# @!macro [new] count_down_latch_method_initialize
610
#
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.
11+
# Create a new `CountDownLatch` with the initial `count`.
1412
#
15-
# @!visibility private
16-
# @!macro internal_implementation_note
17-
class PureCountDownLatch < Synchronization::Object
18-
19-
# @!macro [attach] count_down_latch_method_initialize
20-
#
21-
# Create a new `CountDownLatch` with the initial `count`.
22-
#
23-
# @param [Fixnum] count the initial count
24-
#
25-
# @raise [ArgumentError] if `count` is not an integer or is less than zero
26-
def initialize(count = 1)
27-
unless count.is_a?(Fixnum) && count >= 0
28-
raise ArgumentError.new('count must be in integer greater than or equal zero')
29-
end
30-
super()
31-
synchronize { ns_initialize count }
32-
end
33-
34-
# @!macro [attach] count_down_latch_method_wait
35-
#
36-
# Block on the latch until the counter reaches zero or until `timeout` is reached.
37-
#
38-
# @param [Fixnum] timeout the number of seconds to wait for the counter or `nil`
39-
# to block indefinitely
40-
# @return [Boolean] `true` if the `count` reaches zero else false on `timeout`
41-
def wait(timeout = nil)
42-
synchronize { ns_wait_until(timeout) { @count == 0 } }
43-
end
44-
45-
# @!macro [attach] count_down_latch_method_count_down
46-
#
47-
# Signal the latch to decrement the counter. Will signal all blocked threads when
48-
# the `count` reaches zero.
49-
def count_down
50-
synchronize do
51-
@count -= 1 if @count > 0
52-
ns_broadcast if @count == 0
53-
end
54-
end
55-
56-
# @!macro [attach] count_down_latch_method_count
57-
#
58-
# The current value of the counter.
59-
#
60-
# @return [Fixnum] the current value of the counter
61-
def count
62-
synchronize { @count }
63-
end
64-
65-
protected
66-
67-
def ns_initialize(count)
68-
@count = count
69-
end
70-
end
71-
72-
if Concurrent.on_jruby?
13+
# @param [new] count the initial count
14+
#
15+
# @raise [ArgumentError] if `count` is not an integer or is less than zero
7316

74-
# @!macro count_down_latch
75-
# @!visibility private
76-
# @!macro internal_implementation_note
77-
class JavaCountDownLatch
17+
# @!macro [new] count_down_latch_method_wait
18+
#
19+
# Block on the latch until the counter reaches zero or until `timeout` is reached.
20+
#
21+
# @param [Fixnum] timeout the number of seconds to wait for the counter or `nil`
22+
# to block indefinitely
23+
# @return [Boolean] `true` if the `count` reaches zero else false on `timeout`
7824

79-
# @!macro count_down_latch_method_initialize
80-
def initialize(count = 1)
81-
unless count.is_a?(Fixnum) && count >= 0
82-
raise ArgumentError.new('count must be in integer greater than or equal zero')
83-
end
84-
@latch = java.util.concurrent.CountDownLatch.new(count)
85-
end
25+
# @!macro [new] count_down_latch_method_count_down
26+
#
27+
# Signal the latch to decrement the counter. Will signal all blocked threads when
28+
# the `count` reaches zero.
8629

87-
# @!macro count_down_latch_method_wait
88-
def wait(timeout = nil)
89-
if timeout.nil?
90-
@latch.await
91-
true
92-
else
93-
@latch.await(1000 * timeout, java.util.concurrent.TimeUnit::MILLISECONDS)
94-
end
95-
end
30+
# @!macro [attach] count_down_latch_method_count
31+
#
32+
# The current value of the counter.
33+
#
34+
# @return [Fixnum] the current value of the counter
9635

97-
# @!macro count_down_latch_method_count_down
98-
def count_down
99-
@latch.countDown
100-
end
36+
###################################################################
10137

102-
# @!macro count_down_latch_method_count
103-
def count
104-
@latch.getCount
105-
end
106-
end
38+
# @!macro [new] count_down_latch_public_api
39+
#
40+
# @!method initialize(count = 1)
41+
# @!macro count_down_latch_method_initialize
42+
#
43+
# @!method wait(timeout = nil)
44+
# @!macro count_down_latch_method_wait
45+
#
46+
# @!method count_down
47+
# @!macro count_down_latch_method_count_down
48+
#
49+
# @!method count
50+
# @!macro count_down_latch_method_count
10751

108-
# @!macro count_down_latch
109-
class CountDownLatch < JavaCountDownLatch
110-
end
52+
###################################################################
11153

112-
else
54+
# @!visibility private
55+
# @!macro internal_implementation_note
56+
CountDownLatchImplementation = case
57+
when Concurrent.on_jruby?
58+
JavaCountDownLatch
59+
else
60+
MutexCountDownLatch
61+
end
62+
private_constant :CountDownLatchImplementation
11363

114-
# @!macro count_down_latch
115-
class CountDownLatch < PureCountDownLatch
116-
end
64+
# @!macro [attach] count_down_latch
65+
#
66+
# A synchronization object that allows one thread to wait on multiple other threads.
67+
# The thread that will wait creates a `CountDownLatch` and sets the initial value
68+
# (normally equal to the number of other threads). The initiating thread passes the
69+
# latch to the other threads then waits for the other threads by calling the `#wait`
70+
# method. Each of the other threads calls `#count_down` when done with its work.
71+
# When the latch counter reaches zero the waiting thread is unblocked and continues
72+
# with its work. A `CountDownLatch` can be used only once. Its value cannot be reset.
73+
#
74+
# @!macro count_down_latch_public_api
75+
class CountDownLatch < CountDownLatchImplementation
11776
end
11877
end
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
if Concurrent.on_jruby?
2+
3+
module Concurrent
4+
5+
# @!macro count_down_latch
6+
# @!visibility private
7+
# @!macro internal_implementation_note
8+
class JavaCountDownLatch
9+
10+
# @!macro count_down_latch_method_initialize
11+
def initialize(count = 1)
12+
unless count.is_a?(Fixnum) && count >= 0
13+
raise ArgumentError.new('count must be in integer greater than or equal zero')
14+
end
15+
@latch = java.util.concurrent.CountDownLatch.new(count)
16+
end
17+
18+
# @!macro count_down_latch_method_wait
19+
def wait(timeout = nil)
20+
if timeout.nil?
21+
@latch.await
22+
true
23+
else
24+
@latch.await(1000 * timeout, java.util.concurrent.TimeUnit::MILLISECONDS)
25+
end
26+
end
27+
28+
# @!macro count_down_latch_method_count_down
29+
def count_down
30+
@latch.countDown
31+
end
32+
33+
# @!macro count_down_latch_method_count
34+
def count
35+
@latch.getCount
36+
end
37+
end
38+
end
39+
end
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
require 'concurrent/synchronization'
2+
3+
module Concurrent
4+
5+
# @!macro count_down_latch
6+
# @!visibility private
7+
# @!macro internal_implementation_note
8+
class MutexCountDownLatch < Synchronization::Object
9+
10+
# @!macro count_down_latch_method_initialize
11+
def initialize(count = 1)
12+
unless count.is_a?(Fixnum) && count >= 0
13+
raise ArgumentError.new('count must be in integer greater than or equal zero')
14+
end
15+
super()
16+
synchronize { ns_initialize count }
17+
end
18+
19+
# @!macro count_down_latch_method_wait
20+
def wait(timeout = nil)
21+
synchronize { ns_wait_until(timeout) { @count == 0 } }
22+
end
23+
24+
# @!macro count_down_latch_method_count_down
25+
def count_down
26+
synchronize do
27+
@count -= 1 if @count > 0
28+
ns_broadcast if @count == 0
29+
end
30+
end
31+
32+
# @!macro count_down_latch_method_count
33+
def count
34+
synchronize { @count }
35+
end
36+
37+
protected
38+
39+
def ns_initialize(count)
40+
@count = count
41+
end
42+
end
43+
end

spec/concurrent/atomic/count_down_latch_spec.rb

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

8989
module Concurrent
9090

91-
describe PureCountDownLatch do
91+
describe MutexCountDownLatch do
9292

9393
it_should_behave_like :count_down_latch
9494

@@ -163,7 +163,7 @@ def subject.simulate_spurious_wake_up
163163
end
164164
else
165165
it 'inherits from MutexCountDownLatch' do
166-
expect(CountDownLatch.ancestors).to include(PureCountDownLatch)
166+
expect(CountDownLatch.ancestors).to include(MutexCountDownLatch)
167167
end
168168
end
169169
end

0 commit comments

Comments
 (0)