1
- require 'concurrent/atomic/condition '
1
+ require 'concurrent/synchronized_object '
2
2
3
3
module Concurrent
4
4
@@ -11,7 +11,7 @@ module Concurrent
11
11
# method. Each of the other threads calls `#count_down` when done with its work.
12
12
# When the latch counter reaches zero the waiting thread is unblocked and continues
13
13
# with its work. A `CountDownLatch` can be used only once. Its value cannot be reset.
14
- class MutexCountDownLatch
14
+ class PureCountDownLatch < SynchronizedObject
15
15
16
16
# @!macro [attach] count_down_latch_method_initialize
17
17
#
@@ -21,12 +21,11 @@ class MutexCountDownLatch
21
21
#
22
22
# @raise [ArgumentError] if `count` is not an integer or is less than zero
23
23
def initialize ( count = 1 )
24
+ super ( )
24
25
unless count . is_a? ( Fixnum ) && count >= 0
25
26
raise ArgumentError . new ( 'count must be in integer greater than or equal zero' )
26
27
end
27
- @mutex = Mutex . new
28
- @condition = Condition . new
29
- @count = count
28
+ synchronize { @count = count }
30
29
end
31
30
32
31
# @!macro [attach] count_down_latch_method_wait
@@ -37,25 +36,17 @@ def initialize(count = 1)
37
36
# to block indefinitely
38
37
# @return [Boolean] `true` if the `count` reaches zero else false on `timeout`
39
38
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 } }
49
40
end
50
41
51
42
# @!macro [attach] count_down_latch_method_count_down
52
43
#
53
44
# Signal the latch to decrement the counter. Will signal all blocked threads when
54
45
# the `count` reaches zero.
55
46
def count_down
56
- @mutex . synchronize do
47
+ synchronize do
57
48
@count -= 1 if @count > 0
58
- @condition . broadcast if @count == 0
49
+ ns_broadcast if @count == 0
59
50
end
60
51
end
61
52
@@ -65,7 +56,7 @@ def count_down
65
56
#
66
57
# @return [Fixnum] the current value of the counter
67
58
def count
68
- @mutex . synchronize { @count }
59
+ synchronize { @count }
69
60
end
70
61
end
71
62
@@ -110,7 +101,7 @@ class CountDownLatch < JavaCountDownLatch
110
101
else
111
102
112
103
# @!macro count_down_latch
113
- class CountDownLatch < MutexCountDownLatch
104
+ class CountDownLatch < PureCountDownLatch
114
105
end
115
106
end
116
107
end
0 commit comments