Skip to content

Commit 502d5d1

Browse files
committed
Moved atomic fixnum classes into own files.
1 parent f31e7ac commit 502d5d1

File tree

3 files changed

+189
-164
lines changed

3 files changed

+189
-164
lines changed

lib/concurrent/atomic/atomic_boolean.rb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,7 @@ module Concurrent
108108
#
109109
# @see http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/atomic/AtomicBoolean.html java.util.concurrent.atomic.AtomicBoolean
110110
#
111-
# @see Concurrent::MutexAtomicBoolean
112-
#
113111
# @!macro atomic_boolean_public_api
114112
class AtomicBoolean < AtomicBooleanImplementation
115-
116113
end
117114
end
Lines changed: 98 additions & 161 deletions
Original file line numberDiff line numberDiff line change
@@ -1,160 +1,97 @@
1+
require 'concurrent/atomic/mutex_atomic_fixnum'
12
require 'concurrent/utility/native_extension_loader'
2-
require 'concurrent/synchronization'
33

44
module 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
195132
end
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
require 'concurrent/synchronization'
2+
3+
module Concurrent
4+
5+
# @!macro atomic_fixnum
6+
# @!visibility private
7+
# @!macro internal_implementation_note
8+
class MutexAtomicFixnum < Synchronization::Object
9+
10+
# http://stackoverflow.com/questions/535721/ruby-max-integer
11+
MIN_VALUE = -(2**(0.size * 8 - 2))
12+
MAX_VALUE = (2**(0.size * 8 - 2) - 1)
13+
14+
# @!macro atomic_fixnum_method_initialize
15+
def initialize(initial = 0)
16+
super()
17+
synchronize { ns_initialize(initial) }
18+
end
19+
20+
# @!macro atomic_fixnum_method_value_get
21+
def value
22+
synchronize { @value }
23+
end
24+
25+
# @!macro atomic_fixnum_method_value_set
26+
def value=(value)
27+
synchronize { ns_set(value) }
28+
end
29+
30+
# @!macro atomic_fixnum_method_increment
31+
def increment(delta = 1)
32+
synchronize { ns_set(@value + delta.to_i) }
33+
end
34+
35+
alias_method :up, :increment
36+
37+
# @!macro atomic_fixnum_method_decrement
38+
def decrement(delta = 1)
39+
synchronize { ns_set(@value - delta.to_i) }
40+
end
41+
42+
alias_method :down, :decrement
43+
44+
# @!macro atomic_fixnum_method_compare_and_set
45+
def compare_and_set(expect, update)
46+
synchronize do
47+
if @value == expect.to_i
48+
@value = update.to_i
49+
true
50+
else
51+
false
52+
end
53+
end
54+
end
55+
56+
# @!macro atomic_fixnum_method_update
57+
def update
58+
synchronize do
59+
@value = yield @value
60+
end
61+
end
62+
63+
protected
64+
65+
# @!visibility private
66+
def ns_initialize(initial)
67+
ns_set(initial)
68+
end
69+
70+
private
71+
72+
# @!visibility private
73+
def ns_set(value)
74+
range_check!(value)
75+
@value = value
76+
end
77+
78+
# @!visibility private
79+
def range_check!(value)
80+
if !value.is_a?(Fixnum)
81+
raise ArgumentError.new('value value must be a Fixnum')
82+
elsif value > MAX_VALUE
83+
raise RangeError.new("#{value} is greater than the maximum value of #{MAX_VALUE}")
84+
elsif value < MIN_VALUE
85+
raise RangeError.new("#{value} is less than the maximum value of #{MIN_VALUE}")
86+
else
87+
value
88+
end
89+
end
90+
end
91+
end

0 commit comments

Comments
 (0)