~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/lockable_files.py

(gz) Remove bzrlib/util/elementtree/ package (Martin Packman)

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
 
17
from __future__ import absolute_import
 
18
 
17
19
from bzrlib.lazy_import import lazy_import
18
20
lazy_import(globals(), """
19
21
import warnings
33
35
    )
34
36
 
35
37
 
36
 
# XXX: The tracking here of lock counts and whether the lock is held is
37
 
# somewhat redundant with what's done in LockDir; the main difference is that
38
 
# LockableFiles permits reentrancy.
39
 
 
40
 
class _LockWarner(object):
41
 
    """Hold a counter for a lock and warn if GCed while the count is >= 1.
42
 
 
43
 
    This is separate from LockableFiles because putting a __del__ on
44
 
    LockableFiles can result in uncollectable cycles.
45
 
    """
46
 
 
47
 
    def __init__(self, repr):
48
 
        self.lock_count = 0
49
 
        self.repr = repr
50
 
 
51
 
    def __del__(self):
52
 
        if self.lock_count >= 1:
53
 
            # There should have been a try/finally to unlock this.
54
 
            warnings.warn("%r was gc'd while locked" % self.repr)
55
 
 
56
 
 
57
38
class LockableFiles(object):
58
39
    """Object representing a set of related files locked within the same scope.
59
40
 
68
49
    This class is now deprecated; code should move to using the Transport
69
50
    directly for file operations and using the lock or CountedLock for
70
51
    locking.
71
 
    
 
52
 
72
53
    :ivar _lock: The real underlying lock (e.g. a LockDir)
73
 
    :ivar _counted_lock: A lock decorated with a semaphore, so that it 
74
 
        can be re-entered.
 
54
    :ivar _lock_count: If _lock_mode is true, a positive count of the number
 
55
        of times the lock has been taken (and not yet released) *by this
 
56
        process*, through this particular object instance.
 
57
    :ivar _lock_mode: None, or 'r' or 'w'
75
58
    """
76
59
 
77
 
    # _lock_mode: None, or 'r' or 'w'
78
 
 
79
 
    # _lock_count: If _lock_mode is true, a positive count of the number of
80
 
    # times the lock has been taken *by this process*.
81
 
 
82
60
    def __init__(self, transport, lock_name, lock_class):
83
61
        """Create a LockableFiles group
84
62
 
92
70
        self.lock_name = lock_name
93
71
        self._transaction = None
94
72
        self._lock_mode = None
95
 
        self._lock_warner = _LockWarner(repr(self))
 
73
        self._lock_count = 0
96
74
        self._find_modes()
97
75
        esc_name = self._escape(lock_name)
98
76
        self._lock = lock_class(transport, esc_name,
111
89
    def __repr__(self):
112
90
        return '%s(%r)' % (self.__class__.__name__,
113
91
                           self._transport)
 
92
 
114
93
    def __str__(self):
115
94
        return 'LockableFiles(%s, %s)' % (self.lock_name, self._transport.base)
116
95
 
174
153
        some other way, and need to synchronise this object's state with that
175
154
        fact.
176
155
        """
177
 
        # TODO: Upgrade locking to support using a Transport,
178
 
        # and potentially a remote locking protocol
179
156
        if self._lock_mode:
180
 
            if self._lock_mode != 'w' or not self.get_transaction().writeable():
 
157
            if (self._lock_mode != 'w'
 
158
                or not self.get_transaction().writeable()):
181
159
                raise errors.ReadOnlyError(self)
182
160
            self._lock.validate_token(token)
183
 
            self._lock_warner.lock_count += 1
 
161
            self._lock_count += 1
184
162
            return self._token_from_lock
185
163
        else:
186
164
            token_from_lock = self._lock.lock_write(token=token)
187
165
            #traceback.print_stack()
188
166
            self._lock_mode = 'w'
189
 
            self._lock_warner.lock_count = 1
 
167
            self._lock_count = 1
190
168
            self._set_write_transaction()
191
169
            self._token_from_lock = token_from_lock
192
170
            return token_from_lock
195
173
        if self._lock_mode:
196
174
            if self._lock_mode not in ('r', 'w'):
197
175
                raise ValueError("invalid lock mode %r" % (self._lock_mode,))
198
 
            self._lock_warner.lock_count += 1
 
176
            self._lock_count += 1
199
177
        else:
200
178
            self._lock.lock_read()
201
179
            #traceback.print_stack()
202
180
            self._lock_mode = 'r'
203
 
            self._lock_warner.lock_count = 1
 
181
            self._lock_count = 1
204
182
            self._set_read_transaction()
205
183
 
206
184
    def _set_read_transaction(self):
217
195
    def unlock(self):
218
196
        if not self._lock_mode:
219
197
            return lock.cant_unlock_not_held(self)
220
 
        if self._lock_warner.lock_count > 1:
221
 
            self._lock_warner.lock_count -= 1
 
198
        if self._lock_count > 1:
 
199
            self._lock_count -= 1
222
200
        else:
223
201
            #traceback.print_stack()
224
202
            self._finish_transaction()
225
203
            try:
226
204
                self._lock.unlock()
227
205
            finally:
228
 
                self._lock_mode = self._lock_warner.lock_count = None
229
 
 
230
 
    @property
231
 
    def _lock_count(self):
232
 
        return self._lock_warner.lock_count
 
206
                self._lock_mode = self._lock_count = None
233
207
 
234
208
    def is_locked(self):
235
209
        """Return true if this LockableFiles group is locked"""
236
 
        return self._lock_warner.lock_count >= 1
 
210
        return self._lock_count >= 1
237
211
 
238
212
    def get_physical_lock_status(self):
239
213
        """Return physical lock status.
325
299
    def validate_token(self, token):
326
300
        if token is not None:
327
301
            raise errors.TokenLockingNotSupported(self)
328