IM
Size: a a a
IM
IM
w
w
w
IM
w
ruby
class SynchronizedAccess < Module
def initialize(mutex)
@mutex= mutex
module_eval(<<~RUBY)
def initialize(*)
#{mutex} = Mutex.new
super
end
RUBY
end
def prepended(base)
mod = self
base.singleton_class.send(:define_method, :synchronized) do |m|
mod.synchronized(m)
end
end
def synchronized(method_name)
module_eval(<<~RUBY, __FILE__, __LINE__ + 1)
def #{method_name}(*)
#{@mutex}.synchronize { super }
end
RUBY
end
end
class Foo
prepend SynchronizedAccess.new(:@_mutex)
synchronized def foo
puts "it's safe here"
end
end
w
АД
ruby
class SynchronizedAccess < Module
def initialize(mutex)
@mutex= mutex
module_eval(<<~RUBY)
def initialize(*)
#{mutex} = Mutex.new
super
end
RUBY
end
def prepended(base)
mod = self
base.singleton_class.send(:define_method, :synchronized) do |m|
mod.synchronized(m)
end
end
def synchronized(method_name)
module_eval(<<~RUBY, __FILE__, __LINE__ + 1)
def #{method_name}(*)
#{@mutex}.synchronize { super }
end
RUBY
end
end
class Foo
prepend SynchronizedAccess.new(:@_mutex)
synchronized def foo
puts "it's safe here"
end
end
NS
ruby
class SynchronizedAccess < Module
def initialize(mutex)
@mutex= mutex
module_eval(<<~RUBY)
def initialize(*)
#{mutex} = Mutex.new
super
end
RUBY
end
def prepended(base)
mod = self
base.singleton_class.send(:define_method, :synchronized) do |m|
mod.synchronized(m)
end
end
def synchronized(method_name)
module_eval(<<~RUBY, __FILE__, __LINE__ + 1)
def #{method_name}(*)
#{@mutex}.synchronize { super }
end
RUBY
end
end
class Foo
prepend SynchronizedAccess.new(:@_mutex)
synchronized def foo
puts "it's safe here"
end
end
АД
АД
EM
AD
AD
ruby
class SynchronizedAccess < Module
def initialize(mutex)
@mutex= mutex
module_eval(<<~RUBY)
def initialize(*)
#{mutex} = Mutex.new
super
end
RUBY
end
def prepended(base)
mod = self
base.singleton_class.send(:define_method, :synchronized) do |m|
mod.synchronized(m)
end
end
def synchronized(method_name)
module_eval(<<~RUBY, __FILE__, __LINE__ + 1)
def #{method_name}(*)
#{@mutex}.synchronize { super }
end
RUBY
end
end
class Foo
prepend SynchronizedAccess.new(:@_mutex)
synchronized def foo
puts "it's safe here"
end
end
AD
MM
m
ruby
class SynchronizedAccess < Module
def initialize(mutex)
@mutex= mutex
module_eval(<<~RUBY)
def initialize(*)
#{mutex} = Mutex.new
super
end
RUBY
end
def prepended(base)
mod = self
base.singleton_class.send(:define_method, :synchronized) do |m|
mod.synchronized(m)
end
end
def synchronized(method_name)
module_eval(<<~RUBY, __FILE__, __LINE__ + 1)
def #{method_name}(*)
#{@mutex}.synchronize { super }
end
RUBY
end
end
class Foo
prepend SynchronizedAccess.new(:@_mutex)
synchronized def foo
puts "it's safe here"
end
end
super()
и mutex "протекает" в классclass SynchronizedAccess < ::Module
def initialize
__mutex__= nil
super
define_method(:initialize) do |*args|
__mutex__ = Mutex.new
super(*args)
end
# like `def self.synchronized` but as closure with __mutex__
# defines instance methods on our instance module
# calls original method wrapped in mutex
singleton_class.define_method(:synchronized) do |method_name|
define_method(method_name) do |*args|
__mutex__.synchronize do
super(*args)
end
end
end
end
# to be included in class
# to make class method `Foo.synchronized` available
# as a closure around our instance of SynchronizedAccess
class ClassMethods < ::Module
def initialize(mod)
__mod__ = mod
super()
define_method(:synchronized) do |method_name|
__mod__.synchronized(method_name)
end
end
end
# @note dont like `base.singleton_class.define_method` as it defines method on Foo.singleton_class
def prepended(base)
base.extend ClassMethods.new(self)
super
end
end
class Foo
prepend SynchronizedAccess.new
synchronized def foo
puts "it's safe here"
end
end
NS