~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/counted_lock.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2008-06-05 04:05:05 UTC
  • mfrom: (3473.1.1 ianc-integration)
  • Revision ID: pqm@pqm.ubuntu.com-20080605040505-i9kqxg2fps2qjdi0
Add the 'alias' command (Tim Penhey)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007, 2008 Canonical Ltd
 
1
# Copyright (C) 2007 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
17
17
"""Counted lock class"""
18
18
 
19
19
 
20
 
from bzrlib import (
21
 
    errors,
 
20
from bzrlib.errors import (
 
21
    LockError,
 
22
    ReadOnlyError,
22
23
    )
23
24
 
24
25
 
 
26
# TODO: Pass through lock tokens on lock_write and read, and return them...
 
27
#
 
28
# TODO: Allow upgrading read locks to write?  Conceptually difficult.
 
29
 
 
30
 
25
31
class CountedLock(object):
26
32
    """Decorator around a lock that makes it reentrant.
27
33
 
34
40
        self._lock_mode = None
35
41
        self._lock_count = 0
36
42
 
37
 
    def __repr__(self):
38
 
        return "%s(%r)" % (self.__class__.__name__,
39
 
            self._real_lock)
40
 
 
41
43
    def break_lock(self):
42
44
        self._real_lock.break_lock()
43
45
        self._lock_mode = None
60
62
            self._lock_count = 1
61
63
            self._lock_mode = 'r'
62
64
 
63
 
    def lock_write(self, token=None):
 
65
    def lock_write(self):
64
66
        """Acquire the lock in write mode.
65
67
 
66
68
        If the lock was originally acquired in read mode this will fail.
67
 
 
68
 
        :param token: If non-None, reacquire the lock using this token.
69
69
        """
70
70
        if self._lock_count == 0:
71
 
            return_token = self._real_lock.lock_write(token)
 
71
            self._real_lock.lock_write()
72
72
            self._lock_mode = 'w'
73
 
            self._lock_count += 1
74
 
            return return_token
75
73
        elif self._lock_mode != 'w':
76
 
            raise errors.ReadOnlyError(self)
77
 
        else:
78
 
            self._real_lock.validate_token(token)
79
 
            self._lock_count += 1
80
 
            return token
 
74
            raise ReadOnlyError(self)
 
75
        self._lock_count += 1
81
76
 
82
77
    def unlock(self):
83
78
        if self._lock_count == 0:
84
 
            raise errors.LockNotHeld(self)
 
79
            raise LockError("%s not locked" % (self,))
85
80
        elif self._lock_count == 1:
86
 
            # these are decremented first; if we fail to unlock the most
87
 
            # reasonable assumption is that we still don't have the lock
88
 
            # anymore
 
81
            self._real_lock.unlock()
89
82
            self._lock_mode = None
90
 
            self._lock_count -= 1
91
 
            self._real_lock.unlock()
92
 
        else:
93
 
            self._lock_count -= 1
 
83
        self._lock_count -= 1