~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-06 07:08:07 UTC
  • mfrom: (3474.1.3 countedlock)
  • Revision ID: pqm@pqm.ubuntu.com-20080606070807-q0ro9i4kgllr5mbu
(mbp) CountedLock support for lock tokens

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007 Canonical Ltd
 
1
# Copyright (C) 2007, 2008 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.errors import (
21
 
    LockError,
22
 
    ReadOnlyError,
 
20
from bzrlib import (
 
21
    errors,
23
22
    )
24
23
 
25
24
 
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
 
 
31
25
class CountedLock(object):
32
26
    """Decorator around a lock that makes it reentrant.
33
27
 
40
34
        self._lock_mode = None
41
35
        self._lock_count = 0
42
36
 
 
37
    def __repr__(self):
 
38
        return "%s(%r)" % (self.__class__.__name__,
 
39
            self._real_lock)
 
40
 
43
41
    def break_lock(self):
44
42
        self._real_lock.break_lock()
45
43
        self._lock_mode = None
62
60
            self._lock_count = 1
63
61
            self._lock_mode = 'r'
64
62
 
65
 
    def lock_write(self):
 
63
    def lock_write(self, token=None):
66
64
        """Acquire the lock in write mode.
67
65
 
68
66
        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
 
            self._real_lock.lock_write()
 
71
            return_token = self._real_lock.lock_write(token)
72
72
            self._lock_mode = 'w'
 
73
            self._lock_count += 1
 
74
            return return_token
73
75
        elif self._lock_mode != 'w':
74
 
            raise ReadOnlyError(self)
75
 
        self._lock_count += 1
 
76
            raise errors.ReadOnlyError(self)
 
77
        else:
 
78
            self._real_lock.validate_token(token)
 
79
            self._lock_count += 1
 
80
            return token
76
81
 
77
82
    def unlock(self):
78
83
        if self._lock_count == 0:
79
 
            raise LockError("%s not locked" % (self,))
 
84
            raise errors.LockNotHeld(self)
80
85
        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
 
89
            self._lock_mode = None
 
90
            self._lock_count -= 1
81
91
            self._real_lock.unlock()
82
 
            self._lock_mode = None
83
 
        self._lock_count -= 1
 
92
        else:
 
93
            self._lock_count -= 1