|
| 1 | +module Concurrent |
| 2 | + module Edge |
| 3 | + # @!macro atomic_markable_reference |
| 4 | + class AtomicMarkableReference < ::Concurrent::Synchronization::Object |
| 5 | + # @!macro [attach] atomic_markable_reference_method_initialize |
| 6 | + def initialize(value = nil, mark = false) |
| 7 | + super() |
| 8 | + @Reference = AtomicReference.new ImmutableArray[value, mark] |
| 9 | + ensure_ivar_visibility! |
| 10 | + end |
| 11 | + |
| 12 | + # @!macro [attach] atomic_markable_reference_method_compare_and_set |
| 13 | + # |
| 14 | + # Atomically sets the value and mark to the given updated value and |
| 15 | + # mark given both: |
| 16 | + # - the current value == the expected value && |
| 17 | + # - the current mark == the expected mark |
| 18 | + # |
| 19 | + # @param [Object] old_val the expected value |
| 20 | + # @param [Object] new_val the new value |
| 21 | + # @param [Boolean] old_mark the expected mark |
| 22 | + # @param [Boolean] new_mark the new mark |
| 23 | + # |
| 24 | + # @return [Boolean] `true` if successful. A `false` return indicates |
| 25 | + # that the actual value was not equal to the expected value or the |
| 26 | + # actual mark was not equal to the expected mark |
| 27 | + def compare_and_set(expected_val, new_val, expected_mark, new_mark) |
| 28 | + # Memoize a valid reference to the current AtomicReference for |
| 29 | + # later comparison. |
| 30 | + current = @Reference.get |
| 31 | + curr_val, curr_mark = current |
| 32 | + |
| 33 | + # Ensure that that the expected marks match. |
| 34 | + return false unless expected_mark == curr_mark |
| 35 | + |
| 36 | + if expected_val.is_a? Numeric |
| 37 | + # If the object is a numeric, we need to ensure we are comparing |
| 38 | + # the numerical values |
| 39 | + return false unless expected_val == curr_val |
| 40 | + else |
| 41 | + # Otherwise, we need to ensure we are comparing the object identity. |
| 42 | + # Theoretically, this could be incorrect if a user monkey-patched |
| 43 | + # `Object#equal?`, but they should know that they are playing with |
| 44 | + # fire at that point. |
| 45 | + return false unless expected_val.equal? curr_val |
| 46 | + end |
| 47 | + |
| 48 | + prospect = ImmutableArray[new_val, new_mark] |
| 49 | + |
| 50 | + @Reference.compare_and_set current, prospect |
| 51 | + end |
| 52 | + alias_method :compare_and_swap, :compare_and_set |
| 53 | + |
| 54 | + # @!macro [attach] atomic_markable_reference_method_get |
| 55 | + # |
| 56 | + # Gets the current reference and marked values. |
| 57 | + # |
| 58 | + # @return [ImmutableArray] the current reference and marked values |
| 59 | + def get |
| 60 | + @Reference.get |
| 61 | + end |
| 62 | + |
| 63 | + # @!macro [attach] atomic_markable_reference_method_value |
| 64 | + # |
| 65 | + # Gets the current value of the reference |
| 66 | + # |
| 67 | + # @return [Object] the current value of the reference |
| 68 | + def value |
| 69 | + @Reference.get[0] |
| 70 | + end |
| 71 | + |
| 72 | + # @!macro [attach] atomic_markable_reference_method_mark |
| 73 | + # |
| 74 | + # Gets the current marked value |
| 75 | + # |
| 76 | + # @return [Boolean] the current marked value |
| 77 | + def mark |
| 78 | + @Reference.get[1] |
| 79 | + end |
| 80 | + alias_method :marked?, :mark |
| 81 | + |
| 82 | + # @!macro [attach] atomic_markable_reference_method_set |
| 83 | + # |
| 84 | + # _Unconditionally_ sets to the given value of both the reference and |
| 85 | + # the mark. |
| 86 | + # |
| 87 | + # @param [Object] new_val the new value |
| 88 | + # @param [Boolean] new_mark the new mark |
| 89 | + # |
| 90 | + # @return [ImmutableArray] both the new value and the new mark |
| 91 | + def set(new_val, new_mark) |
| 92 | + @Reference.set ImmutableArray[new_val, new_mark] |
| 93 | + end |
| 94 | + |
| 95 | + # @!macro [attach] atomic_markable_reference_method_update |
| 96 | + # |
| 97 | + # Pass the current value and marked state to the given block, replacing it |
| 98 | + # with the block's results. May retry if the value changes during the |
| 99 | + # block's execution. |
| 100 | + # |
| 101 | + # @yield [Object] Calculate a new value and marked state for the atomic |
| 102 | + # reference using given (old) value and (old) marked |
| 103 | + # @yieldparam [Object] old_val the starting value of the atomic reference |
| 104 | + # @yieldparam [Boolean] old_mark the starting state of marked |
| 105 | + # |
| 106 | + # @return [ImmutableArray] the new value and new mark |
| 107 | + def update |
| 108 | + loop do |
| 109 | + old_val, old_mark = @Reference.get |
| 110 | + new_val, new_mark = yield old_val, old_mark |
| 111 | + |
| 112 | + if compare_and_set old_val, new_val, old_mark, new_mark |
| 113 | + return ImmutableArray[new_val, new_mark] |
| 114 | + end |
| 115 | + end |
| 116 | + end |
| 117 | + |
| 118 | + # @!macro [attach] atomic_markable_reference_method_try_update! |
| 119 | + # |
| 120 | + # Pass the current value to the given block, replacing it |
| 121 | + # with the block's result. Raise an exception if the update |
| 122 | + # fails. |
| 123 | + # |
| 124 | + # @yield [Object] Calculate a new value and marked state for the atomic |
| 125 | + # reference using given (old) value and (old) marked |
| 126 | + # @yieldparam [Object] old_val the starting value of the atomic reference |
| 127 | + # @yieldparam [Boolean] old_mark the starting state of marked |
| 128 | + # |
| 129 | + # @return [ImmutableArray] the new value and marked state |
| 130 | + # |
| 131 | + # @raise [Concurrent::ConcurrentUpdateError] if the update fails |
| 132 | + def try_update! |
| 133 | + old_val, old_mark = @Reference.get |
| 134 | + new_val, new_mark = yield old_val, old_mark |
| 135 | + |
| 136 | + unless compare_and_set old_val, new_val, old_mark, new_mark |
| 137 | + fail ::Concurrent::ConcurrentUpdateError, |
| 138 | + 'AtomicMarkableReference: Update failed due to race condition.', |
| 139 | + 'Note: If you would like to guarantee an update, please use ' \ |
| 140 | + 'the `AtomicMarkableReference#update` method.' |
| 141 | + end |
| 142 | + |
| 143 | + ImmutableArray[new_val, new_mark] |
| 144 | + end |
| 145 | + |
| 146 | + # @!macro [attach] atomic_markable_reference_method_try_update |
| 147 | + # |
| 148 | + # Pass the current value to the given block, replacing it with the |
| 149 | + # block's result. Simply return nil if update fails. |
| 150 | + # |
| 151 | + # @yield [Object] Calculate a new value and marked state for the atomic |
| 152 | + # reference using given (old) value and (old) marked |
| 153 | + # @yieldparam [Object] old_val the starting value of the atomic reference |
| 154 | + # @yieldparam [Boolean] old_mark the starting state of marked |
| 155 | + # |
| 156 | + # @return [ImmutableArray] the new value and marked state, or nil if |
| 157 | + # the update failed |
| 158 | + def try_update |
| 159 | + old_val, old_mark = @Reference.get |
| 160 | + new_val, new_mark = yield old_val, old_mark |
| 161 | + |
| 162 | + return unless compare_and_set old_val, new_val, old_mark, new_mark |
| 163 | + |
| 164 | + ImmutableArray[new_val, new_mark] |
| 165 | + end |
| 166 | + |
| 167 | + # Internal/private ImmutableArray for representing pairs |
| 168 | + class ImmutableArray < Array |
| 169 | + def self.new(*args) |
| 170 | + super(*args).freeze |
| 171 | + end |
| 172 | + end |
| 173 | + end |
| 174 | + end |
| 175 | +end |
0 commit comments