~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/counted_lock.py

Merge bzr.dev and tree-file-ids-as-tuples.

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
"""Counted lock class"""
18
18
 
 
19
from __future__ import absolute_import
19
20
 
20
21
from bzrlib import (
21
22
    errors,
27
28
 
28
29
    This can be used with any object that provides a basic Lock interface,
29
30
    including LockDirs and OS file locks.
 
31
 
 
32
    :ivar _token: While a write lock is held, this is the token 
 
33
        for it.
30
34
    """
31
35
 
32
36
    def __init__(self, real_lock):
43
47
        self._lock_mode = None
44
48
        self._lock_count = 0
45
49
 
 
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
 
46
62
    def is_locked(self):
47
63
        return self._lock_mode is not None
48
64
 
65
81
 
66
82
        If the lock was originally acquired in read mode this will fail.
67
83
 
68
 
        :param token: If non-None, reacquire the lock using this token.
 
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
 
 
88
        :returns: The token from the underlying lock.
69
89
        """
70
90
        if self._lock_count == 0:
71
 
            return_token = self._real_lock.lock_write(token)
 
91
            self._token = self._real_lock.lock_write(token=token)
72
92
            self._lock_mode = 'w'
73
93
            self._lock_count += 1
74
 
            return return_token
 
94
            return self._token
75
95
        elif self._lock_mode != 'w':
76
96
            raise errors.ReadOnlyError(self)
77
97
        else:
78
98
            self._real_lock.validate_token(token)
79
99
            self._lock_count += 1
80
 
            return token
 
100
            return self._token
81
101
 
82
102
    def unlock(self):
83
103
        if self._lock_count == 0: