~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/lock.py

Merge up through 2.2.0.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006, 2007, 2008, 2009 Canonical Ltd
 
1
# Copyright (C) 2005-2010 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
84
84
        return self.lock_url == other.lock_url and self.details == other.details
85
85
 
86
86
    def __repr__(self):
87
 
        return '%s(%s%s)' % (self.__class__.__name__,
 
87
        return '%s(%s, %s)' % (self.__class__.__name__,
88
88
                             self.lock_url, self.details)
89
89
 
90
90
 
 
91
class LogicalLockResult(object):
 
92
    """The result of a lock_read/lock_write/lock_tree_write call on lockables.
 
93
 
 
94
    :ivar unlock: A callable which will unlock the lock.
 
95
    """
 
96
 
 
97
    def __init__(self, unlock):
 
98
        self.unlock = unlock
 
99
 
 
100
    def __repr__(self):
 
101
        return "LogicalLockResult(%s)" % (self.unlock)
 
102
 
 
103
 
 
104
 
91
105
def cant_unlock_not_held(locked_object):
92
106
    """An attempt to unlock failed because the object was not locked.
93
107
 
200
214
 
201
215
            self._open(self.filename, 'rb+')
202
216
            # reserve a slot for this lock - even if the lockf call fails,
203
 
            # at thisi point unlock() will be called, because self.f is set.
 
217
            # at this point unlock() will be called, because self.f is set.
204
218
            # TODO: make this fully threadsafe, if we decide we care.
205
219
            _fcntl_WriteLock._open_locks.add(self.filename)
206
220
            try:
518
532
# We default to using the first available lock class.
519
533
_lock_type, WriteLock, ReadLock = _lock_classes[0]
520
534
 
 
535
 
 
536
class _RelockDebugMixin(object):
 
537
    """Mixin support for -Drelock flag.
 
538
 
 
539
    Add this as a base class then call self._note_lock with 'r' or 'w' when
 
540
    acquiring a read- or write-lock.  If this object was previously locked (and
 
541
    locked the same way), and -Drelock is set, then this will trace.note a
 
542
    message about it.
 
543
    """
 
544
    
 
545
    _prev_lock = None
 
546
 
 
547
    def _note_lock(self, lock_type):
 
548
        if 'relock' in debug.debug_flags and self._prev_lock == lock_type:
 
549
            if lock_type == 'r':
 
550
                type_name = 'read'
 
551
            else:
 
552
                type_name = 'write'
 
553
            trace.note('%r was %s locked again', self, type_name)
 
554
        self._prev_lock = lock_type
 
555