~bzr-pqm/bzr/bzr.dev

3474.1.2 by Martin Pool
CountedLock.unlock should raise LockNotHeld if appropriate
1
# Copyright (C) 2007, 2008 Canonical Ltd
2475.4.1 by Martin Pool
Start adding CountedLock class to partially replace LockableFiles
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2475.4.1 by Martin Pool
Start adding CountedLock class to partially replace LockableFiles
16
6379.6.7 by Jelmer Vernooij
Move importing from future until after doc string, otherwise the doc string will disappear.
17
"""Counted lock class"""
18
6379.6.1 by Jelmer Vernooij
Import absolute_import in a few places.
19
from __future__ import absolute_import
20
3474.1.2 by Martin Pool
CountedLock.unlock should raise LockNotHeld if appropriate
21
from bzrlib import (
22
    errors,
2475.4.1 by Martin Pool
Start adding CountedLock class to partially replace LockableFiles
23
    )
24
25
26
class CountedLock(object):
27
    """Decorator around a lock that makes it reentrant.
28
29
    This can be used with any object that provides a basic Lock interface,
30
    including LockDirs and OS file locks.
3407.2.19 by Martin Pool
CountedLock should manage lock tokens
31
32
    :ivar _token: While a write lock is held, this is the token 
33
        for it.
2475.4.1 by Martin Pool
Start adding CountedLock class to partially replace LockableFiles
34
    """
35
36
    def __init__(self, real_lock):
37
        self._real_lock = real_lock
38
        self._lock_mode = None
39
        self._lock_count = 0
40
3474.1.2 by Martin Pool
CountedLock.unlock should raise LockNotHeld if appropriate
41
    def __repr__(self):
42
        return "%s(%r)" % (self.__class__.__name__,
43
            self._real_lock)
44
2475.4.1 by Martin Pool
Start adding CountedLock class to partially replace LockableFiles
45
    def break_lock(self):
46
        self._real_lock.break_lock()
47
        self._lock_mode = None
48
        self._lock_count = 0
49
4570.3.5 by Martin Pool
Add CountedLock.get_physical_lock_status
50
    def get_physical_lock_status(self):
51
        """Return physical lock status.
52
53
        Returns true if a lock is held on the transport. If no lock is held, or
54
        the underlying locking mechanism does not support querying lock
55
        status, false is returned.
56
        """
57
        try:
58
            return self._real_lock.peek() is not None
59
        except NotImplementedError:
60
            return False
61
2475.4.1 by Martin Pool
Start adding CountedLock class to partially replace LockableFiles
62
    def is_locked(self):
63
        return self._lock_mode is not None
64
65
    def lock_read(self):
66
        """Acquire the lock in read mode.
67
68
        If the lock is already held in either read or write mode this
69
        increments the count and succeeds.  If the lock is not already held,
70
        it is taken in read mode.
71
        """
72
        if self._lock_mode:
73
            self._lock_count += 1
74
        else:
75
            self._real_lock.lock_read()
76
            self._lock_count = 1
77
            self._lock_mode = 'r'
78
3407.2.20 by Martin Pool
Add lock token support to CountedLock
79
    def lock_write(self, token=None):
2475.4.1 by Martin Pool
Start adding CountedLock class to partially replace LockableFiles
80
        """Acquire the lock in write mode.
81
82
        If the lock was originally acquired in read mode this will fail.
3407.2.19 by Martin Pool
CountedLock should manage lock tokens
83
3407.2.20 by Martin Pool
Add lock token support to CountedLock
84
        :param token: If given and the lock is already held, 
85
            then validate that we already hold the real
86
            lock with this token.
87
3407.2.19 by Martin Pool
CountedLock should manage lock tokens
88
        :returns: The token from the underlying lock.
2475.4.1 by Martin Pool
Start adding CountedLock class to partially replace LockableFiles
89
        """
90
        if self._lock_count == 0:
3407.2.20 by Martin Pool
Add lock token support to CountedLock
91
            self._token = self._real_lock.lock_write(token=token)
2475.4.1 by Martin Pool
Start adding CountedLock class to partially replace LockableFiles
92
            self._lock_mode = 'w'
3474.1.3 by Martin Pool
CountedLock now handles and tests lock tokens
93
            self._lock_count += 1
3468.3.2 by Martin Pool
merge trunk
94
            return self._token
2475.4.1 by Martin Pool
Start adding CountedLock class to partially replace LockableFiles
95
        elif self._lock_mode != 'w':
3474.1.2 by Martin Pool
CountedLock.unlock should raise LockNotHeld if appropriate
96
            raise errors.ReadOnlyError(self)
3474.1.3 by Martin Pool
CountedLock now handles and tests lock tokens
97
        else:
98
            self._real_lock.validate_token(token)
99
            self._lock_count += 1
3468.3.4 by Martin Pool
CountedLock.lock_write should return existing known token
100
            return self._token
2475.4.1 by Martin Pool
Start adding CountedLock class to partially replace LockableFiles
101
102
    def unlock(self):
103
        if self._lock_count == 0:
3474.1.3 by Martin Pool
CountedLock now handles and tests lock tokens
104
            raise errors.LockNotHeld(self)
2475.4.1 by Martin Pool
Start adding CountedLock class to partially replace LockableFiles
105
        elif self._lock_count == 1:
3474.1.3 by Martin Pool
CountedLock now handles and tests lock tokens
106
            # these are decremented first; if we fail to unlock the most
107
            # reasonable assumption is that we still don't have the lock
108
            # anymore
109
            self._lock_mode = None
110
            self._lock_count -= 1
2475.4.1 by Martin Pool
Start adding CountedLock class to partially replace LockableFiles
111
            self._real_lock.unlock()
3474.1.3 by Martin Pool
CountedLock now handles and tests lock tokens
112
        else:
113
            self._lock_count -= 1