~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/counted_lock.py

  • Committer: John Arbash Meinel
  • Author(s): Mark Hammond
  • Date: 2008-09-09 17:02:21 UTC
  • mto: This revision was merged to the branch mainline in revision 3697.
  • Revision ID: john@arbash-meinel.com-20080909170221-svim3jw2mrz0amp3
An updated transparent icon for bzr.

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