~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/lock.py

Merge from 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.errors import LockError
 
42
from bzrlib.osutils import realpath
41
43
from bzrlib.trace import mutter
42
 
from bzrlib.errors import LockError
 
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
90
            del self.f 
87
91
 
 
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
99
            self._open(filename, 'wb')
92
100
            try:
 
101
                self.filename = realpath(filename)
 
102
                if self.filename in self.open_locks:
 
103
                    self._clear_f() 
 
104
                    raise LockError("Lock already held.")
 
105
                # reserve a slot for this lock - even if the lockf call fails, 
 
106
                # at thisi point unlock() will be called, because self.f is set.
 
107
                # TODO: make this fully threadsafe, if we decide we care.
 
108
                self.open_locks[self.filename] = self.filename
93
109
                fcntl.lockf(self.f, fcntl.LOCK_EX)
94
110
            except IOError, e:
95
111
                # we should be more precise about whats a locking
96
112
                # error and whats a random-other error
97
113
                raise LockError(e)
98
114
 
 
115
        def unlock(self):
 
116
            del self.open_locks[self.filename]
 
117
            self._unlock()
 
118
 
 
119
 
99
120
    class _fcntl_ReadLock(_fcntl_FileLock):
100
121
 
101
122
        def __init__(self, filename):
108
129
                # error and whats a random-other error
109
130
                raise LockError(e)
110
131
 
 
132
        def unlock(self):
 
133
            self._unlock()
 
134
 
 
135
 
111
136
    WriteLock = _fcntl_WriteLock
112
137
    ReadLock = _fcntl_ReadLock
113
138
 
144
169
                    raise LockError(e)
145
170
 
146
171
 
147
 
 
148
172
        class _w32c_ReadLock(_w32c_FileLock):
149
173
            def __init__(self, filename):
150
174
                _w32c_FileLock._lock(self, filename, 'rb',
156
180
                                     LOCK_EX + LOCK_NB)
157
181
 
158
182
 
159
 
 
160
183
        WriteLock = _w32c_WriteLock
161
184
        ReadLock = _w32c_ReadLock
162
185
 
191
214
                    _msvc_lock(self._open(filename, 'wb'), self.LOCK_EX)
192
215
 
193
216
 
194
 
 
195
217
            def _msvc_lock(f, flags):
196
218
                try:
197
219
                    # Unfortunately, msvcrt.LK_RLCK is equivalent to msvcrt.LK_LOCK
247
269
                    raise LockError(e)
248
270
 
249
271
 
250
 
 
251
272
            WriteLock = _msvc_WriteLock
252
273
            ReadLock = _msvc_ReadLock
253
274
        except ImportError: