~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/lock.py

  • Committer: Martin Pool
  • Date: 2009-07-10 06:46:10 UTC
  • mto: (4525.1.1 integration)
  • mto: This revision was merged to the branch mainline in revision 4526.
  • Revision ID: mbp@sourcefrog.net-20090710064610-sqviksbqp5i34sw2
Rename to per_interrepository

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2010 Canonical Ltd
 
1
# Copyright (C) 2005, 2006, 2007, 2008, 2009 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
37
37
import errno
38
38
import os
39
39
import sys
40
 
import warnings
41
40
 
42
41
from bzrlib import (
43
 
    debug,
44
42
    errors,
45
43
    osutils,
46
44
    trace,
84
82
        return self.lock_url == other.lock_url and self.details == other.details
85
83
 
86
84
    def __repr__(self):
87
 
        return '%s(%s, %s)' % (self.__class__.__name__,
 
85
        return '%s(%s%s)' % (self.__class__.__name__,
88
86
                             self.lock_url, self.details)
89
87
 
90
88
 
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
 
 
105
 
def cant_unlock_not_held(locked_object):
106
 
    """An attempt to unlock failed because the object was not locked.
107
 
 
108
 
    This provides a policy point from which we can generate either a warning 
109
 
    or an exception.
110
 
    """
111
 
    # This is typically masking some other error and called from a finally
112
 
    # block, so it's useful to have the option not to generate a new error
113
 
    # here.  You can use -Werror to make it fatal.  It should possibly also
114
 
    # raise LockNotHeld.
115
 
    if 'unlock' in debug.debug_flags:
116
 
        warnings.warn("%r is already unlocked" % (locked_object,),
117
 
            stacklevel=3)
118
 
    else:
119
 
        raise errors.LockNotHeld(locked_object)
120
 
 
121
 
 
122
89
try:
123
90
    import fcntl
124
91
    have_fcntl = True
204
171
            if self.filename in _fcntl_WriteLock._open_locks:
205
172
                self._clear_f()
206
173
                raise errors.LockContention(self.filename)
207
 
            if self.filename in _fcntl_ReadLock._open_locks:
208
 
                if 'strict_locks' in debug.debug_flags:
209
 
                    self._clear_f()
210
 
                    raise errors.LockContention(self.filename)
211
 
                else:
212
 
                    trace.mutter('Write lock taken w/ an open read lock on: %s'
213
 
                                 % (self.filename,))
214
174
 
215
175
            self._open(self.filename, 'rb+')
216
176
            # reserve a slot for this lock - even if the lockf call fails,
217
 
            # at this point unlock() will be called, because self.f is set.
 
177
            # at thisi point unlock() will be called, because self.f is set.
218
178
            # TODO: make this fully threadsafe, if we decide we care.
219
179
            _fcntl_WriteLock._open_locks.add(self.filename)
220
180
            try:
241
201
        def __init__(self, filename):
242
202
            super(_fcntl_ReadLock, self).__init__()
243
203
            self.filename = osutils.realpath(filename)
244
 
            if self.filename in _fcntl_WriteLock._open_locks:
245
 
                if 'strict_locks' in debug.debug_flags:
246
 
                    # We raise before calling _open so we don't need to
247
 
                    # _clear_f
248
 
                    raise errors.LockContention(self.filename)
249
 
                else:
250
 
                    trace.mutter('Read lock taken w/ an open write lock on: %s'
251
 
                                 % (self.filename,))
252
204
            _fcntl_ReadLock._open_locks.setdefault(self.filename, 0)
253
205
            _fcntl_ReadLock._open_locks[self.filename] += 1
254
206
            self._open(filename, 'rb')
447
399
            DWORD,                 # dwFlagsAndAttributes
448
400
            HANDLE                 # hTemplateFile
449
401
        )((_function_name, ctypes.windll.kernel32))
450
 
 
 
402
    
451
403
    INVALID_HANDLE_VALUE = -1
452
 
 
 
404
    
453
405
    GENERIC_READ = 0x80000000
454
406
    GENERIC_WRITE = 0x40000000
455
407
    FILE_SHARE_READ = 1
456
408
    OPEN_ALWAYS = 4
457
409
    FILE_ATTRIBUTE_NORMAL = 128
458
 
 
 
410
    
459
411
    ERROR_ACCESS_DENIED = 5
460
412
    ERROR_SHARING_VIOLATION = 32
461
413
 
532
484
# We default to using the first available lock class.
533
485
_lock_type, WriteLock, ReadLock = _lock_classes[0]
534
486
 
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