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
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/concurrent-ruby-1.1.10/lib/concurrent-ruby/concurrent/thread_safe/synchronized_delegator.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/concurrent-ruby-1.1.9/lib/concurrent-ruby/concurrent/thread_safe/synchronized_delegator.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/concurrent-ruby-1.1.10/lib/concurrent-ruby/concurrent/thread_safe/synchronized_delegator.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/concurrent-ruby-1.1.9/lib/concurrent-ruby/concurrent/thread_safe/synchronized_delegator.rb
rubypitaya-3.12.5 ./lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/concurrent-ruby-1.1.10/lib/concurrent-ruby/concurrent/thread_safe/synchronized_delegator.rb
rubypitaya-3.12.4 ./lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/concurrent-ruby-1.1.10/lib/concurrent-ruby/concurrent/thread_safe/synchronized_delegator.rb
rubypitaya-3.12.3 ./lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/concurrent-ruby-1.1.10/lib/concurrent-ruby/concurrent/thread_safe/synchronized_delegator.rb
rubypitaya-3.12.2 ./lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/concurrent-ruby-1.1.10/lib/concurrent-ruby/concurrent/thread_safe/synchronized_delegator.rb
o-concurrent-ruby-1.1.13 lib/concurrent-ruby/concurrent/thread_safe/synchronized_delegator.rb
o-concurrent-ruby-1.1.12 lib/concurrent-ruby/concurrent/thread_safe/synchronized_delegator.rb
o-concurrent-ruby-1.1.11 lib/concurrent-ruby/concurrent/thread_safe/synchronized_delegator.rb
concurrent-ruby-1.1.10 lib/concurrent-ruby/concurrent/thread_safe/synchronized_delegator.rb
ric-0.14.2 vendor/bundle/ruby/2.7.0/gems/concurrent-ruby-1.1.9/lib/concurrent-ruby/concurrent/thread_safe/synchronized_delegator.rb
ric-0.14.1 vendor/bundle/ruby/2.7.0/gems/concurrent-ruby-1.1.9/lib/concurrent-ruby/concurrent/thread_safe/synchronized_delegator.rb
phillipug-foodie-0.1.0 .vendor/ruby/3.0.0/gems/concurrent-ruby-1.1.9/lib/concurrent-ruby/concurrent/thread_safe/synchronized_delegator.rb
ric-0.14.0 vendor/bundle/ruby/2.7.0/gems/concurrent-ruby-1.1.9/lib/concurrent-ruby/concurrent/thread_safe/synchronized_delegator.rb
vagrant-unbundled-2.2.19.0 vendor/bundle/ruby/3.0.0/gems/concurrent-ruby-1.1.9/lib/concurrent-ruby/concurrent/thread_safe/synchronized_delegator.rb
date_n_time_picker_activeadmin-0.1.2 vendor/bundle/ruby/2.6.0/gems/concurrent-ruby-1.1.9/lib/concurrent-ruby/concurrent/thread_safe/synchronized_delegator.rb
date_n_time_picker_activeadmin-0.1.1 vendor/bundle/ruby/2.6.0/gems/concurrent-ruby-1.1.9/lib/concurrent-ruby/concurrent/thread_safe/synchronized_delegator.rb
vagrant-unbundled-2.2.18.0 vendor/bundle/ruby/3.0.0/gems/concurrent-ruby-1.1.9/lib/concurrent-ruby/concurrent/thread_safe/synchronized_delegator.rb