Sha256: 9fd10964d8cca1ce5c609f9856e3ceb3365586b467463689f0cade9d0cd4d1f6

Contents?: true

Size: 1.46 KB

Versions: 14

Compression:

Stored size: 1.46 KB

Contents

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__)
        def #{aliased_method}_with_synchronization#{punctuation}(*args, &block)
          #{with}.synchronize do
            #{aliased_method}_without_synchronization#{punctuation}(*args, &block)
          end
        end
      EOS

      alias_method_chain method, :synchronization
    end
  end
end

Version data entries

14 entries across 13 versions & 8 rubygems

Version Path
p8-castronaut-0.6.1.1 vendor/activesupport/lib/active_support/core_ext/module/synchronization.rb
relevance-castronaut-0.6.0 vendor/activesupport/lib/active_support/core_ext/module/synchronization.rb
relevance-castronaut-0.6.1 vendor/activesupport/lib/active_support/core_ext/module/synchronization.rb
relevance-castronaut-0.7.4 vendor/activesupport/lib/active_support/core_ext/module/synchronization.rb
relevance-castronaut-0.7.5 vendor/activesupport/lib/active_support/core_ext/module/synchronization.rb
nbudin-castronaut-0.7.5 vendor/activesupport/lib/active_support/core_ext/module/synchronization.rb
usher-0.7.0 spec/rails2_2/vendor/rails/vendor/rails/activesupport/pkg/activesupport-2.2.2/lib/active_support/core_ext/module/synchronization.rb
usher-0.7.0 spec/rails2_2/vendor/rails/vendor/rails/activesupport/lib/active_support/core_ext/module/synchronization.rb
factorylabs-castronaut-0.7.5 vendor/activesupport/lib/active_support/core_ext/module/synchronization.rb
activesupport-2.2.3 lib/active_support/core_ext/module/synchronization.rb
activesupport-2.2.2 lib/active_support/core_ext/module/synchronization.rb
mack-active_record-0.8.2 lib/gems/activesupport-2.2.2/lib/active_support/core_ext/module/synchronization.rb
mack-facets-0.8.3 lib/gems/activesupport-2.2.2/lib/active_support/core_ext/module/synchronization.rb
mack-facets-0.8.3.1 lib/gems/activesupport-2.2.2/lib/active_support/core_ext/module/synchronization.rb