##// END OF EJS Templates
add a releasefn keyword to lock.lock...
Benoit Boissinot -
r1530:abfab59f default
parent child Browse files
Show More
@@ -1,49 +1,52 b''
1 # lock.py - simple locking scheme for mercurial
1 # lock.py - simple locking scheme for mercurial
2 #
2 #
3 # Copyright 2005 Matt Mackall <mpm@selenic.com>
3 # Copyright 2005 Matt Mackall <mpm@selenic.com>
4 #
4 #
5 # This software may be used and distributed according to the terms
5 # This software may be used and distributed according to the terms
6 # of the GNU General Public License, incorporated herein by reference.
6 # of the GNU General Public License, incorporated herein by reference.
7
7
8 import os, time
8 import os, time
9 import util
9 import util
10
10
11 class LockHeld(Exception):
11 class LockHeld(Exception):
12 pass
12 pass
13
13
14 class lock:
14 class lock:
15 def __init__(self, file, wait=1):
15 def __init__(self, file, wait=1, releasefn=None):
16 self.f = file
16 self.f = file
17 self.held = 0
17 self.held = 0
18 self.wait = wait
18 self.wait = wait
19 self.releasefn = releasefn
19 self.lock()
20 self.lock()
20
21
21 def __del__(self):
22 def __del__(self):
22 self.release()
23 self.release()
23
24
24 def lock(self):
25 def lock(self):
25 while 1:
26 while 1:
26 try:
27 try:
27 self.trylock()
28 self.trylock()
28 return 1
29 return 1
29 except LockHeld, inst:
30 except LockHeld, inst:
30 if self.wait:
31 if self.wait:
31 time.sleep(1)
32 time.sleep(1)
32 continue
33 continue
33 raise inst
34 raise inst
34
35
35 def trylock(self):
36 def trylock(self):
36 pid = os.getpid()
37 pid = os.getpid()
37 try:
38 try:
38 util.makelock(str(pid), self.f)
39 util.makelock(str(pid), self.f)
39 self.held = 1
40 self.held = 1
40 except (OSError, IOError):
41 except (OSError, IOError):
41 raise LockHeld(util.readlock(self.f))
42 raise LockHeld(util.readlock(self.f))
42
43
43 def release(self):
44 def release(self):
44 if self.held:
45 if self.held:
45 self.held = 0
46 self.held = 0
47 if self.releasefn:
48 self.releasefn()
46 try:
49 try:
47 os.unlink(self.f)
50 os.unlink(self.f)
48 except: pass
51 except: pass
49
52
General Comments 0
You need to be logged in to leave comments. Login now