Allow Mutex to be used as drop-in replacement for python's Lock

This commit is contained in:
Luke Campagnola 2017-09-13 22:14:29 -07:00
parent e06fc101f5
commit 1911a26f84
1 changed files with 23 additions and 3 deletions

View File

@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
from ..Qt import QtCore
import traceback
from ..Qt import QtCore
class Mutex(QtCore.QMutex):
"""
@ -17,7 +18,7 @@ class Mutex(QtCore.QMutex):
QtCore.QMutex.__init__(self, *args)
self.l = QtCore.QMutex() ## for serializing access to self.tb
self.tb = []
self.debug = True ## True to enable debugging functions
self.debug = kargs.pop('debug', False) ## True to enable debugging functions
def tryLock(self, timeout=None, id=None):
if timeout is None:
@ -72,6 +73,16 @@ class Mutex(QtCore.QMutex):
finally:
self.l.unlock()
def acquire(self, blocking=True):
"""Mimics threading.Lock.acquire() to allow this class as a drop-in replacement.
"""
return self.tryLock()
def release(self):
"""Mimics threading.Lock.release() to allow this class as a drop-in replacement.
"""
self.unlock()
def depth(self):
self.l.lock()
n = len(self.tb)
@ -91,4 +102,13 @@ class Mutex(QtCore.QMutex):
def __enter__(self):
self.lock()
return self
return self
class RecursiveMutex(Mutex):
"""Mimics threading.RLock class.
"""
def __init__(self, **kwds):
kwds['recursive'] = True
Mutex.__init__(self, **kwds)