Class: ZenDebugger::Mutex

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

Instance Method Summary collapse

Constructor Details

#initializeMutex

Returns a new instance of Mutex.



24
25
26
27
28
# File 'lib/zendebug.rb', line 24

def initialize
  @locker = nil
  @waiting = []
  @locked = false;
end

Instance Method Details

#lockObject



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/zendebug.rb', line 34

def lock
  return if Thread.critical
  return if @locker == Thread.current
  while (Thread.critical = true; @locked)
    @waiting.push Thread.current
    Thread.stop
  end
  @locked = true
  @locker = Thread.current
  Thread.critical = false
  self
end

#locked?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/zendebug.rb', line 30

def locked?
  @locked
end

#unlockObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/zendebug.rb', line 47

def unlock
  return if Thread.critical
  return unless @locked
  unless @locker == Thread.current
    raise RuntimeError, "unlocked by other"
  end
  Thread.critical = true
  t = @waiting.shift
  @locked = false
  @locker = nil
  Thread.critical = false
  t.run if t
  self
end