Show More
@@ -1,52 +1,59 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 | import os, time | |
|
8 | import errno, os, time | |
|
9 | 9 | import util |
|
10 | 10 | |
|
11 |
class Lock |
|
|
11 | class LockException(Exception): | |
|
12 | pass | |
|
13 | class LockHeld(LockException): | |
|
14 | pass | |
|
15 | class LockUnavailable(LockException): | |
|
12 | 16 | pass |
|
13 | 17 | |
|
14 | 18 | class lock(object): |
|
15 | 19 | def __init__(self, file, wait=1, releasefn=None): |
|
16 | 20 | self.f = file |
|
17 | 21 | self.held = 0 |
|
18 | 22 | self.wait = wait |
|
19 | 23 | self.releasefn = releasefn |
|
20 | 24 | self.lock() |
|
21 | 25 | |
|
22 | 26 | def __del__(self): |
|
23 | 27 | self.release() |
|
24 | 28 | |
|
25 | 29 | def lock(self): |
|
26 | 30 | while 1: |
|
27 | 31 | try: |
|
28 | 32 | self.trylock() |
|
29 | 33 | return 1 |
|
30 | 34 | except LockHeld, inst: |
|
31 | 35 | if self.wait: |
|
32 | 36 | time.sleep(1) |
|
33 | 37 | continue |
|
34 | 38 | raise inst |
|
35 | 39 | |
|
36 | 40 | def trylock(self): |
|
37 | 41 | pid = os.getpid() |
|
38 | 42 | try: |
|
39 | 43 | util.makelock(str(pid), self.f) |
|
40 | 44 | self.held = 1 |
|
41 | except (OSError, IOError): | |
|
42 | raise LockHeld(util.readlock(self.f)) | |
|
45 | except (OSError, IOError), why: | |
|
46 | if why.errno == errno.EEXIST: | |
|
47 | raise LockHeld(util.readlock(self.f)) | |
|
48 | else: | |
|
49 | raise LockUnavailable(why) | |
|
43 | 50 | |
|
44 | 51 | def release(self): |
|
45 | 52 | if self.held: |
|
46 | 53 | self.held = 0 |
|
47 | 54 | if self.releasefn: |
|
48 | 55 | self.releasefn() |
|
49 | 56 | try: |
|
50 | 57 | os.unlink(self.f) |
|
51 | 58 | except: pass |
|
52 | 59 |
General Comments 0
You need to be logged in to leave comments.
Login now