~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/lock.py

  • Committer: Michael Ellerman
  • Date: 2006-02-28 14:45:51 UTC
  • mto: (1558.1.18 Aaron's integration)
  • mto: This revision was merged to the branch mainline in revision 1586.
  • Revision ID: michael@ellerman.id.au-20060228144551-3d9941ecde4a0b0a
Update contrib/pwk for -p1 diffs from bzr

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006 Canonical Ltd
 
1
# Copyright (C) 2005 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
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
17
 
18
 
"""Locking using OS file locks or file existence.
 
18
"""Locking wrappers.
19
19
 
20
 
Note: This method of locking is generally deprecated in favour of LockDir, but
21
 
is used to lock local WorkingTrees, and by some old formats.  It's accessed
22
 
through Transport.lock_read(), etc.
 
20
This only does local locking using OS locks for now.
23
21
 
24
22
This module causes two methods, lock() and unlock() to be defined in
25
23
any way that works on the current platform.
26
24
 
27
25
It is not specified whether these locks are reentrant (i.e. can be
28
26
taken repeatedly by a single process) or whether they exclude
29
 
different threads in a single process.  That reentrancy is provided by 
30
 
LockableFiles.
 
27
different threads in a single process.  
 
28
 
 
29
Eventually we may need to use some kind of lock representation that
 
30
will work on a dumb filesystem without actual locking primitives.
31
31
 
32
32
This defines two classes: ReadLock and WriteLock, which can be
33
33
implemented in different ways on different platforms.  Both have an
38
38
import os
39
39
import sys
40
40
 
41
 
from bzrlib.trace import mutter
 
41
from bzrlib.trace import mutter, note, warning
42
42
from bzrlib.errors import LockError
43
43
 
44
 
 
45
44
class _base_Lock(object):
46
45
    def _open(self, filename, filemode):
47
46
        try:
66
65
    def unlock(self):
67
66
        raise NotImplementedError()
68
67
 
 
68
        
 
69
 
 
70
 
 
71
 
69
72
 
70
73
############################################################
71
74
# msvcrt locks
126
129
                    overlapped = pywintypes.OVERLAPPED()
127
130
                    win32file.LockFileEx(self.hfile, lockmode, 0, 0x7fff0000, overlapped)
128
131
                except Exception, e:
129
 
                    if self.f:
130
 
                        self.f.close()
131
 
                        self.f = None
132
132
                    raise LockError(e)
133
133
 
134
134
            def unlock(self):
141
141
                    raise LockError(e)
142
142
 
143
143
 
 
144
 
144
145
        class _w32c_ReadLock(_w32c_FileLock):
145
146
            def __init__(self, filename):
146
147
                _w32c_FileLock._lock(self, filename, 'rb',
152
153
                                     LOCK_EX + LOCK_NB)
153
154
 
154
155
 
 
156
 
155
157
        WriteLock = _w32c_WriteLock
156
158
        ReadLock = _w32c_ReadLock
157
159
 
186
188
                    _msvc_lock(self._open(filename, 'wb'), self.LOCK_EX)
187
189
 
188
190
 
 
191
 
189
192
            def _msvc_lock(f, flags):
190
193
                try:
191
194
                    # Unfortunately, msvcrt.LK_RLCK is equivalent to msvcrt.LK_LOCK
241
244
                    raise LockError(e)
242
245
 
243
246
 
 
247
 
244
248
            WriteLock = _msvc_WriteLock
245
249
            ReadLock = _msvc_ReadLock
246
250
        except ImportError:
247
251
            raise NotImplementedError("please write a locking method "
248
252
                                      "for platform %r" % sys.platform)
 
253
 
 
254
 
 
255
 
 
256
 
 
257
 
 
258
 
 
259