Sha256: cb8237e6572fc5e25a303f81ed91b4e22d36e9a2e6e038229a2838254a5a2a9f

Contents?: true

Size: 1.27 KB

Versions: 103

Compression:

Stored size: 1.27 KB

Contents

require 'delegate'
require 'monitor'

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

Version data entries

103 entries across 103 versions & 16 rubygems

Version Path
harbr-0.2.8 vendor/bundle/ruby/3.2.0/gems/concurrent-ruby-1.2.2/lib/concurrent-ruby/concurrent/thread_safe/synchronized_delegator.rb
harbr-0.2.7 vendor/bundle/ruby/3.2.0/gems/concurrent-ruby-1.2.2/lib/concurrent-ruby/concurrent/thread_safe/synchronized_delegator.rb
harbr-0.2.6 vendor/bundle/ruby/3.2.0/gems/concurrent-ruby-1.2.2/lib/concurrent-ruby/concurrent/thread_safe/synchronized_delegator.rb
harbr-0.2.5 vendor/bundle/ruby/3.2.0/gems/concurrent-ruby-1.2.2/lib/concurrent-ruby/concurrent/thread_safe/synchronized_delegator.rb
harbr-0.2.4 vendor/bundle/ruby/3.2.0/gems/concurrent-ruby-1.2.2/lib/concurrent-ruby/concurrent/thread_safe/synchronized_delegator.rb
harbr-0.2.3 vendor/bundle/ruby/3.2.0/gems/concurrent-ruby-1.2.2/lib/concurrent-ruby/concurrent/thread_safe/synchronized_delegator.rb
harbr-0.2.2 vendor/bundle/ruby/3.2.0/gems/concurrent-ruby-1.2.2/lib/concurrent-ruby/concurrent/thread_safe/synchronized_delegator.rb
harbr-0.2.1 vendor/bundle/ruby/3.2.0/gems/concurrent-ruby-1.2.2/lib/concurrent-ruby/concurrent/thread_safe/synchronized_delegator.rb
harbr-0.2.0 vendor/bundle/ruby/3.2.0/gems/concurrent-ruby-1.2.2/lib/concurrent-ruby/concurrent/thread_safe/synchronized_delegator.rb
harbr-0.1.99 vendor/bundle/ruby/3.2.0/gems/concurrent-ruby-1.2.2/lib/concurrent-ruby/concurrent/thread_safe/synchronized_delegator.rb
harbr-0.1.98 vendor/bundle/ruby/3.2.0/gems/concurrent-ruby-1.2.2/lib/concurrent-ruby/concurrent/thread_safe/synchronized_delegator.rb
harbr-0.1.97 vendor/bundle/ruby/3.2.0/gems/concurrent-ruby-1.2.2/lib/concurrent-ruby/concurrent/thread_safe/synchronized_delegator.rb
harbr-0.1.96 vendor/bundle/ruby/3.2.0/gems/concurrent-ruby-1.2.2/lib/concurrent-ruby/concurrent/thread_safe/synchronized_delegator.rb
harbr-0.1.95 vendor/bundle/ruby/3.2.0/gems/concurrent-ruby-1.2.2/lib/concurrent-ruby/concurrent/thread_safe/synchronized_delegator.rb
harbr-0.1.94 vendor/bundle/ruby/3.2.0/gems/concurrent-ruby-1.2.2/lib/concurrent-ruby/concurrent/thread_safe/synchronized_delegator.rb
harbr-0.1.93 vendor/bundle/ruby/3.2.0/gems/concurrent-ruby-1.2.2/lib/concurrent-ruby/concurrent/thread_safe/synchronized_delegator.rb
harbr-0.1.91 vendor/bundle/ruby/3.2.0/gems/concurrent-ruby-1.2.2/lib/concurrent-ruby/concurrent/thread_safe/synchronized_delegator.rb
harbr-0.1.90 vendor/bundle/ruby/3.2.0/gems/concurrent-ruby-1.2.2/lib/concurrent-ruby/concurrent/thread_safe/synchronized_delegator.rb
harbr-0.1.89 vendor/bundle/ruby/3.2.0/gems/concurrent-ruby-1.2.2/lib/concurrent-ruby/concurrent/thread_safe/synchronized_delegator.rb
harbr-0.1.88 vendor/bundle/ruby/3.2.0/gems/concurrent-ruby-1.2.2/lib/concurrent-ruby/concurrent/thread_safe/synchronized_delegator.rb