[libvirt PATCH 08/10] tools: Use automatic mutex management

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


Signed-off-by: Tim Wiederhake <twiederh at redhat.com>
---
 src/util/virfirewall.c | 13 ++++---------
 tools/virsh.c          | 12 ++++++------
 tools/virt-admin.c     | 12 ++++++------
 tools/vsh.c            |  8 ++++----
 4 files changed, 20 insertions(+), 25 deletions(-)

diff --git a/src/util/virfirewall.c b/src/util/virfirewall.c
index 70092f2ef6..31a8352d4e 100644
--- a/src/util/virfirewall.c
+++ b/src/util/virfirewall.c
@@ -613,9 +613,7 @@ int
 virFirewallApply(virFirewall *firewall)
 {
     size_t i, j;
-    int ret = -1;
-
-    virMutexLock(&ruleLock);
+    VIR_LOCK_GUARD lock = virLockGuardLock(&ruleLock);
 
     if (!firewall || firewall->err) {
         int err = EINVAL;
@@ -624,7 +622,7 @@ virFirewallApply(virFirewall *firewall)
             err = firewall->err;
 
         virReportSystemError(err, "%s", _("Unable to create rule"));
-        goto cleanup;
+        return -1;
     }
 
     VIR_DEBUG("Applying groups for %p", firewall);
@@ -657,13 +655,10 @@ virFirewallApply(virFirewall *firewall)
 
             virErrorRestore(&saved_error);
             VIR_DEBUG("Done rolling back groups for %p", firewall);
-            goto cleanup;
+            return -1;
         }
     }
     VIR_DEBUG("Done applying groups for %p", firewall);
 
-    ret = 0;
- cleanup:
-    virMutexUnlock(&ruleLock);
-    return ret;
+    return 0;
 }
diff --git a/tools/virsh.c b/tools/virsh.c
index 1c75a66fcb..64e0700fcd 100644
--- a/tools/virsh.c
+++ b/tools/virsh.c
@@ -412,13 +412,13 @@ virshDeinit(vshControl *ctl)
     virResetLastError();
 
     if (ctl->eventLoopStarted) {
-        int timer;
+        int timer = -1;
 
-        virMutexLock(&ctl->lock);
-        ctl->quit = true;
-        /* HACK: Add a dummy timeout to break event loop */
-        timer = virEventAddTimeout(0, virshDeinitTimer, NULL, NULL);
-        virMutexUnlock(&ctl->lock);
+        VIR_WITH_MUTEX_LOCK_GUARD(&ctl->lock) {
+            ctl->quit = true;
+            /* HACK: Add a dummy timeout to break event loop */
+            timer = virEventAddTimeout(0, virshDeinitTimer, NULL, NULL);
+        }
 
         virThreadJoin(&ctl->eventLoop);
 
diff --git a/tools/virt-admin.c b/tools/virt-admin.c
index c0818e850a..e010763e21 100644
--- a/tools/virt-admin.c
+++ b/tools/virt-admin.c
@@ -1189,13 +1189,13 @@ vshAdmDeinit(vshControl *ctl)
     virResetLastError();
 
     if (ctl->eventLoopStarted) {
-        int timer;
+        int timer = -1;
 
-        virMutexLock(&ctl->lock);
-        ctl->quit = true;
-        /* HACK: Add a dummy timeout to break event loop */
-        timer = virEventAddTimeout(0, vshAdmDeinitTimer, NULL, NULL);
-        virMutexUnlock(&ctl->lock);
+        VIR_WITH_MUTEX_LOCK_GUARD(&ctl->lock) {
+            ctl->quit = true;
+            /* HACK: Add a dummy timeout to break event loop */
+            timer = virEventAddTimeout(0, vshAdmDeinitTimer, NULL, NULL);
+        }
 
         virThreadJoin(&ctl->eventLoop);
 
diff --git a/tools/vsh.c b/tools/vsh.c
index 5056d7e19d..4ec5e54b5d 100644
--- a/tools/vsh.c
+++ b/tools/vsh.c
@@ -2018,10 +2018,10 @@ vshEventLoop(void *opaque)
     vshControl *ctl = opaque;
 
     while (1) {
-        bool quit;
-        virMutexLock(&ctl->lock);
-        quit = ctl->quit;
-        virMutexUnlock(&ctl->lock);
+        bool quit = false;
+        VIR_WITH_MUTEX_LOCK_GUARD(&ctl->lock) {
+            quit = ctl->quit;
+        }
 
         if (quit)
             break;
-- 
2.31.1




More information about the libvir-list mailing list