concurrent-ruby 0.9.2-java → 1.0.0-java
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +49 -1
- data/README.md +86 -120
- data/lib/concurrent.rb +14 -5
- data/lib/concurrent/agent.rb +587 -0
- data/lib/concurrent/array.rb +39 -0
- data/lib/concurrent/async.rb +296 -149
- data/lib/concurrent/atom.rb +135 -45
- data/lib/concurrent/atomic/abstract_thread_local_var.rb +38 -0
- data/lib/concurrent/atomic/atomic_boolean.rb +83 -118
- data/lib/concurrent/atomic/atomic_fixnum.rb +101 -163
- data/lib/concurrent/atomic/atomic_reference.rb +1 -8
- data/lib/concurrent/atomic/count_down_latch.rb +62 -103
- data/lib/concurrent/atomic/cyclic_barrier.rb +3 -1
- data/lib/concurrent/atomic/event.rb +1 -1
- data/lib/concurrent/atomic/java_count_down_latch.rb +39 -0
- data/lib/concurrent/atomic/java_thread_local_var.rb +50 -0
- data/lib/concurrent/atomic/mutex_atomic_boolean.rb +60 -0
- data/lib/concurrent/atomic/mutex_atomic_fixnum.rb +91 -0
- data/lib/concurrent/atomic/mutex_count_down_latch.rb +43 -0
- data/lib/concurrent/atomic/mutex_semaphore.rb +115 -0
- data/lib/concurrent/atomic/read_write_lock.rb +5 -4
- data/lib/concurrent/atomic/reentrant_read_write_lock.rb +3 -1
- data/lib/concurrent/atomic/ruby_thread_local_var.rb +172 -0
- data/lib/concurrent/atomic/semaphore.rb +84 -178
- data/lib/concurrent/atomic/thread_local_var.rb +65 -294
- data/lib/concurrent/atomic_reference/jruby+truffle.rb +1 -0
- data/lib/concurrent/atomic_reference/jruby.rb +1 -1
- data/lib/concurrent/atomic_reference/mutex_atomic.rb +14 -8
- data/lib/concurrent/atomic_reference/ruby.rb +1 -1
- data/lib/concurrent/atomics.rb +7 -37
- data/lib/concurrent/collection/copy_on_notify_observer_set.rb +7 -15
- data/lib/concurrent/collection/copy_on_write_observer_set.rb +7 -15
- data/lib/concurrent/collection/java_non_concurrent_priority_queue.rb +84 -0
- data/lib/concurrent/collection/map/atomic_reference_map_backend.rb +927 -0
- data/lib/concurrent/collection/map/mri_map_backend.rb +66 -0
- data/lib/concurrent/collection/map/non_concurrent_map_backend.rb +144 -0
- data/lib/concurrent/collection/map/synchronized_map_backend.rb +86 -0
- data/lib/concurrent/collection/non_concurrent_priority_queue.rb +143 -0
- data/lib/concurrent/collection/ruby_non_concurrent_priority_queue.rb +150 -0
- data/lib/concurrent/concern/dereferenceable.rb +9 -24
- data/lib/concurrent/concern/logging.rb +1 -1
- data/lib/concurrent/concern/obligation.rb +11 -20
- data/lib/concurrent/concern/observable.rb +38 -13
- data/lib/concurrent/configuration.rb +23 -152
- data/lib/concurrent/constants.rb +8 -0
- data/lib/concurrent/delay.rb +14 -12
- data/lib/concurrent/exchanger.rb +339 -41
- data/lib/concurrent/executor/abstract_executor_service.rb +134 -0
- data/lib/concurrent/executor/executor_service.rb +23 -359
- data/lib/concurrent/executor/immediate_executor.rb +3 -2
- data/lib/concurrent/executor/java_executor_service.rb +100 -0
- data/lib/concurrent/executor/java_single_thread_executor.rb +3 -3
- data/lib/concurrent/executor/java_thread_pool_executor.rb +3 -4
- data/lib/concurrent/executor/ruby_executor_service.rb +78 -0
- data/lib/concurrent/executor/ruby_single_thread_executor.rb +10 -66
- data/lib/concurrent/executor/ruby_thread_pool_executor.rb +25 -22
- data/lib/concurrent/executor/safe_task_executor.rb +6 -7
- data/lib/concurrent/executor/serial_executor_service.rb +34 -0
- data/lib/concurrent/executor/serialized_execution.rb +10 -33
- data/lib/concurrent/executor/serialized_execution_delegator.rb +28 -0
- data/lib/concurrent/executor/simple_executor_service.rb +1 -10
- data/lib/concurrent/executor/single_thread_executor.rb +20 -10
- data/lib/concurrent/executor/timer_set.rb +8 -10
- data/lib/concurrent/executors.rb +12 -2
- data/lib/concurrent/future.rb +6 -4
- data/lib/concurrent/hash.rb +35 -0
- data/lib/concurrent/immutable_struct.rb +5 -1
- data/lib/concurrent/ivar.rb +12 -16
- data/lib/concurrent/lazy_register.rb +11 -8
- data/lib/concurrent/map.rb +180 -0
- data/lib/concurrent/maybe.rb +6 -3
- data/lib/concurrent/mutable_struct.rb +7 -6
- data/lib/concurrent/mvar.rb +26 -2
- data/lib/concurrent/{executor/executor.rb → options.rb} +5 -29
- data/lib/concurrent/promise.rb +7 -5
- data/lib/concurrent/scheduled_task.rb +13 -71
- data/lib/concurrent/settable_struct.rb +5 -4
- data/lib/concurrent/synchronization.rb +15 -3
- data/lib/concurrent/synchronization/abstract_lockable_object.rb +98 -0
- data/lib/concurrent/synchronization/abstract_object.rb +7 -146
- data/lib/concurrent/synchronization/abstract_struct.rb +2 -3
- data/lib/concurrent/synchronization/condition.rb +6 -4
- data/lib/concurrent/synchronization/jruby_lockable_object.rb +13 -0
- data/lib/concurrent/synchronization/jruby_object.rb +44 -0
- data/lib/concurrent/synchronization/lock.rb +3 -2
- data/lib/concurrent/synchronization/lockable_object.rb +72 -0
- data/lib/concurrent/synchronization/mri_lockable_object.rb +71 -0
- data/lib/concurrent/synchronization/mri_object.rb +43 -0
- data/lib/concurrent/synchronization/object.rb +140 -73
- data/lib/concurrent/synchronization/rbx_lockable_object.rb +65 -0
- data/lib/concurrent/synchronization/rbx_object.rb +30 -73
- data/lib/concurrent/synchronization/volatile.rb +34 -0
- data/lib/concurrent/thread_safe/synchronized_delegator.rb +50 -0
- data/lib/concurrent/thread_safe/util.rb +14 -0
- data/lib/concurrent/thread_safe/util/adder.rb +74 -0
- data/lib/concurrent/thread_safe/util/array_hash_rbx.rb +30 -0
- data/lib/concurrent/thread_safe/util/cheap_lockable.rb +118 -0
- data/lib/concurrent/thread_safe/util/power_of_two_tuple.rb +38 -0
- data/lib/concurrent/thread_safe/util/striped64.rb +241 -0
- data/lib/concurrent/thread_safe/util/volatile.rb +75 -0
- data/lib/concurrent/thread_safe/util/xor_shift_random.rb +50 -0
- data/lib/concurrent/timer_task.rb +3 -4
- data/lib/concurrent/tuple.rb +86 -0
- data/lib/concurrent/tvar.rb +5 -1
- data/lib/concurrent/utility/at_exit.rb +1 -1
- data/lib/concurrent/utility/engine.rb +4 -0
- data/lib/concurrent/utility/monotonic_time.rb +3 -4
- data/lib/concurrent/utility/native_extension_loader.rb +50 -30
- data/lib/concurrent/version.rb +2 -2
- data/lib/concurrent_ruby_ext.jar +0 -0
- metadata +47 -12
- data/lib/concurrent/atomic/condition.rb +0 -78
- data/lib/concurrent/collection/priority_queue.rb +0 -360
- data/lib/concurrent/synchronization/java_object.rb +0 -34
- data/lib/concurrent/synchronization/monitor_object.rb +0 -27
- data/lib/concurrent/synchronization/mutex_object.rb +0 -43
- data/lib/concurrent/utilities.rb +0 -5
- data/lib/concurrent/utility/timeout.rb +0 -39
- data/lib/concurrent/utility/timer.rb +0 -26
- data/lib/concurrent_ruby.rb +0 -2
@@ -1,165 +1,102 @@
|
|
1
|
-
require 'concurrent/
|
1
|
+
require 'concurrent/atomic/mutex_atomic_fixnum'
|
2
2
|
require 'concurrent/synchronization'
|
3
3
|
|
4
4
|
module Concurrent
|
5
5
|
|
6
|
-
|
7
|
-
|
8
|
-
#
|
9
|
-
# fixnum and thread-safe and guaranteed to succeed. Reads and writes may block
|
10
|
-
# briefly but no explicit locking is required.
|
6
|
+
###################################################################
|
7
|
+
|
8
|
+
# @!macro [new] atomic_fixnum_method_initialize
|
11
9
|
#
|
12
|
-
#
|
13
|
-
# Testing with Concurrent::MutexAtomicFixnum...
|
14
|
-
# 3.130000 0.000000 3.130000 ( 3.136505)
|
15
|
-
# Testing with Concurrent::CAtomicFixnum...
|
16
|
-
# 0.790000 0.000000 0.790000 ( 0.785550)
|
10
|
+
# Creates a new `AtomicFixnum` with the given initial value.
|
17
11
|
#
|
18
|
-
#
|
19
|
-
#
|
20
|
-
|
21
|
-
#
|
22
|
-
# 4.520000 0.030000 4.550000 ( 1.187000)
|
12
|
+
# @param [Fixnum] initial the initial value
|
13
|
+
# @raise [ArgumentError] if the initial value is not a `Fixnum`
|
14
|
+
|
15
|
+
# @!macro [new] atomic_fixnum_method_value_get
|
23
16
|
#
|
24
|
-
#
|
17
|
+
# Retrieves the current `Fixnum` value.
|
25
18
|
#
|
26
|
-
#
|
27
|
-
# @!macro internal_implementation_note
|
28
|
-
class MutexAtomicFixnum < Synchronization::Object
|
19
|
+
# @return [Fixnum] the current value
|
29
20
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
# @raise [ArgumentError] if the initial value is not a `Fixnum`
|
40
|
-
def initialize(initial = 0)
|
41
|
-
super()
|
42
|
-
synchronize { ns_initialize(initial) }
|
43
|
-
end
|
44
|
-
|
45
|
-
# @!macro [attach] atomic_fixnum_method_value_get
|
46
|
-
#
|
47
|
-
# Retrieves the current `Fixnum` value.
|
48
|
-
#
|
49
|
-
# @return [Fixnum] the current value
|
50
|
-
def value
|
51
|
-
synchronize { @value }
|
52
|
-
end
|
53
|
-
|
54
|
-
# @!macro [attach] atomic_fixnum_method_value_set
|
55
|
-
#
|
56
|
-
# Explicitly sets the value.
|
57
|
-
#
|
58
|
-
# @param [Fixnum] value the new value to be set
|
59
|
-
#
|
60
|
-
# @return [Fixnum] the current value
|
61
|
-
#
|
62
|
-
# @raise [ArgumentError] if the new value is not a `Fixnum`
|
63
|
-
def value=(value)
|
64
|
-
synchronize { ns_set(value) }
|
65
|
-
end
|
66
|
-
|
67
|
-
# @!macro [attach] atomic_fixnum_method_increment
|
68
|
-
#
|
69
|
-
# Increases the current value by the given amount (defaults to 1).
|
70
|
-
#
|
71
|
-
# @param [Fixnum] delta the amount by which to increase the current value
|
72
|
-
#
|
73
|
-
# @return [Fixnum] the current value after incrementation
|
74
|
-
def increment(delta = 1)
|
75
|
-
synchronize { ns_set(@value + delta.to_i) }
|
76
|
-
end
|
77
|
-
|
78
|
-
alias_method :up, :increment
|
79
|
-
|
80
|
-
# @!macro [attach] atomic_fixnum_method_decrement
|
81
|
-
#
|
82
|
-
# Decreases the current value by the given amount (defaults to 1).
|
83
|
-
#
|
84
|
-
# @param [Fixnum] delta the amount by which to decrease the current value
|
85
|
-
#
|
86
|
-
# @return [Fixnum] the current value after decrementation
|
87
|
-
def decrement(delta = 1)
|
88
|
-
synchronize { ns_set(@value - delta.to_i) }
|
89
|
-
end
|
90
|
-
|
91
|
-
alias_method :down, :decrement
|
21
|
+
# @!macro [new] atomic_fixnum_method_value_set
|
22
|
+
#
|
23
|
+
# Explicitly sets the value.
|
24
|
+
#
|
25
|
+
# @param [Fixnum] value the new value to be set
|
26
|
+
#
|
27
|
+
# @return [Fixnum] the current value
|
28
|
+
#
|
29
|
+
# @raise [ArgumentError] if the new value is not a `Fixnum`
|
92
30
|
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
#
|
101
|
-
# @return [Fixnum] true if the value was updated else false
|
102
|
-
def compare_and_set(expect, update)
|
103
|
-
synchronize do
|
104
|
-
if @value == expect.to_i
|
105
|
-
@value = update.to_i
|
106
|
-
true
|
107
|
-
else
|
108
|
-
false
|
109
|
-
end
|
110
|
-
end
|
111
|
-
end
|
31
|
+
# @!macro [new] atomic_fixnum_method_increment
|
32
|
+
#
|
33
|
+
# Increases the current value by the given amount (defaults to 1).
|
34
|
+
#
|
35
|
+
# @param [Fixnum] delta the amount by which to increase the current value
|
36
|
+
#
|
37
|
+
# @return [Fixnum] the current value after incrementation
|
112
38
|
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
# given (old) value
|
121
|
-
# @yieldparam [Object] old_value the starting value of the atomic reference
|
122
|
-
#
|
123
|
-
# @return [Object] the new value
|
124
|
-
def update
|
125
|
-
synchronize do
|
126
|
-
@value = yield @value
|
127
|
-
end
|
128
|
-
end
|
39
|
+
# @!macro [new] atomic_fixnum_method_decrement
|
40
|
+
#
|
41
|
+
# Decreases the current value by the given amount (defaults to 1).
|
42
|
+
#
|
43
|
+
# @param [Fixnum] delta the amount by which to decrease the current value
|
44
|
+
#
|
45
|
+
# @return [Fixnum] the current value after decrementation
|
129
46
|
|
130
|
-
|
47
|
+
# @!macro [new] atomic_fixnum_method_compare_and_set
|
48
|
+
#
|
49
|
+
# Atomically sets the value to the given updated value if the current
|
50
|
+
# value == the expected value.
|
51
|
+
#
|
52
|
+
# @param [Fixnum] expect the expected value
|
53
|
+
# @param [Fixnum] update the new value
|
54
|
+
#
|
55
|
+
# @return [Fixnum] true if the value was updated else false
|
131
56
|
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
57
|
+
# @!macro [new] atomic_fixnum_method_update
|
58
|
+
#
|
59
|
+
# Pass the current value to the given block, replacing it
|
60
|
+
# with the block's result. May retry if the value changes
|
61
|
+
# during the block's execution.
|
62
|
+
#
|
63
|
+
# @yield [Object] Calculate a new value for the atomic reference using
|
64
|
+
# given (old) value
|
65
|
+
# @yieldparam [Object] old_value the starting value of the atomic reference
|
66
|
+
#
|
67
|
+
# @return [Object] the new value
|
136
68
|
|
137
|
-
|
69
|
+
###################################################################
|
138
70
|
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
71
|
+
# @!macro [new] atomic_fixnum_public_api
|
72
|
+
#
|
73
|
+
# @!method initialize(initial = 0)
|
74
|
+
# @!macro atomic_fixnum_method_initialize
|
75
|
+
#
|
76
|
+
# @!method value
|
77
|
+
# @!macro atomic_fixnum_method_value_get
|
78
|
+
#
|
79
|
+
# @!method value=(value)
|
80
|
+
# @!macro atomic_fixnum_method_value_set
|
81
|
+
#
|
82
|
+
# @!method increment
|
83
|
+
# @!macro atomic_fixnum_method_increment
|
84
|
+
#
|
85
|
+
# @!method decrement
|
86
|
+
# @!macro atomic_fixnum_method_decrement
|
87
|
+
#
|
88
|
+
# @!method compare_and_set(expect, update)
|
89
|
+
# @!macro atomic_fixnum_method_compare_and_set
|
90
|
+
#
|
91
|
+
# @!method update
|
92
|
+
# @!macro atomic_fixnum_method_update
|
144
93
|
|
145
|
-
|
146
|
-
def range_check!(value)
|
147
|
-
if !value.is_a?(Fixnum)
|
148
|
-
raise ArgumentError.new('value value must be a Fixnum')
|
149
|
-
elsif value > MAX_VALUE
|
150
|
-
raise RangeError.new("#{value} is greater than the maximum value of #{MAX_VALUE}")
|
151
|
-
elsif value < MIN_VALUE
|
152
|
-
raise RangeError.new("#{value} is less than the maximum value of #{MIN_VALUE}")
|
153
|
-
else
|
154
|
-
value
|
155
|
-
end
|
156
|
-
end
|
157
|
-
end
|
94
|
+
###################################################################
|
158
95
|
|
159
96
|
# @!visibility private
|
160
97
|
# @!macro internal_implementation_note
|
161
98
|
AtomicFixnumImplementation = case
|
162
|
-
when
|
99
|
+
when defined?(JavaAtomicFixnum)
|
163
100
|
JavaAtomicFixnum
|
164
101
|
when defined?(CAtomicFixnum)
|
165
102
|
CAtomicFixnum
|
@@ -168,28 +105,29 @@ module Concurrent
|
|
168
105
|
end
|
169
106
|
private_constant :AtomicFixnumImplementation
|
170
107
|
|
171
|
-
# @!macro atomic_fixnum
|
108
|
+
# @!macro [attach] atomic_fixnum
|
109
|
+
#
|
110
|
+
# A numeric value that can be updated atomically. Reads and writes to an atomic
|
111
|
+
# fixnum and thread-safe and guaranteed to succeed. Reads and writes may block
|
112
|
+
# briefly but no explicit locking is required.
|
113
|
+
#
|
114
|
+
# @!macro thread_safe_variable_comparison
|
172
115
|
#
|
173
|
-
#
|
116
|
+
# Testing with ruby 2.1.2
|
117
|
+
# Testing with Concurrent::MutexAtomicFixnum...
|
118
|
+
# 3.130000 0.000000 3.130000 ( 3.136505)
|
119
|
+
# Testing with Concurrent::CAtomicFixnum...
|
120
|
+
# 0.790000 0.000000 0.790000 ( 0.785550)
|
121
|
+
#
|
122
|
+
# Testing with jruby 1.9.3
|
123
|
+
# Testing with Concurrent::MutexAtomicFixnum...
|
124
|
+
# 5.460000 2.460000 7.920000 ( 3.715000)
|
125
|
+
# Testing with Concurrent::JavaAtomicFixnum...
|
126
|
+
# 4.520000 0.030000 4.550000 ( 1.187000)
|
127
|
+
#
|
128
|
+
# @see http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/atomic/AtomicLong.html java.util.concurrent.atomic.AtomicLong
|
129
|
+
#
|
130
|
+
# @!macro atomic_fixnum_public_api
|
174
131
|
class AtomicFixnum < AtomicFixnumImplementation
|
175
|
-
|
176
|
-
# @!method initialize(initial = 0)
|
177
|
-
# @!macro atomic_fixnum_method_initialize
|
178
|
-
|
179
|
-
# @!method value
|
180
|
-
# @!macro atomic_fixnum_method_value_get
|
181
|
-
|
182
|
-
# @!method value=(value)
|
183
|
-
# @!macro atomic_fixnum_method_value_set
|
184
|
-
|
185
|
-
# @!method increment
|
186
|
-
# @!macro atomic_fixnum_method_increment
|
187
|
-
|
188
|
-
# @!method decrement
|
189
|
-
# @!macro atomic_fixnum_method_decrement
|
190
|
-
|
191
|
-
# @!method compare_and_set(expect, update)
|
192
|
-
# @!macro atomic_fixnum_method_compare_and_set
|
193
|
-
|
194
132
|
end
|
195
133
|
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
require 'concurrent/
|
1
|
+
require 'concurrent/synchronization'
|
2
2
|
require 'concurrent/utility/engine'
|
3
3
|
require 'concurrent/atomic_reference/concurrent_update_error'
|
4
4
|
require 'concurrent/atomic_reference/mutex_atomic'
|
@@ -40,10 +40,3 @@ else
|
|
40
40
|
class Concurrent::AtomicReference < Concurrent::MutexAtomicReference
|
41
41
|
end
|
42
42
|
end
|
43
|
-
|
44
|
-
module Concurrent
|
45
|
-
|
46
|
-
# @see Concurrent::AtomicReference
|
47
|
-
# @deprecated Use Concurrent::AtomicReference instead.
|
48
|
-
Atomic = AtomicReference
|
49
|
-
end
|
@@ -1,118 +1,77 @@
|
|
1
|
-
require 'concurrent/
|
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
|
-
|
7
|
+
###################################################################
|
8
|
+
|
9
|
+
# @!macro [new] count_down_latch_method_initialize
|
6
10
|
#
|
7
|
-
#
|
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
|
-
#
|
16
|
-
#
|
17
|
-
|
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
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
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
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
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
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
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
|
-
|
98
|
-
def count_down
|
99
|
-
@latch.countDown
|
100
|
-
end
|
36
|
+
###################################################################
|
101
37
|
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
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
|
-
|
109
|
-
class CountDownLatch < JavaCountDownLatch
|
110
|
-
end
|
52
|
+
###################################################################
|
111
53
|
|
112
|
-
|
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
|
-
|
115
|
-
|
116
|
-
|
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
|