debuglock.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. # Locking debugging code -- temporary
  2. # Copyright (C) 2003-2015 John Goerzen & contributors
  3. #
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation; either version 2 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. from threading import Lock, currentThread
  18. import traceback
  19. logfile = open("/tmp/logfile", "wt")
  20. loglock = Lock()
  21. class DebuggingLock:
  22. def __init__(self, name):
  23. self.lock = Lock()
  24. self.name = name
  25. def acquire(self, blocking=1):
  26. self.print_tb("Acquire lock")
  27. self.lock.acquire(blocking)
  28. self.logmsg("===== %s: Thread %s acquired lock\n" %
  29. (self.name, currentThread().getName()))
  30. def release(self):
  31. self.print_tb("Release lock")
  32. self.lock.release()
  33. def logmsg(self, msg):
  34. loglock.acquire()
  35. logfile.write(msg + "\n")
  36. logfile.flush()
  37. loglock.release()
  38. def print_tb(self, msg):
  39. self.logmsg(".... %s: Thread %s attempting to %s\n" %
  40. (self.name, currentThread().getName(), msg) +
  41. "\n".join(traceback.format_list(traceback.extract_stack())))