Skip to content

Commit 0ff56ff

Browse files
committed
Fixed compare_and_set on MutexAtomicReference.
The pure-Ruby version would return false if the lock could not be obtained. This is distinctly different behavior from the C and Java implementations which only return false if the values did not match. This update brings parity to all implementations.
1 parent 2b868ae commit 0ff56ff

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

lib/concurrent/atomic_reference/mutex_atomic.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,14 @@ def get_and_set(new_value)
4242

4343
# @!macro atomic_reference_method_compare_and_set
4444
def _compare_and_set(old_value, new_value)
45-
return false unless @mutex.try_lock
46-
begin
47-
return false unless @value.equal? old_value
48-
@value = new_value
49-
ensure
50-
@mutex.unlock
45+
@mutex.synchronize do
46+
if @value == old_value
47+
@value = new_value
48+
true
49+
else
50+
false
51+
end
5152
end
52-
true
5353
end
5454
end
5555
end

0 commit comments

Comments
 (0)