Class: Concurrent::JavaAtomicFixnum

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)

Constant Summary collapse

MIN_VALUE =
Java::JavaLang::Long::MIN_VALUE
MAX_VALUE =
Java::JavaLang::Long::MAX_VALUE

Instance Method Summary collapse

Constructor Details

#initialize(init = 0) ⇒ JavaAtomicFixnum

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`



131
132
133
134
# File 'lib/concurrent/atomic/atomic_fixnum.rb', line 131

def initialize(init = 0)
  raise ArgumentError.new('initial value must be a Fixnum') unless init.is_a?(Fixnum)
  @atomic = java.util.concurrent.atomic.AtomicLong.new(init)
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



160
161
162
# File 'lib/concurrent/atomic/atomic_fixnum.rb', line 160

def compare_and_set(expect, update)
  @atomic.compare_and_set(expect, update)
end

#decrementFixnum Also known as: down

Decreases the current value by 1.

Returns:

  • (Fixnum)

    the current value after decrementation



154
155
156
# File 'lib/concurrent/atomic/atomic_fixnum.rb', line 154

def decrement
  @atomic.decrement_and_get
end

#incrementFixnum Also known as: up

Increases the current value by 1.

Returns:

  • (Fixnum)

    the current value after incrementation



148
149
150
# File 'lib/concurrent/atomic/atomic_fixnum.rb', line 148

def increment
  @atomic.increment_and_get
end

#valueFixnum

Retrieves the current ‘Fixnum` value.

Returns:

  • (Fixnum)

    the current value



137
138
139
# File 'lib/concurrent/atomic/atomic_fixnum.rb', line 137

def value
  @atomic.get
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`



142
143
144
145
# File 'lib/concurrent/atomic/atomic_fixnum.rb', line 142

def value=(value)
  raise ArgumentError.new('value must be a Fixnum') unless value.is_a?(Fixnum)
  @atomic.set(value)
end