Class: ThreadSafe::Util::VolatileTuple
- Inherits:
-
Object
- Object
- ThreadSafe::Util::VolatileTuple
- Includes:
- Enumerable
- Defined in:
- lib/thread_safe/util/volatile_tuple.rb
Overview
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
Direct Known Subclasses
Constant Summary collapse
- Tuple =
defined?(Rubinius::Tuple) ? Rubinius::Tuple : Array
Instance Method Summary collapse
- #compare_and_set(i, old_value, new_value) ⇒ Object (also: #cas)
- #each ⇒ Object
-
#initialize(size) ⇒ VolatileTuple
constructor
A new instance of VolatileTuple.
- #size ⇒ Object
- #volatile_get(i) ⇒ Object
- #volatile_set(i, value) ⇒ Object
Constructor Details
#initialize(size) ⇒ VolatileTuple
Returns a new instance of VolatileTuple.
15 16 17 18 19 20 21 22 |
# File 'lib/thread_safe/util/volatile_tuple.rb', line 15 def initialize(size) @tuple = tuple = Tuple.new(size) i = 0 while i < size tuple[i] = AtomicReference.new i += 1 end end |
Instance Method Details
#compare_and_set(i, old_value, new_value) ⇒ Object Also known as: cas
32 33 34 |
# File 'lib/thread_safe/util/volatile_tuple.rb', line 32 def compare_and_set(i, old_value, new_value) @tuple[i].compare_and_set(old_value, new_value) end |
#each ⇒ Object
41 42 43 |
# File 'lib/thread_safe/util/volatile_tuple.rb', line 41 def each @tuple.each {|ref| yield ref.get} end |
#size ⇒ Object
37 38 39 |
# File 'lib/thread_safe/util/volatile_tuple.rb', line 37 def size @tuple.size end |
#volatile_get(i) ⇒ Object
24 25 26 |
# File 'lib/thread_safe/util/volatile_tuple.rb', line 24 def volatile_get(i) @tuple[i].get end |
#volatile_set(i, value) ⇒ Object
28 29 30 |
# File 'lib/thread_safe/util/volatile_tuple.rb', line 28 def volatile_set(i, value) @tuple[i].set(value) end |