Patchwork [3,of,5] lock: add a method to prepare the lock for inheritance

login
register
mail settings
Submitter Siddharth Agarwal
Date Sept. 24, 2015, 11:29 p.m.
Message ID <a5358efa5b11aa4fe97b.1443137378@dev6666.prn1.facebook.com>
Download mbox | patch
Permalink /patch/10622/
State Accepted
Headers show

Comments

Siddharth Agarwal - Sept. 24, 2015, 11:29 p.m.
# HG changeset patch
# User Siddharth Agarwal <sid0@fb.com>
# Date 1443116233 25200
#      Thu Sep 24 10:37:13 2015 -0700
# Node ID a5358efa5b11aa4fe97bd91951a68dcaa84e6d15
# Parent  c7be83dfac5d81424808606823dfcc5a42048491
lock: add a method to prepare the lock for inheritance

This is part of a series that will allow locks to be inherited by subprocesses
in limited circumstances.

Patch

diff --git a/mercurial/lock.py b/mercurial/lock.py
--- a/mercurial/lock.py
+++ b/mercurial/lock.py
@@ -158,6 +158,28 @@  class lock(object):
         locker = self._readlock()
         return self._testlock(locker)
 
+    def prepinherit(self):
+        """prepare for the lock to be inherited by a Mercurial subprocess
+
+        Returns a string that will be recognized by the lock in the
+        subprocess. Communicating this string to the subprocess needs to be done
+        separately -- typically by an environment variable.
+        """
+        if not self.held:
+            raise error.LockInheritanceContractViolation(
+                'prepinherit can only be called while lock is held')
+        if self._inherited:
+            raise error.LockInheritanceContractViolation(
+                'prepinherit cannot be called while lock is already inherited')
+        if self.releasefn:
+            self.releasefn()
+        if self._parentheld:
+            lockname = self.parentlock
+        else:
+            lockname = '%s:%s' % (lock._host, self.pid)
+        self._inherited = True
+        return lockname
+
     def release(self):
         """release the lock and execute callback function if any