~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/counted_lock.py

  • Committer: Mark Hammond
  • Date: 2008-08-10 11:16:39 UTC
  • mto: (3606.5.3 1.6)
  • mto: This revision was merged to the branch mainline in revision 3626.
  • Revision ID: mhammond@skippinet.com.au-20080810111639-9upz1wig1s8lvz62
install all files with 'restartreplace uninsrestartdelete' and close
tbzrcache at uninstall time.

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
 
27
27
 
28
28
    This can be used with any object that provides a basic Lock interface,
29
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
30
    """
34
31
 
35
32
    def __init__(self, real_lock):
46
43
        self._lock_mode = None
47
44
        self._lock_count = 0
48
45
 
49
 
    def get_physical_lock_status(self):
50
 
        """Return physical lock status.
51
 
 
52
 
        Returns true if a lock is held on the transport. If no lock is held, or
53
 
        the underlying locking mechanism does not support querying lock
54
 
        status, false is returned.
55
 
        """
56
 
        try:
57
 
            return self._real_lock.peek() is not None
58
 
        except NotImplementedError:
59
 
            return False
60
 
 
61
46
    def is_locked(self):
62
47
        return self._lock_mode is not None
63
48
 
80
65
 
81
66
        If the lock was originally acquired in read mode this will fail.
82
67
 
83
 
        :param token: If given and the lock is already held, 
84
 
            then validate that we already hold the real
85
 
            lock with this token.
86
 
 
87
 
        :returns: The token from the underlying lock.
 
68
        :param token: If non-None, reacquire the lock using this token.
88
69
        """
89
70
        if self._lock_count == 0:
90
 
            self._token = self._real_lock.lock_write(token=token)
 
71
            return_token = self._real_lock.lock_write(token)
91
72
            self._lock_mode = 'w'
92
73
            self._lock_count += 1
93
 
            return self._token
 
74
            return return_token
94
75
        elif self._lock_mode != 'w':
95
76
            raise errors.ReadOnlyError(self)
96
77
        else:
97
78
            self._real_lock.validate_token(token)
98
79
            self._lock_count += 1
99
 
            return self._token
 
80
            return token
100
81
 
101
82
    def unlock(self):
102
83
        if self._lock_count == 0: