~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/lockable_files.py

  • Committer: Martin von Gagern
  • Date: 2011-06-01 12:53:56 UTC
  • mto: This revision was merged to the branch mainline in revision 6009.
  • Revision ID: martin.vgagern@gmx.net-20110601125356-lwozv2vecea6hxfz
Change from no_decorate to classify as name for the argument.

The command line switch remains as --no-classify, to keep backwards
compatibility.  Users are free to include --no-classify in an alias, and
still use --classify to change back.

Show diffs side-by-side

added added

removed removed

Lines of Context:
33
33
    )
34
34
 
35
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
 
36
57
class LockableFiles(object):
37
58
    """Object representing a set of related files locked within the same scope.
38
59
 
47
68
    This class is now deprecated; code should move to using the Transport
48
69
    directly for file operations and using the lock or CountedLock for
49
70
    locking.
50
 
 
 
71
    
51
72
    :ivar _lock: The real underlying lock (e.g. a LockDir)
52
 
    :ivar _lock_count: If _lock_mode is true, a positive count of the number
53
 
        of times the lock has been taken (and not yet released) *by this
54
 
        process*, through this particular object instance.
55
 
    :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.
56
75
    """
57
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
 
58
82
    def __init__(self, transport, lock_name, lock_class):
59
83
        """Create a LockableFiles group
60
84
 
68
92
        self.lock_name = lock_name
69
93
        self._transaction = None
70
94
        self._lock_mode = None
71
 
        self._lock_count = 0
 
95
        self._lock_warner = _LockWarner(repr(self))
72
96
        self._find_modes()
73
97
        esc_name = self._escape(lock_name)
74
98
        self._lock = lock_class(transport, esc_name,
87
111
    def __repr__(self):
88
112
        return '%s(%r)' % (self.__class__.__name__,
89
113
                           self._transport)
90
 
 
91
114
    def __str__(self):
92
115
        return 'LockableFiles(%s, %s)' % (self.lock_name, self._transport.base)
93
116
 
151
174
        some other way, and need to synchronise this object's state with that
152
175
        fact.
153
176
        """
 
177
        # TODO: Upgrade locking to support using a Transport,
 
178
        # and potentially a remote locking protocol
154
179
        if self._lock_mode:
155
 
            if (self._lock_mode != 'w'
156
 
                or not self.get_transaction().writeable()):
 
180
            if self._lock_mode != 'w' or not self.get_transaction().writeable():
157
181
                raise errors.ReadOnlyError(self)
158
182
            self._lock.validate_token(token)
159
 
            self._lock_count += 1
 
183
            self._lock_warner.lock_count += 1
160
184
            return self._token_from_lock
161
185
        else:
162
186
            token_from_lock = self._lock.lock_write(token=token)
163
187
            #traceback.print_stack()
164
188
            self._lock_mode = 'w'
165
 
            self._lock_count = 1
 
189
            self._lock_warner.lock_count = 1
166
190
            self._set_write_transaction()
167
191
            self._token_from_lock = token_from_lock
168
192
            return token_from_lock
171
195
        if self._lock_mode:
172
196
            if self._lock_mode not in ('r', 'w'):
173
197
                raise ValueError("invalid lock mode %r" % (self._lock_mode,))
174
 
            self._lock_count += 1
 
198
            self._lock_warner.lock_count += 1
175
199
        else:
176
200
            self._lock.lock_read()
177
201
            #traceback.print_stack()
178
202
            self._lock_mode = 'r'
179
 
            self._lock_count = 1
 
203
            self._lock_warner.lock_count = 1
180
204
            self._set_read_transaction()
181
205
 
182
206
    def _set_read_transaction(self):
193
217
    def unlock(self):
194
218
        if not self._lock_mode:
195
219
            return lock.cant_unlock_not_held(self)
196
 
        if self._lock_count > 1:
197
 
            self._lock_count -= 1
 
220
        if self._lock_warner.lock_count > 1:
 
221
            self._lock_warner.lock_count -= 1
198
222
        else:
199
223
            #traceback.print_stack()
200
224
            self._finish_transaction()
201
225
            try:
202
226
                self._lock.unlock()
203
227
            finally:
204
 
                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
205
233
 
206
234
    def is_locked(self):
207
235
        """Return true if this LockableFiles group is locked"""
208
 
        return self._lock_count >= 1
 
236
        return self._lock_warner.lock_count >= 1
209
237
 
210
238
    def get_physical_lock_status(self):
211
239
        """Return physical lock status.
297
325
    def validate_token(self, token):
298
326
        if token is not None:
299
327
            raise errors.TokenLockingNotSupported(self)
 
328