Class: Concurrent::Synchronization::Object
Overview
Abstract object providing final, volatile, ans CAS extensions to build other concurrent abstractions.
-
final instance variables see .safe_initialization!
-
volatile instance variables see .attr_volatile
-
volatile instance variables see .attr_atomic
Class Attribute Summary
- .safe_initialization? ⇒ Boolean readonly
Class Method Summary
- .atomic_attribute?(name) ⇒ true, false
- .atomic_attributes(inherited = true) ⇒ ::Array<Symbol>
-
.attr_atomic(*names) ⇒ ::Array<Symbol>
Creates methods for reading and writing to a instance variable with volatile (Java) semantic as .attr_volatile does.
-
.attr_volatile(*names) ⇒ ::Array<Symbol>
Creates methods for reading and writing (as
attr_accessor
does) to a instance variable with volatile (Java) semantic. -
.ensure_safe_initialization_when_final_fields_are_present ⇒ true
For testing purposes, quite slow.
-
.new ⇒ Object
constructor
Has to be called by children.
- .safe_initialization!
- .define_initialize_atomic_fields private
AbstractObject
- Inherited
Instance Method Summary
Constructor Details
.new ⇒ Object
Has to be called by children.
# File 'lib/concurrent-ruby/concurrent/synchronization/object.rb', line 28
def initialize super __initialize_atomic_fields__ end
Class Attribute Details
.safe_initialization? ⇒ Boolean
(readonly)
[ GitHub ]
# File 'lib/concurrent-ruby/concurrent/synchronization/object.rb', line 37
def self.safe_initialization? self.singleton_class < SafeInitialization end
Class Method Details
.atomic_attribute?(name) ⇒ true
, false
# File 'lib/concurrent-ruby/concurrent/synchronization/object.rb', line 125
def self.atomic_attribute?(name) atomic_attributes.include? name end
.atomic_attributes(inherited = true) ⇒ ::Array
<Symbol
>
# File 'lib/concurrent-ruby/concurrent/synchronization/object.rb', line 119
def self.atomic_attributes(inherited = true) @__atomic_fields__ ||= [] ((superclass.atomic_attributes if superclass.respond_to?(:atomic_attributes) && inherited) || []) + @__atomic_fields__ end
.attr_atomic(*names) ⇒ ::Array
<Symbol
>
Creates methods for reading and writing to a instance variable with volatile (Java) semantic as .attr_volatile does. The instance variable should be accessed oly through generated methods. This method generates following methods: value
, value=(new_value) #=> new_value
, swap_value(new_value) #=> old_value
, compare_and_set_value(expected, value) #=> true || false
, update_value(&block)
.
# File 'lib/concurrent-ruby/concurrent/synchronization/object.rb', line 84
def self.attr_atomic(*names) @__atomic_fields__ ||= [] @__atomic_fields__ += names safe_initialization! define_initialize_atomic_fields names.each do |name| ivar = :"@Atomic#{name.to_s.gsub(/(?:^|_)(.)/) { $1.upcase }}" class_eval <<-RUBY, __FILE__, __LINE__ + 1 def #{name} #{ivar}.get end def #{name}=(value) #{ivar}.set value end def swap_#{name}(value) #{ivar}.swap value end def compare_and_set_#{name}(expected, value) #{ivar}.compare_and_set expected, value end def update_#{name}(&block) #{ivar}.update(&block) end RUBY end names.flat_map { |n| [n, :"#{n}=", :"swap_#{n}", :"compare_and_set_#{n}", :"update_#{n}"] } end
.attr_volatile(*names) ⇒ ::Array
<Symbol
>
Creates methods for reading and writing (as attr_accessor
does) to a instance variable with volatile (Java) semantic. The instance variable should be accessed only through generated methods.
.define_initialize_atomic_fields (private)
[ GitHub ]# File 'lib/concurrent-ruby/concurrent/synchronization/object.rb', line 131
def self.define_initialize_atomic_fields assignments = @__atomic_fields__.map do |name| "@Atomic#{name.to_s.gsub(/(?:^|_)(.)/) { $1.upcase }} = Concurrent::AtomicReference.new(nil)" end.join("\n") class_eval <<-RUBY, __FILE__, __LINE__ + 1 def __initialize_atomic_fields__ super #{assignments} end RUBY end
.ensure_safe_initialization_when_final_fields_are_present ⇒ true
For testing purposes, quite slow. Injects assert code to new method which will raise if class instance contains any instance variables with CamelCase names and isn’t .safe_initialization?.
# File 'lib/concurrent-ruby/concurrent/synchronization/object.rb', line 45
def self.ensure_safe_initialization_when_final_fields_are_present Object.class_eval do def self.new(*args, &block) object = super(*args, &block) ensure has_final_field = object.instance_variables.any? { |v| v.to_s =~ /^@[A-Z]/ } if has_final_field && !safe_initialization? raise "there was an instance of #{object.class} with final field but not marked with safe_initialization!" end end end true end
.safe_initialization!
[ GitHub ]# File 'lib/concurrent-ruby/concurrent/synchronization/object.rb', line 33
def self.safe_initialization! extend SafeInitialization unless safe_initialization? end
Instance Method Details
#__initialize_atomic_fields__ (private)
[ GitHub ]# File 'lib/concurrent-ruby/concurrent/synchronization/object.rb', line 146
def __initialize_atomic_fields__ end