~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/branch.py

  • Committer: Martin Pool
  • Date: 2005-06-01 06:09:59 UTC
  • Revision ID: mbp@sourcefrog.net-20050601060959-e0be99c26487bd63
Major rework of locking code:

- New ReadLock and WriteLock objects, with unlock methods, and that
  give a warning if they leak without being unlocked

- The lock file is opened readonly for read locks, which should avoid
  problems when the user only has read permission for a branch.

- Selective definitions of locking code is perhaps simpler now

Show diffs side-by-side

added added

removed removed

Lines of Context:
128
128
    base = None
129
129
    _lock_mode = None
130
130
    _lock_count = None
 
131
    _lock = None
131
132
    
132
133
    def __init__(self, base, init=False, find_root=True):
133
134
        """Create new branch object at a particular location.
157
158
                                     ['use "bzr init" to initialize a new working tree',
158
159
                                      'current bzr can only operate from top-of-tree'])
159
160
        self._check_format()
160
 
        self._lockfile = self.controlfile('branch-lock', 'wb')
161
161
 
162
162
        self.text_store = ImmutableStore(self.controlfilename('text-store'))
163
163
        self.revision_store = ImmutableStore(self.controlfilename('revision-store'))
172
172
 
173
173
 
174
174
    def __del__(self):
175
 
        if self._lock_mode:
 
175
        if self._lock_mode or self._lock:
176
176
            from warnings import warn
177
177
            warn("branch %r was not explicitly unlocked" % self)
178
 
            self.unlock()
 
178
            self._lock.unlock()
179
179
 
180
180
 
181
181
 
187
187
                                self._lock_mode)
188
188
            self._lock_count += 1
189
189
        else:
190
 
            from bzrlib.lock import lock, LOCK_EX
 
190
            from bzrlib.lock import WriteLock
191
191
 
192
 
            lock(self._lockfile, LOCK_EX)
 
192
            self._lock = WriteLock(self.controlfilename('branch-lock'))
193
193
            self._lock_mode = 'w'
194
194
            self._lock_count = 1
195
195
 
201
201
                   "invalid lock mode %r" % self._lock_mode
202
202
            self._lock_count += 1
203
203
        else:
204
 
            from bzrlib.lock import lock, LOCK_SH
 
204
            from bzrlib.lock import ReadLock
205
205
 
206
 
            lock(self._lockfile, LOCK_SH)
 
206
            self._lock = ReadLock(self.controlfilename('branch-lock'))
207
207
            self._lock_mode = 'r'
208
208
            self._lock_count = 1
209
209
                        
217
217
        if self._lock_count > 1:
218
218
            self._lock_count -= 1
219
219
        else:
220
 
            assert self._lock_count == 1
221
 
            from bzrlib.lock import unlock
222
 
            unlock(self._lockfile)
 
220
            self._lock.unlock()
 
221
            self._lock = None
223
222
            self._lock_mode = self._lock_count = None
224
223
 
225
224