text_canvas/RAIILock.py
2020-08-01 22:46:35 -07:00

36 lines
506 B
Python

import threading
class RAIILock:
def __init__(self, lock: threading.RLock, defer=False):
self.__lock = lock
self.__defer = defer
self.__acquired = False
def __enter__(self):
if self.__defer is False:
self.acquire()
return self
def __exit__(self, exc_type, exc_val, exc_tb):
self.release()
def acquire(self):
self.__lock.acquire()
self.__acquired = True
def release(self):
if self.__acquired:
self.__lock.release()
self.__acquired = False