~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/lock.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2010-01-20 18:55:04 UTC
  • mfrom: (4971.2.2 505762)
  • Revision ID: pqm@pqm.ubuntu.com-20100120185504-es1x5ntwauunwxvp
(nmb) Explain bound branches in "branches" help topic

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
45
45
    osutils,
46
46
    trace,
47
47
    )
48
 
from bzrlib.hooks import Hooks
 
48
from bzrlib.hooks import HookPoint, Hooks
49
49
 
50
50
 
51
51
class LockHooks(Hooks):
52
52
 
53
53
    def __init__(self):
54
 
        Hooks.__init__(self, "bzrlib.lock", "Lock.hooks")
55
 
        self.add_hook('lock_acquired',
56
 
            "Called with a bzrlib.lock.LockResult when a physical lock is "
57
 
            "acquired.", (1, 8))
58
 
        self.add_hook('lock_released',
59
 
            "Called with a bzrlib.lock.LockResult when a physical lock is "
60
 
            "released.", (1, 8))
61
 
        self.add_hook('lock_broken',
62
 
            "Called with a bzrlib.lock.LockResult when a physical lock is "
63
 
            "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))
64
64
 
65
65
 
66
66
class Lock(object):
84
84
        return self.lock_url == other.lock_url and self.details == other.details
85
85
 
86
86
    def __repr__(self):
87
 
        return '%s(%s, %s)' % (self.__class__.__name__,
 
87
        return '%s(%s%s)' % (self.__class__.__name__,
88
88
                             self.lock_url, self.details)
89
89
 
90
90
 
91
 
class LogicalLockResult(object):
92
 
    """The result of a lock_read/lock_write/lock_tree_write call on lockables.
93
 
 
94
 
    :ivar unlock: A callable which will unlock the lock.
95
 
    """
96
 
 
97
 
    def __init__(self, unlock):
98
 
        self.unlock = unlock
99
 
 
100
 
    def __repr__(self):
101
 
        return "LogicalLockResult(%s)" % (self.unlock)
102
 
 
103
 
 
104
 
 
105
91
def cant_unlock_not_held(locked_object):
106
92
    """An attempt to unlock failed because the object was not locked.
107
93
 
171
157
            self.f.close()
172
158
            self.f = None
173
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
 
174
166
    def unlock(self):
175
167
        raise NotImplementedError()
176
168
 
208
200
 
209
201
            self._open(self.filename, 'rb+')
210
202
            # reserve a slot for this lock - even if the lockf call fails,
211
 
            # 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.
212
204
            # TODO: make this fully threadsafe, if we decide we care.
213
205
            _fcntl_WriteLock._open_locks.add(self.filename)
214
206
            try: