Class: Concurrent::MutexAtomicFixnum

Inherits:
Object
  • Object
show all
Defined in:
lib/concurrent/atomic/atomic_fixnum.rb

Overview

A numeric value that can be updated atomically. Reads and writes to an atomic fixnum and thread-safe and guaranteed to succeed. Reads and writes may block briefly but no explicit locking is required.

Testing with ruby 2.1.2
Testing with Concurrent::MutexAtomicFixnum...
  3.130000   0.000000   3.130000 (  3.136505)
Testing with Concurrent::CAtomicFixnum...
  0.790000   0.000000   0.790000 (  0.785550)

Testing with jruby 1.9.3
Testing with Concurrent::MutexAtomicFixnum...
  5.460000   2.460000   7.920000 (  3.715000)
Testing with Concurrent::JavaAtomicFixnum...
  4.520000   0.030000   4.550000 (  1.187000)

Direct Known Subclasses

AtomicFixnum

Constant Summary collapse

MIN_VALUE =
-2))
MAX_VALUE =
(2**(0.size * 8 -2) -1)

Instance Method Summary collapse

Constructor Details

#initialize(init = 0) ⇒ MutexAtomicFixnum

Creates a new ‘AtomicFixnum` with the given initial value.

Parameters:

  • init (Fixnum) (defaults to: 0)

    the initial value

Raises:

  • (ArgumentError)

    if the initial value is not a ‘Fixnum`



37
38
39
40
41
# File 'lib/concurrent/atomic/atomic_fixnum.rb', line 37

def initialize(init = 0)
  raise ArgumentError.new('initial value must be a Fixnum') unless init.is_a?(Fixnum)
  @value = init
  @mutex = Mutex.new
end

Instance Method Details

#compare_and_set(expect, update) ⇒ Boolean

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

Parameters:

  • expect (Fixnum)

    the expected value

  • update (Fixnum)

    the new value

Returns:

  • (Boolean)

    true if the value was updated else false



109
110
111
112
113
114
115
116
117
118
119
# File 'lib/concurrent/atomic/atomic_fixnum.rb', line 109

def compare_and_set(expect, update)
  @mutex.lock
  if @value == expect
    @value = update
    true
  else
    false
  end
ensure
  @mutex.unlock
end

#decrementFixnum Also known as: down

Decreases the current value by 1.

Returns:

  • (Fixnum)

    the current value after decrementation



91
92
93
94
95
96
# File 'lib/concurrent/atomic/atomic_fixnum.rb', line 91

def decrement
  @mutex.lock
  @value -= 1
ensure
  @mutex.unlock
end

#incrementFixnum Also known as: up

Increases the current value by 1.

Returns:

  • (Fixnum)

    the current value after incrementation



77
78
79
80
81
82
# File 'lib/concurrent/atomic/atomic_fixnum.rb', line 77

def increment
  @mutex.lock
  @value += 1
ensure
  @mutex.unlock
end

#valueFixnum

Retrieves the current ‘Fixnum` value.

Returns:

  • (Fixnum)

    the current value



48
49
50
51
52
53
# File 'lib/concurrent/atomic/atomic_fixnum.rb', line 48

def value
  @mutex.lock
  @value
ensure
  @mutex.unlock
end

#value=(value) ⇒ Fixnum

Explicitly sets the value.

Parameters:

  • value (Fixnum)

    the new value to be set

Returns:

  • (Fixnum)

    the current value

Raises:

  • (ArgumentError)

    if the new value is not a ‘Fixnum`



64
65
66
67
68
69
70
# File 'lib/concurrent/atomic/atomic_fixnum.rb', line 64

def value=(value)
  raise ArgumentError.new('value must be a Fixnum') unless value.is_a?(Fixnum)
  @mutex.lock
  @value = value
ensure
  @mutex.unlock
end