class Garcon::AtomicMutex
Public Class Methods
new(value = nil)
click to toggle source
# File lib/garcon/task/atomic.rb, line 87 def initialize(value = nil) @mutex = Mutex.new @value = value end
Public Instance Methods
_compare_and_set(old_value, new_value)
click to toggle source
Atomically sets the value to the given updated value if the current value is equal the expected value.
@param [Object] old_value
The expected value.
@param [Object] new_value
The new value.
@return [Boolean]
`true` if successful, `false` indicates that the actual value was not equal to the expected value.
# File lib/garcon/task/atomic.rb, line 140 def _compare_and_set(old_value, new_value) return false unless @mutex.try_lock begin return false unless @value.equal? old_value @value = new_value ensure @mutex.unlock end true end
get()
click to toggle source
Gets the current value.
@return [Object]
The current value.
# File lib/garcon/task/atomic.rb, line 96 def get @mutex.synchronize { @value } end
Also aliased as: value
get_and_set(new_value)
click to toggle source
Atomically sets to the given value and returns the old value.
@param [Object] value
The new value to set.
@return [Object]
The old value.
# File lib/garcon/task/atomic.rb, line 120 def get_and_set(new_value) @mutex.synchronize do old_value = @value @value = new_value old_value end end
Also aliased as: swap
set(value)
click to toggle source
Sets to the given value.
@param [Object] value
The new value to set.
@return [Object]
The new value.
# File lib/garcon/task/atomic.rb, line 108 def set(value) @mutex.synchronize { @value = value } end
Also aliased as: value=