RubyLearning / ProjectTrak / vendor / rails / activesupport / lib / active_support / core_ext / module / synchronization.rb

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 =
  #     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)."

    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."

      module_eval(<<-EOS, __FILE__, __LINE__)
        def #{aliased_method}_with_synchronization#{punctuation}(*args, &block)
          #{with}.synchronize do
            #{aliased_method}_without_synchronization#{punctuation}(*args, &block)

      alias_method_chain method, :synchronization
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.