2
2
3
3
module Concurrent
4
4
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`.
15
19
#
16
- # @param [Fixnum] count the initial count
20
+ # @param [Fixnum] count the initial count
17
21
#
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
19
23
def initialize ( count )
20
24
unless count . is_a? ( Fixnum ) && count >= 0
21
25
raise ArgumentError . new ( 'count must be in integer greater than or equal zero' )
@@ -25,11 +29,13 @@ def initialize(count)
25
29
@count = count
26
30
end
27
31
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.
29
35
#
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`
33
39
def wait ( timeout = nil )
34
40
@mutex . synchronize do
35
41
@@ -42,34 +48,41 @@ def wait(timeout = nil)
42
48
end
43
49
end
44
50
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.
47
55
def count_down
48
56
@mutex . synchronize do
49
57
@count -= 1 if @count > 0
50
58
@condition . broadcast if @count == 0
51
59
end
52
60
end
53
61
54
- # The current value of the counter.
62
+ # @!macro [attach] count_down_latch_method_count
63
+ #
64
+ # The current value of the counter.
55
65
#
56
- # @return [Fixnum] the current value of the counter
66
+ # @return [Fixnum] the current value of the counter
57
67
def count
58
68
@mutex . synchronize { @count }
59
69
end
60
70
end
61
71
62
72
if RUBY_PLATFORM == 'java'
63
73
74
+ # @!macro count_down_latch
64
75
class JavaCountDownLatch
65
76
77
+ # @!macro count_down_latch_method_initialize
66
78
def initialize ( count )
67
79
unless count . is_a? ( Fixnum ) && count >= 0
68
80
raise ArgumentError . new ( 'count must be in integer greater than or equal zero' )
69
81
end
70
82
@latch = java . util . concurrent . CountDownLatch . new ( count )
71
83
end
72
84
85
+ # @!macro count_down_latch_method_wait
73
86
def wait ( timeout = nil )
74
87
if timeout . nil?
75
88
@latch . await
@@ -79,13 +92,25 @@ def wait(timeout = nil)
79
92
end
80
93
end
81
94
95
+ # @!macro count_down_latch_method_count_down
82
96
def count_down
83
97
@latch . countDown
84
98
end
85
99
100
+ # @!macro count_down_latch_method_count
86
101
def count
87
102
@latch . getCount
88
103
end
89
104
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
90
115
end
91
116
end
0 commit comments