Sha256: da9cb0e38789219f43fc48c8e5672e34ca59c60d5b92ac84d37b7f79a14a4ba2

Contents?: true

Size: 1.31 KB

Versions: 2

Compression:

Stored size: 1.31 KB

Contents

require 'thread'
require 'concurrent/atomic_reference/direct_update'
require 'concurrent/atomic_reference/numeric_cas_wrapper'

module Concurrent

  # @!macro atomic_reference
  #
  # @!visibility private
  # @!macro internal_implementation_note
  class MutexAtomicReference
    include Concurrent::AtomicDirectUpdate
    include Concurrent::AtomicNumericCompareAndSetWrapper

    # @!macro atomic_reference_method_initialize
    def initialize(value = nil)
      @mutex = Mutex.new
      @value = value
    end

    # @!macro atomic_reference_method_get
    def get
      @mutex.synchronize { @value }
    end
    alias_method :value, :get

    # @!macro atomic_reference_method_set
    def set(new_value)
      @mutex.synchronize { @value = new_value }
    end
    alias_method :value=, :set

    # @!macro atomic_reference_method_get_and_set
    def get_and_set(new_value)
      @mutex.synchronize do
        old_value = @value
        @value = new_value
        old_value
      end
    end
    alias_method :swap, :get_and_set

    # @!macro atomic_reference_method_compare_and_set
    def _compare_and_set(old_value, new_value)
      return false unless @mutex.try_lock
      begin
        return false unless @value.equal? old_value
        @value = new_value
      ensure
        @mutex.unlock
      end
      true
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
concurrent-ruby-0.9.0.pre2 lib/concurrent/atomic_reference/mutex_atomic.rb
concurrent-ruby-0.9.0.pre2-java lib/concurrent/atomic_reference/mutex_atomic.rb