eric6/E5Utilities/E5MutexLocker.py

changeset 7774
9eed155411f0
child 7775
4a1db75550bd
diff -r fe42bd17d4fe -r 9eed155411f0 eric6/E5Utilities/E5MutexLocker.py
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/eric6/E5Utilities/E5MutexLocker.py	Sat Oct 10 16:03:53 2020 +0200
@@ -0,0 +1,50 @@
+# -*- coding: utf-8 -*-
+
+# Copyright (c) 2020 Detlev Offenbach <detlev@die-offenbachs.de>
+#
+
+"""
+Module implementing a context manager locking and unlocking a mutex.
+"""
+
+import contextlib
+
+
+class E5MutexLocker(contextlib.AbstractContextManager):
+    """
+    Class implementing a context manager locking and unlocking a mutex.
+    """
+    def __init__(self, mutex):
+        """
+        Constructor
+        
+        @param mutex reference to the mutex to be locked
+        @type QMutex or QRecursiveMutex
+        """
+        self.__mutex = mutex
+    
+    def __enter__(self):
+        """
+        Special method called when entering the runtime ccontext.
+        
+        @return reference to the context manager object
+        @rtype E5OverrideCursor
+        """
+        self.__mutex.lock()
+        
+        return self
+    
+    def __exit__(self, exc_type, exc_value, traceback):
+        """
+        Special method called when exiting the runtime ccontext.
+        
+        @param exc_type type of an exception raised in the runtime context
+        @param exc_value value of an exception raised in the runtime context
+        @param traceback traceback of an exception raised in the runtime
+            context
+        @return always returns None to not suppress any exception
+        @rtype None
+        """
+        self.__mutex.unlock()
+        
+        return None

eric ide

mercurial