comparison mercurial/lock.py @ 26383:0fceb34806e1

lock: add a wrapper to os.getpid() to make testing easier This will allow us to fake locks across processes more easily.
author Siddharth Agarwal <sid0@fb.com>
date Thu, 24 Sep 2015 21:26:37 -0700
parents c545d51c901e
children e16f80f89a29
comparison
equal deleted inserted replaced
26382:b673e89affc9 26383:0fceb34806e1
49 self.desc = desc 49 self.desc = desc
50 self.parentlock = parentlock 50 self.parentlock = parentlock
51 self._parentheld = False 51 self._parentheld = False
52 self._inherited = False 52 self._inherited = False
53 self.postrelease = [] 53 self.postrelease = []
54 self.pid = os.getpid() 54 self.pid = self._getpid()
55 self.delay = self.lock() 55 self.delay = self.lock()
56 if self.acquirefn: 56 if self.acquirefn:
57 self.acquirefn() 57 self.acquirefn()
58 58
59 def __del__(self): 59 def __del__(self):
65 # ensure the lock will be removed 65 # ensure the lock will be removed
66 # even if recursive locking did occur 66 # even if recursive locking did occur
67 self.held = 1 67 self.held = 1
68 68
69 self.release() 69 self.release()
70
71 def _getpid(self):
72 # wrapper around os.getpid() to make testing easier
73 return os.getpid()
70 74
71 def lock(self): 75 def lock(self):
72 timeout = self.timeout 76 timeout = self.timeout
73 while True: 77 while True:
74 try: 78 try:
195 delayed to the last release call.""" 199 delayed to the last release call."""
196 if self.held > 1: 200 if self.held > 1:
197 self.held -= 1 201 self.held -= 1
198 elif self.held == 1: 202 elif self.held == 1:
199 self.held = 0 203 self.held = 0
200 if os.getpid() != self.pid: 204 if self._getpid() != self.pid:
201 # we forked, and are not the parent 205 # we forked, and are not the parent
202 return 206 return
203 try: 207 try:
204 if self.releasefn: 208 if self.releasefn:
205 self.releasefn() 209 self.releasefn()