Class: Concurrent::AtomicFixnum

Inherits:
Object
  • Object
show all
Defined in:
lib/concurrent/atomic.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) ⇒ AtomicFixnum

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

Since:

  • 0.5.0



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

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

Instance Method Details

#decrementFixnum Also known as: down

Decreases the current value by 1

Returns:

  • (Fixnum)

    the current value after decrementation



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/concurrent/atomic.rb', line 105

class AtomicFixnum

  # Creates a new +AtomicFixnum+ with the given initial value.
  #
  # @param [Fixnum] init the initial value
  # @raise [ArgumentError] if the initial value is not a +Fixnum+
  def initialize(init = 0)
    raise ArgumentError.new('initial value must be a Fixnum') unless init.is_a?(Fixnum)
    allocate_storage(init)
  end

  if defined? java.util
    include JavaAtomicFixnum
  else
    include MutexAtomicFixnum
  end

  alias_method :up, :increment
  alias_method :down, :decrement
end

#incrementFixnum Also known as: up

Increases the current value by 1

Returns:

  • (Fixnum)

    the current value after incrementation



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/concurrent/atomic.rb', line 105

class AtomicFixnum

  # Creates a new +AtomicFixnum+ with the given initial value.
  #
  # @param [Fixnum] init the initial value
  # @raise [ArgumentError] if the initial value is not a +Fixnum+
  def initialize(init = 0)
    raise ArgumentError.new('initial value must be a Fixnum') unless init.is_a?(Fixnum)
    allocate_storage(init)
  end

  if defined? java.util
    include JavaAtomicFixnum
  else
    include MutexAtomicFixnum
  end

  alias_method :up, :increment
  alias_method :down, :decrement
end

#valueFixnum

Retrieves the current Fixnum value

Returns:

  • (Fixnum)

    the current value



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/concurrent/atomic.rb', line 105

class AtomicFixnum

  # Creates a new +AtomicFixnum+ with the given initial value.
  #
  # @param [Fixnum] init the initial value
  # @raise [ArgumentError] if the initial value is not a +Fixnum+
  def initialize(init = 0)
    raise ArgumentError.new('initial value must be a Fixnum') unless init.is_a?(Fixnum)
    allocate_storage(init)
  end

  if defined? java.util
    include JavaAtomicFixnum
  else
    include MutexAtomicFixnum
  end

  alias_method :up, :increment
  alias_method :down, :decrement
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



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/concurrent/atomic.rb', line 105

class AtomicFixnum

  # Creates a new +AtomicFixnum+ with the given initial value.
  #
  # @param [Fixnum] init the initial value
  # @raise [ArgumentError] if the initial value is not a +Fixnum+
  def initialize(init = 0)
    raise ArgumentError.new('initial value must be a Fixnum') unless init.is_a?(Fixnum)
    allocate_storage(init)
  end

  if defined? java.util
    include JavaAtomicFixnum
  else
    include MutexAtomicFixnum
  end

  alias_method :up, :increment
  alias_method :down, :decrement
end