123456789_123456789_123456789_123456789_123456789_

Class: Concurrent::MutexAtomicReference

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
Instance Chain:
Inherits: Object
Defined in: lib/concurrent-ruby/concurrent/atomic_reference/mutex_atomic.rb

Overview

Note:

**Private Implementation:** This abstraction is a private, internal implementation detail. It should never be used directly.

Class Method Summary

Instance Attribute Summary

Instance Method Summary

AtomicNumericCompareAndSetWrapper - Included

#compare_and_set

Atomically sets the value to the given updated value if the current value == the expected value.

AtomicDirectUpdate - Included

Constructor Details

.new(value = nil) ⇒ MutexAtomicReference

Parameters:

  • value (Object) (defaults to: nil)

    The initial value.

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/atomic_reference/mutex_atomic.rb', line 16

def initialize(value = nil)
  super()
  @Lock = ::Mutex.new
  @value = value
end

Instance Attribute Details

#value (rw)

Alias for #get.

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/atomic_reference/mutex_atomic.rb', line 26

alias_method :value, :get

#value=(new_value) (rw)

Alias for #set.

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/atomic_reference/mutex_atomic.rb', line 32

alias_method :value=, :set

Instance Method Details

#_compare_and_set(old_value, new_value) ⇒ Boolean

Atomically sets the value to the given updated value if the current value == the expected value.

that the actual value was not equal to the expected value.

Parameters:

  • old_value (Object)

    the expected value

  • new_value (Object)

    the new value

Returns:

  • (Boolean)

    true if successful. A false return indicates

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/atomic_reference/mutex_atomic.rb', line 45

def _compare_and_set(old_value, new_value)
  synchronize do
    if @value.equal? old_value
      @value = new_value
      true
    else
      false
    end
  end
end

#compare_and_swap

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/atomic_reference/mutex_atomic.rb', line 13

alias_method :compare_and_swap, :compare_and_set

#getObject Also known as: #value

Gets the current value.

Returns:

  • (Object)

    the current value

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/atomic_reference/mutex_atomic.rb', line 23

def get
  synchronize { @value }
end

#get_and_set(new_value) ⇒ Object Also known as: #swap

Atomically sets to the given value and returns the old value.

Parameters:

  • new_value (Object)

    the new value

Returns:

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/atomic_reference/mutex_atomic.rb', line 35

def get_and_set(new_value)
  synchronize do
    old_value = @value
    @value = new_value
    old_value
  end
end

#set(new_value) ⇒ Object Also known as: #value=

Sets to the given value.

Parameters:

  • new_value (Object)

    the new value

Returns:

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/atomic_reference/mutex_atomic.rb', line 29

def set(new_value)
  synchronize { @value = new_value }
end

#swap(new_value)

Alias for #get_and_set.

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/atomic_reference/mutex_atomic.rb', line 42

alias_method :swap, :get_and_set

#synchronize (private)

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/atomic_reference/mutex_atomic.rb', line 59

def synchronize
  if @Lock.owned?
    yield
  else
    @Lock.synchronize { yield }
  end
end