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.
Files changed (121) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +49 -1
  3. data/README.md +86 -120
  4. data/lib/concurrent.rb +14 -5
  5. data/lib/concurrent/agent.rb +587 -0
  6. data/lib/concurrent/array.rb +39 -0
  7. data/lib/concurrent/async.rb +296 -149
  8. data/lib/concurrent/atom.rb +135 -45
  9. data/lib/concurrent/atomic/abstract_thread_local_var.rb +38 -0
  10. data/lib/concurrent/atomic/atomic_boolean.rb +83 -118
  11. data/lib/concurrent/atomic/atomic_fixnum.rb +101 -163
  12. data/lib/concurrent/atomic/atomic_reference.rb +1 -8
  13. data/lib/concurrent/atomic/count_down_latch.rb +62 -103
  14. data/lib/concurrent/atomic/cyclic_barrier.rb +3 -1
  15. data/lib/concurrent/atomic/event.rb +1 -1
  16. data/lib/concurrent/atomic/java_count_down_latch.rb +39 -0
  17. data/lib/concurrent/atomic/java_thread_local_var.rb +50 -0
  18. data/lib/concurrent/atomic/mutex_atomic_boolean.rb +60 -0
  19. data/lib/concurrent/atomic/mutex_atomic_fixnum.rb +91 -0
  20. data/lib/concurrent/atomic/mutex_count_down_latch.rb +43 -0
  21. data/lib/concurrent/atomic/mutex_semaphore.rb +115 -0
  22. data/lib/concurrent/atomic/read_write_lock.rb +5 -4
  23. data/lib/concurrent/atomic/reentrant_read_write_lock.rb +3 -1
  24. data/lib/concurrent/atomic/ruby_thread_local_var.rb +172 -0
  25. data/lib/concurrent/atomic/semaphore.rb +84 -178
  26. data/lib/concurrent/atomic/thread_local_var.rb +65 -294
  27. data/lib/concurrent/atomic_reference/jruby+truffle.rb +1 -0
  28. data/lib/concurrent/atomic_reference/jruby.rb +1 -1
  29. data/lib/concurrent/atomic_reference/mutex_atomic.rb +14 -8
  30. data/lib/concurrent/atomic_reference/ruby.rb +1 -1
  31. data/lib/concurrent/atomics.rb +7 -37
  32. data/lib/concurrent/collection/copy_on_notify_observer_set.rb +7 -15
  33. data/lib/concurrent/collection/copy_on_write_observer_set.rb +7 -15
  34. data/lib/concurrent/collection/java_non_concurrent_priority_queue.rb +84 -0
  35. data/lib/concurrent/collection/map/atomic_reference_map_backend.rb +927 -0
  36. data/lib/concurrent/collection/map/mri_map_backend.rb +66 -0
  37. data/lib/concurrent/collection/map/non_concurrent_map_backend.rb +144 -0
  38. data/lib/concurrent/collection/map/synchronized_map_backend.rb +86 -0
  39. data/lib/concurrent/collection/non_concurrent_priority_queue.rb +143 -0
  40. data/lib/concurrent/collection/ruby_non_concurrent_priority_queue.rb +150 -0
  41. data/lib/concurrent/concern/dereferenceable.rb +9 -24
  42. data/lib/concurrent/concern/logging.rb +1 -1
  43. data/lib/concurrent/concern/obligation.rb +11 -20
  44. data/lib/concurrent/concern/observable.rb +38 -13
  45. data/lib/concurrent/configuration.rb +23 -152
  46. data/lib/concurrent/constants.rb +8 -0
  47. data/lib/concurrent/delay.rb +14 -12
  48. data/lib/concurrent/exchanger.rb +339 -41
  49. data/lib/concurrent/executor/abstract_executor_service.rb +134 -0
  50. data/lib/concurrent/executor/executor_service.rb +23 -359
  51. data/lib/concurrent/executor/immediate_executor.rb +3 -2
  52. data/lib/concurrent/executor/java_executor_service.rb +100 -0
  53. data/lib/concurrent/executor/java_single_thread_executor.rb +3 -3
  54. data/lib/concurrent/executor/java_thread_pool_executor.rb +3 -4
  55. data/lib/concurrent/executor/ruby_executor_service.rb +78 -0
  56. data/lib/concurrent/executor/ruby_single_thread_executor.rb +10 -66
  57. data/lib/concurrent/executor/ruby_thread_pool_executor.rb +25 -22
  58. data/lib/concurrent/executor/safe_task_executor.rb +6 -7
  59. data/lib/concurrent/executor/serial_executor_service.rb +34 -0
  60. data/lib/concurrent/executor/serialized_execution.rb +10 -33
  61. data/lib/concurrent/executor/serialized_execution_delegator.rb +28 -0
  62. data/lib/concurrent/executor/simple_executor_service.rb +1 -10
  63. data/lib/concurrent/executor/single_thread_executor.rb +20 -10
  64. data/lib/concurrent/executor/timer_set.rb +8 -10
  65. data/lib/concurrent/executors.rb +12 -2
  66. data/lib/concurrent/future.rb +6 -4
  67. data/lib/concurrent/hash.rb +35 -0
  68. data/lib/concurrent/immutable_struct.rb +5 -1
  69. data/lib/concurrent/ivar.rb +12 -16
  70. data/lib/concurrent/lazy_register.rb +11 -8
  71. data/lib/concurrent/map.rb +180 -0
  72. data/lib/concurrent/maybe.rb +6 -3
  73. data/lib/concurrent/mutable_struct.rb +7 -6
  74. data/lib/concurrent/mvar.rb +26 -2
  75. data/lib/concurrent/{executor/executor.rb → options.rb} +5 -29
  76. data/lib/concurrent/promise.rb +7 -5
  77. data/lib/concurrent/scheduled_task.rb +13 -71
  78. data/lib/concurrent/settable_struct.rb +5 -4
  79. data/lib/concurrent/synchronization.rb +15 -3
  80. data/lib/concurrent/synchronization/abstract_lockable_object.rb +98 -0
  81. data/lib/concurrent/synchronization/abstract_object.rb +7 -146
  82. data/lib/concurrent/synchronization/abstract_struct.rb +2 -3
  83. data/lib/concurrent/synchronization/condition.rb +6 -4
  84. data/lib/concurrent/synchronization/jruby_lockable_object.rb +13 -0
  85. data/lib/concurrent/synchronization/jruby_object.rb +44 -0
  86. data/lib/concurrent/synchronization/lock.rb +3 -2
  87. data/lib/concurrent/synchronization/lockable_object.rb +72 -0
  88. data/lib/concurrent/synchronization/mri_lockable_object.rb +71 -0
  89. data/lib/concurrent/synchronization/mri_object.rb +43 -0
  90. data/lib/concurrent/synchronization/object.rb +140 -73
  91. data/lib/concurrent/synchronization/rbx_lockable_object.rb +65 -0
  92. data/lib/concurrent/synchronization/rbx_object.rb +30 -73
  93. data/lib/concurrent/synchronization/volatile.rb +34 -0
  94. data/lib/concurrent/thread_safe/synchronized_delegator.rb +50 -0
  95. data/lib/concurrent/thread_safe/util.rb +14 -0
  96. data/lib/concurrent/thread_safe/util/adder.rb +74 -0
  97. data/lib/concurrent/thread_safe/util/array_hash_rbx.rb +30 -0
  98. data/lib/concurrent/thread_safe/util/cheap_lockable.rb +118 -0
  99. data/lib/concurrent/thread_safe/util/power_of_two_tuple.rb +38 -0
  100. data/lib/concurrent/thread_safe/util/striped64.rb +241 -0
  101. data/lib/concurrent/thread_safe/util/volatile.rb +75 -0
  102. data/lib/concurrent/thread_safe/util/xor_shift_random.rb +50 -0
  103. data/lib/concurrent/timer_task.rb +3 -4
  104. data/lib/concurrent/tuple.rb +86 -0
  105. data/lib/concurrent/tvar.rb +5 -1
  106. data/lib/concurrent/utility/at_exit.rb +1 -1
  107. data/lib/concurrent/utility/engine.rb +4 -0
  108. data/lib/concurrent/utility/monotonic_time.rb +3 -4
  109. data/lib/concurrent/utility/native_extension_loader.rb +50 -30
  110. data/lib/concurrent/version.rb +2 -2
  111. data/lib/concurrent_ruby_ext.jar +0 -0
  112. metadata +47 -12
  113. data/lib/concurrent/atomic/condition.rb +0 -78
  114. data/lib/concurrent/collection/priority_queue.rb +0 -360
  115. data/lib/concurrent/synchronization/java_object.rb +0 -34
  116. data/lib/concurrent/synchronization/monitor_object.rb +0 -27
  117. data/lib/concurrent/synchronization/mutex_object.rb +0 -43
  118. data/lib/concurrent/utilities.rb +0 -5
  119. data/lib/concurrent/utility/timeout.rb +0 -39
  120. data/lib/concurrent/utility/timer.rb +0 -26
  121. data/lib/concurrent_ruby.rb +0 -2
@@ -1,165 +1,102 @@
1
- require 'concurrent/utility/native_extension_loader'
1
+ require 'concurrent/atomic/mutex_atomic_fixnum'
2
2
  require 'concurrent/synchronization'
3
3
 
4
4
  module Concurrent
5
5
 
6
- # @!macro [attach] atomic_fixnum
7
- #
8
- # A numeric value that can be updated atomically. Reads and writes to an atomic
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
- # Testing with ruby 2.1.2
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
- # Testing with jruby 1.9.3
19
- # Testing with Concurrent::MutexAtomicFixnum...
20
- # 5.460000 2.460000 7.920000 ( 3.715000)
21
- # Testing with Concurrent::JavaAtomicFixnum...
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
- # @see http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/atomic/AtomicLong.html java.util.concurrent.atomic.AtomicLong
17
+ # Retrieves the current `Fixnum` value.
25
18
  #
26
- # @!visibility private
27
- # @!macro internal_implementation_note
28
- class MutexAtomicFixnum < Synchronization::Object
19
+ # @return [Fixnum] the current value
29
20
 
30
- # http://stackoverflow.com/questions/535721/ruby-max-integer
31
- MIN_VALUE = -(2**(0.size * 8 - 2))
32
- MAX_VALUE = (2**(0.size * 8 - 2) - 1)
33
-
34
- # @!macro [attach] atomic_fixnum_method_initialize
35
- #
36
- # Creates a new `AtomicFixnum` with the given initial value.
37
- #
38
- # @param [Fixnum] initial the initial value
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
- # @!macro [attach] atomic_fixnum_method_compare_and_set
94
- #
95
- # Atomically sets the value to the given updated value if the current
96
- # value == the expected value.
97
- #
98
- # @param [Fixnum] expect the expected value
99
- # @param [Fixnum] update the new value
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
- # @!macro [attach] atomic_fixnum_method_update
114
- #
115
- # Pass the current value to the given block, replacing it
116
- # with the block's result. May retry if the value changes
117
- # during the block's execution.
118
- #
119
- # @yield [Object] Calculate a new value for the atomic reference using
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
- protected
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
- # @!visibility private
133
- def ns_initialize(initial)
134
- ns_set(initial)
135
- end
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
- private
69
+ ###################################################################
138
70
 
139
- # @!visibility private
140
- def ns_set(value)
141
- range_check!(value)
142
- @value = value
143
- end
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
- # @!visibility private
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 Concurrent.on_jruby?
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
- # @see Concurrent::MutexAtomicFixnum
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/utility/native_extension_loader'
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/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