Class: Debugger::Lock

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-debug/lock.rb

Instance Method Summary collapse

Constructor Details

#initializeLock

Returns a new instance of Lock.



3
4
5
6
7
# File 'lib/ruby-debug/lock.rb', line 3

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

Instance Method Details

#lockObject



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/ruby-debug/lock.rb', line 13

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)


9
10
11
# File 'lib/ruby-debug/lock.rb', line 9

def locked?
  @locked
end

#unlockObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/ruby-debug/lock.rb', line 26

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