|
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' |
2 | 4 |
|
3 | 5 | module Concurrent
|
4 | 6 |
|
5 |
| - # @!macro [attach] count_down_latch |
| 7 | + ################################################################### |
| 8 | + |
| 9 | + # @!macro [new] count_down_latch_method_initialize |
6 | 10 | #
|
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`. |
14 | 12 | #
|
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 |
73 | 16 |
|
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` |
78 | 24 |
|
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. |
86 | 29 |
|
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 |
96 | 35 |
|
97 |
| - # @!macro count_down_latch_method_count_down |
98 |
| - def count_down |
99 |
| - @latch.countDown |
100 |
| - end |
| 36 | + ################################################################### |
101 | 37 |
|
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 |
107 | 51 |
|
108 |
| - # @!macro count_down_latch |
109 |
| - class CountDownLatch < JavaCountDownLatch |
110 |
| - end |
| 52 | + ################################################################### |
111 | 53 |
|
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 |
113 | 63 |
|
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 |
117 | 76 | end
|
118 | 77 | end
|
0 commit comments