[libvirt PATCH 05/11] lxc: Use automatic mutex management

Tim Wiederhake twiederh at redhat.com
Mon Feb 7 13:12:08 UTC 2022


Signed-off-by: Tim Wiederhake <twiederh at redhat.com>
---
 src/lxc/lxc_conf.c       | 31 ++++++++++++++-----------------
 src/lxc/lxc_conf.h       |  9 ---------
 src/lxc/lxc_controller.c | 25 ++++++++++---------------
 src/lxc/lxc_fuse.c       | 12 ++++++------
 4 files changed, 30 insertions(+), 47 deletions(-)

diff --git a/src/lxc/lxc_conf.c b/src/lxc/lxc_conf.c
index cf6679711a..3ab4d61fe3 100644
--- a/src/lxc/lxc_conf.c
+++ b/src/lxc/lxc_conf.c
@@ -159,24 +159,24 @@ virCaps *virLXCDriverGetCapabilities(virLXCDriver *driver,
     virCaps *ret = NULL;
     virCaps *caps = NULL;
 
-    lxcDriverLock(driver);
-    if (!refresh && !driver->caps) {
-        VIR_DEBUG("Capabilities didn't detect any guests. Forcing a refresh.");
-        refresh = true;
+    VIR_WITH_MUTEX_LOCK_GUARD(&driver->lock) {
+        if (!refresh && !driver->caps) {
+            VIR_DEBUG("Capabilities didn't detect any guests. Forcing a refresh.");
+            refresh = true;
+        }
     }
-    lxcDriverUnlock(driver);
 
     if (refresh && !(caps = virLXCDriverCapsInit(driver)))
         return NULL;
 
-    lxcDriverLock(driver);
-    if (refresh) {
-        virObjectUnref(driver->caps);
-        driver->caps = caps;
-    }
+    VIR_WITH_MUTEX_LOCK_GUARD(&driver->lock) {
+        if (refresh) {
+            virObjectUnref(driver->caps);
+            driver->caps = caps;
+        }
 
-    ret = virObjectRef(driver->caps);
-    lxcDriverUnlock(driver);
+        ret = virObjectRef(driver->caps);
+    }
 
     return ret;
 }
@@ -248,11 +248,8 @@ virLXCLoadDriverConfig(virLXCDriverConfig *cfg,
 
 virLXCDriverConfig *virLXCDriverGetConfig(virLXCDriver *driver)
 {
-    virLXCDriverConfig *cfg;
-    lxcDriverLock(driver);
-    cfg = virObjectRef(driver->config);
-    lxcDriverUnlock(driver);
-    return cfg;
+    VIR_LOCK_GUARD lock = virLockGuardLock(&driver->lock);
+    return virObjectRef(driver->config);
 }
 
 static void
diff --git a/src/lxc/lxc_conf.h b/src/lxc/lxc_conf.h
index 5a1351bd63..04e51aa954 100644
--- a/src/lxc/lxc_conf.h
+++ b/src/lxc/lxc_conf.h
@@ -113,12 +113,3 @@ virCaps *virLXCDriverGetCapabilities(virLXCDriver *driver,
                                        bool refresh);
 virDomainXMLOption *lxcDomainXMLConfInit(virLXCDriver *driver,
                                            const char *defsecmodel);
-
-static inline void lxcDriverLock(virLXCDriver *driver)
-{
-    virMutexLock(&driver->lock);
-}
-static inline void lxcDriverUnlock(virLXCDriver *driver)
-{
-    virMutexUnlock(&driver->lock);
-}
diff --git a/src/lxc/lxc_controller.c b/src/lxc/lxc_controller.c
index c4e3b66751..b5289b2448 100644
--- a/src/lxc/lxc_controller.c
+++ b/src/lxc/lxc_controller.c
@@ -995,7 +995,7 @@ static int lxcControllerClearCapabilities(void)
 }
 
 static bool wantReboot;
-static virMutex lock = VIR_MUTEX_INITIALIZER;
+static virMutex mutex = VIR_MUTEX_INITIALIZER;
 
 static int
 virLXCControllerEventSendExit(virLXCController *ctrl,
@@ -1012,13 +1012,13 @@ static void virLXCControllerSignalChildIO(virNetDaemon *dmn G_GNUC_UNUSED,
     ret = waitpid(-1, &status, WNOHANG);
     VIR_DEBUG("Got sig child %d vs %lld", ret, (long long)ctrl->initpid);
     if (ret == ctrl->initpid) {
-        virMutexLock(&lock);
-        if (WIFSIGNALED(status) &&
-            WTERMSIG(status) == SIGHUP) {
-            VIR_DEBUG("Status indicates reboot");
-            wantReboot = true;
+        VIR_WITH_MUTEX_LOCK_GUARD(&mutex) {
+            if (WIFSIGNALED(status) &&
+                WTERMSIG(status) == SIGHUP) {
+                VIR_DEBUG("Status indicates reboot");
+                wantReboot = true;
+            }
         }
-        virMutexUnlock(&lock);
         virLXCControllerEventSendExit(ctrl, wantReboot ? 1 : 0);
     }
 }
@@ -1132,8 +1132,8 @@ static void virLXCControllerConsoleUpdateWatch(virLXCControllerConsole *console)
 static void virLXCControllerConsoleEPoll(int watch, int fd, int events, void *opaque)
 {
     virLXCControllerConsole *console = opaque;
+    VIR_LOCK_GUARD lock = virLockGuardLock(&mutex);
 
-    virMutexLock(&lock);
     VIR_DEBUG("IO event watch=%d fd=%d events=%d fromHost=%zu fromcont=%zu",
               watch, fd, events,
               console->fromHostLen,
@@ -1149,7 +1149,7 @@ static void virLXCControllerConsoleEPoll(int watch, int fd, int events, void *op
             virReportSystemError(errno, "%s",
                                  _("Unable to wait on epoll"));
             virNetDaemonQuit(console->daemon);
-            goto cleanup;
+            return;
         }
 
         if (ret == 0)
@@ -1171,16 +1171,13 @@ static void virLXCControllerConsoleEPoll(int watch, int fd, int events, void *op
             break;
         }
     }
-
- cleanup:
-    virMutexUnlock(&lock);
 }
 
 static void virLXCControllerConsoleIO(int watch, int fd, int events, void *opaque)
 {
     virLXCControllerConsole *console = opaque;
+    VIR_LOCK_GUARD lock = virLockGuardLock(&mutex);
 
-    virMutexLock(&lock);
     VIR_DEBUG("IO event watch=%d fd=%d events=%d fromHost=%zu fromcont=%zu",
               watch, fd, events,
               console->fromHostLen,
@@ -1254,7 +1251,6 @@ static void virLXCControllerConsoleIO(int watch, int fd, int events, void *opaqu
     }
 
     virLXCControllerConsoleUpdateWatch(console);
-    virMutexUnlock(&lock);
     return;
 
  error:
@@ -1262,7 +1258,6 @@ static void virLXCControllerConsoleIO(int watch, int fd, int events, void *opaqu
     virEventRemoveHandle(console->hostWatch);
     console->contWatch = console->hostWatch = -1;
     virNetDaemonQuit(console->daemon);
-    virMutexUnlock(&lock);
 }
 
 
diff --git a/src/lxc/lxc_fuse.c b/src/lxc/lxc_fuse.c
index c2fe8f0e60..2e600e4402 100644
--- a/src/lxc/lxc_fuse.c
+++ b/src/lxc/lxc_fuse.c
@@ -260,11 +260,11 @@ static struct fuse_operations lxcProcOper = {
 
 static void lxcFuseDestroy(struct virLXCFuse *fuse)
 {
-    virMutexLock(&fuse->lock);
+    VIR_LOCK_GUARD lock = virLockGuardLock(&fuse->lock);
+
     fuse_unmount(fuse->mountpoint, fuse->ch);
     fuse_destroy(fuse->fuse);
     fuse->fuse = NULL;
-    virMutexUnlock(&fuse->lock);
 }
 
 static void lxcFuseRun(void *opaque)
@@ -346,10 +346,10 @@ void lxcFreeFuse(struct virLXCFuse **f)
     if (fuse) {
         /* exit fuse_loop, lxcFuseRun thread may try to destroy
          * fuse->fuse at the same time,so add a lock here. */
-        virMutexLock(&fuse->lock);
-        if (fuse->fuse)
-            fuse_exit(fuse->fuse);
-        virMutexUnlock(&fuse->lock);
+        VIR_WITH_MUTEX_LOCK_GUARD(&fuse->lock) {
+            if (fuse->fuse)
+                fuse_exit(fuse->fuse);
+        }
 
         g_free(fuse->mountpoint);
         g_free(*f);
-- 
2.31.1




More information about the libvir-list mailing list