/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/lockable_files.py

  • Committer: Michael Hudson
  • Date: 2009-02-27 02:38:36 UTC
  • mto: This revision was merged to the branch mainline in revision 4121.
  • Revision ID: michael.hudson@canonical.com-20090227023836-htoth0azybo7tvfe
this makes more sense

Show diffs side-by-side

added added

removed removed

Lines of Context:
43
43
# somewhat redundant with what's done in LockDir; the main difference is that
44
44
# LockableFiles permits reentrancy.
45
45
 
46
 
class LockWarner(object):
47
 
 
48
 
    def __init__(self, lock_count_holder, repr):
49
 
        self.lock_count_holder = lock_count_holder
 
46
class _LockCounter(object):
 
47
    """Hold a counter for a lock and warn if it's GCed while >= 1.
 
48
 
 
49
    This is separate from LockableFiles because putting a __del__ on
 
50
    LockableFiles can result in uncollectable cycles.
 
51
    """
 
52
 
 
53
    def __init__(self, repr):
 
54
        self.lock_count = 0
50
55
        self.repr = repr
51
56
 
52
57
    def __del__(self):
53
 
        if self.lock_count_holder[0] >= 1:
 
58
        if self.lock_count >= 1:
54
59
            # do not automatically unlock; there should have been a
55
60
            # try/finally to unlock this.
56
61
            warnings.warn("%r was gc'd while locked" % self.repr)
101
106
        self.lock_name = lock_name
102
107
        self._transaction = None
103
108
        self._lock_mode = None
104
 
        self._lock_count_holder = [0]
105
 
        self._lock_warner = LockWarner(self._lock_count_holder, repr(self))
 
109
        self._lock_counter = _LockCounter(repr(self))
106
110
        self._find_modes()
107
111
        esc_name = self._escape(lock_name)
108
112
        self._lock = lock_class(transport, esc_name,
261
265
            if self._lock_mode != 'w' or not self.get_transaction().writeable():
262
266
                raise errors.ReadOnlyError(self)
263
267
            self._lock.validate_token(token)
264
 
            self._lock_count_holder[0] += 1
 
268
            self._lock_counter.lock_count += 1
265
269
            return self._token_from_lock
266
270
        else:
267
271
            token_from_lock = self._lock.lock_write(token=token)
268
272
            #traceback.print_stack()
269
273
            self._lock_mode = 'w'
270
 
            self._lock_count_holder[0] = 1
 
274
            self._lock_counter.lock_count = 1
271
275
            self._set_transaction(transactions.WriteTransaction())
272
276
            self._token_from_lock = token_from_lock
273
277
            return token_from_lock
276
280
        if self._lock_mode:
277
281
            if self._lock_mode not in ('r', 'w'):
278
282
                raise ValueError("invalid lock mode %r" % (self._lock_mode,))
279
 
            self._lock_count_holder[0] += 1
 
283
            self._lock_counter.lock_count += 1
280
284
        else:
281
285
            self._lock.lock_read()
282
286
            #traceback.print_stack()
283
287
            self._lock_mode = 'r'
284
 
            self._lock_count_holder[0] = 1
 
288
            self._lock_counter.lock_count = 1
285
289
            self._set_transaction(transactions.ReadOnlyTransaction())
286
290
            # 5K may be excessive, but hey, its a knob.
287
291
            self.get_transaction().set_cache_size(5000)
289
293
    def unlock(self):
290
294
        if not self._lock_mode:
291
295
            raise errors.LockNotHeld(self)
292
 
        if self._lock_count_holder[0] > 1:
293
 
            self._lock_count_holder[0] -= 1
 
296
        if self._lock_counter.lock_count > 1:
 
297
            self._lock_counter.lock_count -= 1
294
298
        else:
295
299
            #traceback.print_stack()
296
300
            self._finish_transaction()
297
301
            try:
298
302
                self._lock.unlock()
299
303
            finally:
300
 
                self._lock_mode = self._lock_count_holder[0] = None
 
304
                self._lock_mode = self._lock_counter.lock_count = None
301
305
 
302
306
    @property
303
307
    def _lock_count(self):
304
 
        return self._lock_count_holder[0]
 
308
        return self._lock_counter.lock_count
305
309
 
306
310
    def is_locked(self):
307
311
        """Return true if this LockableFiles group is locked"""
308
 
        return self._lock_count_holder[0] >= 1
 
312
        return self._lock_counter.lock_count >= 1
309
313
 
310
314
    def get_physical_lock_status(self):
311
315
        """Return physical lock status.