|
| 1 | +require 'thread' |
| 2 | +require 'concurrent/atomic_reference/numeric_cas_wrapper' |
| 3 | + |
| 4 | +module Concurrent |
| 5 | + # @!macro atomic_markable_reference |
| 6 | + class MutexAtomicMarkableReference |
| 7 | + include AtomicNumericCompareAndSetWrapper |
| 8 | + |
| 9 | + # @!macro [attach] atomic_markable_reference_method_initialize |
| 10 | + def initialize(value = nil, marked = false) |
| 11 | + @mutex = Mutex.new |
| 12 | + @value, @marked = value, marked |
| 13 | + end |
| 14 | + |
| 15 | + # @!macro [attach] atomic_markable_reference_method_get |
| 16 | + # |
| 17 | + # Gets the current reference and marked values. |
| 18 | + # |
| 19 | + # @return [Array] the current reference and marked values |
| 20 | + def get |
| 21 | + @mutex.synchronize { [@value, @marked] } |
| 22 | + end |
| 23 | + |
| 24 | + # @!macro [attach] atomic_markable_reference_method_value |
| 25 | + # |
| 26 | + # Gets the current value of the reference |
| 27 | + # |
| 28 | + # @return [Object] the current value of the reference |
| 29 | + def value |
| 30 | + @mutex.synchronize { @value } |
| 31 | + end |
| 32 | + |
| 33 | + # @!macro [attach] atomic_markable_reference_method_marked? |
| 34 | + # |
| 35 | + # Gets the current marked value |
| 36 | + # |
| 37 | + # @return [Boolean] the current marked value |
| 38 | + def marked? |
| 39 | + @mutex.synchronize { @marked } |
| 40 | + end |
| 41 | + alias_method :marked, :marked? |
| 42 | + |
| 43 | + # @!macro [attach] atomic_markable_reference_method_set |
| 44 | + # |
| 45 | + # Sets to the given value of both the reference and the mark. |
| 46 | + # |
| 47 | + # @param [Object] new_val the new value |
| 48 | + # @param [Boolean] new_mark the new mark |
| 49 | + # |
| 50 | + # @return [Array] both the new value and the new mark |
| 51 | + def set(new_val, new_mark) |
| 52 | + @mutex.synchronize do |
| 53 | + @value, @marked = new_val, new_mark |
| 54 | + |
| 55 | + [@value, @marked] |
| 56 | + end |
| 57 | + end |
| 58 | + |
| 59 | + # @!macro [attach] atomic_markable_reference_method_update |
| 60 | + # |
| 61 | + # Pass the current value and marked state to the given block, replacing it |
| 62 | + # with the block's results. May retry if the value changes during the |
| 63 | + # block's execution. |
| 64 | + # |
| 65 | + # @yield [Object] Calculate a new value and marked state for the atomic |
| 66 | + # reference using given (old) value and (old) marked |
| 67 | + # @yieldparam [Object] old_val the starting value of the atomic reference |
| 68 | + # @yieldparam [Boolean] old_mark the starting state of marked |
| 69 | + # |
| 70 | + # @return [Array] the new value and new mark |
| 71 | + def update |
| 72 | + loop do |
| 73 | + old_val, old_mark = value, marked? |
| 74 | + new_val, new_mark = yield old_val, old_mark |
| 75 | + |
| 76 | + if compare_and_set old_val, new_val, old_mark, new_mark |
| 77 | + return [new_val, new_mark] |
| 78 | + end |
| 79 | + end |
| 80 | + end |
| 81 | + |
| 82 | + # @!macro [attach] atomic_markable_reference_method_try_update |
| 83 | + # |
| 84 | + # Pass the current value to the given block, replacing it |
| 85 | + # with the block's result. Raise an exception if the update |
| 86 | + # fails. |
| 87 | + # |
| 88 | + # @yield [Object] Calculate a new value and marked state for the atomic |
| 89 | + # reference using given (old) value and (old) marked |
| 90 | + # @yieldparam [Object] old_val the starting value of the atomic reference |
| 91 | + # @yieldparam [Boolean] old_mark the starting state of marked |
| 92 | + # |
| 93 | + # @return [Array] the new value and marked state |
| 94 | + # |
| 95 | + # @raise [Concurrent::ConcurrentUpdateError] if the update fails |
| 96 | + def try_update |
| 97 | + old_val, old_mark = value, marked? |
| 98 | + new_val, new_mark = yield old_val, old_mark |
| 99 | + |
| 100 | + unless compare_and_set old_val, new_val, old_mark, new_mark |
| 101 | + err = [ConcurrentUpdateError, 'Update failed'] |
| 102 | + err << ConcurrentUpdateError::CONC_UP_ERR_BACKTRACE if $VERBOSE |
| 103 | + |
| 104 | + fail(*err) |
| 105 | + end |
| 106 | + |
| 107 | + [new_val, new_mark] |
| 108 | + end |
| 109 | + |
| 110 | + # @!macro [attach] atomic_markable_reference_method_compare_and_set |
| 111 | + # |
| 112 | + # Atomically sets the value and mark to the given updated value and |
| 113 | + # mark given both: |
| 114 | + # - the current value == the expected value && |
| 115 | + # - the current mark == the expected mark |
| 116 | + # |
| 117 | + # @param [Object] old_val the expected value |
| 118 | + # @param [Object] new_val the new value |
| 119 | + # @param [Boolean] old_mark the expected mark |
| 120 | + # @param [Boolean] new_mark the new mark |
| 121 | + # |
| 122 | + # @return [Boolean] `true` if successful. A `false` return indicates |
| 123 | + # that the actual value was not equal to the expected value or the |
| 124 | + # actual mark was not equal to the expected mark |
| 125 | + def _compare_and_set(old_val, new_val, old_mark, new_mark) #:nodoc: |
| 126 | + return false unless @mutex.try_lock |
| 127 | + |
| 128 | + begin |
| 129 | + return false unless @value.equal?(old_val) && @marked == old_mark |
| 130 | + |
| 131 | + @value, @marked = new_val, new_mark |
| 132 | + ensure |
| 133 | + @mutex.unlock |
| 134 | + end |
| 135 | + |
| 136 | + true |
| 137 | + end |
| 138 | + end |
| 139 | +end |
0 commit comments