Sha256: f815a0b1e335ffa8fbbec1dc7db446ea4be59a29285cfc7f91ba9989d63c0221

Contents?: true

Size: 1.99 KB

Versions: 215

Compression:

Stored size: 1.99 KB

Contents

require 'thread'
require 'active_support/core_ext/module/aliasing'
require 'active_support/core_ext/array/extract_options'
require 'active_support/core_ext/module/deprecation'

class Module
  # Synchronize access around a method, delegating synchronization to a
  # particular mutex. A mutex (either a Mutex, or any object that responds to
  # #synchronize and yields to a block) must be provided as a final :with option.
  # The :with option should be a symbol or string, and can represent a method,
  # constant, or instance or class variable.
  # Example:
  #   class SharedCache
  #     @@lock = Mutex.new
  #     def expire
  #       ...
  #     end
  #     synchronize :expire, :with => :@@lock
  #   end
  def synchronize(*methods)
    options = methods.extract_options!
    unless options.is_a?(Hash) && with = options[:with]
      raise ArgumentError, "Synchronization needs a mutex. Supply an options hash with a :with key as the last argument (e.g. synchronize :hello, :with => :@mutex)."
    end

    methods.each do |method|
      aliased_method, punctuation = method.to_s.sub(/([?!=])$/, ''), $1

      if method_defined?("#{aliased_method}_without_synchronization#{punctuation}")
        raise ArgumentError, "#{method} is already synchronized. Double synchronization is not currently supported."
      end

      module_eval(<<-EOS, __FILE__, __LINE__ + 1)
        def #{aliased_method}_with_synchronization#{punctuation}(*args, &block)     # def expire_with_synchronization(*args, &block)
          #{with}.synchronize do                                                    #   @@lock.synchronize do
            #{aliased_method}_without_synchronization#{punctuation}(*args, &block)  #     expire_without_synchronization(*args, &block)
          end                                                                       #   end
        end                                                                         # end
      EOS

      alias_method_chain method, :synchronization
    end
  end
  deprecate :synchronize
end

Version data entries

215 entries across 171 versions & 24 rubygems

Version Path
active_mailer-0.0.3 test/fixtures/dummyapp_rails_3.2/vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.12/lib/active_support/core_ext/module/synchronization.rb
activesupport-3.2.12 lib/active_support/core_ext/module/synchronization.rb
graphael-on-rails-0.5.1 vendor/bundle/gems/activesupport-3.2.11/lib/active_support/core_ext/module/synchronization.rb
graphael-on-rails-0.0.1 vendor/bundle/gems/activesupport-3.2.11/lib/active_support/core_ext/module/synchronization.rb
challah-0.9.1.beta.3 vendor/bundle/gems/activesupport-3.2.11/lib/active_support/core_ext/module/synchronization.rb
devise_sociable-0.1.0 vendor/bundle/gems/activesupport-3.2.11/lib/active_support/core_ext/module/synchronization.rb
backbone-queryparams-rails-0.0.1 vendor/bundle/gems/activesupport-3.2.11/lib/active_support/core_ext/module/synchronization.rb
railscast-assets-0.0.2 vendor/bundle/gems/activesupport-3.2.11/lib/active_support/core_ext/module/synchronization.rb
railscast-assets-0.0.2 vendor/bundle/gems/backbone-forms-on-rails-0.10.0/vendor/bundle/gems/activesupport-3.2.11/lib/active_support/core_ext/module/synchronization.rb
backbone-forms-on-rails-0.10.0 vendor/bundle/gems/backbone-forms-on-rails-0.10.0/vendor/bundle/gems/activesupport-3.2.11/lib/active_support/core_ext/module/synchronization.rb
backbone-forms-on-rails-0.10.0 vendor/bundle/gems/activesupport-3.2.11/lib/active_support/core_ext/module/synchronization.rb
backbone-deep-rails-0.0.2 vendor/bundle/gems/activesupport-3.2.11/lib/active_support/core_ext/module/synchronization.rb
backbone-deep-rails-0.0.1 vendor/bundle/gems/activesupport-3.2.11/lib/active_support/core_ext/module/synchronization.rb
challah-0.9.1.beta vendor/bundle/gems/activesupport-3.2.11/lib/active_support/core_ext/module/synchronization.rb
activesupport-3.2.11 lib/active_support/core_ext/module/synchronization.rb
sunrise-cms-0.5.0.rc1 vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.10/lib/active_support/core_ext/module/synchronization.rb
challah-0.9.0 vendor/bundle/gems/activesupport-3.2.9/lib/active_support/core_ext/module/synchronization.rb
activesupport-3.2.10 lib/active_support/core_ext/module/synchronization.rb
classiccms-0.6.9 vendor/bundle/gems/activesupport-3.2.3/lib/active_support/core_ext/module/synchronization.rb
jquery_regex-0.0.3 vendor/bundle/gems/jquery_regex-0.0.3/vendor/bundle/gems/jquery_regex-0.0.2/vendor/bundle/gems/activesupport-3.2.9/lib/active_support/core_ext/module/synchronization.rb