~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/lock.py

  • Committer: Vincent Ladeuil
  • Date: 2010-01-25 15:55:48 UTC
  • mto: (4985.1.4 add-attr-cleanup)
  • mto: This revision was merged to the branch mainline in revision 4988.
  • Revision ID: v.ladeuil+lp@free.fr-20100125155548-0l352pujvt5bzl5e
Deploy addAttrCleanup on the whole test suite.

Several use case worth mentioning:

- setting a module or any other object attribute is the majority
by far. In some cases the setting itself is deferred but most of
the time we want to set at the same time we add the cleanup.

- there multiple occurrences of protecting hooks or ui factory
which are now useless (the test framework takes care of that now),

- there was some lambda uses that can now be avoided.

That first cleanup already simplifies things a lot.

Show diffs side-by-side

added added

removed removed

Lines of Context:
37
37
import errno
38
38
import os
39
39
import sys
 
40
import warnings
40
41
 
41
42
from bzrlib import (
 
43
    debug,
42
44
    errors,
43
45
    osutils,
44
46
    trace,
86
88
                             self.lock_url, self.details)
87
89
 
88
90
 
 
91
def cant_unlock_not_held(locked_object):
 
92
    """An attempt to unlock failed because the object was not locked.
 
93
 
 
94
    This provides a policy point from which we can generate either a warning 
 
95
    or an exception.
 
96
    """
 
97
    # This is typically masking some other error and called from a finally
 
98
    # block, so it's useful to have the option not to generate a new error
 
99
    # here.  You can use -Werror to make it fatal.  It should possibly also
 
100
    # raise LockNotHeld.
 
101
    if 'unlock' in debug.debug_flags:
 
102
        warnings.warn("%r is already unlocked" % (locked_object,),
 
103
            stacklevel=3)
 
104
    else:
 
105
        raise errors.LockNotHeld(locked_object)
 
106
 
 
107
 
89
108
try:
90
109
    import fcntl
91
110
    have_fcntl = True
171
190
            if self.filename in _fcntl_WriteLock._open_locks:
172
191
                self._clear_f()
173
192
                raise errors.LockContention(self.filename)
 
193
            if self.filename in _fcntl_ReadLock._open_locks:
 
194
                if 'strict_locks' in debug.debug_flags:
 
195
                    self._clear_f()
 
196
                    raise errors.LockContention(self.filename)
 
197
                else:
 
198
                    trace.mutter('Write lock taken w/ an open read lock on: %s'
 
199
                                 % (self.filename,))
174
200
 
175
201
            self._open(self.filename, 'rb+')
176
202
            # reserve a slot for this lock - even if the lockf call fails,
201
227
        def __init__(self, filename):
202
228
            super(_fcntl_ReadLock, self).__init__()
203
229
            self.filename = osutils.realpath(filename)
 
230
            if self.filename in _fcntl_WriteLock._open_locks:
 
231
                if 'strict_locks' in debug.debug_flags:
 
232
                    # We raise before calling _open so we don't need to
 
233
                    # _clear_f
 
234
                    raise errors.LockContention(self.filename)
 
235
                else:
 
236
                    trace.mutter('Read lock taken w/ an open write lock on: %s'
 
237
                                 % (self.filename,))
204
238
            _fcntl_ReadLock._open_locks.setdefault(self.filename, 0)
205
239
            _fcntl_ReadLock._open_locks[self.filename] += 1
206
240
            self._open(filename, 'rb')
399
433
            DWORD,                 # dwFlagsAndAttributes
400
434
            HANDLE                 # hTemplateFile
401
435
        )((_function_name, ctypes.windll.kernel32))
402
 
    
 
436
 
403
437
    INVALID_HANDLE_VALUE = -1
404
 
    
 
438
 
405
439
    GENERIC_READ = 0x80000000
406
440
    GENERIC_WRITE = 0x40000000
407
441
    FILE_SHARE_READ = 1
408
442
    OPEN_ALWAYS = 4
409
443
    FILE_ATTRIBUTE_NORMAL = 128
410
 
    
 
444
 
411
445
    ERROR_ACCESS_DENIED = 5
412
446
    ERROR_SHARING_VIOLATION = 32
413
447
 
484
518
# We default to using the first available lock class.
485
519
_lock_type, WriteLock, ReadLock = _lock_classes[0]
486
520
 
 
521
 
 
522
class _RelockDebugMixin(object):
 
523
    """Mixin support for -Drelock flag.
 
524
 
 
525
    Add this as a base class then call self._note_lock with 'r' or 'w' when
 
526
    acquiring a read- or write-lock.  If this object was previously locked (and
 
527
    locked the same way), and -Drelock is set, then this will trace.note a
 
528
    message about it.
 
529
    """
 
530
    
 
531
    _prev_lock = None
 
532
 
 
533
    def _note_lock(self, lock_type):
 
534
        if 'relock' in debug.debug_flags and self._prev_lock == lock_type:
 
535
            if lock_type == 'r':
 
536
                type_name = 'read'
 
537
            else:
 
538
                type_name = 'write'
 
539
            trace.note('%r was %s locked again', self, type_name)
 
540
        self._prev_lock = lock_type
 
541