Commit 3410bd1b authored by Vincent Pelletier's avatar Vincent Pelletier

Add a debug_lock parameter to custom locking class, allowing to turn locks...

Add a debug_lock parameter to custom locking class, allowing to turn locks into debugging locks. Thos locks will complain (in a way similar to deadlocks) when acquired twice without being released. This makes it easier to assert execution flow when tracking down a bug.


git-svn-id: https://svn.erp5.org/repos/neo/branches/prototype3@1249 71dcc9de-d417-0410-9af5-da40c76e7ee4
parent e4b8fd4b
...@@ -54,8 +54,9 @@ class LockUser(object): ...@@ -54,8 +54,9 @@ class LockUser(object):
return ''.join(traceback.format_list(self.stack)) return ''.join(traceback.format_list(self.stack))
class VerboseLockBase(object): class VerboseLockBase(object):
def __init__(self, reentrant=False): def __init__(self, reentrant=False, debug_lock=False):
self.reentrant = reentrant self.reentrant = reentrant
self.debug_lock = debug_lock
self.owner = None self.owner = None
self.waiting = [] self.waiting = []
self._note('%s@%X created by %r', self.__class__.__name__, id(self), LockUser(1)) self._note('%s@%X created by %r', self.__class__.__name__, id(self), LockUser(1))
...@@ -75,8 +76,11 @@ class VerboseLockBase(object): ...@@ -75,8 +76,11 @@ class VerboseLockBase(object):
me = LockUser() me = LockUser()
owner = self._getOwner() owner = self._getOwner()
self._note('[%r]%s.acquire(%s) Waiting for lock. Owned by:%r Waiting:%r', me, self, blocking, owner, self.waiting) self._note('[%r]%s.acquire(%s) Waiting for lock. Owned by:%r Waiting:%r', me, self, blocking, owner, self.waiting)
if not self.reentrant and blocking and me == owner: if (self.debug_lock and owner is not None) or (not self.reentrant and blocking and me == owner):
if me == owner:
self._note('[%r]%s.acquire(%s): Deadlock detected: I already own this lock:%r', me, self, blocking, owner) self._note('[%r]%s.acquire(%s): Deadlock detected: I already own this lock:%r', me, self, blocking, owner)
else:
self._note('[%r]%s.acquire(%s): debug lock triggered: %r', me, self, blocking, owner)
self._note('Owner traceback:\n%s', owner.formatStack()) self._note('Owner traceback:\n%s', owner.formatStack())
self._note('My traceback:\n%s', me.formatStack()) self._note('My traceback:\n%s', me.formatStack())
self.waiting.append(me) self.waiting.append(me)
...@@ -99,8 +103,8 @@ class VerboseLockBase(object): ...@@ -99,8 +103,8 @@ class VerboseLockBase(object):
return '<%s@%X>' % (self.__class__.__name__, id(self)) return '<%s@%X>' % (self.__class__.__name__, id(self))
class VerboseRLock(VerboseLockBase): class VerboseRLock(VerboseLockBase):
def __init__(self, verbose=None): def __init__(self, verbose=None, debug_lock=False):
super(VerboseRLock, self).__init__(reentrant=True) super(VerboseRLock, self).__init__(reentrant=True, debug_lock=debug_lock)
self.lock = threading_RLock() self.lock = threading_RLock()
def _locked(self): def _locked(self):
...@@ -110,8 +114,8 @@ class VerboseRLock(VerboseLockBase): ...@@ -110,8 +114,8 @@ class VerboseRLock(VerboseLockBase):
return self.lock._is_owned() return self.lock._is_owned()
class VerboseLock(VerboseLockBase): class VerboseLock(VerboseLockBase):
def __init__(self, verbose=None): def __init__(self, verbose=None, debug_lock=False):
super(VerboseLock, self).__init__() super(VerboseLock, self).__init__(debug_lock=debug_lock)
self.lock = threading_Lock() self.lock = threading_Lock()
def locked(self): def locked(self):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment