Class: Concurrent::AtomicFixnum — Concurrent

Class List Method List File List

# Class: Concurrent::AtomicFixnum

Inherits:
  AtomicFixnumImplementation
  - Object
  - Concurrent::AtomicFixnum
  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)

See Also:

- java.util.concurrent.atomic.AtomicLong

## Instance Method Summary (collapse)

- - (Fixnum) compare_and_set(expect, update)

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

- - (Fixnum) decrement

  Decreases the current value by the given amount (defaults to 1).

- - (Fixnum) increment

  Increases the current value by the given amount (defaults to 1).

- - (undocumented) initialize(initial = 0) constructor

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

- - (String) to_s (also: #inspect)

  Short string representation.

- - (Object) update {|Object| ... }

  Pass the current value to the given block, replacing it with the block's result.

- - (Fixnum) value

  Retrieves the current `Fixnum` value.

- - (Fixnum) value=(value)

  Explicitly sets the value.

## Constructor Details

### - (`undocumented`) initialize(initial = 0)

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

Parameters:

- initial (`Fixnum`) (defaults to: `0`) —

  the initial value

Raises:

- (`ArgumentError`) —

  if the initial value is not a `Fixnum`

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

    class AtomicFixnum < AtomicFixnumImplementation
      # @return [String] Short string representation.
      def to_s
        format '<#%s:0x%x value:%s>', self.class, object_id << 1, value
      end

      alias_method :inspect, :to_s
    end

## Instance Method Details

### - (`Fixnum`) compare_and_set(expect, update)

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:

- (`Fixnum`) —

  true if the value was updated else false

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

    class AtomicFixnum < AtomicFixnumImplementation
      # @return [String] Short string representation.
      def to_s
        format '<#%s:0x%x value:%s>', self.class, object_id << 1, value
      end

      alias_method :inspect, :to_s
    end

### - (`Fixnum`) decrement

Decreases the current value by the given amount (defaults to 1).

Parameters:

- delta (`Fixnum`) —

  the amount by which to decrease the current value

Returns:

- (`Fixnum`) —

  the current value after decrementation

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

    class AtomicFixnum < AtomicFixnumImplementation
      # @return [String] Short string representation.
      def to_s
        format '<#%s:0x%x value:%s>', self.class, object_id << 1, value
      end

      alias_method :inspect, :to_s
    end

### - (`Fixnum`) increment

Increases the current value by the given amount (defaults to 1).

Parameters:

- delta (`Fixnum`) —

  the amount by which to increase the current value

Returns:

- (`Fixnum`) —

  the current value after incrementation

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

    class AtomicFixnum < AtomicFixnumImplementation
      # @return [String] Short string representation.
      def to_s
        format '<#%s:0x%x value:%s>', self.class, object_id << 1, value
      end

      alias_method :inspect, :to_s
    end

### - (`String`) to_s Also known as: inspect

Returns Short string representation.

Returns:

- (`String`) —

  Short string representation.

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

    def to_s
      format '<#%s:0x%x value:%s>', self.class, object_id << 1, value
    end

### - (`Object`) update {|Object| ... }

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

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

    class AtomicFixnum < AtomicFixnumImplementation
      # @return [String] Short string representation.
      def to_s
        format '<#%s:0x%x value:%s>', self.class, object_id << 1, value
      end

      alias_method :inspect, :to_s
    end

### - (`Fixnum`) value

Retrieves the current `Fixnum` value.

Returns:

- (`Fixnum`) —

  the current value

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

    class AtomicFixnum < AtomicFixnumImplementation
      # @return [String] Short string representation.
      def to_s
        format '<#%s:0x%x value:%s>', self.class, object_id << 1, value
      end

      alias_method :inspect, :to_s
    end

### - (`Fixnum`) value=(value)

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`

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

    class AtomicFixnum < AtomicFixnumImplementation
      # @return [String] Short string representation.
      def to_s
        format '<#%s:0x%x value:%s>', self.class, object_id << 1, value
      end

      alias_method :inspect, :to_s
    end