concurrent-ruby 1.0.5 → 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
- data/CHANGELOG.md +65 -0
- data/Gemfile +39 -0
- data/{LICENSE.txt → LICENSE.md} +2 -0
- data/README.md +207 -105
- data/Rakefile +314 -0
- data/ext/concurrent-ruby/ConcurrentRubyService.java +17 -0
- data/ext/concurrent-ruby/com/concurrent_ruby/ext/AtomicReferenceLibrary.java +175 -0
- data/ext/concurrent-ruby/com/concurrent_ruby/ext/JRubyMapBackendLibrary.java +248 -0
- data/ext/concurrent-ruby/com/concurrent_ruby/ext/JavaAtomicBooleanLibrary.java +93 -0
- data/ext/concurrent-ruby/com/concurrent_ruby/ext/JavaAtomicFixnumLibrary.java +113 -0
- data/ext/concurrent-ruby/com/concurrent_ruby/ext/JavaSemaphoreLibrary.java +159 -0
- data/ext/concurrent-ruby/com/concurrent_ruby/ext/SynchronizationLibrary.java +306 -0
- data/ext/concurrent-ruby/com/concurrent_ruby/ext/jsr166e/ConcurrentHashMap.java +31 -0
- data/ext/concurrent-ruby/com/concurrent_ruby/ext/jsr166e/ConcurrentHashMapV8.java +3863 -0
- data/ext/concurrent-ruby/com/concurrent_ruby/ext/jsr166e/LongAdder.java +203 -0
- data/ext/concurrent-ruby/com/concurrent_ruby/ext/jsr166e/Striped64.java +342 -0
- data/ext/concurrent-ruby/com/concurrent_ruby/ext/jsr166e/nounsafe/ConcurrentHashMapV8.java +3800 -0
- data/ext/concurrent-ruby/com/concurrent_ruby/ext/jsr166e/nounsafe/LongAdder.java +204 -0
- data/ext/concurrent-ruby/com/concurrent_ruby/ext/jsr166e/nounsafe/Striped64.java +291 -0
- data/ext/concurrent-ruby/com/concurrent_ruby/ext/jsr166y/ThreadLocalRandom.java +199 -0
- data/lib/concurrent/agent.rb +7 -7
- data/lib/concurrent/array.rb +59 -32
- data/lib/concurrent/async.rb +4 -4
- data/lib/concurrent/atom.rb +9 -9
- data/lib/concurrent/atomic/atomic_boolean.rb +24 -20
- data/lib/concurrent/atomic/atomic_fixnum.rb +27 -23
- data/lib/concurrent/atomic/atomic_markable_reference.rb +164 -0
- data/lib/concurrent/atomic/atomic_reference.rb +185 -32
- data/lib/concurrent/atomic/count_down_latch.rb +6 -6
- data/lib/concurrent/atomic/cyclic_barrier.rb +1 -1
- data/lib/concurrent/atomic/event.rb +1 -1
- data/lib/concurrent/atomic/java_count_down_latch.rb +9 -6
- data/lib/concurrent/atomic/mutex_atomic_boolean.rb +2 -0
- data/lib/concurrent/atomic/mutex_count_down_latch.rb +1 -0
- data/lib/concurrent/atomic/read_write_lock.rb +2 -1
- data/lib/concurrent/atomic/reentrant_read_write_lock.rb +3 -1
- data/lib/concurrent/atomic/semaphore.rb +8 -8
- data/lib/concurrent/atomic/thread_local_var.rb +7 -7
- data/lib/concurrent/atomic_reference/mutex_atomic.rb +3 -8
- data/lib/concurrent/atomic_reference/numeric_cas_wrapper.rb +1 -1
- data/lib/concurrent/atomics.rb +0 -43
- data/lib/concurrent/collection/lock_free_stack.rb +158 -0
- data/lib/concurrent/collection/map/atomic_reference_map_backend.rb +3 -3
- data/lib/concurrent/collection/map/non_concurrent_map_backend.rb +1 -2
- data/lib/concurrent/collection/non_concurrent_priority_queue.rb +29 -29
- data/lib/concurrent/concern/dereferenceable.rb +1 -1
- data/lib/concurrent/concern/logging.rb +6 -1
- data/lib/concurrent/concern/observable.rb +7 -7
- data/lib/concurrent/concurrent_ruby.jar +0 -0
- data/lib/concurrent/configuration.rb +1 -6
- data/lib/concurrent/constants.rb +1 -1
- data/lib/concurrent/dataflow.rb +2 -1
- data/lib/concurrent/delay.rb +9 -7
- data/lib/concurrent/exchanger.rb +21 -25
- data/lib/concurrent/executor/abstract_executor_service.rb +2 -2
- data/lib/concurrent/executor/cached_thread_pool.rb +1 -1
- data/lib/concurrent/executor/executor_service.rb +15 -15
- data/lib/concurrent/executor/fixed_thread_pool.rb +18 -18
- data/lib/concurrent/executor/java_thread_pool_executor.rb +10 -7
- data/lib/concurrent/executor/single_thread_executor.rb +2 -2
- data/lib/concurrent/executor/thread_pool_executor.rb +6 -6
- data/lib/concurrent/executor/timer_set.rb +1 -1
- data/lib/concurrent/future.rb +4 -1
- data/lib/concurrent/hash.rb +53 -30
- data/lib/concurrent/ivar.rb +5 -6
- data/lib/concurrent/map.rb +178 -81
- data/lib/concurrent/maybe.rb +1 -1
- data/lib/concurrent/mutable_struct.rb +15 -14
- data/lib/concurrent/mvar.rb +2 -2
- data/lib/concurrent/promise.rb +53 -21
- data/lib/concurrent/promises.rb +1936 -0
- data/lib/concurrent/re_include.rb +58 -0
- data/lib/concurrent/set.rb +66 -0
- data/lib/concurrent/settable_struct.rb +1 -0
- data/lib/concurrent/synchronization/abstract_lockable_object.rb +5 -5
- data/lib/concurrent/synchronization/abstract_struct.rb +6 -4
- data/lib/concurrent/synchronization/lockable_object.rb +6 -6
- data/lib/concurrent/synchronization/{mri_lockable_object.rb → mutex_lockable_object.rb} +19 -14
- data/lib/concurrent/synchronization/object.rb +8 -4
- data/lib/concurrent/synchronization/truffleruby_object.rb +46 -0
- data/lib/concurrent/synchronization/volatile.rb +11 -9
- data/lib/concurrent/synchronization.rb +4 -5
- data/lib/concurrent/thread_safe/util/data_structures.rb +63 -0
- data/lib/concurrent/thread_safe/util/striped64.rb +9 -4
- data/lib/concurrent/timer_task.rb +5 -2
- data/lib/concurrent/tuple.rb +1 -1
- data/lib/concurrent/tvar.rb +2 -2
- data/lib/concurrent/utility/193.rb +17 -0
- data/lib/concurrent/utility/at_exit.rb +1 -1
- data/lib/concurrent/utility/engine.rb +4 -4
- data/lib/concurrent/utility/monotonic_time.rb +3 -3
- data/lib/concurrent/utility/native_extension_loader.rb +31 -33
- data/lib/concurrent/utility/processor_counter.rb +0 -2
- data/lib/concurrent/version.rb +2 -2
- data/lib/concurrent-ruby.rb +1 -0
- data/lib/concurrent.rb +26 -20
- metadata +33 -18
- data/lib/concurrent/atomic_reference/concurrent_update_error.rb +0 -8
- data/lib/concurrent/atomic_reference/direct_update.rb +0 -81
- data/lib/concurrent/atomic_reference/jruby+truffle.rb +0 -2
- data/lib/concurrent/atomic_reference/jruby.rb +0 -16
- data/lib/concurrent/atomic_reference/rbx.rb +0 -22
- data/lib/concurrent/atomic_reference/ruby.rb +0 -32
- data/lib/concurrent/edge.rb +0 -26
- data/lib/concurrent/lazy_register.rb +0 -81
- data/lib/concurrent/synchronization/truffle_lockable_object.rb +0 -9
- data/lib/concurrent/synchronization/truffle_object.rb +0 -31
- data/lib/concurrent/thread_safe/util/array_hash_rbx.rb +0 -30
@@ -1,51 +1,204 @@
|
|
1
1
|
require 'concurrent/synchronization'
|
2
2
|
require 'concurrent/utility/engine'
|
3
|
-
require 'concurrent/atomic_reference/
|
4
|
-
require 'concurrent/atomic_reference/mutex_atomic'
|
5
|
-
|
6
|
-
begin
|
7
|
-
# force fallback impl with FORCE_ATOMIC_FALLBACK=1
|
8
|
-
if /[^0fF]/ =~ ENV['FORCE_ATOMIC_FALLBACK']
|
9
|
-
ruby_engine = 'mutex_atomic'
|
10
|
-
else
|
11
|
-
ruby_engine = Concurrent.ruby_engine
|
12
|
-
end
|
3
|
+
require 'concurrent/atomic_reference/numeric_cas_wrapper'
|
13
4
|
|
14
|
-
|
15
|
-
|
16
|
-
#
|
5
|
+
# Shim for TruffleRuby::AtomicReference
|
6
|
+
if Concurrent.on_truffleruby? && !defined?(TruffleRuby::AtomicReference)
|
7
|
+
# @!visibility private
|
8
|
+
module TruffleRuby
|
9
|
+
AtomicReference = Truffle::AtomicReference
|
10
|
+
end
|
17
11
|
end
|
18
12
|
|
19
|
-
|
13
|
+
module Concurrent
|
20
14
|
|
21
|
-
#
|
22
|
-
|
23
|
-
|
15
|
+
# Define update methods that use direct paths
|
16
|
+
#
|
17
|
+
# @!visibility private
|
18
|
+
# @!macro internal_implementation_note
|
19
|
+
module AtomicDirectUpdate
|
24
20
|
|
25
|
-
|
21
|
+
# @!macro atomic_reference_method_update
|
22
|
+
#
|
23
|
+
# Pass the current value to the given block, replacing it
|
24
|
+
# with the block's result. May retry if the value changes
|
25
|
+
# during the block's execution.
|
26
|
+
#
|
27
|
+
# @yield [Object] Calculate a new value for the atomic reference using
|
28
|
+
# given (old) value
|
29
|
+
# @yieldparam [Object] old_value the starting value of the atomic reference
|
30
|
+
# @return [Object] the new value
|
31
|
+
def update
|
32
|
+
true until compare_and_set(old_value = get, new_value = yield(old_value))
|
33
|
+
new_value
|
34
|
+
end
|
26
35
|
|
27
|
-
|
28
|
-
|
36
|
+
# @!macro atomic_reference_method_try_update
|
37
|
+
#
|
38
|
+
# Pass the current value to the given block, replacing it
|
39
|
+
# with the block's result. Return nil if the update fails.
|
40
|
+
#
|
41
|
+
# @yield [Object] Calculate a new value for the atomic reference using
|
42
|
+
# given (old) value
|
43
|
+
# @yieldparam [Object] old_value the starting value of the atomic reference
|
44
|
+
# @note This method was altered to avoid raising an exception by default.
|
45
|
+
# Instead, this method now returns `nil` in case of failure. For more info,
|
46
|
+
# please see: https://github.com/ruby-concurrency/concurrent-ruby/pull/336
|
47
|
+
# @return [Object] the new value, or nil if update failed
|
48
|
+
def try_update
|
49
|
+
old_value = get
|
50
|
+
new_value = yield old_value
|
51
|
+
|
52
|
+
return unless compare_and_set old_value, new_value
|
53
|
+
|
54
|
+
new_value
|
55
|
+
end
|
56
|
+
|
57
|
+
# @!macro atomic_reference_method_try_update!
|
58
|
+
#
|
59
|
+
# Pass the current value to the given block, replacing it
|
60
|
+
# with the block's result. Raise an exception if the update
|
61
|
+
# fails.
|
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
|
+
# @note This behavior mimics the behavior of the original
|
67
|
+
# `AtomicReference#try_update` API. The reason this was changed was to
|
68
|
+
# avoid raising exceptions (which are inherently slow) by default. For more
|
69
|
+
# info: https://github.com/ruby-concurrency/concurrent-ruby/pull/336
|
70
|
+
# @return [Object] the new value
|
71
|
+
# @raise [Concurrent::ConcurrentUpdateError] if the update fails
|
72
|
+
def try_update!
|
73
|
+
old_value = get
|
74
|
+
new_value = yield old_value
|
75
|
+
unless compare_and_set(old_value, new_value)
|
76
|
+
if $VERBOSE
|
77
|
+
raise ConcurrentUpdateError, "Update failed"
|
78
|
+
else
|
79
|
+
raise ConcurrentUpdateError, "Update failed", ConcurrentUpdateError::CONC_UP_ERR_BACKTRACE
|
80
|
+
end
|
81
|
+
end
|
82
|
+
new_value
|
83
|
+
end
|
29
84
|
end
|
30
85
|
|
31
|
-
|
86
|
+
require 'concurrent/atomic_reference/mutex_atomic'
|
32
87
|
|
33
88
|
# @!macro atomic_reference
|
34
|
-
|
89
|
+
#
|
90
|
+
# An object reference that may be updated atomically. All read and write
|
91
|
+
# operations have java volatile semantic.
|
92
|
+
#
|
93
|
+
# @!macro thread_safe_variable_comparison
|
94
|
+
#
|
95
|
+
# @see http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/atomic/AtomicReference.html
|
96
|
+
# @see http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/atomic/package-summary.html
|
97
|
+
#
|
98
|
+
# @!method initialize(value = nil)
|
99
|
+
# @!macro atomic_reference_method_initialize
|
100
|
+
# @param [Object] value The initial value.
|
101
|
+
#
|
102
|
+
# @!method get
|
103
|
+
# @!macro atomic_reference_method_get
|
104
|
+
# Gets the current value.
|
105
|
+
# @return [Object] the current value
|
106
|
+
#
|
107
|
+
# @!method set(new_value)
|
108
|
+
# @!macro atomic_reference_method_set
|
109
|
+
# Sets to the given value.
|
110
|
+
# @param [Object] new_value the new value
|
111
|
+
# @return [Object] the new value
|
112
|
+
#
|
113
|
+
# @!method get_and_set(new_value)
|
114
|
+
# @!macro atomic_reference_method_get_and_set
|
115
|
+
# Atomically sets to the given value and returns the old value.
|
116
|
+
# @param [Object] new_value the new value
|
117
|
+
# @return [Object] the old value
|
118
|
+
#
|
119
|
+
# @!method compare_and_set(old_value, new_value)
|
120
|
+
# @!macro atomic_reference_method_compare_and_set
|
121
|
+
#
|
122
|
+
# Atomically sets the value to the given updated value if
|
123
|
+
# the current value == the expected value.
|
124
|
+
#
|
125
|
+
# @param [Object] old_value the expected value
|
126
|
+
# @param [Object] new_value the new value
|
127
|
+
#
|
128
|
+
# @return [Boolean] `true` if successful. A `false` return indicates
|
129
|
+
# that the actual value was not equal to the expected value.
|
130
|
+
#
|
131
|
+
# @!method update
|
132
|
+
# @!macro atomic_reference_method_update
|
133
|
+
#
|
134
|
+
# @!method try_update
|
135
|
+
# @!macro atomic_reference_method_try_update
|
136
|
+
#
|
137
|
+
# @!method try_update!
|
138
|
+
# @!macro atomic_reference_method_try_update!
|
139
|
+
|
140
|
+
|
141
|
+
# @!macro internal_implementation_note
|
142
|
+
class ConcurrentUpdateError < ThreadError
|
143
|
+
# frozen pre-allocated backtrace to speed ConcurrentUpdateError
|
144
|
+
CONC_UP_ERR_BACKTRACE = ['backtrace elided; set verbose to enable'].freeze
|
35
145
|
end
|
36
146
|
|
37
|
-
|
147
|
+
# @!macro internal_implementation_note
|
148
|
+
AtomicReferenceImplementation = case
|
149
|
+
when Concurrent.on_cruby? && Concurrent.c_extensions_loaded?
|
150
|
+
# @!visibility private
|
151
|
+
# @!macro internal_implementation_note
|
152
|
+
class CAtomicReference
|
153
|
+
include AtomicDirectUpdate
|
154
|
+
include AtomicNumericCompareAndSetWrapper
|
155
|
+
alias_method :compare_and_swap, :compare_and_set
|
156
|
+
end
|
157
|
+
CAtomicReference
|
158
|
+
when Concurrent.on_jruby?
|
159
|
+
# @!visibility private
|
160
|
+
# @!macro internal_implementation_note
|
161
|
+
class JavaAtomicReference
|
162
|
+
include AtomicDirectUpdate
|
163
|
+
end
|
164
|
+
JavaAtomicReference
|
165
|
+
when Concurrent.on_truffleruby?
|
166
|
+
class TruffleRubyAtomicReference < TruffleRuby::AtomicReference
|
167
|
+
include AtomicDirectUpdate
|
168
|
+
alias_method :value, :get
|
169
|
+
alias_method :value=, :set
|
170
|
+
alias_method :compare_and_swap, :compare_and_set
|
171
|
+
alias_method :swap, :get_and_set
|
172
|
+
end
|
173
|
+
when Concurrent.on_rbx?
|
174
|
+
# @note Extends `Rubinius::AtomicReference` version adding aliases
|
175
|
+
# and numeric logic.
|
176
|
+
#
|
177
|
+
# @!visibility private
|
178
|
+
# @!macro internal_implementation_note
|
179
|
+
class RbxAtomicReference < Rubinius::AtomicReference
|
180
|
+
alias_method :_compare_and_set, :compare_and_set
|
181
|
+
include AtomicDirectUpdate
|
182
|
+
include AtomicNumericCompareAndSetWrapper
|
183
|
+
alias_method :value, :get
|
184
|
+
alias_method :value=, :set
|
185
|
+
alias_method :swap, :get_and_set
|
186
|
+
alias_method :compare_and_swap, :compare_and_set
|
187
|
+
end
|
188
|
+
RbxAtomicReference
|
189
|
+
else
|
190
|
+
MutexAtomicReference
|
191
|
+
end
|
192
|
+
private_constant :AtomicReferenceImplementation
|
38
193
|
|
39
194
|
# @!macro atomic_reference
|
40
|
-
class
|
41
|
-
end
|
42
|
-
end
|
195
|
+
class AtomicReference < AtomicReferenceImplementation
|
43
196
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
end
|
197
|
+
# @return [String] Short string representation.
|
198
|
+
def to_s
|
199
|
+
format '%s value:%s>', super[0..-2], get
|
200
|
+
end
|
49
201
|
|
50
|
-
|
202
|
+
alias_method :inspect, :to_s
|
203
|
+
end
|
51
204
|
end
|
@@ -6,7 +6,7 @@ module Concurrent
|
|
6
6
|
|
7
7
|
###################################################################
|
8
8
|
|
9
|
-
# @!macro
|
9
|
+
# @!macro count_down_latch_method_initialize
|
10
10
|
#
|
11
11
|
# Create a new `CountDownLatch` with the initial `count`.
|
12
12
|
#
|
@@ -14,7 +14,7 @@ module Concurrent
|
|
14
14
|
#
|
15
15
|
# @raise [ArgumentError] if `count` is not an integer or is less than zero
|
16
16
|
|
17
|
-
# @!macro
|
17
|
+
# @!macro count_down_latch_method_wait
|
18
18
|
#
|
19
19
|
# Block on the latch until the counter reaches zero or until `timeout` is reached.
|
20
20
|
#
|
@@ -22,12 +22,12 @@ module Concurrent
|
|
22
22
|
# to block indefinitely
|
23
23
|
# @return [Boolean] `true` if the `count` reaches zero else false on `timeout`
|
24
24
|
|
25
|
-
# @!macro
|
25
|
+
# @!macro count_down_latch_method_count_down
|
26
26
|
#
|
27
27
|
# Signal the latch to decrement the counter. Will signal all blocked threads when
|
28
28
|
# the `count` reaches zero.
|
29
29
|
|
30
|
-
# @!macro
|
30
|
+
# @!macro count_down_latch_method_count
|
31
31
|
#
|
32
32
|
# The current value of the counter.
|
33
33
|
#
|
@@ -35,7 +35,7 @@ module Concurrent
|
|
35
35
|
|
36
36
|
###################################################################
|
37
37
|
|
38
|
-
# @!macro
|
38
|
+
# @!macro count_down_latch_public_api
|
39
39
|
#
|
40
40
|
# @!method initialize(count = 1)
|
41
41
|
# @!macro count_down_latch_method_initialize
|
@@ -61,7 +61,7 @@ module Concurrent
|
|
61
61
|
end
|
62
62
|
private_constant :CountDownLatchImplementation
|
63
63
|
|
64
|
-
# @!macro
|
64
|
+
# @!macro count_down_latch
|
65
65
|
#
|
66
66
|
# A synchronization object that allows one thread to wait on multiple other threads.
|
67
67
|
# The thread that will wait creates a `CountDownLatch` and sets the initial value
|
@@ -23,7 +23,7 @@ module Concurrent
|
|
23
23
|
# # use main as well
|
24
24
|
# process.call 2
|
25
25
|
#
|
26
|
-
# # here we can be sure that all jobs are processed
|
26
|
+
# # here we can be sure that all jobs are processed
|
27
27
|
class CyclicBarrier < Synchronization::LockableObject
|
28
28
|
|
29
29
|
# @!visibility private
|
@@ -9,20 +9,23 @@ if Concurrent.on_jruby?
|
|
9
9
|
|
10
10
|
# @!macro count_down_latch_method_initialize
|
11
11
|
def initialize(count = 1)
|
12
|
-
|
13
|
-
|
14
|
-
end
|
12
|
+
Utility::NativeInteger.ensure_integer_and_bounds(count)
|
13
|
+
Utility::NativeInteger.ensure_positive(count)
|
15
14
|
@latch = java.util.concurrent.CountDownLatch.new(count)
|
16
15
|
end
|
17
16
|
|
18
17
|
# @!macro count_down_latch_method_wait
|
19
18
|
def wait(timeout = nil)
|
19
|
+
result = nil
|
20
20
|
if timeout.nil?
|
21
|
-
@latch.await
|
22
|
-
true
|
21
|
+
Synchronization::JRuby.sleep_interruptibly { @latch.await }
|
22
|
+
result = true
|
23
23
|
else
|
24
|
-
|
24
|
+
Synchronization::JRuby.sleep_interruptibly do
|
25
|
+
result = @latch.await(1000 * timeout, java.util.concurrent.TimeUnit::MILLISECONDS)
|
26
|
+
end
|
25
27
|
end
|
28
|
+
result
|
26
29
|
end
|
27
30
|
|
28
31
|
# @!macro count_down_latch_method_count_down
|
@@ -193,7 +193,8 @@ module Concurrent
|
|
193
193
|
#
|
194
194
|
# @return [Boolean] true if the lock is successfully released
|
195
195
|
def release_write_lock
|
196
|
-
|
196
|
+
return true unless running_writer?
|
197
|
+
c = @Counter.update { |counter| counter - RUNNING_WRITER }
|
197
198
|
@ReadLock.broadcast
|
198
199
|
@WriteLock.signal if waiting_writers(c) > 0
|
199
200
|
true
|
@@ -21,7 +21,9 @@ module Concurrent
|
|
21
21
|
# also acquire a read lock OR a write lock more than once. Only when the read (or
|
22
22
|
# write) lock is released as many times as it was acquired, will the thread
|
23
23
|
# actually let it go, allowing other threads which might have been waiting
|
24
|
-
# to proceed.
|
24
|
+
# to proceed. Therefore the lock can be upgraded by first acquiring
|
25
|
+
# read lock and then write lock and that the lock can be downgraded by first
|
26
|
+
# having both read and write lock a releasing just the write lock.
|
25
27
|
#
|
26
28
|
# If both read and write locks are acquired by the same thread, it is not strictly
|
27
29
|
# necessary to release them in the same order they were acquired. In other words,
|
@@ -5,7 +5,7 @@ module Concurrent
|
|
5
5
|
|
6
6
|
###################################################################
|
7
7
|
|
8
|
-
# @!macro
|
8
|
+
# @!macro semaphore_method_initialize
|
9
9
|
#
|
10
10
|
# Create a new `Semaphore` with the initial `count`.
|
11
11
|
#
|
@@ -13,7 +13,7 @@ module Concurrent
|
|
13
13
|
#
|
14
14
|
# @raise [ArgumentError] if `count` is not an integer or is less than zero
|
15
15
|
|
16
|
-
# @!macro
|
16
|
+
# @!macro semaphore_method_acquire
|
17
17
|
#
|
18
18
|
# Acquires the given number of permits from this semaphore,
|
19
19
|
# blocking until all are available.
|
@@ -25,19 +25,19 @@ module Concurrent
|
|
25
25
|
#
|
26
26
|
# @return [nil]
|
27
27
|
|
28
|
-
# @!macro
|
28
|
+
# @!macro semaphore_method_available_permits
|
29
29
|
#
|
30
30
|
# Returns the current number of permits available in this semaphore.
|
31
31
|
#
|
32
32
|
# @return [Integer]
|
33
33
|
|
34
|
-
# @!macro
|
34
|
+
# @!macro semaphore_method_drain_permits
|
35
35
|
#
|
36
36
|
# Acquires and returns all permits that are immediately available.
|
37
37
|
#
|
38
38
|
# @return [Integer]
|
39
39
|
|
40
|
-
# @!macro
|
40
|
+
# @!macro semaphore_method_try_acquire
|
41
41
|
#
|
42
42
|
# Acquires the given number of permits from this semaphore,
|
43
43
|
# only if all are available at the time of invocation or within
|
@@ -54,7 +54,7 @@ module Concurrent
|
|
54
54
|
# @return [Boolean] `false` if no permits are available, `true` when
|
55
55
|
# acquired a permit
|
56
56
|
|
57
|
-
# @!macro
|
57
|
+
# @!macro semaphore_method_release
|
58
58
|
#
|
59
59
|
# Releases the given number of permits, returning them to the semaphore.
|
60
60
|
#
|
@@ -66,7 +66,7 @@ module Concurrent
|
|
66
66
|
|
67
67
|
###################################################################
|
68
68
|
|
69
|
-
# @!macro
|
69
|
+
# @!macro semaphore_public_api
|
70
70
|
#
|
71
71
|
# @!method initialize(count)
|
72
72
|
# @!macro semaphore_method_initialize
|
@@ -98,7 +98,7 @@ module Concurrent
|
|
98
98
|
end
|
99
99
|
private_constant :SemaphoreImplementation
|
100
100
|
|
101
|
-
# @!macro
|
101
|
+
# @!macro semaphore
|
102
102
|
#
|
103
103
|
# A counting semaphore. Conceptually, a semaphore maintains a set of
|
104
104
|
# permits. Each {#acquire} blocks if necessary until a permit is
|
@@ -6,7 +6,7 @@ module Concurrent
|
|
6
6
|
|
7
7
|
###################################################################
|
8
8
|
|
9
|
-
# @!macro
|
9
|
+
# @!macro thread_local_var_method_initialize
|
10
10
|
#
|
11
11
|
# Creates a thread local variable.
|
12
12
|
#
|
@@ -14,20 +14,20 @@ module Concurrent
|
|
14
14
|
# @param [Proc] default_block Optional block that gets called to obtain the
|
15
15
|
# default value for each thread
|
16
16
|
|
17
|
-
# @!macro
|
17
|
+
# @!macro thread_local_var_method_get
|
18
18
|
#
|
19
19
|
# Returns the value in the current thread's copy of this thread-local variable.
|
20
20
|
#
|
21
21
|
# @return [Object] the current value
|
22
22
|
|
23
|
-
# @!macro
|
23
|
+
# @!macro thread_local_var_method_set
|
24
24
|
#
|
25
25
|
# Sets the current thread's copy of this thread-local variable to the specified value.
|
26
26
|
#
|
27
27
|
# @param [Object] value the value to set
|
28
28
|
# @return [Object] the new value
|
29
29
|
|
30
|
-
# @!macro
|
30
|
+
# @!macro thread_local_var_method_bind
|
31
31
|
#
|
32
32
|
# Bind the given value to thread local storage during
|
33
33
|
# execution of the given block.
|
@@ -39,9 +39,9 @@ module Concurrent
|
|
39
39
|
|
40
40
|
###################################################################
|
41
41
|
|
42
|
-
# @!macro
|
42
|
+
# @!macro thread_local_var_public_api
|
43
43
|
#
|
44
|
-
# @!method initialize(default = nil)
|
44
|
+
# @!method initialize(default = nil, &default_block)
|
45
45
|
# @!macro thread_local_var_method_initialize
|
46
46
|
#
|
47
47
|
# @!method value
|
@@ -65,7 +65,7 @@ module Concurrent
|
|
65
65
|
end
|
66
66
|
private_constant :ThreadLocalVarImplementation
|
67
67
|
|
68
|
-
# @!macro
|
68
|
+
# @!macro thread_local_var
|
69
69
|
#
|
70
70
|
# A `ThreadLocalVar` is a variable where the value is different for each thread.
|
71
71
|
# Each variable may have a default value, but when you modify the variable only
|
@@ -1,16 +1,11 @@
|
|
1
|
-
require 'concurrent/synchronization'
|
2
|
-
require 'concurrent/atomic_reference/direct_update'
|
3
|
-
require 'concurrent/atomic_reference/numeric_cas_wrapper'
|
4
|
-
|
5
1
|
module Concurrent
|
6
2
|
|
7
|
-
# @!macro atomic_reference
|
8
|
-
#
|
9
3
|
# @!visibility private
|
10
4
|
# @!macro internal_implementation_note
|
11
5
|
class MutexAtomicReference < Synchronization::LockableObject
|
12
|
-
include
|
13
|
-
include
|
6
|
+
include AtomicDirectUpdate
|
7
|
+
include AtomicNumericCompareAndSetWrapper
|
8
|
+
alias_method :compare_and_swap, :compare_and_set
|
14
9
|
|
15
10
|
# @!macro atomic_reference_method_initialize
|
16
11
|
def initialize(value = nil)
|
data/lib/concurrent/atomics.rb
CHANGED
@@ -1,46 +1,3 @@
|
|
1
|
-
# @!macro [new] atomic_reference
|
2
|
-
#
|
3
|
-
# An object reference that may be updated atomically. All read and write
|
4
|
-
# operations have java volatile semantic.
|
5
|
-
#
|
6
|
-
# @!macro thread_safe_variable_comparison
|
7
|
-
#
|
8
|
-
# @see http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/atomic/AtomicReference.html
|
9
|
-
# @see http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/atomic/package-summary.html
|
10
|
-
#
|
11
|
-
# @!method initialize
|
12
|
-
# @!macro [new] atomic_reference_method_initialize
|
13
|
-
# @param [Object] value The initial value.
|
14
|
-
#
|
15
|
-
# @!method get
|
16
|
-
# @!macro [new] atomic_reference_method_get
|
17
|
-
# Gets the current value.
|
18
|
-
# @return [Object] the current value
|
19
|
-
#
|
20
|
-
# @!method set
|
21
|
-
# @!macro [new] atomic_reference_method_set
|
22
|
-
# Sets to the given value.
|
23
|
-
# @param [Object] new_value the new value
|
24
|
-
# @return [Object] the new value
|
25
|
-
#
|
26
|
-
# @!method get_and_set
|
27
|
-
# @!macro [new] atomic_reference_method_get_and_set
|
28
|
-
# Atomically sets to the given value and returns the old value.
|
29
|
-
# @param [Object] new_value the new value
|
30
|
-
# @return [Object] the old value
|
31
|
-
#
|
32
|
-
# @!method compare_and_set
|
33
|
-
# @!macro [new] atomic_reference_method_compare_and_set
|
34
|
-
#
|
35
|
-
# Atomically sets the value to the given updated value if
|
36
|
-
# the current value == the expected value.
|
37
|
-
#
|
38
|
-
# @param [Object] old_value the expected value
|
39
|
-
# @param [Object] new_value the new value
|
40
|
-
#
|
41
|
-
# @return [Boolean] `true` if successful. A `false` return indicates
|
42
|
-
# that the actual value was not equal to the expected value.
|
43
|
-
|
44
1
|
require 'concurrent/atomic/atomic_reference'
|
45
2
|
require 'concurrent/atomic/atomic_boolean'
|
46
3
|
require 'concurrent/atomic/atomic_fixnum'
|