Module: Threadsafety::DSL

Defined in:
lib/threadsafety.rb

Instance Method Summary collapse

Instance Method Details

#threadsafe(&block) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/threadsafety.rb', line 42

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)


38
39
40
# File 'lib/threadsafety.rb', line 38

def threadsafe?
  Thread.current[:_threadsafe]
end

#threadsafety_mutex=(v) ⇒ Object



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

def threadsafety_mutex= v
  @__mutex = v
end