Top Level Namespace

Instance Method Summary collapse

Instance Method Details

#threadlock(*funcs) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/threadlock.rb', line 3

def threadlock(*funcs)
  for f in funcs.flatten
    f2 = f.to_s
          .gsub(/\=/, "_eQUAL")
          .gsub(/\!/, "_nOT")
          .gsub(/\~/, "_tILDE")
          .gsub(/\</, "_lESS")
          .gsub(/\>/, "_mORE")
          .gsub(/\+/, "_aDD")
          .gsub(/\-/, "_sUB")
          .gsub(/\*/, "_mULT")
          .gsub(/\//, "_dIV")
          .gsub(/\//, "_mOD")
          .gsub(/^/, "___")
    
    class_eval\
    ("
      alias :#{f2} :#{f}
      def #{f}(*args)
        @___threadlock___ ||= Monitor.new
        @___threadlock___.synchronize do
          #{f2}(*args)
        end
      end
    ")
  end
end