Module: ZeevexThreadsafe::ThreadSafer::ClassMethods

Defined in:
lib/zeevex_threadsafe/thread_safer.rb

Instance Method Summary collapse

Instance Method Details

#make_thread_safe(*methods) ⇒ Object



40
41
42
43
44
45
46
47
48
49
# File 'lib/zeevex_threadsafe/thread_safer.rb', line 40

def make_thread_safe *methods
  methods.each do |method|
    method = method.to_sym
    if method_defined?(method)
      make_thread_safe_now method
    else
      @delayed_thread_safe_methods << method
    end
  end
end

#make_thread_safe_now(*methods) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/zeevex_threadsafe/thread_safer.rb', line 51

def make_thread_safe_now *methods
  methods.each do |method|
    old_name = method.to_sym
    new_name = (old_name.to_s + "_without_mutex").to_sym
    alias_method new_name, old_name
    myprox = lambda do |*args, &block|
      _ts_mutex.synchronize do
        @in_thread_safe_method = old_name
        res = __send__ new_name, *args, &block
        @in_thread_safe_method = nil
        res
      end
    end
    define_method old_name, myprox
  end
end

#method_added_with_thread_safe(method) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/zeevex_threadsafe/thread_safer.rb', line 32

def method_added_with_thread_safe(method)
  if !method.to_s.match(/_without_mutex$/) && @delayed_thread_safe_methods.include?(method)
    @delayed_thread_safe_methods.delete method
    make_thread_safe(method)
  end
  method_added_without_thread_safe(method) if method_defined?(:method_added_without_thread_safe)
end