Class: RecursiveMutex

Inherits:
Mutex
  • Object
show all
Defined in:
lib/thread/recursive_mutex.rb

Overview

A recursive mutex lets you lock in various threads recursively, allowing you to do multiple locks one inside another.

You really shouldn’t use this, but in some cases it makes your life easier.

Instance Method Summary collapse

Constructor Details

#initializeRecursiveMutex

Returns a new instance of RecursiveMutex.



18
19
20
21
22
# File 'lib/thread/recursive_mutex.rb', line 18

def initialize
  @threads = Hash.new { |h, k| h[k] = 0 }

  super
end

Instance Method Details

#lockObject

Lock the mutex.



25
26
27
28
29
30
31
# File 'lib/thread/recursive_mutex.rb', line 25

def lock
  @thread[Thread.current] += 1

  if @thread[Thread.current] == 1
    super
  end
end

#unlockObject

Unlock the mutex.



34
35
36
37
38
39
40
41
42
# File 'lib/thread/recursive_mutex.rb', line 34

def unlock
  @thread[Thread.current] -= 1

  if @thread[Thread.current] == 0
    @thread.delete(Thread.current)

    super
  end
end