module Garcon::AtomicDirectUpdate

Define update methods that use direct paths

Public Instance Methods

try_update() { |old_value| ... } click to toggle source

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

@yield [Object]

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

@yieldparam [Object] old_value

The starting value of the atomic reference.

@raise [Garcon::ConcurrentUpdateError]

If the update fails

@return [Object] the new value

# File lib/garcon/task/atomic.rb, line 51
def try_update
  old_value = get
  new_value = yield old_value
  unless compare_and_set(old_value, new_value)
    raise ConcurrentUpdateError, "Update failed"
  end
  new_value
end
update() { |old_value| ... } click to toggle source

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.

@yield [Object]

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

@yieldparam [Object] old_value

The starting value of the atomic reference

@return [Object]

The new value
# File lib/garcon/task/atomic.rb, line 34
def update
  true until compare_and_set(old_value = get, new_value = yield(old_value))
  new_value
end