Sha256: d9af882277bc9ac9a21025baa5d601a674ae0430723ed55cff4f5326386f2850

Contents?: true

Size: 845 Bytes

Versions: 103

Compression:

Stored size: 845 Bytes

Contents

require 'concurrent/errors'

module Concurrent

  # Define update methods that use direct paths
  #
  # @!visibility private
  # @!macro internal_implementation_note
  module AtomicDirectUpdate
    def update
      true until compare_and_set(old_value = get, new_value = yield(old_value))
      new_value
    end

    def try_update
      old_value = get
      new_value = yield old_value

      return unless compare_and_set old_value, new_value

      new_value
    end

    def try_update!
      old_value = get
      new_value = yield old_value
      unless compare_and_set(old_value, new_value)
        if $VERBOSE
          raise ConcurrentUpdateError, "Update failed"
        else
          raise ConcurrentUpdateError, "Update failed", ConcurrentUpdateError::CONC_UP_ERR_BACKTRACE
        end
      end
      new_value
    end
  end
end

Version data entries

103 entries across 103 versions & 16 rubygems

Version Path
concurrent-ruby-1.2.1 lib/concurrent-ruby/concurrent/atomic_reference/atomic_direct_update.rb
devcycle-ruby-server-sdk-2.0.0 vendor/bundle/ruby/3.0.0/gems/concurrent-ruby-1.2.0/lib/concurrent-ruby/concurrent/atomic_reference/atomic_direct_update.rb
concurrent-ruby-1.2.0 lib/concurrent-ruby/concurrent/atomic_reference/atomic_direct_update.rb