|
| 1 | +require 'concurrent/atomic/atomic_reference' |
| 2 | + |
| 3 | +module Concurrent |
| 4 | + |
| 5 | + # A fixed size array with volatile (synchronized, thread safe) getters/setters. |
| 6 | + # Mixes in Ruby's `Enumerable` module for enhanced search, sort, and traversal. |
| 7 | + # |
| 8 | + # @example |
| 9 | + # tuple = Concurrent::Tuple.new(16) |
| 10 | + # |
| 11 | + # tuple.set(0, :foo) #=> :foo | volatile write |
| 12 | + # tuple.get(0) #=> :foo | volatile read |
| 13 | + # tuple.compare_and_set(0, :foo, :bar) #=> true | strong CAS |
| 14 | + # tuple.cas(0, :foo, :baz) #=> false | strong CAS |
| 15 | + # tuple.get(0) #=> :bar | volatile read |
| 16 | + # |
| 17 | + # @see https://en.wikipedia.org/wiki/Tuple Tuple entry at Wikipedia |
| 18 | + # @see http://www.erlang.org/doc/reference_manual/data_types.html#id70396 Erlang Tuple |
| 19 | + # @see http://ruby-doc.org/core-2.2.2/Enumerable.html Enumerable |
| 20 | + class Tuple |
| 21 | + include Enumerable |
| 22 | + |
| 23 | + # The (fixed) size of the tuple. |
| 24 | + attr_reader :size |
| 25 | + |
| 26 | + # @!visibility private |
| 27 | + Tuple = defined?(Rubinius::Tuple) ? Rubinius::Tuple : Array |
| 28 | + private_constant :Tuple |
| 29 | + |
| 30 | + # Create a new tuple of the given size. |
| 31 | + # |
| 32 | + # @param [Integer] size the number of elements in the tuple |
| 33 | + def initialize(size) |
| 34 | + @size = size |
| 35 | + @tuple = tuple = Tuple.new(size) |
| 36 | + i = 0 |
| 37 | + while i < size |
| 38 | + tuple[i] = Concurrent::AtomicReference.new |
| 39 | + i += 1 |
| 40 | + end |
| 41 | + end |
| 42 | + |
| 43 | + # Get the value of the element at the given index. |
| 44 | + # |
| 45 | + # @param [Integer] i the index from which to retrieve the value |
| 46 | + # @return [Object] the value at the given index or nil if the index is out of bounds |
| 47 | + def get(i) |
| 48 | + return nil if i >= @size || i < 0 |
| 49 | + @tuple[i].get |
| 50 | + end |
| 51 | + alias_method :volatile_get, :get |
| 52 | + |
| 53 | + # Set the element at the given index to the given value |
| 54 | + # |
| 55 | + # @param [Integer] i the index for the element to set |
| 56 | + # @param [Object] value the value to set at the given index |
| 57 | + # |
| 58 | + # @return [Object] the new value of the element at the given index or nil if the index is out of bounds |
| 59 | + def set(i, value) |
| 60 | + return nil if i >= @size || i < 0 |
| 61 | + @tuple[i].set(value) |
| 62 | + end |
| 63 | + alias_method :volatile_set, :set |
| 64 | + |
| 65 | + # Set the value at the given index to the new value if and only if the current |
| 66 | + # value matches the given old value. |
| 67 | + # |
| 68 | + # @param [Integer] i the index for the element to set |
| 69 | + # @param [Object] old_value the value to compare against the current value |
| 70 | + # @param [Object] new_value the value to set at the given index |
| 71 | + # |
| 72 | + # @return [Boolean] true if the value at the given element was set else false |
| 73 | + def compare_and_set(i, old_value, new_value) |
| 74 | + return false if i >= @size || i < 0 |
| 75 | + @tuple[i].compare_and_set(old_value, new_value) |
| 76 | + end |
| 77 | + alias_method :cas, :compare_and_set |
| 78 | + |
| 79 | + # Calls the given block once for each element in self, passing that element as a parameter. |
| 80 | + # |
| 81 | + # @yieldparam [Object] ref the `Concurrent::AtomicReference` object at the current index |
| 82 | + def each |
| 83 | + @tuple.each {|ref| yield ref.get} |
| 84 | + end |
| 85 | + end |
| 86 | +end |
0 commit comments