class Concurrent::Tuple

A fixed size array with volatile (synchronized, thread safe) getters/setters. Mixes in Ruby's `Enumerable` module for enhanced search, sort, and traversal.

@example

tuple = Concurrent::Tuple.new(16)

tuple.set(0, :foo)                   #=> :foo  | volatile write
tuple.get(0)                         #=> :foo  | volatile read
tuple.compare_and_set(0, :foo, :bar) #=> true  | strong CAS
tuple.cas(0, :foo, :baz)             #=> false | strong CAS
tuple.get(0)                         #=> :bar  | volatile read

@see en.wikipedia.org/wiki/Tuple Tuple entry at Wikipedia @see www.erlang.org/doc/reference_manual/data_types.html#id70396 Erlang Tuple @see ruby-doc.org/core-2.2.2/Enumerable.html Enumerable

Constants

Tuple

@!visibility private

Attributes

size[R]

The (fixed) size of the tuple.

Public Class Methods

new(size) click to toggle source

Create a new tuple of the given size.

@param [Integer] size the number of elements in the tuple

# File lib/concurrent-ruby/concurrent/tuple.rb, line 33
def initialize(size)
  @size = size
  @tuple = tuple = Tuple.new(size)
  i = 0
  while i < size
    tuple[i] = Concurrent::AtomicReference.new
    i += 1
  end
end

Public Instance Methods

cas(i, old_value, new_value)
Alias for: compare_and_set
compare_and_set(i, old_value, new_value) click to toggle source

Set the value at the given index to the new value if and only if the current value matches the given old value.

@param [Integer] i the index for the element to set @param [Object] old_value the value to compare against the current value @param [Object] new_value the value to set at the given index

@return [Boolean] true if the value at the given element was set else false

# File lib/concurrent-ruby/concurrent/tuple.rb, line 73
def compare_and_set(i, old_value, new_value)
  return false if i >= @size || i < 0
  @tuple[i].compare_and_set(old_value, new_value)
end
Also aliased as: cas
each() { |get| ... } click to toggle source

Calls the given block once for each element in self, passing that element as a parameter.

@yieldparam [Object] ref the `Concurrent::AtomicReference` object at the current index

# File lib/concurrent-ruby/concurrent/tuple.rb, line 82
def each
  @tuple.each {|ref| yield ref.get}
end
get(i) click to toggle source

Get the value of the element at the given index.

@param [Integer] i the index from which to retrieve the value @return [Object] the value at the given index or nil if the index is out of bounds

# File lib/concurrent-ruby/concurrent/tuple.rb, line 47
def get(i)
  return nil if i >= @size || i < 0
  @tuple[i].get
end
Also aliased as: volatile_get
set(i, value) click to toggle source

Set the element at the given index to the given value

@param [Integer] i the index for the element to set @param [Object] value the value to set at the given index

@return [Object] the new value of the element at the given index or nil if the index is out of bounds

# File lib/concurrent-ruby/concurrent/tuple.rb, line 59
def set(i, value)
  return nil if i >= @size || i < 0
  @tuple[i].set(value)
end
Also aliased as: volatile_set
volatile_get(i)
Alias for: get
volatile_set(i, value)
Alias for: set