Module: Garcon::AtomicDirectUpdate

Included in:
AtomicMutex
Defined in:
lib/garcon/task/atomic.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:

Raises:

  • (Garcon::ConcurrentUpdateError)

    If the update fails



51
52
53
54
55
56
57
58
# 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 {|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:



34
35
36
37
# 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