~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/lock.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2009-03-06 06:48:25 UTC
  • mfrom: (4070.8.6 debug-config)
  • Revision ID: pqm@pqm.ubuntu.com-20090306064825-kbpwggw21dygeix6
(mbp) debug_flags configuration option

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, 2006, 2007, 2008 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
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
17
 
18
18
"""Locking using OS file locks or file existence.
42
42
    osutils,
43
43
    trace,
44
44
    )
45
 
from bzrlib.hooks import HookPoint, Hooks
 
45
from bzrlib.hooks import Hooks
46
46
 
47
47
 
48
48
class LockHooks(Hooks):
49
49
 
50
50
    def __init__(self):
51
51
        Hooks.__init__(self)
52
 
        self.create_hook(HookPoint('lock_acquired',
53
 
            "Called with a bzrlib.lock.LockResult when a physical lock is "
54
 
            "acquired.", (1, 8), None))
55
 
        self.create_hook(HookPoint('lock_released',
56
 
            "Called with a bzrlib.lock.LockResult when a physical lock is "
57
 
            "released.", (1, 8), None))
 
52
 
 
53
        # added in 1.8; called with a LockResult when a physical lock is
 
54
        # acquired
 
55
        self['lock_acquired'] = []
 
56
 
 
57
        # added in 1.8; called with a LockResult when a physical lock is
 
58
        # acquired
 
59
        self['lock_released'] = []
58
60
 
59
61
 
60
62
class Lock(object):
183
185
                    self.unlock()
184
186
                # we should be more precise about whats a locking
185
187
                # error and whats a random-other error
186
 
                raise errors.LockContention(self.filename, e)
 
188
                raise errors.LockContention(e)
187
189
 
188
190
        def unlock(self):
189
191
            _fcntl_WriteLock._open_locks.remove(self.filename)
207
209
            except IOError, e:
208
210
                # we should be more precise about whats a locking
209
211
                # error and whats a random-other error
210
 
                raise errors.LockContention(self.filename, e)
 
212
                raise errors.LockContention(e)
211
213
 
212
214
        def unlock(self):
213
215
            count = _fcntl_ReadLock._open_locks[self.filename]
275
277
                fcntl.lockf(new_f, fcntl.LOCK_EX | fcntl.LOCK_NB)
276
278
            except IOError, e:
277
279
                # TODO: Raise a more specific error based on the type of error
278
 
                raise errors.LockContention(self.filename, e)
 
280
                raise errors.LockContention(e)
279
281
            _fcntl_WriteLock._open_locks.add(self.filename)
280
282
 
281
283
            self.f = new_f
320
322
                raise
321
323
            except Exception, e:
322
324
                self._clear_f()
323
 
                raise errors.LockContention(filename, e)
 
325
                raise errors.LockContention(e)
324
326
 
325
327
        def unlock(self):
326
328
            overlapped = pywintypes.OVERLAPPED()
328
330
                win32file.UnlockFileEx(self.hfile, 0, 0x7fff0000, overlapped)
329
331
                self._clear_f()
330
332
            except Exception, e:
331
 
                raise errors.LockContention(self.filename, e)
 
333
                raise errors.LockContention(e)
332
334
 
333
335
 
334
336
    class _w32c_ReadLock(_w32c_FileLock):
437
439
                last_err = _GetLastError()
438
440
                if last_err in (ERROR_LOCK_VIOLATION,):
439
441
                    raise errors.LockContention(filename)
440
 
                raise errors.LockContention(filename,
441
 
                    'Unknown locking error: %s' % (last_err,))
 
442
                raise errors.LockContention('Unknown locking error: %s'
 
443
                                            % (last_err,))
442
444
 
443
445
        def unlock(self):
444
446
            overlapped = OVERLAPPED()
452
454
            if result == 0:
453
455
                self._clear_f()
454
456
                last_err = _GetLastError()
455
 
                raise errors.LockContention(self.filename,
456
 
                    'Unknown unlocking error: %s' % (last_err,))
 
457
                raise errors.LockContention('Unknown unlocking error: %s'
 
458
                                            % (last_err,))
457
459
 
458
460
 
459
461
    class _ctypes_ReadLock(_ctypes_FileLock):