changeset 15583:926a06f7a353

lock: add mechanism to register post release callback
author Pierre-Yves David <pierre-yves.david@ens-lyon.org>
date Mon, 28 Nov 2011 15:05:26 +0100
parents d8fa35c28335
children 9df9444e96ec
files mercurial/localrepo.py mercurial/lock.py
diffstat 2 files changed, 16 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/localrepo.py	Sat Nov 26 00:10:31 2011 +0100
+++ b/mercurial/localrepo.py	Mon Nov 28 15:05:26 2011 +0100
@@ -912,6 +912,15 @@
             acquirefn()
         return l
 
+    def _postrelease(self, callback):
+        """add a callback to the current repository lock.
+
+        The callback will be executed on lock release."""
+        l = self._lockref and self._lockref()
+        assert l is not None
+        assert l.held
+        l.postreleasehooks.append(callback)
+
     def lock(self, wait=True):
         '''Lock the repository store (.hg/store) and return a weak reference
         to the lock. Use this before modifying the store (e.g. committing or
--- a/mercurial/lock.py	Sat Nov 26 00:10:31 2011 +0100
+++ b/mercurial/lock.py	Mon Nov 28 15:05:26 2011 +0100
@@ -35,6 +35,7 @@
         self.timeout = timeout
         self.releasefn = releasefn
         self.desc = desc
+        self.postreleasehooks  = []
         self.lock()
 
     def __del__(self):
@@ -119,6 +120,10 @@
             return locker
 
     def release(self):
+        """release the lock and execute callback function if any
+
+        If the lock have been aquired multiple time, the actual release is
+        delayed to the last relase call."""
         if self.held > 1:
             self.held -= 1
         elif self.held == 1:
@@ -129,6 +134,8 @@
                 util.unlink(self.f)
             except OSError:
                 pass
+            for callback in self.postreleasehooks:
+                callback()
 
 def release(*locks):
     for lock in locks: