123456789_123456789_123456789_123456789_123456789_

Class: Concurrent::Synchronization::Object

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Subclasses:
AbstractExchanger, Atom, AtomicMarkableReference, Cancellation, JavaExchanger, LazyRegister, LockFreeQueue, LockFreeStack, MVar, Maybe, ProcessingActor, ReadWriteLock, ReentrantReadWriteLock, RubyExchanger, TVar, Throttle, WrappingExecutor, Channel::Tick, ErlangActor::AbstractActor, ErlangActor::AbstractSignal, ErlangActor::Ask, ErlangActor::DeMonitor, ErlangActor::DownSignal, ErlangActor::Environment, ErlangActor::Kill, ErlangActor::Link, ErlangActor::Monitor, ErlangActor::OnPool, ErlangActor::OnThread, ErlangActor::Pid, ErlangActor::Terminate, ErlangActor::UnLink, LockFreeQueue::Node, Promises::AbstractAnyPromise, Promises::AbstractEventFuture, Promises::AbstractFlatPromise, Promises::AbstractPromise, Promises::AnyFulfilledFuturePromise, Promises::AnyResolvedEventPromise, Promises::AnyResolvedFuturePromise, Promises::BlockedPromise, Promises::BlockedTaskPromise, Promises::ChainPromise, Promises::Channel, Promises::DelayPromise, Promises::Event, Promises::EventWrapperPromise, Promises::FlatEventPromise, Promises::FlatFuturePromise, Promises::Future, Promises::FutureWrapperPromise, Promises::ImmediateEventPromise, Promises::ImmediateFuturePromise, Promises::InnerPromise, Promises::RescuePromise, Promises::ResolvableEvent, Promises::ResolvableEventPromise, Promises::ResolvableFuture, Promises::ResolvableFuturePromise, Promises::RunFuturePromise, Promises::ScheduledPromise, Promises::ThenPromise, Promises::ZipEventEventPromise, Promises::ZipEventsPromise, Promises::ZipFutureEventPromise, Promises::ZipFuturesPromise, RubyExchanger::Node, AbstractLockableObject, JRubyLockableObject, MonitorLockableObject, MutexLockableObject, Throttle::ProxyExecutor, Edge::LockFreeLinkedSet::Head, Edge::LockFreeLinkedSet::Node, Edge::LockFreeLinkedSet::Tail
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
Instance Chain:
Inherits: Concurrent::Synchronization::AbstractObject
Defined in: lib/concurrent-ruby/concurrent/synchronization/object.rb

Overview

Abstract object providing final, volatile, ans CAS extensions to build other concurrent abstractions.

Class Attribute Summary

Class Method Summary

AbstractObject - Inherited

Instance Method Summary

Constructor Details

.newObject

Has to be called by children.

[ GitHub ]

  
# 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

Returns:

  • (true, false)

    is the attribute with name atomic?

[ GitHub ]

  
# 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>

Parameters:

  • inherited (true, false) (defaults to: true)

    should inherited volatile with CAS fields be returned?

Returns:

  • (::Array<Symbol>)

    Returns defined volatile with CAS fields on this class.

[ GitHub ]

  
# 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).

Parameters:

  • names (::Array<Symbol>)

    of the instance variables to be volatile with CAS.

Returns:

  • (::Array<Symbol>)

    names of defined method names.

[ GitHub ]

  
# 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.

Parameters:

  • names (::Array<Symbol>)

    of the instance variables to be volatile

Returns:

  • (::Array<Symbol>)

    names of defined method names

[ GitHub ]

.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_presenttrue

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?.

Raises:

  • when offend found

[ GitHub ]

  
# 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