[libvirt PATCH v3 1/2] nwfilter_driver: Use automatic mutex management

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Signed-off-by: Tim Wiederhake <twiederh@xxxxxxxxxx>
---
 src/nwfilter/nwfilter_driver.c | 94 +++++++++++++++-------------------
 1 file changed, 40 insertions(+), 54 deletions(-)

diff --git a/src/nwfilter/nwfilter_driver.c b/src/nwfilter/nwfilter_driver.c
index c5f0169623..cac73c50e5 100644
--- a/src/nwfilter/nwfilter_driver.c
+++ b/src/nwfilter/nwfilter_driver.c
@@ -57,15 +57,6 @@ static int nwfilterStateReload(void);
 
 static virMutex driverMutex = VIR_MUTEX_INITIALIZER;
 
-static void nwfilterDriverLock(void)
-{
-    virMutexLock(&driverMutex);
-}
-static void nwfilterDriverUnlock(void)
-{
-    virMutexUnlock(&driverMutex);
-}
-
 #ifdef WITH_FIREWALLD
 
 static void
@@ -204,6 +195,7 @@ nwfilterStateInitialize(bool privileged,
                         virStateInhibitCallback callback G_GNUC_UNUSED,
                         void *opaque G_GNUC_UNUSED)
 {
+    VIR_LOCK_GUARD lock = virLockGuardLock(&driverMutex);
     GDBusConnection *sysbus = NULL;
 
     if (root != NULL) {
@@ -230,8 +222,6 @@ nwfilterStateInitialize(bool privileged,
     if (!privileged)
         return VIR_DRV_STATE_INIT_SKIPPED;
 
-    nwfilterDriverLock();
-
     driver->stateDir = g_strdup(RUNSTATEDIR "/libvirt/nwfilter");
 
     if (g_mkdir_with_parents(driver->stateDir, S_IRWXU) < 0) {
@@ -290,13 +280,10 @@ nwfilterStateInitialize(bool privileged,
     if (virNWFilterBuildAll(driver, false) < 0)
         goto error;
 
-    nwfilterDriverUnlock();
-
     return VIR_DRV_STATE_INIT_COMPLETE;
 
  error:
-    nwfilterDriverUnlock();
-    nwfilterStateCleanup();
+    nwfilterStateCleanupLocked();
 
     return VIR_DRV_STATE_INIT_ERROR;
 
@@ -335,16 +322,15 @@ nwfilterStateReload(void)
     /* shut down all threads -- they will be restarted if necessary */
     virNWFilterLearnThreadsTerminate(true);
 
-    nwfilterDriverLock();
-    virNWFilterWriteLockFilterUpdates();
-
-    virNWFilterObjListLoadAllConfigs(driver->nwfilters, driver->configDir);
+    VIR_WITH_MUTEX_LOCK_GUARD(&driverMutex) {
+        virNWFilterWriteLockFilterUpdates();
 
-    virNWFilterUnlockFilterUpdates();
+        virNWFilterObjListLoadAllConfigs(driver->nwfilters, driver->configDir);
 
-    virNWFilterBuildAll(driver, false);
+        virNWFilterUnlockFilterUpdates();
 
-    nwfilterDriverUnlock();
+        virNWFilterBuildAll(driver, false);
+    }
 
     return 0;
 }
@@ -422,13 +408,13 @@ static virNWFilterPtr
 nwfilterLookupByUUID(virConnectPtr conn,
                      const unsigned char *uuid)
 {
-    virNWFilterObj *obj;
+    virNWFilterObj *obj = NULL;
     virNWFilterDef *def;
     virNWFilterPtr nwfilter = NULL;
 
-    nwfilterDriverLock();
-    obj = nwfilterObjFromNWFilter(uuid);
-    nwfilterDriverUnlock();
+    VIR_WITH_MUTEX_LOCK_GUARD(&driverMutex) {
+        obj = nwfilterObjFromNWFilter(uuid);
+    }
 
     if (!obj)
         return NULL;
@@ -449,13 +435,13 @@ static virNWFilterPtr
 nwfilterLookupByName(virConnectPtr conn,
                      const char *name)
 {
-    virNWFilterObj *obj;
+    virNWFilterObj *obj = NULL;
     virNWFilterDef *def;
     virNWFilterPtr nwfilter = NULL;
 
-    nwfilterDriverLock();
-    obj = virNWFilterObjListFindByName(driver->nwfilters, name);
-    nwfilterDriverUnlock();
+    VIR_WITH_MUTEX_LOCK_GUARD(&driverMutex) {
+        obj = virNWFilterObjListFindByName(driver->nwfilters, name);
+    }
 
     if (!obj) {
         virReportError(VIR_ERR_NO_NWFILTER,
@@ -478,14 +464,15 @@ nwfilterLookupByName(virConnectPtr conn,
 static int
 nwfilterConnectNumOfNWFilters(virConnectPtr conn)
 {
-    int ret;
+    int ret = -1;
     if (virConnectNumOfNWFiltersEnsureACL(conn) < 0)
         return -1;
 
-    nwfilterDriverLock();
-    ret = virNWFilterObjListNumOfNWFilters(driver->nwfilters, conn,
-                                           virConnectNumOfNWFiltersCheckACL);
-    nwfilterDriverUnlock();
+    VIR_WITH_MUTEX_LOCK_GUARD(&driverMutex) {
+        ret = virNWFilterObjListNumOfNWFilters(driver->nwfilters, conn,
+                                               virConnectNumOfNWFiltersCheckACL);
+    }
+
     return ret;
 }
 
@@ -495,16 +482,17 @@ nwfilterConnectListNWFilters(virConnectPtr conn,
                              char **const names,
                              int maxnames)
 {
-    int nnames;
+    int nnames = -1;
 
     if (virConnectListNWFiltersEnsureACL(conn) < 0)
         return -1;
 
-    nwfilterDriverLock();
-    nnames = virNWFilterObjListGetNames(driver->nwfilters, conn,
-                                    virConnectListNWFiltersCheckACL,
-                                    names, maxnames);
-    nwfilterDriverUnlock();
+    VIR_WITH_MUTEX_LOCK_GUARD(&driverMutex) {
+        nnames = virNWFilterObjListGetNames(driver->nwfilters, conn,
+                                            virConnectListNWFiltersCheckACL,
+                                            names, maxnames);
+    }
+
     return nnames;
 }
 
@@ -514,17 +502,17 @@ nwfilterConnectListAllNWFilters(virConnectPtr conn,
                                 virNWFilterPtr **nwfilters,
                                 unsigned int flags)
 {
-    int ret;
+    int ret = -1;
 
     virCheckFlags(0, -1);
 
     if (virConnectListAllNWFiltersEnsureACL(conn) < 0)
         return -1;
 
-    nwfilterDriverLock();
-    ret = virNWFilterObjListExport(conn, driver->nwfilters, nwfilters,
-                                   virConnectListAllNWFiltersCheckACL);
-    nwfilterDriverUnlock();
+    VIR_WITH_MUTEX_LOCK_GUARD(&driverMutex) {
+        ret = virNWFilterObjListExport(conn, driver->nwfilters, nwfilters,
+                                       virConnectListAllNWFiltersCheckACL);
+    }
 
     return ret;
 }
@@ -535,6 +523,7 @@ nwfilterDefineXMLFlags(virConnectPtr conn,
                        const char *xml,
                        unsigned int flags)
 {
+    VIR_LOCK_GUARD lock = virLockGuardLock(&driverMutex);
     virNWFilterDef *def;
     virNWFilterObj *obj = NULL;
     virNWFilterDef *objdef;
@@ -549,7 +538,6 @@ nwfilterDefineXMLFlags(virConnectPtr conn,
         return NULL;
     }
 
-    nwfilterDriverLock();
     virNWFilterWriteLockFilterUpdates();
 
     if (!(def = virNWFilterDefParseString(xml, flags)))
@@ -576,7 +564,6 @@ nwfilterDefineXMLFlags(virConnectPtr conn,
         virNWFilterObjUnlock(obj);
 
     virNWFilterUnlockFilterUpdates();
-    nwfilterDriverUnlock();
     return nwfilter;
 }
 
@@ -592,11 +579,11 @@ nwfilterDefineXML(virConnectPtr conn,
 static int
 nwfilterUndefine(virNWFilterPtr nwfilter)
 {
+    VIR_LOCK_GUARD lock = virLockGuardLock(&driverMutex);
     virNWFilterObj *obj;
     virNWFilterDef *def;
     int ret = -1;
 
-    nwfilterDriverLock();
     virNWFilterWriteLockFilterUpdates();
 
     if (!(obj = nwfilterObjFromNWFilter(nwfilter->uuid)))
@@ -625,7 +612,6 @@ nwfilterUndefine(virNWFilterPtr nwfilter)
         virNWFilterObjUnlock(obj);
 
     virNWFilterUnlockFilterUpdates();
-    nwfilterDriverUnlock();
     return ret;
 }
 
@@ -634,15 +620,15 @@ static char *
 nwfilterGetXMLDesc(virNWFilterPtr nwfilter,
                    unsigned int flags)
 {
-    virNWFilterObj *obj;
+    virNWFilterObj *obj = NULL;
     virNWFilterDef *def;
     char *ret = NULL;
 
     virCheckFlags(0, NULL);
 
-    nwfilterDriverLock();
-    obj = nwfilterObjFromNWFilter(nwfilter->uuid);
-    nwfilterDriverUnlock();
+    VIR_WITH_MUTEX_LOCK_GUARD(&driverMutex) {
+        obj = nwfilterObjFromNWFilter(nwfilter->uuid);
+    }
 
     if (!obj)
         return NULL;
-- 
2.31.1




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]

  Powered by Linux