Sha256: 8a58d6f581700dac66fd50d80faf8daf46454ae1fdf85b1d26267b9c36529c32

Contents?: true

Size: 1.39 KB

Versions: 211

Compression:

Stored size: 1.39 KB

Contents

require 'delegate'
require 'monitor'

module Concurrent
  unless defined?(SynchronizedDelegator)

    # 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
    #
    # @!visibility private
    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
  end
end

Version data entries

211 entries across 188 versions & 44 rubygems

Version Path
concurrent-ruby-1.1.7 lib/concurrent-ruby/concurrent/thread_safe/synchronized_delegator.rb
metanorma-cli-1.3.4 gems/ruby/2.6.0/gems/concurrent-ruby-1.1.6/lib/concurrent-ruby/concurrent/thread_safe/synchronized_delegator.rb
metanorma-cli-1.3.3.1 gems/ruby/2.6.0/gems/concurrent-ruby-1.1.6/lib/concurrent-ruby/concurrent/thread_safe/synchronized_delegator.rb
logstash-output-scalyr-0.1.5 vendor/bundle/jruby/2.5.0/gems/concurrent-ruby-1.1.5/lib/concurrent/thread_safe/synchronized_delegator.rb
logstash-output-scalyr-0.1.5 vendor/bundle/jruby/2.5.0/gems/concurrent-ruby-1.1.6/lib/concurrent-ruby/concurrent/thread_safe/synchronized_delegator.rb
files.com-1.0.1 vendor/bundle/ruby/2.5.0/gems/concurrent-ruby-1.1.6/lib/concurrent-ruby/concurrent/thread_safe/synchronized_delegator.rb
logstash-output-scalyr-0.1.4 vendor/bundle/jruby/2.5.0/gems/concurrent-ruby-1.1.5/lib/concurrent/thread_safe/synchronized_delegator.rb
logstash-output-scalyr-0.1.4 vendor/bundle/jruby/2.5.0/gems/concurrent-ruby-1.1.6/lib/concurrent-ruby/concurrent/thread_safe/synchronized_delegator.rb
logstash-output-scalyr-0.1.3 vendor/bundle/jruby/2.5.0/gems/concurrent-ruby-1.1.5/lib/concurrent/thread_safe/synchronized_delegator.rb
logstash-output-scalyr-0.1.3 vendor/bundle/jruby/2.5.0/gems/concurrent-ruby-1.1.6/lib/concurrent-ruby/concurrent/thread_safe/synchronized_delegator.rb
vagrant-unbundled-2.2.9.0 vendor/bundle/ruby/2.7.0/gems/concurrent-ruby-1.1.6/lib/concurrent-ruby/concurrent/thread_safe/synchronized_delegator.rb
files.com-1.0.55 docs/vendor/bundle/ruby/2.5.0/gems/concurrent-ruby-1.1.4/lib/concurrent/thread_safe/synchronized_delegator.rb
logstash-output-scalyr-0.1.2 vendor/bundle/jruby/2.5.0/gems/concurrent-ruby-1.1.6/lib/concurrent-ruby/concurrent/thread_safe/synchronized_delegator.rb
logstash-output-scalyr-0.1.2 vendor/bundle/jruby/2.5.0/gems/concurrent-ruby-1.1.5/lib/concurrent/thread_safe/synchronized_delegator.rb
vagrant-unbundled-2.2.8.0 vendor/bundle/ruby/2.7.0/gems/concurrent-ruby-1.1.6/lib/concurrent-ruby/concurrent/thread_safe/synchronized_delegator.rb
argon-1.3.1 vendor/bundle/ruby/2.7.0/gems/concurrent-ruby-1.1.6/lib/concurrent-ruby/concurrent/thread_safe/synchronized_delegator.rb
symbolic_enum-1.1.5 vendor/bundle/ruby/2.7.0/gems/concurrent-ruby-1.1.6/lib/concurrent-ruby/concurrent/thread_safe/synchronized_delegator.rb
concurrent-ruby-1.1.6 lib/concurrent-ruby/concurrent/thread_safe/synchronized_delegator.rb
vagrant-unbundled-2.2.7.0 vendor/bundle/ruby/2.7.0/gems/concurrent-ruby-1.1.5/lib/concurrent/thread_safe/synchronized_delegator.rb
vagrant-unbundled-2.2.7.0 vendor/bundle/ruby/2.6.0/gems/concurrent-ruby-1.1.5/lib/concurrent/thread_safe/synchronized_delegator.rb