class Concurrent::AtomicMarkableReference
java.util.concurrent.atomic.AtomicMarkableReference
@see docs.oracle.com/javase/7/docs/api/java/util/concurrent/atomic/AtomicMarkableReference.html<br><br>that can be updated atomically.
An atomic reference which maintains an object reference along with a mark bit
def compare_and_set(expected_val, new_val, expected_mark, new_mark)
-
(Boolean)
- `true` if successful. A `false` return indicates
Parameters:
-
new_mark
(Boolean
) -- the new mark -
expected_mark
(Boolean
) -- the expected mark -
new_val
(Object
) -- the new value -
expected_val
(Object
) -- the expected value
def compare_and_set(expected_val, new_val, expected_mark, new_mark) # Memoize a valid reference to the current AtomicReference for # later comparison. current = reference curr_val, curr_mark = current # Ensure that that the expected marks match. return false unless expected_mark == curr_mark if expected_val.is_a? Numeric # If the object is a numeric, we need to ensure we are comparing # the numerical values return false unless expected_val == curr_val else # Otherwise, we need to ensure we are comparing the object identity. # Theoretically, this could be incorrect if a user monkey-patched # `Object#equal?`, but they should know that they are playing with # fire at that point. return false unless expected_val.equal? curr_val end prospect = immutable_array(new_val, new_mark) compare_and_set_reference current, prospect end
def get
-
(Array)
- the current reference and marked values
def get reference end
def immutable_array(*args)
def immutable_array(*args) args.freeze end
def initialize(value = nil, mark = false)
def initialize(value = nil, mark = false) super() self.reference = immutable_array(value, mark) end
def mark
-
(Boolean)
- the current marked value
def mark reference[1] end
def set(new_val, new_mark)
-
(Array)
- both the new value and the new mark
Parameters:
-
new_mark
(Boolean
) -- the new mark -
new_val
(Object
) -- the new value
def set(new_val, new_mark) self.reference = immutable_array(new_val, new_mark) end
def try_update
-
(Array)
- the new value and marked state, or nil if
Other tags:
- Yieldparam: old_mark - the starting state of marked
Yieldparam: old_val - the starting value of the atomic reference
Other tags:
- Yield: - Calculate a new value and marked state for the atomic
def try_update old_val, old_mark = reference new_val, new_mark = yield old_val, old_mark return unless compare_and_set old_val, new_val, old_mark, new_mark immutable_array(new_val, new_mark) end
def try_update!
-
(Concurrent::ConcurrentUpdateError)
- if the update fails
Returns:
-
(Array)
- the new value and marked state
Other tags:
- Yieldparam: old_mark - the starting state of marked
Yieldparam: old_val - the starting value of the atomic reference
Other tags:
- Yield: - Calculate a new value and marked state for the atomic
def try_update! old_val, old_mark = reference new_val, new_mark = yield old_val, old_mark unless compare_and_set old_val, new_val, old_mark, new_mark fail ::Concurrent::ConcurrentUpdateError, 'AtomicMarkableReference: Update failed due to race condition.', 'Note: If you would like to guarantee an update, please use ' + 'the `AtomicMarkableReference#update` method.' end immutable_array(new_val, new_mark) end
def update
-
(Array)
- the new value and new mark
Other tags:
- Yieldparam: old_mark - the starting state of marked
Yieldparam: old_val - the starting value of the atomic reference
Other tags:
- Yield: - Calculate a new value and marked state for the atomic
def update loop do old_val, old_mark = reference new_val, new_mark = yield old_val, old_mark if compare_and_set old_val, new_val, old_mark, new_mark return immutable_array(new_val, new_mark) end end end
def value
-
(Object)
- the current value of the reference
def value reference[0] end