~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/lock.py

  • Committer: Andrew Bennetts
  • Date: 2009-12-03 02:24:54 UTC
  • mfrom: (4634.101.4 2.0)
  • mto: This revision was merged to the branch mainline in revision 4857.
  • Revision ID: andrew.bennetts@canonical.com-20091203022454-m2gyhbcdqi1t7ujz
Merge lp:bzr/2.0 into lp:bzr.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2010 Canonical Ltd
 
1
# Copyright (C) 2005, 2006, 2007, 2008, 2009 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
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
 
17
18
"""Locking using OS file locks or file existence.
18
19
 
19
20
Note: This method of locking is generally deprecated in favour of LockDir, but
33
34
unlock() method.
34
35
"""
35
36
 
36
 
from __future__ import absolute_import
37
 
 
38
37
import errno
39
38
import os
40
39
import sys
46
45
    osutils,
47
46
    trace,
48
47
    )
49
 
from bzrlib.hooks import Hooks
50
 
from bzrlib.i18n import gettext
 
48
from bzrlib.hooks import HookPoint, Hooks
 
49
 
51
50
 
52
51
class LockHooks(Hooks):
53
52
 
54
53
    def __init__(self):
55
 
        Hooks.__init__(self, "bzrlib.lock", "Lock.hooks")
56
 
        self.add_hook('lock_acquired',
57
 
            "Called with a bzrlib.lock.LockResult when a physical lock is "
58
 
            "acquired.", (1, 8))
59
 
        self.add_hook('lock_released',
60
 
            "Called with a bzrlib.lock.LockResult when a physical lock is "
61
 
            "released.", (1, 8))
62
 
        self.add_hook('lock_broken',
63
 
            "Called with a bzrlib.lock.LockResult when a physical lock is "
64
 
            "broken.", (1, 15))
 
54
        Hooks.__init__(self)
 
55
        self.create_hook(HookPoint('lock_acquired',
 
56
            "Called with a bzrlib.lock.LockResult when a physical lock is "
 
57
            "acquired.", (1, 8), None))
 
58
        self.create_hook(HookPoint('lock_released',
 
59
            "Called with a bzrlib.lock.LockResult when a physical lock is "
 
60
            "released.", (1, 8), None))
 
61
        self.create_hook(HookPoint('lock_broken',
 
62
            "Called with a bzrlib.lock.LockResult when a physical lock is "
 
63
            "broken.", (1, 15), None))
65
64
 
66
65
 
67
66
class Lock(object):
85
84
        return self.lock_url == other.lock_url and self.details == other.details
86
85
 
87
86
    def __repr__(self):
88
 
        return '%s(%s, %s)' % (self.__class__.__name__,
 
87
        return '%s(%s%s)' % (self.__class__.__name__,
89
88
                             self.lock_url, self.details)
90
89
 
91
90
 
92
 
class LogicalLockResult(object):
93
 
    """The result of a lock_read/lock_write/lock_tree_write call on lockables.
94
 
 
95
 
    :ivar unlock: A callable which will unlock the lock.
96
 
    """
97
 
 
98
 
    def __init__(self, unlock):
99
 
        self.unlock = unlock
100
 
 
101
 
    def __repr__(self):
102
 
        return "LogicalLockResult(%s)" % (self.unlock)
103
 
 
104
 
 
105
 
 
106
91
def cant_unlock_not_held(locked_object):
107
92
    """An attempt to unlock failed because the object was not locked.
108
93
 
172
157
            self.f.close()
173
158
            self.f = None
174
159
 
 
160
    def __del__(self):
 
161
        if self.f:
 
162
            from warnings import warn
 
163
            warn("lock on %r not released" % self.f)
 
164
            self.unlock()
 
165
 
175
166
    def unlock(self):
176
167
        raise NotImplementedError()
177
168
 
209
200
 
210
201
            self._open(self.filename, 'rb+')
211
202
            # reserve a slot for this lock - even if the lockf call fails,
212
 
            # at this point unlock() will be called, because self.f is set.
 
203
            # at thisi point unlock() will be called, because self.f is set.
213
204
            # TODO: make this fully threadsafe, if we decide we care.
214
205
            _fcntl_WriteLock._open_locks.add(self.filename)
215
206
            try:
536
527
    locked the same way), and -Drelock is set, then this will trace.note a
537
528
    message about it.
538
529
    """
539
 
 
 
530
    
540
531
    _prev_lock = None
541
532
 
542
533
    def _note_lock(self, lock_type):
545
536
                type_name = 'read'
546
537
            else:
547
538
                type_name = 'write'
548
 
            trace.note(gettext('{0!r} was {1} locked again'), self, type_name)
 
539
            trace.note('%r was %s locked again', self, type_name)
549
540
        self._prev_lock = lock_type
550
541