comparison mercurial/lock.py @ 1787:e431344e604c

add a timeout when a lock is held (default 1024 sec) - change the wait keyword from lock.lock to timeout, a negative timeout of means "wait forever" - refactor the two lock functions from localrepo.py - make them use the timeout (default 1024, can be changed with ui.timeout in the config file - update the doc
author Benoit Boissinot <benoit.boissinot@ens-lyon.org>
date Tue, 21 Feb 2006 23:21:15 +0100
parents e6e70450edb9
children cd5c1db2132a
comparison
equal deleted inserted replaced
1782:b9671b41e360 1787:e431344e604c
14 pass 14 pass
15 class LockUnavailable(LockException): 15 class LockUnavailable(LockException):
16 pass 16 pass
17 17
18 class lock(object): 18 class lock(object):
19 def __init__(self, file, wait=1, releasefn=None): 19 def __init__(self, file, timeout=-1, releasefn=None):
20 self.f = file 20 self.f = file
21 self.held = 0 21 self.held = 0
22 self.wait = wait 22 self.timeout = timeout
23 self.releasefn = releasefn 23 self.releasefn = releasefn
24 self.lock() 24 self.lock()
25 25
26 def __del__(self): 26 def __del__(self):
27 self.release() 27 self.release()
28 28
29 def lock(self): 29 def lock(self):
30 timeout = self.timeout
30 while 1: 31 while 1:
31 try: 32 try:
32 self.trylock() 33 self.trylock()
33 return 1 34 return 1
34 except LockHeld, inst: 35 except LockHeld, inst:
35 if self.wait: 36 if timeout != 0:
36 time.sleep(1) 37 time.sleep(1)
38 if timeout > 0:
39 timeout -= 1
37 continue 40 continue
38 raise inst 41 raise inst
39 42
40 def trylock(self): 43 def trylock(self):
41 pid = os.getpid() 44 pid = os.getpid()