]> code.delx.au - offlineimap/blob - offlineimap/head/offlineimap/ui/debuglock.py
e8c817031aab94de6ab23d935fe322e97176e4e9
[offlineimap] / offlineimap / head / offlineimap / ui / debuglock.py
1 # Locking debugging code -- temporary
2 # Copyright (C) 2003 John Goerzen
3 # <jgoerzen@complete.org>
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; version 2 of the License.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
18 from threading import *
19 import traceback
20 logfile = open("/tmp/logfile", "wt")
21 loglock = Lock()
22
23 class DebuggingLock:
24 def __init__(self, name):
25 self.lock = Lock()
26 self.name = name
27
28 def acquire(self, blocking = 1):
29 self.print_tb("Acquire lock")
30 self.lock.acquire(blocking)
31 self.logmsg("===== %s: Thread %s acquired lock\n" % (self.name, currentThread().getName()))
32
33 def release(self):
34 self.print_tb("Release lock")
35 self.lock.release()
36
37 def logmsg(self, msg):
38 loglock.acquire()
39 logfile.write(msg + "\n")
40 logfile.flush()
41 loglock.release()
42
43 def print_tb(self, msg):
44 self.logmsg(".... %s: Thread %s attempting to %s\n" % \
45 (self.name, currentThread().getName(), msg) + \
46 "\n".join(traceback.format_list(traceback.extract_stack())))
47
48