diff --git a/lib/locking.py b/lib/locking.py index 3e25917c40f6e7388017e5a848f8f72933edb8ac..19c41eba66598d19d0e95af1ac7b27bf8ce8afe4 100644 --- a/lib/locking.py +++ b/lib/locking.py @@ -692,6 +692,14 @@ class SharedLock(object): finally: self.__lock.release() + def _release_save(self): + shared = self.__is_sharer() + self.release() + return shared + + def _acquire_restore(self, shared): + self.acquire(shared=shared) + # Whenever we want to acquire a full LockSet we pass None as the value # to acquire. Hide this behind this nicely named constant. diff --git a/test/ganeti.locking_unittest.py b/test/ganeti.locking_unittest.py index 170e8197b39b70fcca9de631dbca8b8c76b3b187..f767ccec9e7f4d1707c189ecfff1c1ecf5489b9b 100755 --- a/test/ganeti.locking_unittest.py +++ b/test/ganeti.locking_unittest.py @@ -698,6 +698,27 @@ class TestSharedLock(_ThreadedTestCase): self.assertRaises(Queue.Empty, self.done.get_nowait) +class TestSharedLockInCondition(_ThreadedTestCase): + """SharedLock as a condition lock tests""" + + def setUp(self): + _ThreadedTestCase.setUp(self) + self.sl = locking.SharedLock() + self.cond = threading.Condition(self.sl) + + def testKeepMode(self): + self.cond.acquire(shared=1) + self.assert_(self.sl._is_owned(shared=1)) + self.cond.wait(0) + self.assert_(self.sl._is_owned(shared=1)) + self.cond.release() + self.cond.acquire(shared=0) + self.assert_(self.sl._is_owned(shared=0)) + self.cond.wait(0) + self.assert_(self.sl._is_owned(shared=0)) + self.cond.release() + + class TestSSynchronizedDecorator(_ThreadedTestCase): """Shared Lock Synchronized decorator test"""