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
-
#try_update {|Object| ... } ⇒ Object
Pass the current value to the given block, replacing it with the block’s result.
-
#update {|Object| ... } ⇒ Object
Pass the current value to the given block, replacing it with the block’s result.
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.
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.
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 |