Change in vdsm[master]: rwlock: Support non-blocking acquire

nsoffer at redhat.com nsoffer at redhat.com
Sun Jun 28 00:15:00 UTC 2015


Nir Soffer has uploaded a new change for review.

Change subject: rwlock: Support non-blocking acquire
......................................................................

rwlock: Support non-blocking acquire

This patch adds non-blocking acquire suggested in
https://gerrit.ovirt.org/42773. This is a simpler alternative to timed
acquire, suggested in https://gerrit.ovirt.org/42909.

Change-Id: Iec721a07087349050bfe9aa11aacf3be9695fb85
Signed-off-by: Nir Soffer <nsoffer at redhat.com>
---
M lib/vdsm/rwlock.py
M tests/rwlock_test.py
2 files changed, 30 insertions(+), 48 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/28/42928/1

diff --git a/lib/vdsm/rwlock.py b/lib/vdsm/rwlock.py
index c6ce1ea..2281851 100644
--- a/lib/vdsm/rwlock.py
+++ b/lib/vdsm/rwlock.py
@@ -32,17 +32,23 @@
         self._readers = set()
         self._writer = None
 
-    def acquireWrite(self):
+    def acquireWrite(self, block=True):
         with self._lock:
             if self._writer or self._readers or self._waiters:
+                if not block:
+                    return False
                 self._wait(True)
             self._writer = threading.current_thread()
+            return True
 
-    def acquireRead(self):
+    def acquireRead(self, block=True):
         with self._lock:
             if self._writer or self._waiters:
+                if not block:
+                    return False
                 self._wait(False)
             self._readers.add(threading.current_thread())
+            return True
 
     def release(self):
         me = threading.current_thread()
diff --git a/tests/rwlock_test.py b/tests/rwlock_test.py
index 1c28f26..7ef5aa6 100644
--- a/tests/rwlock_test.py
+++ b/tests/rwlock_test.py
@@ -121,54 +121,30 @@
             for t in threads:
                 t.stop()
 
-    @slowtest
-    def test_shared_context_blocks_writer(self):
-        lock = RWLock()
-        writer = RWThread(lock.exclusive)
-        try:
-            with lock.shared:
-                writer.start()
-                if not writer.ready.wait(2):
-                    raise RuntimeError("Timeout waiting for writer thread")
-                # Writer must block
-                self.assertFalse(writer.acquired.wait(1))
-        finally:
-            writer.stop()
-
-    def test_shared_context_allows_reader(self):
-        lock = RWLock()
-        with lock.shared:
-            reader = RWThread(lock.shared)
-            with utils.running(reader):
-                self.assertTrue(reader.acquired.wait(1))
-
-    @slowtest
-    def test_exclusive_context_blocks_writer(self):
-        lock = RWLock()
-        writer = RWThread(lock.exclusive)
-        try:
-            with lock.exclusive:
-                writer.start()
-                if not writer.ready.wait(2):
-                    raise RuntimeError("Timeout waiting for writer thread")
-                # Reader must block
-                self.assertFalse(writer.acquired.wait(1))
-        finally:
-            writer.stop()
-
-    @slowtest
-    def test_exclusive_context_blocks_reader(self):
+    def test_reader_blocks_writer(self):
         lock = RWLock()
         reader = RWThread(lock.shared)
-        try:
-            with lock.exclusive:
-                reader.start()
-                if not reader.ready.wait(2):
-                    raise RuntimeError("Timeout waiting for reader thread")
-                # Reader must block
-                self.assertFalse(reader.acquired.wait(1))
-        finally:
-            reader.stop()
+        with utils.running(reader):
+            if not reader.acquired.wait(2):
+                raise RuntimeError("Timeout waiting for reader thread")
+            self.assertFalse(lock.acquireWrite(block=False))
+
+    def test_writer_blocks_writer(self):
+        lock = RWLock()
+        writer = RWThread(lock.exclusive)
+        with utils.running(writer):
+            if not writer.acquired.wait(2):
+                raise RuntimeError("Timeout waiting for writer thread")
+            self.assertFalse(lock.acquireWrite(block=False))
+
+
+    def test_writer_blocks_reader(self):
+        lock = RWLock()
+        writer = RWThread(lock.exclusive)
+        with utils.running(writer):
+            if not writer.acquired.wait(2):
+                raise RuntimeError("Timeout waiting for writer thread")
+            self.assertFalse(lock.acquireRead(block=False))
 
 
 @expandPermutations


-- 
To view, visit https://gerrit.ovirt.org/42928
To unsubscribe, visit https://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Iec721a07087349050bfe9aa11aacf3be9695fb85
Gerrit-PatchSet: 1
Gerrit-Project: vdsm
Gerrit-Branch: master
Gerrit-Owner: Nir Soffer <nsoffer at redhat.com>


More information about the vdsm-patches mailing list