123456789_123456789_123456789_123456789_123456789_

Module: Concurrent::AtomicNumericCompareAndSetWrapper

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Included In:
Defined in: lib/concurrent-ruby/concurrent/atomic_reference/numeric_cas_wrapper.rb

Overview

Note:

Private Implementation: This abstraction is a private, internal implementation detail. It should never be used directly.

Special "compare and set" handling of numeric values.

Instance Method Summary

Instance Method Details

#compare_and_set(old_value, new_value) ⇒ Boolean Also known as: #compare_and_swap

Atomically sets the value to the given updated value if the current value == the expected value.

that the actual value was not equal to the expected value.

Parameters:

  • old_value (Object)

    the expected value

  • new_value (Object)

    the new value

Returns:

  • (Boolean)

    true if successful. A false return indicates

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/atomic_reference/numeric_cas_wrapper.rb', line 10

def compare_and_set(old_value, new_value)
  if old_value.kind_of? Numeric
    # NaN is never == to itself; match it explicitly so #update can terminate.
    expected_nan = old_value.respond_to?(:nan?) && old_value.nan?
    while true
      old = get

      return false unless old.kind_of? Numeric

      if expected_nan
        return false unless old.respond_to?(:nan?) && old.nan?
      else
        return false unless old == old_value
      end

      result = _compare_and_set(old, new_value)
      return result if result
    end
  else
    _compare_and_set(old_value, new_value)
  end
end

#compare_and_swap(old_value, new_value)

Alias for #compare_and_set.

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/atomic_reference/numeric_cas_wrapper.rb', line 33

alias_method :compare_and_swap, :compare_and_set