Show More
@@ -1,62 +1,62 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 errno, os, time |
|
8 | from demandload import * | |
9 | import util |
|
9 | demandload(globals(), 'errno os time util') | |
10 |
|
10 | |||
11 | class LockException(Exception): |
|
11 | class LockException(Exception): | |
12 | pass |
|
12 | pass | |
13 | class LockHeld(LockException): |
|
13 | class LockHeld(LockException): | |
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, timeout=-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.timeout = timeout |
|
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 | timeout = self.timeout | |
31 | while 1: |
|
31 | while 1: | |
32 | try: |
|
32 | try: | |
33 | self.trylock() |
|
33 | self.trylock() | |
34 | return 1 |
|
34 | return 1 | |
35 | except LockHeld, inst: |
|
35 | except LockHeld, inst: | |
36 | if timeout != 0: |
|
36 | if timeout != 0: | |
37 | time.sleep(1) |
|
37 | time.sleep(1) | |
38 | if timeout > 0: |
|
38 | if timeout > 0: | |
39 | timeout -= 1 |
|
39 | timeout -= 1 | |
40 | continue |
|
40 | continue | |
41 | raise inst |
|
41 | raise inst | |
42 |
|
42 | |||
43 | def trylock(self): |
|
43 | def trylock(self): | |
44 | pid = os.getpid() |
|
44 | pid = os.getpid() | |
45 | try: |
|
45 | try: | |
46 | util.makelock(str(pid), self.f) |
|
46 | util.makelock(str(pid), self.f) | |
47 | self.held = 1 |
|
47 | self.held = 1 | |
48 | except (OSError, IOError), why: |
|
48 | except (OSError, IOError), why: | |
49 | if why.errno == errno.EEXIST: |
|
49 | if why.errno == errno.EEXIST: | |
50 | raise LockHeld(util.readlock(self.f)) |
|
50 | raise LockHeld(util.readlock(self.f)) | |
51 | else: |
|
51 | else: | |
52 | raise LockUnavailable(why) |
|
52 | raise LockUnavailable(why) | |
53 |
|
53 | |||
54 | def release(self): |
|
54 | def release(self): | |
55 | if self.held: |
|
55 | if self.held: | |
56 | self.held = 0 |
|
56 | self.held = 0 | |
57 | if self.releasefn: |
|
57 | if self.releasefn: | |
58 | self.releasefn() |
|
58 | self.releasefn() | |
59 | try: |
|
59 | try: | |
60 | os.unlink(self.f) |
|
60 | os.unlink(self.f) | |
61 | except: pass |
|
61 | except: pass | |
62 |
|
62 |
General Comments 0
You need to be logged in to leave comments.
Login now