Sha256: 2aabe013e7106bd697d5a7c7d080e8c7b75b1a01008560031d9b02f27b1106d9

Contents?: true

Size: 1010 Bytes

Versions: 8

Compression:

Stored size: 1010 Bytes

Contents

# This class provides a trivial way to synchronize all calls to a given object
# by wrapping it with a Delegator that performs Mutex#lock/unlock calls around
# the delegated #send. Example:
#
#   array = [] # not thread-safe on many impls
#   array = MutexedDelegator.new(array) # thread-safe
#
# A simple Mutex 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

require 'delegate'

unless defined?(SynchronizedDelegator)
  class SynchronizedDelegator < SimpleDelegator
    def initialize(*)
      super
      @mutex = Mutex.new
    end
    
    def method_missing(m, *args, &block)
      begin
        mutex = @mutex
	mutex.lock
	super
      ensure
	mutex.unlock
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 2 rubygems

Version Path
thread_safe-0.1.3 lib/thread_safe/synchronized_delegator.rb
thread_safe-0.1.3-java lib/thread_safe/synchronized_delegator.rb
thread_safe-ianunruh-0.1.1 lib/thread_safe/synchronized_delegator.rb
thread_safe-ianunruh-0.1.1-java lib/thread_safe/synchronized_delegator.rb
thread_safe-0.1.2 lib/thread_safe/synchronized_delegator.rb
thread_safe-0.1.2-java lib/thread_safe/synchronized_delegator.rb
thread_safe-0.1.1 lib/thread_safe/synchronized_delegator.rb
thread_safe-0.1.1-java lib/thread_safe/synchronized_delegator.rb