Rev 2512: Add CountedLock support for passing token to and from lock_write. in http://sourcefrog.net/bzr/counted-lock

Martin Pool mbp at sourcefrog.net
Thu Jun 7 00:38:42 BST 2007


At http://sourcefrog.net/bzr/counted-lock

------------------------------------------------------------
revno: 2512
revision-id: mbp at sourcefrog.net-20070606233823-m5t33u2altd078sx
parent: pqm at pqm.ubuntu.com-20070606050006-o4yiw7jnwytgf561
committer: Martin Pool <mbp at sourcefrog.net>
branch nick: counted-lock
timestamp: Thu 2007-06-07 09:38:23 +1000
message:
  Add CountedLock support for passing token to and from lock_write.
modified:
  bzrlib/counted_lock.py         counted_lock.py-20070502135927-7dk86io3ok7ctx6k-1
  bzrlib/tests/test_counted_lock.py test_counted_lock.py-20070502135927-7dk86io3ok7ctx6k-2
=== modified file 'bzrlib/counted_lock.py'
--- a/bzrlib/counted_lock.py	2007-05-02 14:00:53 +0000
+++ b/bzrlib/counted_lock.py	2007-06-06 23:38:23 +0000
@@ -23,11 +23,6 @@
     )
 
 
-# TODO: Pass through lock tokens on lock_write and read, and return them...
-#
-# TODO: Allow upgrading read locks to write?  Conceptually difficult.
-
-
 class CountedLock(object):
     """Decorator around a lock that makes it reentrant.
 
@@ -65,18 +60,22 @@
             self._lock_count = 1
             self._lock_mode = 'r'
 
-    def lock_write(self):
+    def lock_write(self, token=None):
         """Acquire the lock in write mode.
 
         If the lock was originally acquired in read mode this will fail.
         """
         if self._lock_count == 0:
             assert self._lock_mode is None
-            self._real_lock.lock_write()
+            if token is not None:
+                token = self._real_lock.lock_write(token=token)
+            else:
+                token = self._real_lock.lock_write()
             self._lock_mode = 'w'
         elif self._lock_mode != 'w':
             raise ReadOnlyError(self)
         self._lock_count += 1
+        return token
 
     def unlock(self):
         if self._lock_count == 0:

=== modified file 'bzrlib/tests/test_counted_lock.py'
--- a/bzrlib/tests/test_counted_lock.py	2007-05-02 14:00:53 +0000
+++ b/bzrlib/tests/test_counted_lock.py	2007-06-06 23:38:23 +0000
@@ -21,7 +21,13 @@
     LockError,
     ReadOnlyError,
     )
-from bzrlib.tests import TestCase
+from bzrlib.lockdir import (
+    LockDir,
+    )
+from bzrlib.tests import (
+    TestCase,
+    TestCaseInTempDir,
+    )
 
 
 class DummyLock(object):
@@ -43,6 +49,7 @@
         self._assert_not_locked()
         self._lock_mode = 'w'
         self._calls.append('lock_write')
+        return 'dummy-token'
 
     def unlock(self):
         self._assert_locked()
@@ -168,3 +175,23 @@
         l.break_lock()
         self.assertFalse(l.is_locked())
         self.assertFalse(real_lock.is_locked())
+
+
+class TestCountedLockTokens(TestCaseInTempDir):
+    # Tests for handling of lock tokens, which are passed through when the
+    # lock is taken or released.  For these we use a real LockDir, rather than
+    # duplicating the token functionality in DummyLock
+
+    def test_lock_token(self):
+        t = self.get_transport()
+        real_lock = LockDir(t, 'lock')
+        real_lock.create()
+        l = CountedLock(real_lock)
+        token = l.lock_write()
+        self.assertTrue(token is not None)
+        # normally you could not reenter a held lockdir, but it can be done by
+        # passing a correct token
+        l2 = CountedLock(LockDir(t, 'lock'))
+        l2.lock_write(token=token)
+        l2.unlock()
+        l.unlock()




More information about the bazaar-commits mailing list