Class: Concurrent::Tuple
Relationships & Source Files | |
Extension / Inclusion / Inheritance Descendants | |
Subclasses:
|
|
Super Chains via Extension / Inclusion / Inheritance | |
Instance Chain:
self,
Enumerable
|
|
Inherits: | Object |
Defined in: | 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.
Class Method Summary
-
.new(size) ⇒ Tuple
constructor
Create a new tuple of the given size.
Instance Attribute Summary
-
#size
readonly
The (fixed) size of the tuple.
Instance Method Summary
-
#cas(i, old_value, new_value)
Alias for #compare_and_set.
-
#compare_and_set(i, old_value, new_value) ⇒ Boolean
(also: #cas)
Set
the value at the given index to the new value if and only if the current value matches the given old value. -
#each {|ref| ... }
Calls the given block once for each element in self, passing that element as a parameter.
-
#get(i) ⇒ Object
(also: #volatile_get)
Get the value of the element at the given index.
-
#set(i, value) ⇒ Object
(also: #volatile_set)
Set
the element at the given index to the given value. -
#volatile_get(i)
Alias for #get.
-
#volatile_set(i, value)
Alias for #set.
Constructor Details
.new(size) ⇒ Tuple
Create a new tuple of the given size.
# File 'lib/concurrent-ruby/concurrent/tuple.rb', line 29
def initialize(size) @size = size @tuple = tuple = ::Array.new(size) i = 0 while i < size tuple[i] = Concurrent::AtomicReference.new i += 1 end end
Instance Attribute Details
#size (readonly)
The (fixed) size of the tuple.
# File 'lib/concurrent-ruby/concurrent/tuple.rb', line 24
attr_reader :size
Instance Method Details
#cas(i, old_value, new_value)
Alias for #compare_and_set.
# File 'lib/concurrent-ruby/concurrent/tuple.rb', line 73
alias_method :cas, :compare_and_set
#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.
# File 'lib/concurrent-ruby/concurrent/tuple.rb', line 69
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| ... }
Calls the given block once for each element in self, passing that element as a parameter.
# File 'lib/concurrent-ruby/concurrent/tuple.rb', line 78
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.
# File 'lib/concurrent-ruby/concurrent/tuple.rb', line 43
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
# File 'lib/concurrent-ruby/concurrent/tuple.rb', line 55
def set(i, value) return nil if i >= @size || i < 0 @tuple[i].set(value) end
#volatile_get(i)
Alias for #get.
# File 'lib/concurrent-ruby/concurrent/tuple.rb', line 47
alias_method :volatile_get, :get
#volatile_set(i, value)
Alias for #set.
# File 'lib/concurrent-ruby/concurrent/tuple.rb', line 59
alias_method :volatile_set, :set