Class: ReentrantMutex

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

Instance Method Summary collapse

Constructor Details

#initializeReentrantMutex

Returns a new instance of ReentrantMutex.



2
3
4
5
6
7
# File 'lib/reentrant_mutex.rb', line 2

def initialize
  @count_mutex = Mutex.new
  @counts = Hash.new(0)

  super
end

Instance Method Details

#lockObject



20
21
22
23
# File 'lib/reentrant_mutex.rb', line 20

def lock
  c = increase_count Thread.current
  super if c <= 1
end

#synchronizeObject

Raises:

  • (ThreadError)


9
10
11
12
13
14
15
16
17
18
# File 'lib/reentrant_mutex.rb', line 9

def synchronize
  raise ThreadError, 'Must be called with a block' unless block_given?

  begin
    lock
    yield
  ensure
    unlock
  end
end

#unlockObject



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

def unlock
  c = decrease_count Thread.current
  if c <= 0
    super
    delete_count Thread.current
  end
end