Class: Concurrent::Tuple

Inherits:
Object show all
Includes:
Enumerable
Defined in:
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/concurrent-ruby-1.1.10/lib/concurrent-ruby/concurrent/tuple.rb

Overview

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

Examples:

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 Also:

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Enumerable

#as_json, #compact_blank, #exclude?, #excluding, #in_order_of, #including, #index_by, #index_with, #many?, #maximum, #minimum, #pick, #pluck, #sole, #sum

Constructor Details

#initialize(size) ⇒ Tuple

Create a new tuple of the given size.

Parameters:

  • size (Integer)

    the number of elements in the tuple



33
34
35
36
37
38
39
40
41
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/concurrent-ruby-1.1.10/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

Instance Attribute Details

#sizeObject (readonly)

The (fixed) size of the tuple.



24
25
26
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/concurrent-ruby-1.1.10/lib/concurrent-ruby/concurrent/tuple.rb', line 24

def size
  @size
end

Instance Method Details

#compare_and_set(i, old_value, new_value) ⇒ Boolean Also known as: cas

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

Parameters:

  • i (Integer)

    the index for the element to set

  • old_value (Object)

    the value to compare against the current value

  • new_value (Object)

    the value to set at the given index

Returns:

  • (Boolean)

    true if the value at the given element was set else false



73
74
75
76
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/concurrent-ruby-1.1.10/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

#each {|ref| ... } ⇒ Object

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

Yield Parameters:

  • ref (Object)

    the ‘Concurrent::AtomicReference` object at the current index



82
83
84
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/concurrent-ruby-1.1.10/lib/concurrent-ruby/concurrent/tuple.rb', line 82

def each
  @tuple.each {|ref| yield ref.get}
end

#get(i) ⇒ Object Also known as: volatile_get

Get the value of the element at the given index.

Parameters:

  • i (Integer)

    the index from which to retrieve the value

Returns:

  • (Object)

    the value at the given index or nil if the index is out of bounds



47
48
49
50
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/concurrent-ruby-1.1.10/lib/concurrent-ruby/concurrent/tuple.rb', line 47

def get(i)
  return nil if i >= @size || i < 0
  @tuple[i].get
end

#set(i, value) ⇒ Object Also known as: volatile_set

Set the element at the given index to the given value

Parameters:

  • i (Integer)

    the index for the element to set

  • value (Object)

    the value to set at the given index

Returns:

  • (Object)

    the new value of the element at the given index or nil if the index is out of bounds



59
60
61
62
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/concurrent-ruby-1.1.10/lib/concurrent-ruby/concurrent/tuple.rb', line 59

def set(i, value)
  return nil if i >= @size || i < 0
  @tuple[i].set(value)
end