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.

Instance Method Summary collapse

Constructor Details

#initialize(init = 0) ⇒ JavaAtomicFixnum

Returns a new instance of JavaAtomicFixnum.

Raises:

  • (ArgumentError)

Since:

  • 0.5.0



111
112
113
114
# File 'lib/concurrent/atomic/atomic_fixnum.rb', line 111

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

Since:

  • 0.5.0



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

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

Since:

  • 0.5.0



139
140
141
# File 'lib/concurrent/atomic/atomic_fixnum.rb', line 139

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

Since:

  • 0.5.0



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

def increment
  @atomic.increment_and_get
end

#valueFixnum

Retrieves the current ‘Fixnum` value.

Returns:

  • (Fixnum)

    the current value

Since:

  • 0.5.0



118
119
120
# File 'lib/concurrent/atomic/atomic_fixnum.rb', line 118

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`

Since:

  • 0.5.0



124
125
126
127
# File 'lib/concurrent/atomic/atomic_fixnum.rb', line 124

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