[libvirt PATCH 10/10] qemumonitortestutils: Use automatic mutex management

Tim Wiederhake twiederh at redhat.com
Fri Feb 11 10:30:46 UTC 2022


Signed-off-by: Tim Wiederhake <twiederh at redhat.com>
---
 tests/qemumonitortestutils.c | 65 ++++++++++++++++--------------------
 1 file changed, 28 insertions(+), 37 deletions(-)

diff --git a/tests/qemumonitortestutils.c b/tests/qemumonitortestutils.c
index ce8e6e1645..86300da68a 100644
--- a/tests/qemumonitortestutils.c
+++ b/tests/qemumonitortestutils.c
@@ -253,12 +253,11 @@ qemuMonitorTestIO(virNetSocket *sock,
 {
     qemuMonitorTest *test = opaque;
     bool err = false;
+    VIR_LOCK_GUARD lock = virLockGuardLock(&test->lock);
 
-    virMutexLock(&test->lock);
-    if (test->quit) {
-        virMutexUnlock(&test->lock);
+    if (test->quit)
         return;
-    }
+
     if (events & VIR_EVENT_HANDLE_WRITABLE) {
         ssize_t ret;
         if ((ret = virNetSocketWrite(sock,
@@ -336,7 +335,6 @@ qemuMonitorTestIO(virNetSocket *sock,
 
         virNetSocketUpdateIOCallback(sock, events);
     }
-    virMutexUnlock(&test->lock);
 }
 
 
@@ -345,24 +343,22 @@ qemuMonitorTestWorker(void *opaque)
 {
     qemuMonitorTest *test = opaque;
 
-    virMutexLock(&test->lock);
-
-    while (!test->quit) {
-        virMutexUnlock(&test->lock);
+    while (true) {
+        VIR_WITH_MUTEX_LOCK_GUARD(&test->lock) {
+            if (test->quit) {
+                test->running = false;
+                return;
+            }
+        }
 
         if (virEventRunDefaultImpl() < 0) {
-            virMutexLock(&test->lock);
-            test->quit = true;
-            break;
+            VIR_WITH_MUTEX_LOCK_GUARD(&test->lock) {
+                test->quit = true;
+                test->running = false;
+                return;
+            }
         }
-
-        virMutexLock(&test->lock);
     }
-
-    test->running = false;
-
-    virMutexUnlock(&test->lock);
-    return;
 }
 
 
@@ -383,13 +379,13 @@ qemuMonitorTestFree(qemuMonitorTest *test)
     if (!test)
         return;
 
-    virMutexLock(&test->lock);
-    if (test->running) {
-        test->quit = true;
-        /* HACK: Add a dummy timeout to break event loop */
-        timer = virEventAddTimeout(0, qemuMonitorTestFreeTimer, NULL, NULL);
+    VIR_WITH_MUTEX_LOCK_GUARD(&test->lock) {
+        if (test->running) {
+            test->quit = true;
+            /* HACK: Add a dummy timeout to break event loop */
+            timer = virEventAddTimeout(0, qemuMonitorTestFreeTimer, NULL, NULL);
+        }
     }
-    virMutexUnlock(&test->lock);
 
     if (test->client) {
         virNetSocketRemoveIOCallback(test->client);
@@ -463,9 +459,9 @@ qemuMonitorTestAddHandler(qemuMonitorTest *test,
     item->freecb = freecb;
     item->opaque = opaque;
 
-    virMutexLock(&test->lock);
-    VIR_APPEND_ELEMENT(test->items, test->nitems, item);
-    virMutexUnlock(&test->lock);
+    VIR_WITH_MUTEX_LOCK_GUARD(&test->lock) {
+        VIR_APPEND_ELEMENT(test->items, test->nitems, item);
+    }
 
     return 0;
 }
@@ -1072,16 +1068,11 @@ qemuMonitorCommonTestInit(qemuMonitorTest *test)
                                   NULL) < 0)
         return -1;
 
-    virMutexLock(&test->lock);
-    if (virThreadCreate(&test->thread,
-                        true,
-                        qemuMonitorTestWorker,
-                        test) < 0) {
-        virMutexUnlock(&test->lock);
-        return -1;
+    VIR_WITH_MUTEX_LOCK_GUARD(&test->lock) {
+        if (virThreadCreate(&test->thread, true, qemuMonitorTestWorker, test) < 0)
+            return -1;
+        test->started = test->running = true;
     }
-    test->started = test->running = true;
-    virMutexUnlock(&test->lock);
 
     return 0;
 }
-- 
2.31.1




More information about the libvir-list mailing list