2003-01-06 12:07:16 +01:00
|
|
|
# Locking debugging code -- temporary
|
2015-01-01 21:41:11 +01:00
|
|
|
# Copyright (C) 2003-2015 John Goerzen & contributors
|
2003-01-06 12:07:16 +01:00
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
2003-04-16 21:23:45 +02:00
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
2003-01-06 12:07:16 +01:00
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
2006-08-12 06:15:55 +02:00
|
|
|
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
2003-01-06 12:07:16 +01:00
|
|
|
|
2011-05-11 18:25:13 +02:00
|
|
|
from threading import Lock, currentThread
|
2003-01-06 12:07:16 +01:00
|
|
|
import traceback
|
2020-08-29 19:55:52 +02:00
|
|
|
|
2003-01-06 12:07:16 +01:00
|
|
|
logfile = open("/tmp/logfile", "wt")
|
2003-01-06 22:58:29 +01:00
|
|
|
loglock = Lock()
|
2003-01-06 12:07:16 +01:00
|
|
|
|
2020-08-29 19:55:52 +02:00
|
|
|
|
2003-01-06 12:07:16 +01:00
|
|
|
class DebuggingLock:
|
|
|
|
def __init__(self, name):
|
|
|
|
self.lock = Lock()
|
|
|
|
self.name = name
|
2013-07-21 21:00:23 +02:00
|
|
|
|
2020-08-29 19:55:52 +02:00
|
|
|
def acquire(self, blocking=1):
|
2003-01-06 12:07:16 +01:00
|
|
|
self.print_tb("Acquire lock")
|
|
|
|
self.lock.acquire(blocking)
|
2020-08-29 19:55:52 +02:00
|
|
|
self.logmsg("===== %s: Thread %s acquired lock\n" %
|
|
|
|
(self.name, currentThread().getName()))
|
2003-01-06 12:07:16 +01:00
|
|
|
|
|
|
|
def release(self):
|
|
|
|
self.print_tb("Release lock")
|
|
|
|
self.lock.release()
|
|
|
|
|
2003-01-06 22:58:29 +01:00
|
|
|
def logmsg(self, msg):
|
|
|
|
loglock.acquire()
|
|
|
|
logfile.write(msg + "\n")
|
2003-01-06 12:07:16 +01:00
|
|
|
logfile.flush()
|
2003-01-06 22:58:29 +01:00
|
|
|
loglock.release()
|
|
|
|
|
|
|
|
def print_tb(self, msg):
|
2020-08-29 19:55:52 +02:00
|
|
|
self.logmsg(".... %s: Thread %s attempting to %s\n" % \
|
2003-01-06 22:58:29 +01:00
|
|
|
(self.name, currentThread().getName(), msg) + \
|
|
|
|
"\n".join(traceback.format_list(traceback.extract_stack())))
|