Class: Concurrent::MutexAtomic

Inherits:
Object
  • Object
show all
Includes:
AtomicDirectUpdate, AtomicNumericCompareAndSetWrapper
Defined in:
lib/concurrent/atomic_reference/mutex_atomic.rb

Overview

An object reference that may be updated atomically.

Testing with ruby 2.1.2

*** Sequential updates ***
                 user     system      total        real
no lock      0.000000   0.000000   0.000000 (  0.005502)
mutex        0.030000   0.000000   0.030000 (  0.025158)
MutexAtomic  0.100000   0.000000   0.100000 (  0.103096)
CAtomic      0.040000   0.000000   0.040000 (  0.034012)

*** Parallel updates ***
                 user     system      total        real
no lock      0.010000   0.000000   0.010000 (  0.009387)
mutex        0.030000   0.010000   0.040000 (  0.032545)
MutexAtomic  0.830000   2.280000   3.110000 (  2.146622)
CAtomic      0.040000   0.000000   0.040000 (  0.038332)

Testing with jruby 1.9.3

*** Sequential updates ***
                 user     system      total        real
no lock      0.170000   0.000000   0.170000 (  0.051000)
mutex        0.370000   0.010000   0.380000 (  0.121000)
MutexAtomic  1.530000   0.020000   1.550000 (  0.471000)
JavaAtomic   0.370000   0.010000   0.380000 (  0.112000)

*** Parallel updates ***
                 user     system      total        real
no lock      0.390000   0.000000   0.390000 (  0.105000)
mutex        0.480000   0.040000   0.520000 (  0.145000)
MutexAtomic  1.600000   0.180000   1.780000 (  0.511000)
JavaAtomic   0.460000   0.010000   0.470000 (  0.131000)

See Also:

Direct Known Subclasses

Atomic

Instance Method Summary (collapse)

Constructor Details

- (MutexAtomic) initialize(value = nil)

Returns a new instance of MutexAtomic



13
14
15
16
# File 'lib/concurrent/atomic_reference/mutex_atomic.rb', line 13

def initialize(value = nil)
  @mutex = Mutex.new
  @value = value
end

Instance Method Details

- (Boolean) _compare_and_set(old_value, new_value)

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



66
67
68
69
70
71
72
73
74
75
# File 'lib/concurrent/atomic_reference/mutex_atomic.rb', line 66

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

- (Boolean) compare_and_set(old_value, new_value) Also known as: compare_and_swap Originally defined in module AtomicNumericCompareAndSetWrapper

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

- (Object) get Also known as: value

Gets the current value.

Returns:

  • (Object)

    the current value



23
24
25
# File 'lib/concurrent/atomic_reference/mutex_atomic.rb', line 23

def get
  @mutex.synchronize { @value }
end

- (Object) get_and_set(new_value) Also known as: swap

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

Parameters:

  • new_value (Object)

    the new value

Returns:

  • (Object)

    the old value



47
48
49
50
51
52
53
# File 'lib/concurrent/atomic_reference/mutex_atomic.rb', line 47

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

- (Object) set(new_value) Also known as: value=

Sets to the given value.

Parameters:

  • new_value (Object)

    the new value

Returns:

  • (Object)

    the new value



35
36
37
# File 'lib/concurrent/atomic_reference/mutex_atomic.rb', line 35

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

- (Object) try_update {|Object| ... } Originally defined in module AtomicDirectUpdate

Pass the current value to the given block, replacing it with the block's result. Raise an exception if the update fails.

Yields:

  • (Object)

    Calculate a new value for the atomic reference using given (old) value

Yield Parameters:

  • old_value (Object)

    the starting value of the atomic reference

Returns:

  • (Object)

    the new value

Raises:

- (Object) update {|Object| ... } Originally defined in module AtomicDirectUpdate

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.

Yields:

  • (Object)

    Calculate a new value for the atomic reference using given (old) value

Yield Parameters:

  • old_value (Object)

    the starting value of the atomic reference

Returns:

  • (Object)

    the new value