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