~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/lock.py

Merge bzr.dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# Copyright (C) 2005, 2006 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
5
5
# the Free Software Foundation; either version 2 of the License, or
6
6
# (at your option) any later version.
7
 
 
 
7
#
8
8
# This program is distributed in the hope that it will be useful,
9
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
11
# GNU General Public License for more details.
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
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
38
38
import os
39
39
import sys
40
40
 
41
 
from bzrlib.trace import mutter, note, warning
42
 
from bzrlib.errors import LockError
 
41
from bzrlib.errors import LockError, LockContention
 
42
from bzrlib.osutils import realpath
 
43
from bzrlib.trace import mutter
 
44
 
43
45
 
44
46
class _base_Lock(object):
 
47
 
45
48
    def _open(self, filename, filemode):
46
49
        try:
47
50
            self.f = open(filename, filemode)
65
68
    def unlock(self):
66
69
        raise NotImplementedError()
67
70
 
68
 
        
69
 
 
70
 
 
71
 
 
72
71
 
73
72
############################################################
74
73
# msvcrt locks
78
77
    import fcntl
79
78
 
80
79
    class _fcntl_FileLock(_base_Lock):
 
80
 
81
81
        f = None
82
82
 
83
 
        def unlock(self):
 
83
        def _unlock(self):
84
84
            fcntl.lockf(self.f, fcntl.LOCK_UN)
 
85
            self._clear_f()
 
86
 
 
87
        def _clear_f(self):
 
88
            """Clear the self.f attribute cleanly."""
85
89
            self.f.close()
86
 
            del self.f 
 
90
            del self.f
 
91
 
87
92
 
88
93
    class _fcntl_WriteLock(_fcntl_FileLock):
 
94
 
 
95
        open_locks = {}
 
96
 
89
97
        def __init__(self, filename):
90
98
            # standard IO errors get exposed directly.
91
 
            self._open(filename, 'wb')
 
99
            self._open(filename, 'rb+')
 
100
            self.filename = realpath(filename)
 
101
            if self.filename in self.open_locks:
 
102
                self._clear_f()
 
103
                raise LockContention("Lock already held.")
 
104
            # reserve a slot for this lock - even if the lockf call fails,
 
105
            # at thisi point unlock() will be called, because self.f is set.
 
106
            # TODO: make this fully threadsafe, if we decide we care.
 
107
            self.open_locks[self.filename] = self.filename
92
108
            try:
93
 
                fcntl.lockf(self.f, fcntl.LOCK_EX)
 
109
                # LOCK_NB will cause IOError to be raised if we can't grab a
 
110
                # lock right away.
 
111
                fcntl.lockf(self.f, fcntl.LOCK_EX | fcntl.LOCK_NB)
94
112
            except IOError, e:
 
113
                if e.errno in (errno.EAGAIN, errno.EACCES):
 
114
                    # We couldn't grab the lock
 
115
                    self.unlock()
95
116
                # we should be more precise about whats a locking
96
117
                # error and whats a random-other error
97
118
                raise LockError(e)
98
119
 
 
120
        def unlock(self):
 
121
            del self.open_locks[self.filename]
 
122
            self._unlock()
 
123
 
 
124
 
99
125
    class _fcntl_ReadLock(_fcntl_FileLock):
100
126
 
101
127
        def __init__(self, filename):
102
128
            # standard IO errors get exposed directly.
103
129
            self._open(filename, 'rb')
104
130
            try:
105
 
                fcntl.lockf(self.f, fcntl.LOCK_SH)
 
131
                # LOCK_NB will cause IOError to be raised if we can't grab a
 
132
                # lock right away.
 
133
                fcntl.lockf(self.f, fcntl.LOCK_SH | fcntl.LOCK_NB)
106
134
            except IOError, e:
107
135
                # we should be more precise about whats a locking
108
136
                # error and whats a random-other error
109
137
                raise LockError(e)
110
138
 
 
139
        def unlock(self):
 
140
            self._unlock()
 
141
 
 
142
 
111
143
    WriteLock = _fcntl_WriteLock
112
144
    ReadLock = _fcntl_ReadLock
113
145
 
129
161
                    overlapped = pywintypes.OVERLAPPED()
130
162
                    win32file.LockFileEx(self.hfile, lockmode, 0, 0x7fff0000, overlapped)
131
163
                except Exception, e:
 
164
                    if self.f:
 
165
                        self.f.close()
 
166
                        self.f = None
132
167
                    raise LockError(e)
133
168
 
134
169
            def unlock(self):
141
176
                    raise LockError(e)
142
177
 
143
178
 
144
 
 
145
179
        class _w32c_ReadLock(_w32c_FileLock):
146
180
            def __init__(self, filename):
147
181
                _w32c_FileLock._lock(self, filename, 'rb',
149
183
 
150
184
        class _w32c_WriteLock(_w32c_FileLock):
151
185
            def __init__(self, filename):
152
 
                _w32c_FileLock._lock(self, filename, 'wb',
 
186
                _w32c_FileLock._lock(self, filename, 'rb+',
153
187
                                     LOCK_EX + LOCK_NB)
154
188
 
155
189
 
156
 
 
157
190
        WriteLock = _w32c_WriteLock
158
191
        ReadLock = _w32c_ReadLock
159
192
 
172
205
                LOCK_SH = 1
173
206
                LOCK_EX = 2
174
207
                LOCK_NB = 4
 
208
 
175
209
                def unlock(self):
176
210
                    _msvc_unlock(self.f)
177
211
                    self.f.close()
180
214
 
181
215
            class _msvc_ReadLock(_msvc_FileLock):
182
216
                def __init__(self, filename):
183
 
                    _msvc_lock(self._open(filename, 'rb'), self.LOCK_SH)
 
217
                    _msvc_lock(self._open(filename, 'rb'),
 
218
                               self.LOCK_SH | self.LOCK_NB)
184
219
 
185
220
 
186
221
            class _msvc_WriteLock(_msvc_FileLock):
187
222
                def __init__(self, filename):
188
 
                    _msvc_lock(self._open(filename, 'wb'), self.LOCK_EX)
189
 
 
 
223
                    _msvc_lock(self._open(filename, 'rb+'),
 
224
                               self.LOCK_EX | self.LOCK_NB)
190
225
 
191
226
 
192
227
            def _msvc_lock(f, flags):
244
279
                    raise LockError(e)
245
280
 
246
281
 
247
 
 
248
282
            WriteLock = _msvc_WriteLock
249
283
            ReadLock = _msvc_ReadLock
250
284
        except ImportError: