~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/lock.py

  • Committer: John Arbash Meinel
  • Date: 2009-07-08 15:43:51 UTC
  • mto: This revision was merged to the branch mainline in revision 4521.
  • Revision ID: john@arbash-meinel.com-20090708154351-u0t41fwjqm28pbnu
Add comments in the finally sections as to why we want them.

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,
47
45
    )
48
 
from bzrlib.hooks import Hooks
 
46
from bzrlib.hooks import HookPoint, Hooks
49
47
 
50
48
 
51
49
class LockHooks(Hooks):
52
50
 
53
51
    def __init__(self):
54
 
        Hooks.__init__(self, "bzrlib.lock", "Lock.hooks")
55
 
        self.add_hook('lock_acquired',
56
 
            "Called with a bzrlib.lock.LockResult when a physical lock is "
57
 
            "acquired.", (1, 8))
58
 
        self.add_hook('lock_released',
59
 
            "Called with a bzrlib.lock.LockResult when a physical lock is "
60
 
            "released.", (1, 8))
61
 
        self.add_hook('lock_broken',
62
 
            "Called with a bzrlib.lock.LockResult when a physical lock is "
63
 
            "broken.", (1, 15))
 
52
        Hooks.__init__(self)
 
53
        self.create_hook(HookPoint('lock_acquired',
 
54
            "Called with a bzrlib.lock.LockResult when a physical lock is "
 
55
            "acquired.", (1, 8), None))
 
56
        self.create_hook(HookPoint('lock_released',
 
57
            "Called with a bzrlib.lock.LockResult when a physical lock is "
 
58
            "released.", (1, 8), None))
 
59
        self.create_hook(HookPoint('lock_broken',
 
60
            "Called with a bzrlib.lock.LockResult when a physical lock is "
 
61
            "broken.", (1, 15), None))
64
62
 
65
63
 
66
64
class Lock(object):
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
171
138
            self.f.close()
172
139
            self.f = None
173
140
 
 
141
    def __del__(self):
 
142
        if self.f:
 
143
            from warnings import warn
 
144
            warn("lock on %r not released" % self.f)
 
145
            self.unlock()
 
146
 
174
147
    def unlock(self):
175
148
        raise NotImplementedError()
176
149
 
198
171
            if self.filename in _fcntl_WriteLock._open_locks:
199
172
                self._clear_f()
200
173
                raise errors.LockContention(self.filename)
201
 
            if self.filename in _fcntl_ReadLock._open_locks:
202
 
                if 'strict_locks' in debug.debug_flags:
203
 
                    self._clear_f()
204
 
                    raise errors.LockContention(self.filename)
205
 
                else:
206
 
                    trace.mutter('Write lock taken w/ an open read lock on: %s'
207
 
                                 % (self.filename,))
208
174
 
209
175
            self._open(self.filename, 'rb+')
210
176
            # reserve a slot for this lock - even if the lockf call fails,
211
 
            # 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.
212
178
            # TODO: make this fully threadsafe, if we decide we care.
213
179
            _fcntl_WriteLock._open_locks.add(self.filename)
214
180
            try:
235
201
        def __init__(self, filename):
236
202
            super(_fcntl_ReadLock, self).__init__()
237
203
            self.filename = osutils.realpath(filename)
238
 
            if self.filename in _fcntl_WriteLock._open_locks:
239
 
                if 'strict_locks' in debug.debug_flags:
240
 
                    # We raise before calling _open so we don't need to
241
 
                    # _clear_f
242
 
                    raise errors.LockContention(self.filename)
243
 
                else:
244
 
                    trace.mutter('Read lock taken w/ an open write lock on: %s'
245
 
                                 % (self.filename,))
246
204
            _fcntl_ReadLock._open_locks.setdefault(self.filename, 0)
247
205
            _fcntl_ReadLock._open_locks[self.filename] += 1
248
206
            self._open(filename, 'rb')
441
399
            DWORD,                 # dwFlagsAndAttributes
442
400
            HANDLE                 # hTemplateFile
443
401
        )((_function_name, ctypes.windll.kernel32))
444
 
 
 
402
    
445
403
    INVALID_HANDLE_VALUE = -1
446
 
 
 
404
    
447
405
    GENERIC_READ = 0x80000000
448
406
    GENERIC_WRITE = 0x40000000
449
407
    FILE_SHARE_READ = 1
450
408
    OPEN_ALWAYS = 4
451
409
    FILE_ATTRIBUTE_NORMAL = 128
452
 
 
 
410
    
453
411
    ERROR_ACCESS_DENIED = 5
454
412
    ERROR_SHARING_VIOLATION = 32
455
413
 
526
484
# We default to using the first available lock class.
527
485
_lock_type, WriteLock, ReadLock = _lock_classes[0]
528
486
 
529
 
 
530
 
class _RelockDebugMixin(object):
531
 
    """Mixin support for -Drelock flag.
532
 
 
533
 
    Add this as a base class then call self._note_lock with 'r' or 'w' when
534
 
    acquiring a read- or write-lock.  If this object was previously locked (and
535
 
    locked the same way), and -Drelock is set, then this will trace.note a
536
 
    message about it.
537
 
    """
538
 
    
539
 
    _prev_lock = None
540
 
 
541
 
    def _note_lock(self, lock_type):
542
 
        if 'relock' in debug.debug_flags and self._prev_lock == lock_type:
543
 
            if lock_type == 'r':
544
 
                type_name = 'read'
545
 
            else:
546
 
                type_name = 'write'
547
 
            trace.note('%r was %s locked again', self, type_name)
548
 
        self._prev_lock = lock_type
549