~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/counted_lock.py

  • Committer: Martin Pool
  • Date: 2005-05-17 07:51:51 UTC
  • Revision ID: mbp@sourcefrog.net-20050517075151-b64853a6a38a6225
- export bzrlib.find_branch

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007, 2008 Canonical Ltd
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
15
 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
 
 
17
 
"""Counted lock class"""
18
 
 
19
 
 
20
 
from bzrlib import (
21
 
    errors,
22
 
    )
23
 
 
24
 
 
25
 
class CountedLock(object):
26
 
    """Decorator around a lock that makes it reentrant.
27
 
 
28
 
    This can be used with any object that provides a basic Lock interface,
29
 
    including LockDirs and OS file locks.
30
 
 
31
 
    :ivar _token: While a write lock is held, this is the token 
32
 
        for it.
33
 
    """
34
 
 
35
 
    def __init__(self, real_lock):
36
 
        self._real_lock = real_lock
37
 
        self._lock_mode = None
38
 
        self._lock_count = 0
39
 
 
40
 
    def __repr__(self):
41
 
        return "%s(%r)" % (self.__class__.__name__,
42
 
            self._real_lock)
43
 
 
44
 
    def break_lock(self):
45
 
        self._real_lock.break_lock()
46
 
        self._lock_mode = None
47
 
        self._lock_count = 0
48
 
 
49
 
    def is_locked(self):
50
 
        return self._lock_mode is not None
51
 
 
52
 
    def lock_read(self):
53
 
        """Acquire the lock in read mode.
54
 
 
55
 
        If the lock is already held in either read or write mode this
56
 
        increments the count and succeeds.  If the lock is not already held,
57
 
        it is taken in read mode.
58
 
        """
59
 
        if self._lock_mode:
60
 
            self._lock_count += 1
61
 
        else:
62
 
            self._real_lock.lock_read()
63
 
            self._lock_count = 1
64
 
            self._lock_mode = 'r'
65
 
 
66
 
    def lock_write(self, token=None):
67
 
        """Acquire the lock in write mode.
68
 
 
69
 
        If the lock was originally acquired in read mode this will fail.
70
 
 
71
 
        :param token: If given and the lock is already held, 
72
 
            then validate that we already hold the real
73
 
            lock with this token.
74
 
 
75
 
        :returns: The token from the underlying lock.
76
 
        """
77
 
        if self._lock_count == 0:
78
 
            self._token = self._real_lock.lock_write(token=token)
79
 
            self._lock_mode = 'w'
80
 
            self._lock_count += 1
81
 
            return self._token
82
 
        elif self._lock_mode != 'w':
83
 
            raise errors.ReadOnlyError(self)
84
 
        else:
85
 
            self._real_lock.validate_token(token)
86
 
            self._lock_count += 1
87
 
            return self._token
88
 
 
89
 
    def unlock(self):
90
 
        if self._lock_count == 0:
91
 
            raise errors.LockNotHeld(self)
92
 
        elif self._lock_count == 1:
93
 
            # these are decremented first; if we fail to unlock the most
94
 
            # reasonable assumption is that we still don't have the lock
95
 
            # anymore
96
 
            self._lock_mode = None
97
 
            self._lock_count -= 1
98
 
            self._real_lock.unlock()
99
 
        else:
100
 
            self._lock_count -= 1