123456789_123456789_123456789_123456789_123456789_

Class: Concurrent::SynchronizedDelegator

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, SimpleDelegator
Instance Chain:
self, SimpleDelegator
Inherits: SimpleDelegator
  • ::Object
Defined in: lib/concurrent-ruby/concurrent/thread_safe/synchronized_delegator.rb

Overview

This class provides a trivial way to synchronize all calls to a given object by wrapping it with a Delegator that performs Monitor#enter/exit calls around the delegated #send. Example:

array = [] # not thread-safe on many impls
array = SynchronizedDelegator.new([]) # thread-safe

A simple Monitor provides a very coarse-grained way to synchronize a given object, in that it will cause synchronization for methods that have no need for it, but this is a trivial way to get thread-safety where none may exist currently on some implementations.

This class is currently being considered for inclusion into stdlib, via bugs.ruby-lang.org/issues/8556

Class Method Summary

Instance Method Summary

Constructor Details

.new(obj) ⇒ SynchronizedDelegator

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/thread_safe/synchronized_delegator.rb', line 31

def initialize(obj)
  __setobj__(obj)
  @monitor = Monitor.new
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block)

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/thread_safe/synchronized_delegator.rb', line 36

def method_missing(method, *args, &block)
  monitor = @monitor
  begin
    monitor.enter
    super
  ensure
    monitor.exit
  end
end

Instance Method Details

#setup

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/thread_safe/synchronized_delegator.rb', line 22

def setup
  @old_abort = Thread.abort_on_exception
  Thread.abort_on_exception = true
end

#teardown

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/thread_safe/synchronized_delegator.rb', line 27

def teardown
  Thread.abort_on_exception = @old_abort
end