1+ require 'concurrent/atomic/mutex_atomic_fixnum'
12require 'concurrent/utility/native_extension_loader'
2- require 'concurrent/synchronization'
33
44module Concurrent
55
6- # @!macro [attach] atomic_fixnum
6+ ###################################################################
7+
8+ # @!macro [new] atomic_fixnum_method_initialize
79 #
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.
10+ # Creates a new `AtomicFixnum` with the given initial value.
1111 #
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)
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
2316 #
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.
2518 #
26- # @!visibility private
27- # @!macro internal_implementation_note
28- class MutexAtomicFixnum < Synchronization ::Object
29-
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
19+ # @return [Fixnum] the current value
6620
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`
9230
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
11238
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
12946
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
13156
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
13668
137- private
69+ ###################################################################
13870
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
14493
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+ ###################################################################
15895
15996 # @!visibility private
16097 # @!macro internal_implementation_note
@@ -168,28 +105,28 @@ def range_check!(value)
168105 end
169106 private_constant :AtomicFixnumImplementation
170107
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+ # Testing with ruby 2.1.2
115+ # Testing with Concurrent::MutexAtomicFixnum...
116+ # 3.130000 0.000000 3.130000 ( 3.136505)
117+ # Testing with Concurrent::CAtomicFixnum...
118+ # 0.790000 0.000000 0.790000 ( 0.785550)
172119 #
173- # @see Concurrent::MutexAtomicFixnum
120+ # Testing with jruby 1.9.3
121+ # Testing with Concurrent::MutexAtomicFixnum...
122+ # 5.460000 2.460000 7.920000 ( 3.715000)
123+ # Testing with Concurrent::JavaAtomicFixnum...
124+ # 4.520000 0.030000 4.550000 ( 1.187000)
125+ #
126+ # @see http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/atomic/AtomicLong.html java.util.concurrent.atomic.AtomicLong
127+ #
128+ # @!macro atomic_fixnum_public_api
174129 class AtomicFixnum < AtomicFixnumImplementation
175130
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-
194131 end
195132end
0 commit comments