Sha256: 11a91074f8c460579f40e6ccde0dcd2ca4e2d76e6a3e5ff1fd2570f6da95e931

Contents?: true

Size: 1.19 KB

Versions: 104

Compression:

Stored size: 1.19 KB

Contents

require 'delegate'
require 'monitor'

# 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
# https://bugs.ruby-lang.org/issues/8556
class SynchronizedDelegator < SimpleDelegator
  def setup
    @old_abort = Thread.abort_on_exception
    Thread.abort_on_exception = true
  end

  def teardown
    Thread.abort_on_exception = @old_abort
  end

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

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

end unless defined?(SynchronizedDelegator)

Version data entries

104 entries across 98 versions & 30 rubygems

Version Path
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/thread_safe-0.3.6/lib/thread_safe/synchronized_delegator.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/thread_safe-0.3.6/lib/thread_safe/synchronized_delegator.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/thread_safe-0.3.6/lib/thread_safe/synchronized_delegator.rb
mumukit-content-type-1.12.1 vendor/bundle/ruby/2.7.0/gems/thread_safe-0.3.6/lib/thread_safe/synchronized_delegator.rb
mumukit-content-type-1.12.0 vendor/bundle/ruby/2.7.0/gems/thread_safe-0.3.6/lib/thread_safe/synchronized_delegator.rb
logstash-output-scalyr-0.2.1.beta vendor/bundle/jruby/2.5.0/gems/thread_safe-0.3.6-java/lib/thread_safe/synchronized_delegator.rb
logstash-output-scalyr-0.2.0 vendor/bundle/jruby/2.5.0/gems/thread_safe-0.3.6-java/lib/thread_safe/synchronized_delegator.rb
logstash-output-scalyr-0.2.0.beta vendor/bundle/jruby/2.5.0/gems/thread_safe-0.3.6-java/lib/thread_safe/synchronized_delegator.rb
logstash-output-scalyr-0.1.26.beta vendor/bundle/jruby/2.5.0/gems/thread_safe-0.3.6-java/lib/thread_safe/synchronized_delegator.rb
logstash-output-scalyr-0.1.25.beta vendor/bundle/jruby/2.5.0/gems/thread_safe-0.3.6-java/lib/thread_safe/synchronized_delegator.rb
logstash-output-scalyr-0.1.24.beta vendor/bundle/jruby/2.5.0/gems/thread_safe-0.3.6-java/lib/thread_safe/synchronized_delegator.rb
logstash-output-scalyr-0.1.23.beta vendor/bundle/jruby/2.5.0/gems/thread_safe-0.3.6-java/lib/thread_safe/synchronized_delegator.rb
logstash-output-scalyr-0.1.22.beta vendor/bundle/jruby/2.5.0/gems/thread_safe-0.3.6-java/lib/thread_safe/synchronized_delegator.rb
logstash-output-scalyr-0.1.21.beta vendor/bundle/jruby/2.5.0/gems/thread_safe-0.3.6-java/lib/thread_safe/synchronized_delegator.rb
mumukit-content-type-1.11.1 vendor/bundle/ruby/2.6.0/gems/thread_safe-0.3.6/lib/thread_safe/synchronized_delegator.rb
logstash-output-scalyr-0.1.20.beta vendor/bundle/jruby/2.5.0/gems/thread_safe-0.3.6-java/lib/thread_safe/synchronized_delegator.rb
logstash-output-scalyr-0.1.19.beta vendor/bundle/jruby/2.5.0/gems/thread_safe-0.3.6-java/lib/thread_safe/synchronized_delegator.rb
logstash-output-scalyr-0.1.18.beta vendor/bundle/jruby/2.5.0/gems/thread_safe-0.3.6-java/lib/thread_safe/synchronized_delegator.rb
logstash-output-scalyr-0.1.17.beta vendor/bundle/jruby/2.5.0/gems/thread_safe-0.3.6-java/lib/thread_safe/synchronized_delegator.rb
logstash-output-scalyr-0.1.16.beta vendor/bundle/jruby/2.5.0/gems/thread_safe-0.3.6-java/lib/thread_safe/synchronized_delegator.rb