concurrent-ruby 0.7.0-java
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.
- data/LICENSE.txt +21 -0
- data/README.md +217 -0
- data/lib/concurrent.rb +45 -0
- data/lib/concurrent/actor.rb +104 -0
- data/lib/concurrent/actor/behaviour.rb +70 -0
- data/lib/concurrent/actor/behaviour/abstract.rb +48 -0
- data/lib/concurrent/actor/behaviour/awaits.rb +21 -0
- data/lib/concurrent/actor/behaviour/buffer.rb +54 -0
- data/lib/concurrent/actor/behaviour/errors_on_unknown_message.rb +12 -0
- data/lib/concurrent/actor/behaviour/executes_context.rb +18 -0
- data/lib/concurrent/actor/behaviour/linking.rb +42 -0
- data/lib/concurrent/actor/behaviour/pausing.rb +77 -0
- data/lib/concurrent/actor/behaviour/removes_child.rb +16 -0
- data/lib/concurrent/actor/behaviour/sets_results.rb +36 -0
- data/lib/concurrent/actor/behaviour/supervised.rb +58 -0
- data/lib/concurrent/actor/behaviour/supervising.rb +34 -0
- data/lib/concurrent/actor/behaviour/terminates_children.rb +13 -0
- data/lib/concurrent/actor/behaviour/termination.rb +54 -0
- data/lib/concurrent/actor/context.rb +153 -0
- data/lib/concurrent/actor/core.rb +213 -0
- data/lib/concurrent/actor/default_dead_letter_handler.rb +9 -0
- data/lib/concurrent/actor/envelope.rb +41 -0
- data/lib/concurrent/actor/errors.rb +27 -0
- data/lib/concurrent/actor/internal_delegations.rb +49 -0
- data/lib/concurrent/actor/public_delegations.rb +40 -0
- data/lib/concurrent/actor/reference.rb +81 -0
- data/lib/concurrent/actor/root.rb +37 -0
- data/lib/concurrent/actor/type_check.rb +48 -0
- data/lib/concurrent/actor/utils.rb +10 -0
- data/lib/concurrent/actor/utils/ad_hoc.rb +21 -0
- data/lib/concurrent/actor/utils/balancer.rb +40 -0
- data/lib/concurrent/actor/utils/broadcast.rb +52 -0
- data/lib/concurrent/actor/utils/pool.rb +59 -0
- data/lib/concurrent/actress.rb +3 -0
- data/lib/concurrent/agent.rb +230 -0
- data/lib/concurrent/async.rb +284 -0
- data/lib/concurrent/atomic.rb +91 -0
- data/lib/concurrent/atomic/atomic_boolean.rb +202 -0
- data/lib/concurrent/atomic/atomic_fixnum.rb +203 -0
- data/lib/concurrent/atomic/condition.rb +67 -0
- data/lib/concurrent/atomic/copy_on_notify_observer_set.rb +118 -0
- data/lib/concurrent/atomic/copy_on_write_observer_set.rb +117 -0
- data/lib/concurrent/atomic/count_down_latch.rb +116 -0
- data/lib/concurrent/atomic/cyclic_barrier.rb +106 -0
- data/lib/concurrent/atomic/event.rb +98 -0
- data/lib/concurrent/atomic/synchronization.rb +51 -0
- data/lib/concurrent/atomic/thread_local_var.rb +82 -0
- data/lib/concurrent/atomic_reference/concurrent_update_error.rb +8 -0
- data/lib/concurrent/atomic_reference/direct_update.rb +50 -0
- data/lib/concurrent/atomic_reference/jruby.rb +14 -0
- data/lib/concurrent/atomic_reference/mutex_atomic.rb +77 -0
- data/lib/concurrent/atomic_reference/numeric_cas_wrapper.rb +25 -0
- data/lib/concurrent/atomic_reference/rbx.rb +19 -0
- data/lib/concurrent/atomic_reference/ruby.rb +37 -0
- data/lib/concurrent/atomics.rb +11 -0
- data/lib/concurrent/channel/buffered_channel.rb +85 -0
- data/lib/concurrent/channel/channel.rb +41 -0
- data/lib/concurrent/channel/unbuffered_channel.rb +35 -0
- data/lib/concurrent/channel/waitable_list.rb +40 -0
- data/lib/concurrent/channels.rb +5 -0
- data/lib/concurrent/collection/blocking_ring_buffer.rb +71 -0
- data/lib/concurrent/collection/priority_queue.rb +305 -0
- data/lib/concurrent/collection/ring_buffer.rb +59 -0
- data/lib/concurrent/collections.rb +3 -0
- data/lib/concurrent/configuration.rb +161 -0
- data/lib/concurrent/dataflow.rb +108 -0
- data/lib/concurrent/delay.rb +104 -0
- data/lib/concurrent/dereferenceable.rb +101 -0
- data/lib/concurrent/errors.rb +30 -0
- data/lib/concurrent/exchanger.rb +34 -0
- data/lib/concurrent/executor/cached_thread_pool.rb +44 -0
- data/lib/concurrent/executor/executor.rb +282 -0
- data/lib/concurrent/executor/fixed_thread_pool.rb +33 -0
- data/lib/concurrent/executor/immediate_executor.rb +65 -0
- data/lib/concurrent/executor/java_cached_thread_pool.rb +31 -0
- data/lib/concurrent/executor/java_fixed_thread_pool.rb +41 -0
- data/lib/concurrent/executor/java_single_thread_executor.rb +22 -0
- data/lib/concurrent/executor/java_thread_pool_executor.rb +180 -0
- data/lib/concurrent/executor/per_thread_executor.rb +100 -0
- data/lib/concurrent/executor/ruby_cached_thread_pool.rb +29 -0
- data/lib/concurrent/executor/ruby_fixed_thread_pool.rb +32 -0
- data/lib/concurrent/executor/ruby_single_thread_executor.rb +74 -0
- data/lib/concurrent/executor/ruby_thread_pool_executor.rb +288 -0
- data/lib/concurrent/executor/ruby_thread_pool_worker.rb +72 -0
- data/lib/concurrent/executor/safe_task_executor.rb +35 -0
- data/lib/concurrent/executor/serialized_execution.rb +126 -0
- data/lib/concurrent/executor/single_thread_executor.rb +35 -0
- data/lib/concurrent/executor/thread_pool_executor.rb +68 -0
- data/lib/concurrent/executor/timer_set.rb +143 -0
- data/lib/concurrent/executors.rb +9 -0
- data/lib/concurrent/future.rb +125 -0
- data/lib/concurrent/ivar.rb +111 -0
- data/lib/concurrent/lazy_register.rb +58 -0
- data/lib/concurrent/logging.rb +17 -0
- data/lib/concurrent/mvar.rb +200 -0
- data/lib/concurrent/obligation.rb +171 -0
- data/lib/concurrent/observable.rb +40 -0
- data/lib/concurrent/options_parser.rb +48 -0
- data/lib/concurrent/promise.rb +170 -0
- data/lib/concurrent/scheduled_task.rb +79 -0
- data/lib/concurrent/timer_task.rb +341 -0
- data/lib/concurrent/tvar.rb +248 -0
- data/lib/concurrent/utilities.rb +3 -0
- data/lib/concurrent/utility/processor_count.rb +152 -0
- data/lib/concurrent/utility/timeout.rb +35 -0
- data/lib/concurrent/utility/timer.rb +21 -0
- data/lib/concurrent/version.rb +3 -0
- data/lib/concurrent_ruby.rb +1 -0
- data/lib/concurrent_ruby_ext.jar +0 -0
- data/lib/concurrent_ruby_ext.so +0 -0
- data/lib/extension_helper.rb +28 -0
- metadata +163 -0
@@ -0,0 +1,91 @@
|
|
1
|
+
#####################################################################
|
2
|
+
# Attempt to check for the deprecated ruby-atomic gem and warn the
|
3
|
+
# user that they should use the new implementation instead.
|
4
|
+
|
5
|
+
if defined?(Atomic)
|
6
|
+
warn <<-RUBY
|
7
|
+
[ATOMIC] Detected an `Atomic` class, which may indicate a dependency
|
8
|
+
on the ruby-atomic gem. That gem has been deprecated and merged into
|
9
|
+
the concurrent-ruby gem. Please use the Concurrent::Atomic class for
|
10
|
+
atomic references and not the Atomic class.
|
11
|
+
RUBY
|
12
|
+
end
|
13
|
+
#####################################################################
|
14
|
+
|
15
|
+
require 'concurrent/atomic_reference/concurrent_update_error'
|
16
|
+
require 'concurrent/atomic_reference/mutex_atomic'
|
17
|
+
|
18
|
+
begin
|
19
|
+
# force fallback impl with FORCE_ATOMIC_FALLBACK=1
|
20
|
+
if /[^0fF]/ =~ ENV['FORCE_ATOMIC_FALLBACK']
|
21
|
+
ruby_engine = 'mutex_atomic'
|
22
|
+
else
|
23
|
+
ruby_engine = defined?(RUBY_ENGINE)? RUBY_ENGINE : 'ruby'
|
24
|
+
end
|
25
|
+
|
26
|
+
require "concurrent/atomic_reference/#{ruby_engine}"
|
27
|
+
rescue LoadError
|
28
|
+
warn 'Compiled extensions not installed, pure Ruby Atomic will be used.'
|
29
|
+
end
|
30
|
+
|
31
|
+
if defined? Concurrent::JavaAtomic
|
32
|
+
|
33
|
+
# @!macro [attach] atomic_reference
|
34
|
+
#
|
35
|
+
# An object reference that may be updated atomically.
|
36
|
+
#
|
37
|
+
# Testing with ruby 2.1.2
|
38
|
+
#
|
39
|
+
# *** Sequential updates ***
|
40
|
+
# user system total real
|
41
|
+
# no lock 0.000000 0.000000 0.000000 ( 0.005502)
|
42
|
+
# mutex 0.030000 0.000000 0.030000 ( 0.025158)
|
43
|
+
# MutexAtomic 0.100000 0.000000 0.100000 ( 0.103096)
|
44
|
+
# CAtomic 0.040000 0.000000 0.040000 ( 0.034012)
|
45
|
+
#
|
46
|
+
# *** Parallel updates ***
|
47
|
+
# user system total real
|
48
|
+
# no lock 0.010000 0.000000 0.010000 ( 0.009387)
|
49
|
+
# mutex 0.030000 0.010000 0.040000 ( 0.032545)
|
50
|
+
# MutexAtomic 0.830000 2.280000 3.110000 ( 2.146622)
|
51
|
+
# CAtomic 0.040000 0.000000 0.040000 ( 0.038332)
|
52
|
+
#
|
53
|
+
# Testing with jruby 1.9.3
|
54
|
+
#
|
55
|
+
# *** Sequential updates ***
|
56
|
+
# user system total real
|
57
|
+
# no lock 0.170000 0.000000 0.170000 ( 0.051000)
|
58
|
+
# mutex 0.370000 0.010000 0.380000 ( 0.121000)
|
59
|
+
# MutexAtomic 1.530000 0.020000 1.550000 ( 0.471000)
|
60
|
+
# JavaAtomic 0.370000 0.010000 0.380000 ( 0.112000)
|
61
|
+
#
|
62
|
+
# *** Parallel updates ***
|
63
|
+
# user system total real
|
64
|
+
# no lock 0.390000 0.000000 0.390000 ( 0.105000)
|
65
|
+
# mutex 0.480000 0.040000 0.520000 ( 0.145000)
|
66
|
+
# MutexAtomic 1.600000 0.180000 1.780000 ( 0.511000)
|
67
|
+
# JavaAtomic 0.460000 0.010000 0.470000 ( 0.131000)
|
68
|
+
#
|
69
|
+
# @see http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/atomic/AtomicReference.html
|
70
|
+
# @see http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/atomic/package-summary.html
|
71
|
+
class Concurrent::Atomic < Concurrent::JavaAtomic
|
72
|
+
end
|
73
|
+
|
74
|
+
elsif defined? Concurrent::RbxAtomic
|
75
|
+
|
76
|
+
# @!macro atomic_reference
|
77
|
+
class Concurrent::Atomic < Concurrent::RbxAtomic
|
78
|
+
end
|
79
|
+
|
80
|
+
elsif Concurrent.allow_c_native_class?('CAtomic')
|
81
|
+
|
82
|
+
# @!macro atomic_reference
|
83
|
+
class Concurrent::Atomic < Concurrent::CAtomic
|
84
|
+
end
|
85
|
+
|
86
|
+
else
|
87
|
+
|
88
|
+
# @!macro atomic_reference
|
89
|
+
class Concurrent::Atomic < Concurrent::MutexAtomic
|
90
|
+
end
|
91
|
+
end
|
@@ -0,0 +1,202 @@
|
|
1
|
+
require_relative '../../extension_helper'
|
2
|
+
Concurrent.safe_require_c_extensions
|
3
|
+
|
4
|
+
module Concurrent
|
5
|
+
|
6
|
+
# @!macro [attach] atomic_boolean
|
7
|
+
#
|
8
|
+
# A boolean value that can be updated atomically. Reads and writes to an atomic
|
9
|
+
# boolean and thread-safe and guaranteed to succeed. Reads and writes may block
|
10
|
+
# briefly but no explicit locking is required.
|
11
|
+
#
|
12
|
+
# Testing with ruby 2.1.2
|
13
|
+
# Testing with Concurrent::MutexAtomicBoolean...
|
14
|
+
# 2.790000 0.000000 2.790000 ( 2.791454)
|
15
|
+
# Testing with Concurrent::CAtomicBoolean...
|
16
|
+
# 0.740000 0.000000 0.740000 ( 0.740206)
|
17
|
+
#
|
18
|
+
# Testing with jruby 1.9.3
|
19
|
+
# Testing with Concurrent::MutexAtomicBoolean...
|
20
|
+
# 5.240000 2.520000 7.760000 ( 3.683000)
|
21
|
+
# Testing with Concurrent::JavaAtomicBoolean...
|
22
|
+
# 3.340000 0.010000 3.350000 ( 0.855000)
|
23
|
+
#
|
24
|
+
# @see http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/atomic/AtomicBoolean.html java.util.concurrent.atomic.AtomicBoolean
|
25
|
+
class MutexAtomicBoolean
|
26
|
+
|
27
|
+
# @!macro [attach] atomic_boolean_method_initialize
|
28
|
+
#
|
29
|
+
# Creates a new `AtomicBoolean` with the given initial value.
|
30
|
+
#
|
31
|
+
# @param [Boolean] initial the initial value
|
32
|
+
def initialize(initial = false)
|
33
|
+
@value = !!initial
|
34
|
+
@mutex = Mutex.new
|
35
|
+
end
|
36
|
+
|
37
|
+
# @!macro [attach] atomic_boolean_method_value_get
|
38
|
+
#
|
39
|
+
# Retrieves the current `Boolean` value.
|
40
|
+
#
|
41
|
+
# @return [Boolean] the current value
|
42
|
+
def value
|
43
|
+
@mutex.lock
|
44
|
+
@value
|
45
|
+
ensure
|
46
|
+
@mutex.unlock
|
47
|
+
end
|
48
|
+
|
49
|
+
# @!macro [attach] atomic_boolean_method_value_set
|
50
|
+
#
|
51
|
+
# Explicitly sets the value.
|
52
|
+
#
|
53
|
+
# @param [Boolean] value the new value to be set
|
54
|
+
#
|
55
|
+
# @return [Boolean] the current value
|
56
|
+
def value=(value)
|
57
|
+
@mutex.lock
|
58
|
+
@value = !!value
|
59
|
+
@value
|
60
|
+
ensure
|
61
|
+
@mutex.unlock
|
62
|
+
end
|
63
|
+
|
64
|
+
# @!macro [attach] atomic_boolean_method_true_question
|
65
|
+
#
|
66
|
+
# Is the current value `true`
|
67
|
+
#
|
68
|
+
# @return [Boolean] true if the current value is `true`, else false
|
69
|
+
def true?
|
70
|
+
@mutex.lock
|
71
|
+
@value
|
72
|
+
ensure
|
73
|
+
@mutex.unlock
|
74
|
+
end
|
75
|
+
|
76
|
+
# @!macro atomic_boolean_method_false_question
|
77
|
+
#
|
78
|
+
# Is the current value `false`
|
79
|
+
#
|
80
|
+
# @return [Boolean] true if the current value is `false`, else false
|
81
|
+
def false?
|
82
|
+
@mutex.lock
|
83
|
+
!@value
|
84
|
+
ensure
|
85
|
+
@mutex.unlock
|
86
|
+
end
|
87
|
+
|
88
|
+
# @!macro [attach] atomic_boolean_method_make_true
|
89
|
+
#
|
90
|
+
# Explicitly sets the value to true.
|
91
|
+
#
|
92
|
+
# @return [Boolean] true is value has changed, otherwise false
|
93
|
+
def make_true
|
94
|
+
@mutex.lock
|
95
|
+
old = @value
|
96
|
+
@value = true
|
97
|
+
!old
|
98
|
+
ensure
|
99
|
+
@mutex.unlock
|
100
|
+
end
|
101
|
+
|
102
|
+
# @!macro [attach] atomic_boolean_method_make_false
|
103
|
+
#
|
104
|
+
# Explicitly sets the value to false.
|
105
|
+
#
|
106
|
+
# @return [Boolean] true is value has changed, otherwise false
|
107
|
+
def make_false
|
108
|
+
@mutex.lock
|
109
|
+
old = @value
|
110
|
+
@value = false
|
111
|
+
old
|
112
|
+
ensure
|
113
|
+
@mutex.unlock
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
if RUBY_PLATFORM == 'java'
|
118
|
+
|
119
|
+
# @!macro atomic_boolean
|
120
|
+
class JavaAtomicBoolean
|
121
|
+
|
122
|
+
# @!macro atomic_boolean_method_initialize
|
123
|
+
#
|
124
|
+
def initialize(initial = false)
|
125
|
+
@atomic = java.util.concurrent.atomic.AtomicBoolean.new(!!initial)
|
126
|
+
end
|
127
|
+
|
128
|
+
# @!macro atomic_boolean_method_value_get
|
129
|
+
#
|
130
|
+
def value
|
131
|
+
@atomic.get
|
132
|
+
end
|
133
|
+
|
134
|
+
# @!macro atomic_boolean_method_value_set
|
135
|
+
#
|
136
|
+
def value=(value)
|
137
|
+
@atomic.set(!!value)
|
138
|
+
end
|
139
|
+
|
140
|
+
# @!macro atomic_boolean_method_true_question
|
141
|
+
def true?
|
142
|
+
@atomic.get
|
143
|
+
end
|
144
|
+
|
145
|
+
# @!macro atomic_boolean_method_false_question
|
146
|
+
def false?
|
147
|
+
!@atomic.get
|
148
|
+
end
|
149
|
+
|
150
|
+
# @!macro atomic_boolean_method_make_true
|
151
|
+
def make_true
|
152
|
+
@atomic.compareAndSet(false, true)
|
153
|
+
end
|
154
|
+
|
155
|
+
# @!macro atomic_boolean_method_make_false
|
156
|
+
def make_false
|
157
|
+
@atomic.compareAndSet(true, false)
|
158
|
+
end
|
159
|
+
end
|
160
|
+
|
161
|
+
# @!macro atomic_boolean
|
162
|
+
class AtomicBoolean < JavaAtomicBoolean
|
163
|
+
end
|
164
|
+
|
165
|
+
elsif Concurrent.allow_c_native_class?('CAtomicBoolean')
|
166
|
+
|
167
|
+
# @!macro atomic_boolean
|
168
|
+
class CAtomicBoolean
|
169
|
+
|
170
|
+
# @!method initialize
|
171
|
+
# @!macro atomic_boolean_method_initialize
|
172
|
+
|
173
|
+
# @!method value
|
174
|
+
# @!macro atomic_boolean_method_value_get
|
175
|
+
|
176
|
+
# @!method value=
|
177
|
+
# @!macro atomic_boolean_method_value_set
|
178
|
+
|
179
|
+
# @!method true?
|
180
|
+
# @!macro atomic_boolean_method_true_question
|
181
|
+
|
182
|
+
# @!method false?
|
183
|
+
# @!macro atomic_boolean_method_false_question
|
184
|
+
|
185
|
+
# @!method make_true
|
186
|
+
# @!macro atomic_boolean_method_make_true
|
187
|
+
|
188
|
+
# @!method make_false
|
189
|
+
# @!macro atomic_boolean_method_make_false
|
190
|
+
end
|
191
|
+
|
192
|
+
# @!macro atomic_boolean
|
193
|
+
class AtomicBoolean < CAtomicBoolean
|
194
|
+
end
|
195
|
+
|
196
|
+
else
|
197
|
+
|
198
|
+
# @!macro atomic_boolean
|
199
|
+
class AtomicBoolean < MutexAtomicBoolean
|
200
|
+
end
|
201
|
+
end
|
202
|
+
end
|
@@ -0,0 +1,203 @@
|
|
1
|
+
require_relative '../../extension_helper'
|
2
|
+
Concurrent.safe_require_c_extensions
|
3
|
+
|
4
|
+
module Concurrent
|
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.
|
11
|
+
#
|
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)
|
17
|
+
#
|
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)
|
23
|
+
#
|
24
|
+
# @see http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/atomic/AtomicLong.html java.util.concurrent.atomic.AtomicLong
|
25
|
+
class MutexAtomicFixnum
|
26
|
+
|
27
|
+
# http://stackoverflow.com/questions/535721/ruby-max-integer
|
28
|
+
MIN_VALUE = -(2**(0.size * 8 -2))
|
29
|
+
MAX_VALUE = (2**(0.size * 8 -2) -1)
|
30
|
+
|
31
|
+
# @!macro [attach] atomic_fixnum_method_initialize
|
32
|
+
#
|
33
|
+
# Creates a new `AtomicFixnum` with the given initial value.
|
34
|
+
#
|
35
|
+
# @param [Fixnum] init the initial value
|
36
|
+
# @raise [ArgumentError] if the initial value is not a `Fixnum`
|
37
|
+
def initialize(init = 0)
|
38
|
+
raise ArgumentError.new('initial value must be a Fixnum') unless init.is_a?(Fixnum)
|
39
|
+
@value = init
|
40
|
+
@mutex = Mutex.new
|
41
|
+
end
|
42
|
+
|
43
|
+
# @!macro [attach] atomic_fixnum_method_value_get
|
44
|
+
#
|
45
|
+
# Retrieves the current `Fixnum` value.
|
46
|
+
#
|
47
|
+
# @return [Fixnum] the current value
|
48
|
+
def value
|
49
|
+
@mutex.lock
|
50
|
+
@value
|
51
|
+
ensure
|
52
|
+
@mutex.unlock
|
53
|
+
end
|
54
|
+
|
55
|
+
# @!macro [attach] atomic_fixnum_method_value_set
|
56
|
+
#
|
57
|
+
# Explicitly sets the value.
|
58
|
+
#
|
59
|
+
# @param [Fixnum] value the new value to be set
|
60
|
+
#
|
61
|
+
# @return [Fixnum] the current value
|
62
|
+
#
|
63
|
+
# @raise [ArgumentError] if the new value is not a `Fixnum`
|
64
|
+
def value=(value)
|
65
|
+
raise ArgumentError.new('value must be a Fixnum') unless value.is_a?(Fixnum)
|
66
|
+
@mutex.lock
|
67
|
+
@value = value
|
68
|
+
ensure
|
69
|
+
@mutex.unlock
|
70
|
+
end
|
71
|
+
|
72
|
+
# @!macro [attach] atomic_fixnum_method_increment
|
73
|
+
#
|
74
|
+
# Increases the current value by 1.
|
75
|
+
#
|
76
|
+
# @return [Fixnum] the current value after incrementation
|
77
|
+
def increment
|
78
|
+
@mutex.lock
|
79
|
+
@value += 1
|
80
|
+
ensure
|
81
|
+
@mutex.unlock
|
82
|
+
end
|
83
|
+
|
84
|
+
alias_method :up, :increment
|
85
|
+
|
86
|
+
# @!macro [attach] atomic_fixnum_method_decrement
|
87
|
+
#
|
88
|
+
# Decreases the current value by 1.
|
89
|
+
#
|
90
|
+
# @return [Fixnum] the current value after decrementation
|
91
|
+
def decrement
|
92
|
+
@mutex.lock
|
93
|
+
@value -= 1
|
94
|
+
ensure
|
95
|
+
@mutex.unlock
|
96
|
+
end
|
97
|
+
|
98
|
+
alias_method :down, :decrement
|
99
|
+
|
100
|
+
# @!macro [attach] atomic_fixnum_method_compare_and_set
|
101
|
+
#
|
102
|
+
# Atomically sets the value to the given updated value if the current
|
103
|
+
# value == the expected value.
|
104
|
+
#
|
105
|
+
# @param [Fixnum] expect the expected value
|
106
|
+
# @param [Fixnum] update the new value
|
107
|
+
#
|
108
|
+
# @return [Boolean] true if the value was updated else false
|
109
|
+
def compare_and_set(expect, update)
|
110
|
+
@mutex.lock
|
111
|
+
if @value == expect
|
112
|
+
@value = update
|
113
|
+
true
|
114
|
+
else
|
115
|
+
false
|
116
|
+
end
|
117
|
+
ensure
|
118
|
+
@mutex.unlock
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
if RUBY_PLATFORM == 'java'
|
123
|
+
|
124
|
+
# @!macro atomic_fixnum
|
125
|
+
class JavaAtomicFixnum
|
126
|
+
|
127
|
+
MIN_VALUE = Java::JavaLang::Long::MIN_VALUE
|
128
|
+
MAX_VALUE = Java::JavaLang::Long::MAX_VALUE
|
129
|
+
|
130
|
+
# @!macro atomic_fixnum_method_initialize
|
131
|
+
def initialize(init = 0)
|
132
|
+
raise ArgumentError.new('initial value must be a Fixnum') unless init.is_a?(Fixnum)
|
133
|
+
@atomic = java.util.concurrent.atomic.AtomicLong.new(init)
|
134
|
+
end
|
135
|
+
|
136
|
+
# @!macro atomic_fixnum_method_value_get
|
137
|
+
def value
|
138
|
+
@atomic.get
|
139
|
+
end
|
140
|
+
|
141
|
+
# @!macro atomic_fixnum_method_value_set
|
142
|
+
def value=(value)
|
143
|
+
raise ArgumentError.new('value must be a Fixnum') unless value.is_a?(Fixnum)
|
144
|
+
@atomic.set(value)
|
145
|
+
end
|
146
|
+
|
147
|
+
# @!macro atomic_fixnum_method_increment
|
148
|
+
def increment
|
149
|
+
@atomic.increment_and_get
|
150
|
+
end
|
151
|
+
alias_method :up, :increment
|
152
|
+
|
153
|
+
# @!macro atomic_fixnum_method_decrement
|
154
|
+
def decrement
|
155
|
+
@atomic.decrement_and_get
|
156
|
+
end
|
157
|
+
alias_method :down, :decrement
|
158
|
+
|
159
|
+
# @!macro atomic_fixnum_method_compare_and_set
|
160
|
+
def compare_and_set(expect, update)
|
161
|
+
@atomic.compare_and_set(expect, update)
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
# @!macro atomic_fixnum
|
166
|
+
class AtomicFixnum < JavaAtomicFixnum
|
167
|
+
end
|
168
|
+
|
169
|
+
elsif Concurrent.allow_c_native_class?('CAtomicFixnum')
|
170
|
+
|
171
|
+
# @!macro atomic_fixnum
|
172
|
+
class CAtomicFixnum
|
173
|
+
|
174
|
+
# @!method initialize
|
175
|
+
# @!macro atomic_fixnum_method_initialize
|
176
|
+
|
177
|
+
# @!method value
|
178
|
+
# @!macro atomic_fixnum_method_value_get
|
179
|
+
|
180
|
+
# @!method value=
|
181
|
+
# @!macro atomic_fixnum_method_value_set
|
182
|
+
|
183
|
+
# @!method increment
|
184
|
+
# @!macro atomic_fixnum_method_increment
|
185
|
+
|
186
|
+
# @!method decrement
|
187
|
+
# @!macro atomic_fixnum_method_decrement
|
188
|
+
|
189
|
+
# @!method compare_and_set
|
190
|
+
# @!macro atomic_fixnum_method_compare_and_set
|
191
|
+
end
|
192
|
+
|
193
|
+
# @!macro atomic_fixnum
|
194
|
+
class AtomicFixnum < CAtomicFixnum
|
195
|
+
end
|
196
|
+
|
197
|
+
else
|
198
|
+
|
199
|
+
# @!macro atomic_fixnum
|
200
|
+
class AtomicFixnum < MutexAtomicFixnum
|
201
|
+
end
|
202
|
+
end
|
203
|
+
end
|