class Concurrent::AtomicMarkableReference
def compare_and_set(expected_val, new_val, expected_mark, new_mark)
-
(Boolean)
- `true` if successful. A `false` return indicates
Parameters:
-
new_mark
(Boolean
) -- the new mark -
expected_mark
(Boolean
) -- the expected mark -
new_val
(Object
) -- the new value -
expected_val
(Object
) -- the expected value
def compare_and_set(expected_val, new_val, expected_mark, new_mark) # Memoize a valid reference to the current AtomicReference for # later comparison. current = reference curr_val, curr_mark = current # Ensure that that the expected marks match. return false unless expected_mark == curr_mark if expected_val.is_a? Numeric # If the object is a numeric, we need to ensure we are comparing # the numerical values return false unless expected_val == curr_val else # Otherwise, we need to ensure we are comparing the object identity. # Theoretically, this could be incorrect if a user monkey-patched # `Object#equal?`, but they should know that they are playing with # fire at that point. return false unless expected_val.equal? curr_val end prospect = immutable_array(new_val, new_mark) compare_and_set_reference current, prospect end