Sha256: 74a8d41491767651584927924c2f33ce1776bd30abf5f22ac04668e5d1df7639

Contents?: true

Size: 1.66 KB

Versions: 107

Compression:

Stored size: 1.66 KB

Contents

require 'concurrent/synchronization/safe_initialization'
require 'concurrent/utility/native_integer'

module Concurrent

  # @!macro atomic_fixnum
  # @!visibility private
  # @!macro internal_implementation_note
  class MutexAtomicFixnum
    extend Concurrent::Synchronization::SafeInitialization

    # @!macro atomic_fixnum_method_initialize
    def initialize(initial = 0)
      super()
      @Lock = ::Mutex.new
      ns_set(initial)
    end

    # @!macro atomic_fixnum_method_value_get
    def value
      synchronize { @value }
    end

    # @!macro atomic_fixnum_method_value_set
    def value=(value)
      synchronize { ns_set(value) }
    end

    # @!macro atomic_fixnum_method_increment
    def increment(delta = 1)
      synchronize { ns_set(@value + delta.to_i) }
    end

    alias_method :up, :increment

    # @!macro atomic_fixnum_method_decrement
    def decrement(delta = 1)
      synchronize { ns_set(@value - delta.to_i) }
    end

    alias_method :down, :decrement

    # @!macro atomic_fixnum_method_compare_and_set
    def compare_and_set(expect, update)
      synchronize do
        if @value == expect.to_i
          @value = update.to_i
          true
        else
          false
        end
      end
    end

    # @!macro atomic_fixnum_method_update
    def update
      synchronize do
        @value = yield @value
      end
    end

    protected

    # @!visibility private
    def synchronize
      if @Lock.owned?
        yield
      else
        @Lock.synchronize { yield }
      end
    end

    private

    # @!visibility private
    def ns_set(value)
      Utility::NativeInteger.ensure_integer_and_bounds value
      @value = value
    end
  end
end

Version data entries

107 entries across 106 versions & 18 rubygems

Version Path
harbr-0.1.50 vendor/bundle/ruby/3.2.0/gems/concurrent-ruby-1.2.2/lib/concurrent-ruby/concurrent/atomic/mutex_atomic_fixnum.rb
harbr-0.1.49 vendor/bundle/ruby/3.2.0/gems/concurrent-ruby-1.2.2/lib/concurrent-ruby/concurrent/atomic/mutex_atomic_fixnum.rb
harbr-0.1.48 vendor/bundle/ruby/3.2.0/gems/concurrent-ruby-1.2.2/lib/concurrent-ruby/concurrent/atomic/mutex_atomic_fixnum.rb
harbr-0.1.47 vendor/bundle/ruby/3.2.0/gems/concurrent-ruby-1.2.2/lib/concurrent-ruby/concurrent/atomic/mutex_atomic_fixnum.rb
harbr-0.1.46 vendor/bundle/ruby/3.2.0/gems/concurrent-ruby-1.2.2/lib/concurrent-ruby/concurrent/atomic/mutex_atomic_fixnum.rb
harbr-0.1.45 vendor/bundle/ruby/3.2.0/gems/concurrent-ruby-1.2.2/lib/concurrent-ruby/concurrent/atomic/mutex_atomic_fixnum.rb
harbr-0.1.44 vendor/bundle/ruby/3.2.0/gems/concurrent-ruby-1.2.2/lib/concurrent-ruby/concurrent/atomic/mutex_atomic_fixnum.rb
harbr-0.1.43 vendor/bundle/ruby/3.2.0/gems/concurrent-ruby-1.2.2/lib/concurrent-ruby/concurrent/atomic/mutex_atomic_fixnum.rb
harbr-0.1.42 vendor/bundle/ruby/3.2.0/gems/concurrent-ruby-1.2.2/lib/concurrent-ruby/concurrent/atomic/mutex_atomic_fixnum.rb
harbr-0.1.41 vendor/bundle/ruby/3.2.0/gems/concurrent-ruby-1.2.2/lib/concurrent-ruby/concurrent/atomic/mutex_atomic_fixnum.rb
harbr-0.1.39 vendor/bundle/ruby/3.2.0/gems/concurrent-ruby-1.2.2/lib/concurrent-ruby/concurrent/atomic/mutex_atomic_fixnum.rb
harbr-0.1.38 vendor/bundle/ruby/3.2.0/gems/concurrent-ruby-1.2.2/lib/concurrent-ruby/concurrent/atomic/mutex_atomic_fixnum.rb
harbr-0.1.37 vendor/bundle/ruby/3.2.0/gems/concurrent-ruby-1.2.2/lib/concurrent-ruby/concurrent/atomic/mutex_atomic_fixnum.rb
honeybadger-5.4.0 vendor/bundle/ruby/3.2.0/gems/concurrent-ruby-1.2.2/lib/concurrent-ruby/concurrent/atomic/mutex_atomic_fixnum.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/concurrent-ruby-1.2.2/lib/concurrent-ruby/concurrent/atomic/mutex_atomic_fixnum.rb
honeybadger-5.3.0 vendor/bundle/ruby/3.2.0/gems/concurrent-ruby-1.2.2/lib/concurrent-ruby/concurrent/atomic/mutex_atomic_fixnum.rb
mlh-rubocop-config-1.0.2 vendor/bundle/ruby/3.2.0/gems/concurrent-ruby-1.2.2/lib/concurrent-ruby/concurrent/atomic/mutex_atomic_fixnum.rb
fablicop-1.10.3 vendor/bundle/ruby/3.2.0/gems/concurrent-ruby-1.2.2/lib/concurrent-ruby/concurrent/atomic/mutex_atomic_fixnum.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/concurrent-ruby-1.2.2/lib/concurrent-ruby/concurrent/atomic/mutex_atomic_fixnum.rb
fablicop-1.10.2 vendor/bundle/ruby/3.2.0/gems/concurrent-ruby-1.2.2/lib/concurrent-ruby/concurrent/atomic/mutex_atomic_fixnum.rb