/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/lock.py

  • Committer: Robert Collins
  • Date: 2010-05-06 11:08:10 UTC
  • mto: This revision was merged to the branch mainline in revision 5223.
  • Revision ID: robertc@robertcollins.net-20100506110810-h3j07fh5gmw54s25
Cleaner matcher matching revised unlocking protocol.

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
 
190
190
            if self.filename in _fcntl_WriteLock._open_locks:
191
191
                self._clear_f()
192
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,))
193
200
 
194
201
            self._open(self.filename, 'rb+')
195
202
            # reserve a slot for this lock - even if the lockf call fails,
196
 
            # at thisi point unlock() will be called, because self.f is set.
 
203
            # at this point unlock() will be called, because self.f is set.
197
204
            # TODO: make this fully threadsafe, if we decide we care.
198
205
            _fcntl_WriteLock._open_locks.add(self.filename)
199
206
            try:
220
227
        def __init__(self, filename):
221
228
            super(_fcntl_ReadLock, self).__init__()
222
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,))
223
238
            _fcntl_ReadLock._open_locks.setdefault(self.filename, 0)
224
239
            _fcntl_ReadLock._open_locks[self.filename] += 1
225
240
            self._open(filename, 'rb')
418
433
            DWORD,                 # dwFlagsAndAttributes
419
434
            HANDLE                 # hTemplateFile
420
435
        )((_function_name, ctypes.windll.kernel32))
421
 
    
 
436
 
422
437
    INVALID_HANDLE_VALUE = -1
423
 
    
 
438
 
424
439
    GENERIC_READ = 0x80000000
425
440
    GENERIC_WRITE = 0x40000000
426
441
    FILE_SHARE_READ = 1
427
442
    OPEN_ALWAYS = 4
428
443
    FILE_ATTRIBUTE_NORMAL = 128
429
 
    
 
444
 
430
445
    ERROR_ACCESS_DENIED = 5
431
446
    ERROR_SHARING_VIOLATION = 32
432
447
 
503
518
# We default to using the first available lock class.
504
519
_lock_type, WriteLock, ReadLock = _lock_classes[0]
505
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