summaryrefslogtreecommitdiff
path: root/python/qpid/debug.py
blob: 357340747f34329741ee134ca91ef2570dcd4305 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import traceback, time, sys

from threading import RLock

def stackdump(*args):
  print args
  code = []
  for threadId, stack in sys._current_frames().items():
    code.append("\n# ThreadID: %s" % threadId)
    for filename, lineno, name, line in traceback.extract_stack(stack):
      code.append('File: "%s", line %d, in %s' % (filename, lineno, name))
      if line:
        code.append("  %s" % (line.strip()))
  print "\n".join(code)

import signal
signal.signal(signal.SIGQUIT, stackdump)

#out = open("/tmp/stacks.txt", "write")

class LoudLock:

  def __init__(self):
    self.lock = RLock()

  def acquire(self, blocking=1):
    import threading
    while not self.lock.acquire(blocking=0):
      time.sleep(1)
      print >> out, "TRYING"
#      print self.lock._RLock__owner, threading._active
#      stackdump()
      traceback.print_stack(None, None, out)
      print >> out, "TRYING"
    print >> out, "ACQUIRED"
    traceback.print_stack(None, None, out)
    print >> out, "ACQUIRED"
    return True

  def _is_owned(self):
    return self.lock._is_owned()

  def release(self):
    self.lock.release()