Rev 2560: Return token directly from LockDir.acquire to avoid unnecessary peek() in http://sourcefrog.net/bzr/dlock
Martin Pool
mbp at sourcefrog.net
Wed Jun 27 10:17:24 BST 2007
At http://sourcefrog.net/bzr/dlock
------------------------------------------------------------
revno: 2560
revision-id: mbp at sourcefrog.net-20070627091722-ouf4om2ib0e4s7ft
parent: mbp at sourcefrog.net-20070627081353-f3402usvh4zlcs7q
committer: Martin Pool <mbp at sourcefrog.net>
branch nick: dlock
timestamp: Wed 2007-06-27 19:17:22 +1000
message:
Return token directly from LockDir.acquire to avoid unnecessary peek()
modified:
bzrlib/lockdir.py lockdir.py-20060220222025-98258adf27fbdda3
bzrlib/tests/test_lockdir.py test_lockdir.py-20060220222025-33d4221569a3d600
=== modified file 'bzrlib/lockdir.py'
--- a/bzrlib/lockdir.py 2007-06-27 08:13:53 +0000
+++ b/bzrlib/lockdir.py 2007-06-27 09:17:22 +0000
@@ -192,6 +192,8 @@
If you wish to block until the lock can be obtained, call wait_lock()
instead.
+
+ :return: The lock token.
"""
if self._fake_read_lock:
raise LockContention(self)
@@ -229,6 +231,7 @@
# safe on all platforms.
# FIXME: we should remove the pending lock if we fail,
# https://bugs.launchpad.net/bzr/+bug/109169
+ return self.nonce
except errors.PermissionDenied:
self._trace("... lock failed, permission denied")
raise
@@ -392,6 +395,8 @@
is raised. Either way, this function should return within
approximately `timeout` seconds. (It may be a bit more if
a transport operation takes a long time to complete.)
+
+ :return: The lock token.
"""
if timeout is None:
timeout = _DEFAULT_TIMEOUT_SECONDS
@@ -405,8 +410,7 @@
last_info = None
while True:
try:
- self.attempt_lock()
- return
+ return self.attempt_lock()
except LockContention:
pass
new_info = self.peek()
@@ -432,8 +436,10 @@
deadline_str)
if time.time() + poll < deadline:
+ self._trace("waiting %ss", poll)
time.sleep(poll)
else:
+ self._trace("timeout after waiting %ss", timeout)
raise LockContention(self)
def leave_in_place(self):
@@ -466,8 +472,7 @@
self._locked_via_token = True
return token
else:
- self.wait_lock()
- return self.peek().get('nonce')
+ return self.wait_lock()
def lock_read(self):
"""Compatibility-mode shared lock.
@@ -488,15 +493,19 @@
"""Wait a certain period for a lock to be released."""
# XXX: the transport interface doesn't let us guard
# against operations there taking a long time.
+ #
+ # XXX: Is this really needed? Do people want to wait for the lock but
+ # not acquire it? As of bzr 0.17, this seems to only be called from
+ # the test suite.
deadline = time.time() + timeout
while True:
if self.peek():
return
if time.time() + poll < deadline:
- self._trace("Waiting %ss", poll)
+ self._trace("waiting %ss", poll)
time.sleep(poll)
else:
- self._trace("Timeout after waiting %ss", timeout)
+ self._trace("temeout after waiting %ss", timeout)
raise LockContention(self)
def _format_lock_info(self, info):
=== modified file 'bzrlib/tests/test_lockdir.py'
--- a/bzrlib/tests/test_lockdir.py 2007-06-12 16:24:23 +0000
+++ b/bzrlib/tests/test_lockdir.py 2007-06-27 09:17:22 +0000
@@ -625,3 +625,11 @@
os.mkdir(lock_path)
osutils.make_readonly(lock_path)
self.assertRaises(errors.PermissionDenied, ld1.attempt_lock)
+
+ def test_lock_by_token(self):
+ ld1 = self.get_lock()
+ token = ld1.lock_write()
+ self.assertNotEqual(None, token)
+ ld2 = self.get_lock()
+ t2 = ld2.lock_write(token)
+ self.assertEqual(token, t2)
More information about the bazaar-commits
mailing list