Class: Garcon::AtomicMutex
- Defined in:
- lib/garcon/task/atomic.rb
Instance Method Summary collapse
-
#_compare_and_set(old_value, new_value) ⇒ Boolean
Atomically sets the value to the given updated value if the current value is equal the expected value.
-
#get ⇒ Object
(also: #value)
Gets the current value.
-
#get_and_set(new_value) ⇒ Object
(also: #swap)
Atomically sets to the given value and returns the old value.
-
#initialize(value = nil) ⇒ AtomicMutex
constructor
A new instance of AtomicMutex.
-
#set(value) ⇒ Object
(also: #value=)
Sets to the given value.
Methods included from AtomicNumericCompareAndSetWrapper
Methods included from AtomicDirectUpdate
Constructor Details
#initialize(value = nil) ⇒ AtomicMutex
Returns a new instance of AtomicMutex.
87 88 89 90 |
# File 'lib/garcon/task/atomic.rb', line 87 def initialize(value = nil) @mutex = Mutex.new @value = value end |
Instance Method Details
#_compare_and_set(old_value, new_value) ⇒ Boolean
Atomically sets the value to the given updated value if the current value is equal the expected value.
140 141 142 143 144 145 146 147 148 149 |
# 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 ⇒ Object Also known as: value
Gets the current value.
96 97 98 |
# File 'lib/garcon/task/atomic.rb', line 96 def get @mutex.synchronize { @value } end |
#get_and_set(new_value) ⇒ Object Also known as: swap
Atomically sets to the given value and returns the old value.
120 121 122 123 124 125 126 |
# 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 |
#set(value) ⇒ Object Also known as: value=
Sets to the given value.
108 109 110 |
# File 'lib/garcon/task/atomic.rb', line 108 def set(value) @mutex.synchronize { @value = value } end |