# 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
defsynchronize(*methods)
options=methods.extract_options!
unlessoptions.is_a?(Hash)&&with=options[:with]
raiseArgumentError,"Synchronization needs a mutex. Supply an options hash with a :with key as the last argument (e.g. synchronize :hello, :with => :@mutex)."
NOTE: Defined in +active_support/core_ext/module/qualified_const.rb+.
h4. Synchronization
The +synchronize+ macro declares a method to be synchronized:
<ruby>
class Counter
@@mutex = Mutex.new
attr_reader :value
def initialize
@value = 0
end
def incr
@value += 1 # non-atomic
end
synchronize :incr, :with => '@@mutex'
end
</ruby>
The method receives the name of an action, and a +:with+ option with code. The code is evaluated in the context of the receiver each time the method is invoked, and it should evaluate to a +Mutex+ instance or any other object that responds to +synchronize+ and accepts a block.
NOTE: Defined in +active_support/core_ext/module/synchronization.rb+.
h4. Reachable
A named module is reachable if it is stored in its corresponding constant. It means you can reach the module object via the constant.