Class: Rbgo::ReentrantMutex

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

Instance Method Summary collapse

Constructor Details

#initializeReentrantMutex

Returns a new instance of ReentrantMutex.



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

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

  super
end

Instance Method Details

#lockObject



44
45
46
47
48
# File 'lib/rbgo/reentrant_mutex.rb', line 44

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

#synchronizeObject

Raises:

  • (ThreadError)


33
34
35
36
37
38
39
40
41
42
# File 'lib/rbgo/reentrant_mutex.rb', line 33

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

  begin
    lock
    yield
  ensure
    unlock
  end
end

#try_lockObject



59
60
61
62
63
64
65
66
67
68
# File 'lib/rbgo/reentrant_mutex.rb', line 59

def try_lock
  if owned?
    lock
    return true
  else
    ok = super
    increase_count Thread.current if ok
    return ok
  end
end

#unlockObject



50
51
52
53
54
55
56
57
# File 'lib/rbgo/reentrant_mutex.rb', line 50

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