Sha256: dba99134a6d2a33515b0fd55ea1b5be7b03b67f9ff6e2f3fc66b1b8446c853dc

Contents?: true

Size: 1 KB

Versions: 10

Compression:

Stored size: 1 KB

Contents

module ThreadSafe
  module Util
    # A fixed size array with volatile volatile getters/setters.
    # Usage:
    #   arr = VolatileTuple.new(16)
    #   arr.volatile_set(0, :foo)
    #   arr.volatile_get(0)    # => :foo
    #   arr.cas(0, :foo, :bar) # => true
    #   arr.volatile_get(0)    # => :bar
    class VolatileTuple
      include Enumerable

      Tuple = defined?(Rubinius::Tuple) ? Rubinius::Tuple : Array

      def initialize(size)
        @tuple = tuple = Tuple.new(size)
        i = 0
        while i < size
          tuple[i] = AtomicReference.new
          i += 1
        end
      end

      def volatile_get(i)
        @tuple[i].get
      end

      def volatile_set(i, value)
        @tuple[i].set(value)
      end

      def compare_and_set(i, old_value, new_value)
        @tuple[i].compare_and_set(old_value, new_value)
      end
      alias_method :cas, :compare_and_set

      def size
        @tuple.size
      end

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

Version data entries

10 entries across 10 versions & 3 rubygems

Version Path
thread_safe-0.1.3 lib/thread_safe/util/volatile_tuple.rb
thread_safe-0.1.3-java lib/thread_safe/util/volatile_tuple.rb
thread_safe-ianunruh-0.1.1 lib/thread_safe/util/volatile_tuple.rb
thread_safe-ianunruh-0.1.1-java lib/thread_safe/util/volatile_tuple.rb
thread_safe-0.1.2 lib/thread_safe/util/volatile_tuple.rb
thread_safe-0.1.2-java lib/thread_safe/util/volatile_tuple.rb
thread_safe-0.1.1 lib/thread_safe/util/volatile_tuple.rb
thread_safe-0.1.1-java lib/thread_safe/util/volatile_tuple.rb
challah-1.0.0 vendor/bundle/gems/thread_safe-0.1.0/lib/thread_safe/util/volatile_tuple.rb
thread_safe-0.1.0 lib/thread_safe/util/volatile_tuple.rb