Module: Concurrent::AtomicDirectUpdate

Defined in:
lib/concurrent/atomic_reference/direct_update.rb

Overview

Define update methods that use direct paths

Instance Method Summary collapse

Instance Method Details

#try_update {|Object| ... } ⇒ Object

Pass the current value to the given block, replacing it with the block’s result. Raise an exception if the update fails.

Yields:

  • (Object)

    Calculate a new value for the atomic reference using given (old) value

Yield Parameters:

  • old_value (Object)

    the starting value of the atomic reference

Returns:

  • (Object)

    the new value

Raises:



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/concurrent/atomic_reference/direct_update.rb', line 37

def try_update
  old_value = get
  new_value = yield old_value
  unless compare_and_set(old_value, new_value)
    if $VERBOSE
      raise ConcurrentUpdateError, "Update failed"
    else
      raise ConcurrentUpdateError, "Update failed", ConcurrentUpdateError::CONC_UP_ERR_BACKTRACE
    end
  end
  new_value
end

#update {|Object| ... } ⇒ Object

Pass the current value to the given block, replacing it with the block’s result. May retry if the value changes during the block’s execution.

Yields:

  • (Object)

    Calculate a new value for the atomic reference using given (old) value

Yield Parameters:

  • old_value (Object)

    the starting value of the atomic reference

Returns:

  • (Object)

    the new value



19
20
21
22
# File 'lib/concurrent/atomic_reference/direct_update.rb', line 19

def update
  true until compare_and_set(old_value = get, new_value = yield(old_value))
  new_value
end