Sha256: c92b8590871f62f380cb79bb52cd7ad699e8b9d6506627f0dd2cdf08a3975240

Contents?: true

Size: 1.6 KB

Versions: 35

Compression:

Stored size: 1.6 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

  # Work-around for 1.8 std-lib not passing block around to delegate.
  # @private
  def method_missing(method, *args, &block)
    monitor = @monitor
    begin
      monitor.enter
      target = self.__getobj__
      if target.respond_to?(method)
        target.__send__(method, *args, &block)
      else
        super(method, *args, &block)
      end
    ensure
      monitor.exit
    end
  end if RUBY_VERSION[0, 3] == '1.8'

end unless defined?(SynchronizedDelegator)

Version data entries

35 entries across 33 versions & 17 rubygems

Version Path
logstash-input-fifo-0.9.1 vendor/bundle/jruby/1.9/gems/thread_safe-0.3.5-java/lib/thread_safe/synchronized_delegator.rb
logstash-input-fifo-0.9.0 vendor/bundle/jruby/1.9/gems/thread_safe-0.3.5-java/lib/thread_safe/synchronized_delegator.rb
logstash-input-salesforce-3.0.0 vendor/jruby/1.9/gems/thread_safe-0.3.5-java/lib/thread_safe/synchronized_delegator.rb
abaci-0.3.0 vendor/bundle/gems/thread_safe-0.3.5/lib/thread_safe/synchronized_delegator.rb
ivanvc-logstash-input-s3-3.1.1.4 vendor/local/gems/thread_safe-0.3.5-java/lib/thread_safe/synchronized_delegator.rb
ivanvc-logstash-input-s3-3.1.1.3 vendor/local/gems/thread_safe-0.3.5-java/lib/thread_safe/synchronized_delegator.rb
ivanvc-logstash-input-s3-3.1.1.2 vendor/local/gems/thread_safe-0.3.5-java/lib/thread_safe/synchronized_delegator.rb
second_step-0.1.2 secondstep-notify-1.0.0-osx/lib/ruby/lib/ruby/gems/2.2.0/gems/thread_safe-0.3.5/lib/thread_safe/synchronized_delegator.rb
logstash-filter-delta-1.1.0 vendor/bundle/jruby/2.2.0/gems/thread_safe-0.3.5-java/lib/thread_safe/synchronized_delegator.rb
logstash-filter-delta-1.0.1 vendor/bundle/jruby/2.2.0/gems/thread_safe-0.3.5-java/lib/thread_safe/synchronized_delegator.rb
logstash-filter-base64-1.0.4 vendor/bundle/jruby/2.2.0/gems/thread_safe-0.3.5-java/lib/thread_safe/synchronized_delegator.rb
logstash-filter-base64-1.0.3 vendor/bundle/jruby/2.2.0/gems/thread_safe-0.3.5-java/lib/thread_safe/synchronized_delegator.rb
logstash-filter-delta-1.0.0 vendor/bundle/jruby/2.2.0/gems/thread_safe-0.3.5-java/lib/thread_safe/synchronized_delegator.rb
logstash-filter-base64-1.0.2 vendor/bundle/jruby/2.3.0/gems/thread_safe-0.3.5-java/lib/thread_safe/synchronized_delegator.rb
logstash-filter-base64-1.0.1 vendor/bundle/jruby/2.3.0/gems/thread_safe-0.3.5-java/lib/thread_safe/synchronized_delegator.rb
logstash-filter-base64-1.0.0 vendor/bundle/jruby/2.3.0/gems/thread_safe-0.3.5-java/lib/thread_safe/synchronized_delegator.rb
able-neo4j-1.0.0 vendor/bundle/jruby/1.9/gems/thread_safe-0.3.5-java/lib/thread_safe/synchronized_delegator.rb
ish_lib_manager-0.0.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/thread_safe-0.3.5/lib/thread_safe/synchronized_delegator.rb
angular-rails4-templates-0.4.1 vendor/ruby/2.1.0/gems/thread_safe-0.3.5/lib/thread_safe/synchronized_delegator.rb
angular-rails4-templates-0.4.0 vendor/ruby/2.1.0/gems/thread_safe-0.3.5/lib/thread_safe/synchronized_delegator.rb