~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/lockable_files.py

  • Committer: Wouter van Heyst
  • Date: 2011-05-18 10:03:10 UTC
  • mto: This revision was merged to the branch mainline in revision 5893.
  • Revision ID: larstiq@larstiq.dyndns.org-20110518100310-7i5uj6nfcdcw34tz
Fix remaining failing blackbox tests under pypy due to refcount/flush interaction.

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
 
from __future__ import absolute_import
18
 
 
19
17
from bzrlib.lazy_import import lazy_import
20
18
lazy_import(globals(), """
21
19
import warnings
35
33
    )
36
34
 
37
35
 
 
36
# XXX: The tracking here of lock counts and whether the lock is held is
 
37
# somewhat redundant with what's done in LockDir; the main difference is that
 
38
# LockableFiles permits reentrancy.
 
39
 
 
40
class _LockWarner(object):
 
41
    """Hold a counter for a lock and warn if GCed while the count is >= 1.
 
42
 
 
43
    This is separate from LockableFiles because putting a __del__ on
 
44
    LockableFiles can result in uncollectable cycles.
 
45
    """
 
46
 
 
47
    def __init__(self, repr):
 
48
        self.lock_count = 0
 
49
        self.repr = repr
 
50
 
 
51
    def __del__(self):
 
52
        if self.lock_count >= 1:
 
53
            # There should have been a try/finally to unlock this.
 
54
            warnings.warn("%r was gc'd while locked" % self.repr)
 
55
 
 
56
 
38
57
class LockableFiles(object):
39
58
    """Object representing a set of related files locked within the same scope.
40
59
 
49
68
    This class is now deprecated; code should move to using the Transport
50
69
    directly for file operations and using the lock or CountedLock for
51
70
    locking.
52
 
 
 
71
    
53
72
    :ivar _lock: The real underlying lock (e.g. a LockDir)
54
 
    :ivar _lock_count: If _lock_mode is true, a positive count of the number
55
 
        of times the lock has been taken (and not yet released) *by this
56
 
        process*, through this particular object instance.
57
 
    :ivar _lock_mode: None, or 'r' or 'w'
 
73
    :ivar _counted_lock: A lock decorated with a semaphore, so that it 
 
74
        can be re-entered.
58
75
    """
59
76
 
 
77
    # _lock_mode: None, or 'r' or 'w'
 
78
 
 
79
    # _lock_count: If _lock_mode is true, a positive count of the number of
 
80
    # times the lock has been taken *by this process*.
 
81
 
60
82
    def __init__(self, transport, lock_name, lock_class):
61
83
        """Create a LockableFiles group
62
84
 
70
92
        self.lock_name = lock_name
71
93
        self._transaction = None
72
94
        self._lock_mode = None
73
 
        self._lock_count = 0
 
95
        self._lock_warner = _LockWarner(repr(self))
74
96
        self._find_modes()
75
97
        esc_name = self._escape(lock_name)
76
98
        self._lock = lock_class(transport, esc_name,
89
111
    def __repr__(self):
90
112
        return '%s(%r)' % (self.__class__.__name__,
91
113
                           self._transport)
92
 
 
93
114
    def __str__(self):
94
115
        return 'LockableFiles(%s, %s)' % (self.lock_name, self._transport.base)
95
116
 
153
174
        some other way, and need to synchronise this object's state with that
154
175
        fact.
155
176
        """
 
177
        # TODO: Upgrade locking to support using a Transport,
 
178
        # and potentially a remote locking protocol
156
179
        if self._lock_mode:
157
 
            if (self._lock_mode != 'w'
158
 
                or not self.get_transaction().writeable()):
 
180
            if self._lock_mode != 'w' or not self.get_transaction().writeable():
159
181
                raise errors.ReadOnlyError(self)
160
182
            self._lock.validate_token(token)
161
 
            self._lock_count += 1
 
183
            self._lock_warner.lock_count += 1
162
184
            return self._token_from_lock
163
185
        else:
164
186
            token_from_lock = self._lock.lock_write(token=token)
165
187
            #traceback.print_stack()
166
188
            self._lock_mode = 'w'
167
 
            self._lock_count = 1
 
189
            self._lock_warner.lock_count = 1
168
190
            self._set_write_transaction()
169
191
            self._token_from_lock = token_from_lock
170
192
            return token_from_lock
173
195
        if self._lock_mode:
174
196
            if self._lock_mode not in ('r', 'w'):
175
197
                raise ValueError("invalid lock mode %r" % (self._lock_mode,))
176
 
            self._lock_count += 1
 
198
            self._lock_warner.lock_count += 1
177
199
        else:
178
200
            self._lock.lock_read()
179
201
            #traceback.print_stack()
180
202
            self._lock_mode = 'r'
181
 
            self._lock_count = 1
 
203
            self._lock_warner.lock_count = 1
182
204
            self._set_read_transaction()
183
205
 
184
206
    def _set_read_transaction(self):
195
217
    def unlock(self):
196
218
        if not self._lock_mode:
197
219
            return lock.cant_unlock_not_held(self)
198
 
        if self._lock_count > 1:
199
 
            self._lock_count -= 1
 
220
        if self._lock_warner.lock_count > 1:
 
221
            self._lock_warner.lock_count -= 1
200
222
        else:
201
223
            #traceback.print_stack()
202
224
            self._finish_transaction()
203
225
            try:
204
226
                self._lock.unlock()
205
227
            finally:
206
 
                self._lock_mode = self._lock_count = None
 
228
                self._lock_mode = self._lock_warner.lock_count = None
 
229
 
 
230
    @property
 
231
    def _lock_count(self):
 
232
        return self._lock_warner.lock_count
207
233
 
208
234
    def is_locked(self):
209
235
        """Return true if this LockableFiles group is locked"""
210
 
        return self._lock_count >= 1
 
236
        return self._lock_warner.lock_count >= 1
211
237
 
212
238
    def get_physical_lock_status(self):
213
239
        """Return physical lock status.
299
325
    def validate_token(self, token):
300
326
        if token is not None:
301
327
            raise errors.TokenLockingNotSupported(self)
 
328