~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/counted_lock.py

(jameinel) Allow 'bzr serve' to interpret SIGHUP as a graceful shutdown.
 (bug #795025) (John A Meinel)

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
 
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.
30
33
    """
31
34
 
32
35
    def __init__(self, real_lock):
43
46
        self._lock_mode = None
44
47
        self._lock_count = 0
45
48
 
 
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
 
46
61
    def is_locked(self):
47
62
        return self._lock_mode is not None
48
63
 
65
80
 
66
81
        If the lock was originally acquired in read mode this will fail.
67
82
 
68
 
        :param token: If non-None, reacquire the lock using this token.
 
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.
69
88
        """
70
89
        if self._lock_count == 0:
71
 
            return_token = self._real_lock.lock_write(token)
 
90
            self._token = self._real_lock.lock_write(token=token)
72
91
            self._lock_mode = 'w'
73
92
            self._lock_count += 1
74
 
            return return_token
 
93
            return self._token
75
94
        elif self._lock_mode != 'w':
76
95
            raise errors.ReadOnlyError(self)
77
96
        else:
78
97
            self._real_lock.validate_token(token)
79
98
            self._lock_count += 1
80
 
            return token
 
99
            return self._token
81
100
 
82
101
    def unlock(self):
83
102
        if self._lock_count == 0: