Froze rails gems
[depot.git] / vendor / rails / activesupport / lib / active_support / core_ext / module / synchronization.rb
1 class Module
2 # Synchronize access around a method, delegating synchronization to a
3 # particular mutex. A mutex (either a Mutex, or any object that responds to
4 # #synchronize and yields to a block) must be provided as a final :with option.
5 # The :with option should be a symbol or string, and can represent a method,
6 # constant, or instance or class variable.
7 # Example:
8 # class SharedCache
9 # @@lock = Mutex.new
10 # def expire
11 # ...
12 # end
13 # synchronize :expire, :with => :@@lock
14 # end
15 def synchronize(*methods)
16 options = methods.extract_options!
17 unless options.is_a?(Hash) && with = options[:with]
18 raise ArgumentError, "Synchronization needs a mutex. Supply an options hash with a :with key as the last argument (e.g. synchronize :hello, :with => :@mutex)."
19 end
20
21 methods.each do |method|
22 aliased_method, punctuation = method.to_s.sub(/([?!=])$/, ''), $1
23
24 if method_defined?("#{aliased_method}_without_synchronization#{punctuation}")
25 raise ArgumentError, "#{method} is already synchronized. Double synchronization is not currently supported."
26 end
27
28 module_eval(<<-EOS, __FILE__, __LINE__)
29 def #{aliased_method}_with_synchronization#{punctuation}(*args, &block)
30 #{with}.synchronize do
31 #{aliased_method}_without_synchronization#{punctuation}(*args, &block)
32 end
33 end
34 EOS
35
36 alias_method_chain method, :synchronization
37 end
38 end
39 end