[PATCH 1 of 4 V5] lock: adds possibility of acquiring/releasing locks multiple times

liscju piotr.listkiewicz at gmail.com
Thu Nov 12 13:17:44 UTC 2015


# HG changeset patch
# User liscju <piotr.listkiewicz at gmail.com>
# Date 1447331722 -3600
#      Thu Nov 12 13:35:22 2015 +0100
# Node ID 3a0f7224a3466e49068c0cd355640152631d1ba3
# Parent  0e3d093c468e2c168a07f2ba2b5d4d44ddd8758c
lock: adds possibility of acquiring/releasing locks multiple times

This patch introduce times parameter to lock class, which
gives a possibility of proper releasing all locks and
acquiring them again proper number of times.

diff -r 0e3d093c468e -r 3a0f7224a346 mercurial/lock.py
--- a/mercurial/lock.py	Wed Nov 11 15:08:08 2015 -0600
+++ b/mercurial/lock.py	Thu Nov 12 13:35:22 2015 +0100
@@ -40,7 +40,7 @@
     _host = None
 
     def __init__(self, vfs, file, timeout=-1, releasefn=None, acquirefn=None,
-                 desc=None, inheritchecker=None, parentlock=None):
+                 desc=None, inheritchecker=None, parentlock=None, times=1):
         self.vfs = vfs
         self.f = file
         self.held = 0
@@ -54,7 +54,7 @@
         self._inherited = False
         self.postrelease  = []
         self.pid = self._getpid()
-        self.delay = self.lock()
+        self.delay = self.lock(times)
         if self.acquirefn:
             self.acquirefn()
 
@@ -74,11 +74,11 @@
         # wrapper around os.getpid() to make testing easier
         return os.getpid()
 
-    def lock(self):
+    def lock(self, times=1):
         timeout = self.timeout
         while True:
             try:
-                self._trylock()
+                self._trylock(times)
                 return self.timeout - timeout
             except error.LockHeld as inst:
                 if timeout != 0:
@@ -89,9 +89,9 @@
                 raise error.LockHeld(errno.ETIMEDOUT, inst.filename, self.desc,
                                      inst.locker)
 
-    def _trylock(self):
+    def _trylock(self, times=1):
         if self.held:
-            self.held += 1
+            self.held += times
             return
         if lock._host is None:
             lock._host = socket.gethostname()
@@ -101,7 +101,7 @@
             retry -= 1
             try:
                 self.vfs.makelock(lockname, self.f)
-                self.held = 1
+                self.held = times
             except (OSError, IOError) as why:
                 if why.errno == errno.EEXIST:
                     locker = self._readlock()
@@ -111,7 +111,7 @@
                     # but the lockfile to not be removed.
                     if locker == self.parentlock:
                         self._parentheld = True
-                        self.held = 1
+                        self.held = times
                         return
                     locker = self._testlock(locker)
                     if locker is not None:
@@ -203,6 +203,12 @@
                 self.acquirefn()
             self._inherited = False
 
+    def releaseall(self):
+        origheld = self.held
+        while self.held > 0:
+            self.release()
+        return origheld
+
     def release(self):
         """release the lock and execute callback function if any
 


More information about the Mercurial-devel mailing list