Skip to content

Commit 5752ab5

Browse files
committed
AtomicReference code cleanup
1 parent 0a51fb3 commit 5752ab5

File tree

7 files changed

+50
-72
lines changed

7 files changed

+50
-72
lines changed

lib/concurrent/atomic/atomic_reference.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ class ConcurrentUpdateError < ThreadError
144144
class CAtomicReference
145145
include AtomicDirectUpdate
146146
include AtomicNumericCompareAndSetWrapper
147+
alias_method :compare_and_swap, :compare_and_set
147148
end
148149
CAtomicReference
149150
when Concurrent.on_jruby?
@@ -156,6 +157,8 @@ class JavaAtomicReference
156157
when Concurrent.on_truffleruby?
157158
class TruffleRubyAtomicReference < Truffle::AtomicReference
158159
include AtomicDirectUpdate
160+
alias_method :compare_and_swap, :compare_and_set
161+
alias_method :swap, :get_and_set
159162
end
160163
when Concurrent.on_rbx?
161164
# @note Extends `Rubinius::AtomicReference` version adding aliases
@@ -164,12 +167,13 @@ class TruffleRubyAtomicReference < Truffle::AtomicReference
164167
# @!visibility private
165168
# @!macro internal_implementation_note
166169
class RbxAtomicReference < Rubinius::AtomicReference
167-
alias _compare_and_set compare_and_set
170+
alias_method :_compare_and_set, :compare_and_set
168171
include AtomicDirectUpdate
169172
include AtomicNumericCompareAndSetWrapper
170173
alias_method :value, :get
171174
alias_method :value=, :set
172175
alias_method :swap, :get_and_set
176+
alias_method :compare_and_swap, :compare_and_set
173177
end
174178
RbxAtomicReference
175179
else

lib/concurrent/atomic_reference/mutex_atomic.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module Concurrent
55
class MutexAtomicReference < Synchronization::LockableObject
66
include AtomicDirectUpdate
77
include AtomicNumericCompareAndSetWrapper
8+
alias_method :compare_and_swap, :compare_and_set
89

910
# @!macro atomic_reference_method_initialize
1011
def initialize(value = nil)

lib/concurrent/atomic_reference/numeric_cas_wrapper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@ def compare_and_set(old_value, new_value)
2323
_compare_and_set(old_value, new_value)
2424
end
2525
end
26-
alias_method :compare_and_swap, :compare_and_set
26+
2727
end
2828
end

spec/concurrent/atomic/atomic_boolean_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ module Concurrent
142142

143143
if defined? Concurrent::CAtomicBoolean
144144

145-
RSpec.describe CAtomicBoolean, ext: true do
145+
RSpec.describe CAtomicBoolean do
146146
it_should_behave_like :atomic_boolean
147147
end
148148
end
@@ -166,7 +166,7 @@ module Concurrent
166166
expect(AtomicBoolean.ancestors).to include(JavaAtomicBoolean)
167167
end
168168
elsif defined? Concurrent::CAtomicBoolean
169-
it 'inherits from CAtomicBoolean', ext: true do
169+
it 'inherits from CAtomicBoolean' do
170170
expect(AtomicBoolean.ancestors).to include(CAtomicBoolean)
171171
end
172172
else

spec/concurrent/atomic/atomic_fixnum_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ module Concurrent
204204

205205
if defined? Concurrent::CAtomicFixnum
206206

207-
RSpec.describe CAtomicFixnum, ext: true do
207+
RSpec.describe CAtomicFixnum do
208208
it_should_behave_like :atomic_fixnum
209209
end
210210
end
@@ -228,7 +228,7 @@ module Concurrent
228228
expect(AtomicFixnum.ancestors).to include(JavaAtomicFixnum)
229229
end
230230
elsif defined? Concurrent::CAtomicFixnum
231-
it 'inherits from CAtomicFixnum', ext: true do
231+
it 'inherits from CAtomicFixnum' do
232232
expect(AtomicFixnum.ancestors).to include(CAtomicFixnum)
233233
end
234234
else

spec/concurrent/atomic/atomic_reference_spec.rb

Lines changed: 39 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
end
1010

1111
specify :test_value do
12-
atomic = described_class.new(0)
12+
atomic = described_class.new(0)
1313
atomic.value = 1
1414

1515
expect(atomic.value).to eq 1
@@ -18,7 +18,7 @@
1818
specify :test_update do
1919
# use a number outside JRuby's fixnum cache range, to ensure identity is preserved
2020
atomic = described_class.new(1000)
21-
res = atomic.update {|v| v + 1}
21+
res = atomic.update { |v| v + 1 }
2222

2323
expect(atomic.value).to eq 1001
2424
expect(res).to eq 1001
@@ -27,7 +27,7 @@
2727
specify :test_try_update do
2828
# use a number outside JRuby's fixnum cache range, to ensure identity is preserved
2929
atomic = described_class.new(1000)
30-
res = atomic.try_update {|v| v + 1}
30+
res = atomic.try_update { |v| v + 1 }
3131

3232
expect(atomic.value).to eq 1001
3333
expect(res).to eq 1001
@@ -36,15 +36,15 @@
3636
specify :test_try_update_bang do
3737
# use a number outside JRuby's fixnum cache range, to ensure identity is preserved
3838
atomic = described_class.new(1000)
39-
res = atomic.try_update! {|v| v + 1}
39+
res = atomic.try_update! { |v| v + 1 }
4040

4141
expect(atomic.value).to eq 1001
4242
expect(res).to eq 1001
4343
end
4444

4545
specify :test_swap do
4646
atomic = described_class.new(1000)
47-
res = atomic.swap(1001)
47+
res = atomic.swap(1001)
4848

4949
expect(atomic.value).to eq 1001
5050
expect(res).to eq 1000
@@ -54,8 +54,8 @@
5454
# use a number outside JRuby's fixnum cache range, to ensure identity is preserved
5555
atomic = described_class.new(1000)
5656
expect(
57-
# assigning within block exploits implementation detail for test
58-
atomic.try_update {|v| atomic.value = 1001 ; v + 1}
57+
# assigning within block exploits implementation detail for test
58+
atomic.try_update { |v| atomic.value = 1001; v + 1 }
5959
).to be_falsey
6060
end
6161

@@ -64,7 +64,7 @@
6464
atomic = described_class.new(1000)
6565
expect {
6666
# assigning within block exploits implementation detail for test
67-
atomic.try_update! {|v| atomic.value = 1001 ; v + 1}
67+
atomic.try_update! { |v| atomic.value = 1001; v + 1 }
6868
}.to raise_error Concurrent::ConcurrentUpdateError
6969
end
7070

@@ -73,7 +73,7 @@
7373
# use a number outside JRuby's fixnum cache range, to ensure identity is preserved
7474
atomic = described_class.new(1000)
7575
# assigning within block exploits implementation detail for test
76-
atomic.update{|v| tries += 1 ; atomic.value = 1001 ; v + 1}
76+
atomic.update { |v| tries += 1; atomic.value = 1001; v + 1 }
7777

7878
expect(tries).to eq 2
7979
end
@@ -82,64 +82,64 @@
8282
atomic = described_class.new(0)
8383

8484
# 9-bit idempotent Fixnum (JRuby)
85-
max_8 = 2**256 - 1
86-
min_8 = -(2**256)
85+
max_8 = 2 ** 256 - 1
86+
min_8 = -(2 ** 256)
8787

8888
atomic.set(max_8)
8989
max_8.upto(max_8 + 2) do |i|
90-
expect(atomic.compare_and_swap(i, i+1)).to be_truthy, "CAS failed for numeric #{i} => #{i + 1}"
90+
expect(atomic.compare_and_swap(i, i + 1)).to be_truthy, "CAS failed for numeric #{i} => #{i + 1}"
9191
end
9292

9393
atomic.set(min_8)
9494
min_8.downto(min_8 - 2) do |i|
95-
expect(atomic.compare_and_swap(i, i-1)).to be_truthy, "CAS failed for numeric #{i} => #{i - 1}"
95+
expect(atomic.compare_and_swap(i, i - 1)).to be_truthy, "CAS failed for numeric #{i} => #{i - 1}"
9696
end
9797

9898
# 64-bit idempotent Fixnum (MRI, Rubinius)
99-
max_64 = 2**62 - 1
100-
min_64 = -(2**62)
99+
max_64 = 2 ** 62 - 1
100+
min_64 = -(2 ** 62)
101101

102102
atomic.set(max_64)
103103
max_64.upto(max_64 + 2) do |i|
104-
expect(atomic.compare_and_swap(i, i+1)).to be_truthy, "CAS failed for numeric #{i} => #{i + 1}"
104+
expect(atomic.compare_and_swap(i, i + 1)).to be_truthy, "CAS failed for numeric #{i} => #{i + 1}"
105105
end
106106

107107
atomic.set(min_64)
108108
min_64.downto(min_64 - 2) do |i|
109-
expect(atomic.compare_and_swap(i, i-1)).to be_truthy, "CAS failed for numeric #{i} => #{i - 1}"
109+
expect(atomic.compare_and_swap(i, i - 1)).to be_truthy, "CAS failed for numeric #{i} => #{i - 1}"
110110
end
111111

112112
## 64-bit overflow into Bignum (JRuby)
113-
max_64 = 2**63 - 1
114-
min_64 = (-2**63)
113+
max_64 = 2 ** 63 - 1
114+
min_64 = (-2 ** 63)
115115

116116
atomic.set(max_64)
117117
max_64.upto(max_64 + 2) do |i|
118-
expect(atomic.compare_and_swap(i, i+1)).to be_truthy, "CAS failed for numeric #{i} => #{i + 1}"
118+
expect(atomic.compare_and_swap(i, i + 1)).to be_truthy, "CAS failed for numeric #{i} => #{i + 1}"
119119
end
120120

121121
atomic.set(min_64)
122122
min_64.downto(min_64 - 2) do |i|
123-
expect(atomic.compare_and_swap(i, i-1)).to be_truthy, "CAS failed for numeric #{i} => #{i - 1}"
123+
expect(atomic.compare_and_swap(i, i - 1)).to be_truthy, "CAS failed for numeric #{i} => #{i - 1}"
124124
end
125125

126126
# non-idempotent Float (JRuby, Rubinius, MRI < 2.0.0 or 32-bit)
127127
atomic.set(1.0 + 0.1)
128128
expect(atomic.compare_and_set(1.0 + 0.1, 1.2)).to be_truthy, "CAS failed for #{1.0 + 0.1} => 1.2"
129129

130130
# Bignum
131-
atomic.set(2**100)
132-
expect(atomic.compare_and_set(2**100, 0)).to be_truthy, "CAS failed for #{2**100} => 0"
131+
atomic.set(2 ** 100)
132+
expect(atomic.compare_and_set(2 ** 100, 0)).to be_truthy, "CAS failed for #{2 ** 100} => 0"
133133

134134
# Rational
135135
require 'rational' unless ''.respond_to? :to_r
136-
atomic.set(Rational(1,3))
137-
expect(atomic.compare_and_set(Rational(1,3), 0)).to be_truthy, "CAS failed for #{Rational(1,3)} => 0"
136+
atomic.set(Rational(1, 3))
137+
expect(atomic.compare_and_set(Rational(1, 3), 0)).to be_truthy, "CAS failed for #{Rational(1, 3)} => 0"
138138

139139
# Complex
140140
require 'complex' unless ''.respond_to? :to_c
141-
atomic.set(Complex(1,2))
142-
expect(atomic.compare_and_set(Complex(1,2), 0)).to be_truthy, "CAS failed for #{Complex(1,2)} => 0"
141+
atomic.set(Complex(1, 2))
142+
expect(atomic.compare_and_set(Complex(1, 2), 0)).to be_truthy, "CAS failed for #{Complex(1, 2)} => 0"
143143
end
144144
end
145145

@@ -162,53 +162,22 @@ module Concurrent
162162
end
163163

164164
if defined? Concurrent::CAtomicReference
165-
RSpec.describe CAtomicReference, ext: true do
165+
RSpec.describe CAtomicReference do
166166
it_should_behave_like :atomic_reference
167167
end
168-
elsif defined? Concurrent::JavaAtomicReference
168+
end
169+
if defined? Concurrent::JavaAtomicReference
169170
RSpec.describe JavaAtomicReference do
170171
it_should_behave_like :atomic_reference
171172
end
172-
elsif defined? Concurrent::RbxAtomicReference
173+
end
174+
if defined? Concurrent::RbxAtomicReference
173175
RSpec.describe RbxAtomicReference do
174176
it_should_behave_like :atomic_reference
175177
end
176178
end
177-
178-
RSpec.describe AtomicReference do
179-
if Concurrent.on_jruby?
180-
it 'inherits from JavaAtomicReference' do
181-
expect(AtomicReference.ancestors).to include(Concurrent::JavaAtomicReference)
182-
end
183-
elsif Concurrent.allow_c_extensions?
184-
it 'inherits from CAtomicReference' do
185-
expect(AtomicReference.ancestors).to include(Concurrent::CAtomicReference)
186-
end
187-
elsif Concurrent.on_rbx?
188-
it 'inherits from RbxAtomicReference' do
189-
expect(AtomicReference.ancestors).to include(Concurrent::RbxAtomicReference)
190-
end
191-
else
192-
it 'inherits from MutexAtomicReference' do
193-
expect(AtomicReference.ancestors).to include(Concurrent::MutexAtomicReference)
194-
end
195-
end
196-
end
197-
198-
RSpec.describe MutexAtomicReference do
199-
it_should_behave_like :atomic_reference
200-
end
201-
202-
if defined? Concurrent::CAtomicReference
203-
RSpec.describe CAtomicReference, ext: true do
204-
it_should_behave_like :atomic_reference
205-
end
206-
elsif defined? Concurrent::JavaAtomicReference
207-
RSpec.describe JavaAtomicReference do
208-
it_should_behave_like :atomic_reference
209-
end
210-
elsif defined? Concurrent::RbxAtomicReference
211-
RSpec.describe RbxAtomicReference do
179+
if defined? Concurrent::TruffleRubyAtomicReference
180+
RSpec.describe TruffleRubyAtomicReference do
212181
it_should_behave_like :atomic_reference
213182
end
214183
end
@@ -219,13 +188,17 @@ module Concurrent
219188
expect(described_class.ancestors).to include(Concurrent::JavaAtomicReference)
220189
end
221190
elsif Concurrent.allow_c_extensions?
222-
it 'inherits from CAtomicReference', ext: true do
191+
it 'inherits from CAtomicReference' do
223192
expect(described_class.ancestors).to include(Concurrent::CAtomicReference)
224193
end
225194
elsif Concurrent.on_rbx?
226195
it 'inherits from RbxAtomicReference' do
227196
expect(described_class.ancestors).to include(Concurrent::RbxAtomicReference)
228197
end
198+
elsif Concurrent.on_truffleruby?
199+
it 'inherits from TruffleRubyAtomicReference' do
200+
expect(described_class.ancestors).to include(Concurrent::TruffleRubyAtomicReference)
201+
end
229202
else
230203
it 'inherits from MutexAtomicReference' do
231204
expect(described_class.ancestors).to include(Concurrent::MutexAtomicReference)

0 commit comments

Comments
 (0)