Skip to content

Commit 6f663b3

Browse files
committed
AtomicMarkableReference: update try_update/try_update! APIs
1 parent 53c0422 commit 6f663b3

File tree

2 files changed

+9
-13
lines changed

2 files changed

+9
-13
lines changed

lib/concurrent/edge/atomic_markable_reference.rb

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,7 @@ def compare_and_set(expected_val, new_val, expected_mark, new_mark)
4747

4848
prospect = ImmutableArray[new_val, new_mark]
4949

50-
# If we guarantee internally that `current` will never be a Numeric, we
51-
# can skip a type check in `compare_and_set` and directly call
52-
# `_compare_and_set`. This is possible since we always internally wrap
53-
# the users `value` and `mark` in an ImmutableArray.
54-
@Reference._compare_and_set current, prospect
50+
@Reference.compare_and_set current, prospect
5551
end
5652
alias_method :compare_and_swap, :compare_and_set
5753

@@ -119,7 +115,7 @@ def update
119115
end
120116
end
121117

122-
# @!macro [attach] atomic_markable_reference_method_try_update
118+
# @!macro [attach] atomic_markable_reference_method_try_update!
123119
#
124120
# Pass the current value to the given block, replacing it
125121
# with the block's result. Raise an exception if the update
@@ -133,7 +129,7 @@ def update
133129
# @return [ImmutableArray] the new value and marked state
134130
#
135131
# @raise [Concurrent::ConcurrentUpdateError] if the update fails
136-
def try_update
132+
def try_update!
137133
old_val, old_mark = @Reference.get
138134
new_val, new_mark = yield old_val, old_mark
139135

@@ -147,7 +143,7 @@ def try_update
147143
ImmutableArray[new_val, new_mark]
148144
end
149145

150-
# @!macro [attach] atomic_markable_reference_method_try_update_no_exception
146+
# @!macro [attach] atomic_markable_reference_method_try_update
151147
#
152148
# Pass the current value to the given block, replacing it with the
153149
# block's result. Simply return nil if update fails.
@@ -159,7 +155,7 @@ def try_update
159155
#
160156
# @return [ImmutableArray] the new value and marked state, or nil if
161157
# the update failed
162-
def try_update_no_exception
158+
def try_update
163159
old_val, old_mark = @Reference.get
164160
new_val, new_mark = yield old_val, old_mark
165161

spec/concurrent/atomic/atomic_markable_reference_spec.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@
2626
end
2727
end
2828

29-
describe '#try_update' do
29+
describe '#try_update!' do
3030
it 'updates the value and mark' do
31-
val, mark = subject.try_update { |v, m| [v + 1, !m] }
31+
val, mark = subject.try_update! { |v, m| [v + 1, !m] }
3232

3333
expect(subject.value).to eq 1001
3434
expect(val).to eq 1001
@@ -37,15 +37,15 @@
3737

3838
it 'raises ConcurrentUpdateError when attempting to set inside of block' do
3939
expect do
40-
subject.try_update do |v, m|
40+
subject.try_update! do |v, m|
4141
subject.set(1001, false)
4242
[v + 1, !m]
4343
end
4444
end.to raise_error Concurrent::ConcurrentUpdateError
4545
end
4646
end
4747

48-
describe '#try_update_no_exception' do
48+
describe '#try_update' do
4949
it 'updates the value and mark' do
5050
val, mark = subject.try_update { |v, m| [v + 1, !m] }
5151

0 commit comments

Comments
 (0)