Module: Threadsafety::DSL

Defined in:
lib/threadsafety.rb

Instance Method Summary collapse

Instance Method Details

#threadsafe(&block) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/threadsafety.rb', line 37

def threadsafe &block
  if threadsafe?
    block.call
  else
    t = Thread.current
  
    begin
      (@__mutex || $__mutex).synchronize do
          t[:_threadsafe] = true
          block.call
      end
    ensure
      t[:_threadsafe] = false
    end
  end
end

#threadsafe?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/threadsafety.rb', line 33

def threadsafe?
  Thread.current[:_threadsafe]
end

#threadsafety_mutex=(v) ⇒ Object



29
30
31
# File 'lib/threadsafety.rb', line 29

def threadsafety_mutex= v
  @__mutex = v
end