Sha256: 0752364dfda6109362a45708a550b1cfac2972f3296b8287fdf1c8b80f37d90d

Contents?: true

Size: 822 Bytes

Versions: 8

Compression:

Stored size: 822 Bytes

Contents

require 'concurrent/atomic_reference/concurrent_update_error'

module Concurrent

  # Define update methods that delegate to @ref field
  class Atomic
    # Pass the current value to the given block, replacing it
    # with the block's result. May retry if the value changes
    # during the block's execution.
    def update
      true until @ref.compare_and_set(old_value = @ref.get, new_value = yield(old_value))
      new_value
    end

    def try_update
      old_value = @ref.get
      new_value = yield old_value
      unless @ref.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

8 entries across 8 versions & 1 rubygems

Version Path
concurrent-ruby-0.7.0.rc0 lib/concurrent/atomic_reference/delegated_update.rb
concurrent-ruby-0.7.0.rc0-x86_64-linux lib/concurrent/atomic_reference/delegated_update.rb
concurrent-ruby-0.7.0.rc0-x86_64-darwin-13 lib/concurrent/atomic_reference/delegated_update.rb
concurrent-ruby-0.7.0.rc0-x86-solaris-2.11 lib/concurrent/atomic_reference/delegated_update.rb
concurrent-ruby-0.7.0.rc0-x86-mingw32 lib/concurrent/atomic_reference/delegated_update.rb
concurrent-ruby-0.7.0.rc0-x86-linux lib/concurrent/atomic_reference/delegated_update.rb
concurrent-ruby-0.7.0.rc0-x64-mingw32 lib/concurrent/atomic_reference/delegated_update.rb
concurrent-ruby-0.7.0.rc0-java lib/concurrent/atomic_reference/delegated_update.rb