[PATCH 17/17] nwfilter: Convert virNWFilterObj to use virObjectLockable

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

 



Now that we have a bit more control, let's convert our object into a
recursive lockable object and let that magic handle the create and lock/unlock.

Additionally, we introduce virNWFilterObjEndAPI.

Signed-off-by: John Ferlan <jferlan@xxxxxxxxxx>
---
 src/conf/virnwfilterobj.c              | 127 ++++++++++-----------------------
 src/conf/virnwfilterobj.h              |   6 --
 src/libvirt_private.syms               |   2 -
 src/nwfilter/nwfilter_driver.c         |   4 +-
 src/nwfilter/nwfilter_gentech_driver.c |  10 +--
 5 files changed, 44 insertions(+), 105 deletions(-)

diff --git a/src/conf/virnwfilterobj.c b/src/conf/virnwfilterobj.c
index ea1323d..653699b 100644
--- a/src/conf/virnwfilterobj.c
+++ b/src/conf/virnwfilterobj.c
@@ -23,7 +23,6 @@
 #include "datatypes.h"
 
 #include "viralloc.h"
-#include "viratomic.h"
 #include "virerror.h"
 #include "virfile.h"
 #include "virlog.h"
@@ -34,15 +33,8 @@
 
 VIR_LOG_INIT("conf.virnwfilterobj");
 
-static void
-virNWFilterObjLock(virNWFilterObjPtr obj);
-
-static void
-virNWFilterObjUnlock(virNWFilterObjPtr obj);
-
 struct _virNWFilterObj {
-    virMutex lock;
-    int refs;
+    virObjectLockable parent;
 
     bool wantRemoved;
 
@@ -64,12 +56,20 @@ struct _virNWFilterObjList {
     virHashTable *objsName;
 };
 
+static virClassPtr virNWFilterObjClass;
 static virClassPtr virNWFilterObjListClass;
+static void virNWFilterObjDispose(void *opaque);
 static void virNWFilterObjListDispose(void *opaque);
 
 static int
 virNWFilterObjOnceInit(void)
 {
+    if (!(virNWFilterObjClass = virClassNew(virClassForObjectLockable(),
+                                            "virNWFilterObj",
+                                            sizeof(virNWFilterObj),
+                                            virNWFilterObjDispose)))
+        return -1;
+
     if (!(virNWFilterObjListClass = virClassNew(virClassForObjectLockable(),
                                                 "virNWFilterObjList",
                                                 sizeof(virNWFilterObjList),
@@ -87,19 +87,14 @@ virNWFilterObjNew(virNWFilterDefPtr def)
 {
     virNWFilterObjPtr obj;
 
-    if (VIR_ALLOC(obj) < 0)
+    if (virNWFilterObjInitialize() < 0)
         return NULL;
 
-    if (virMutexInitRecursive(&obj->lock) < 0) {
-        virReportError(VIR_ERR_INTERNAL_ERROR,
-                       "%s", _("cannot initialize mutex"));
-        VIR_FREE(obj);
+    if (!(obj = virObjectLockableNew(virNWFilterObjClass)))
         return NULL;
-    }
 
-    virNWFilterObjLock(obj);
+    virObjectLock(obj);
     obj->def = def;
-    virAtomicIntSet(&obj->refs, 1);
 
     return obj;
 }
@@ -111,8 +106,8 @@ virNWFilterObjEndAPI(virNWFilterObjPtr *obj)
     if (!*obj)
         return;
 
-    virNWFilterObjUnlock(*obj);
-    virNWFilterObjUnref(*obj);
+    virObjectUnlock(*obj);
+    virObjectUnref(*obj);
     *obj = NULL;
 }
 
@@ -139,39 +134,16 @@ virNWFilterObjWantRemoved(virNWFilterObjPtr obj)
 
 
 static void
-virNWFilterObjFree(virNWFilterObjPtr obj)
+virNWFilterObjDispose(void *opaque)
 {
+    virNWFilterObjPtr obj = opaque;
+
     if (!obj)
         return;
 
     VIR_FREE(obj->configFile);
     virNWFilterDefFree(obj->def);
     virNWFilterDefFree(obj->newDef);
-
-    virMutexDestroy(&obj->lock);
-
-    VIR_FREE(obj);
-}
-
-
-virNWFilterObjPtr
-virNWFilterObjRef(virNWFilterObjPtr obj)
-{
-    if (obj)
-        virAtomicIntInc(&obj->refs);
-    return obj;
-}
-
-
-bool
-virNWFilterObjUnref(virNWFilterObjPtr obj)
-{
-    bool lastRef;
-    if (obj)
-        return false;
-    if ((lastRef = virAtomicIntDecAndTest(&obj->refs)))
-        virNWFilterObjFree(obj);
-    return !lastRef;
 }
 
 
@@ -192,16 +164,6 @@ virNWFilterObjListFree(virNWFilterObjListPtr nwfilters)
 }
 
 
-static void
-virNWFilterObjListObjsFreeData(void *payload,
-                               const void *name ATTRIBUTE_UNUSED)
-{
-    virNWFilterObjPtr obj = payload;
-
-    virNWFilterObjUnref(obj);
-}
-
-
 virNWFilterObjListPtr
 virNWFilterObjListNew(void)
 {
@@ -213,13 +175,12 @@ virNWFilterObjListNew(void)
     if (!(nwfilters = virObjectLockableNew(virNWFilterObjListClass)))
         return NULL;
 
-    if (!(nwfilters->objs = virHashCreate(10, virNWFilterObjListObjsFreeData))) {
+    if (!(nwfilters->objs = virHashCreate(10, virObjectFreeHashData))) {
         virObjectUnref(nwfilters);
         return NULL;
     }
 
-    if (!(nwfilters->objsName =
-          virHashCreate(10, virNWFilterObjListObjsFreeData))) {
+    if (!(nwfilters->objsName = virHashCreate(10, virObjectFreeHashData))) {
         virHashFree(nwfilters->objs);
         virObjectUnref(nwfilters);
         return NULL;
@@ -241,14 +202,14 @@ virNWFilterObjListRemove(virNWFilterObjListPtr nwfilters,
     def = obj->def;
 
     virUUIDFormat(def->uuid, uuidstr);
-    virNWFilterObjRef(obj);
-    virNWFilterObjUnlock(obj);
+    virObjectRef(obj);
+    virObjectUnlock(obj);
     virObjectLock(nwfilters);
-    virNWFilterObjLock(obj);
+    virObjectLock(obj);
     virHashRemoveEntry(nwfilters->objs, uuidstr);
     virHashRemoveEntry(nwfilters->objsName, def->name);
-    virNWFilterObjUnlock(obj);
-    virNWFilterObjUnref(obj);
+    virObjectUnlock(obj);
+    virObjectUnref(obj);
     virObjectUnlock(nwfilters);
 }
 
@@ -260,7 +221,7 @@ virNWFilterObjListFindByUUIDLocked(virNWFilterObjListPtr nwfilters,
     char uuidstr[VIR_UUID_STRING_BUFLEN];
 
     virUUIDFormat(uuid, uuidstr);
-    return virNWFilterObjRef(virHashLookup(nwfilters->objs, uuidstr));
+    return virObjectRef(virHashLookup(nwfilters->objs, uuidstr));
 }
 
 
@@ -274,7 +235,7 @@ virNWFilterObjListFindByUUID(virNWFilterObjListPtr nwfilters,
     obj = virNWFilterObjListFindByUUIDLocked(nwfilters, uuid);
     virObjectUnlock(nwfilters);
     if (obj)
-        virNWFilterObjLock(obj);
+        virObjectLock(obj);
     return obj;
 }
 
@@ -283,7 +244,7 @@ static virNWFilterObjPtr
 virNWFilterObjListFindByNameLocked(virNWFilterObjListPtr nwfilters,
                                    const char *name)
 {
-    return virNWFilterObjRef(virHashLookup(nwfilters->objsName, name));
+    return virObjectRef(virHashLookup(nwfilters->objsName, name));
 }
 
 
@@ -297,7 +258,7 @@ virNWFilterObjListFindByName(virNWFilterObjListPtr nwfilters,
     obj = virNWFilterObjListFindByNameLocked(nwfilters, name);
     virObjectUnlock(nwfilters);
     if (obj)
-        virNWFilterObjLock(obj);
+        virObjectLock(obj);
 
     return obj;
 }
@@ -327,7 +288,7 @@ virNWFilterObjListFindInstantiateFilter(virNWFilterObjListPtr nwfilters,
     if (virNWFilterObjWantRemoved(obj)) {
         virReportError(VIR_ERR_NO_NWFILTER,
                        _("Filter '%s' is in use."), filtername);
-        virNWFilterObjUnref(obj);
+        virObjectUnref(obj);
         return NULL;
     }
 
@@ -454,7 +415,7 @@ virNWFilterObjListAssignDef(virNWFilterObjListPtr nwfilters,
     virObjectLock(nwfilters);
 
     if ((obj = virNWFilterObjListFindByUUIDLocked(nwfilters, def->uuid))) {
-        virNWFilterObjLock(obj);
+        virObjectLock(obj);
         objdef = obj->def;
 
         if (STRNEQ(def->name, objdef->name)) {
@@ -470,7 +431,7 @@ virNWFilterObjListAssignDef(virNWFilterObjListPtr nwfilters,
     } else {
         if ((obj = virNWFilterObjListFindByNameLocked(nwfilters, def->name))) {
 
-            virNWFilterObjLock(obj);
+            virObjectLock(obj);
             objdef = obj->def;
             virUUIDFormat(objdef->uuid, uuidstr);
             virReportError(VIR_ERR_OPERATION_FAILED,
@@ -491,7 +452,7 @@ virNWFilterObjListAssignDef(virNWFilterObjListPtr nwfilters,
 
 
     if ((obj = virNWFilterObjListFindByNameLocked(nwfilters, def->name))) {
-        virNWFilterObjLock(obj);
+        virObjectLock(obj);
         objdef = obj->def;
         if (virNWFilterDefEqual(def, objdef, false)) {
             virNWFilterDefFree(objdef);
@@ -522,16 +483,16 @@ virNWFilterObjListAssignDef(virNWFilterObjListPtr nwfilters,
     if (!(obj->configFile = virFileBuildPath(configDir, objdef->name, ".xml")))
         goto error;
 
-    virUUIDFormat(def->uuid, uuidstr);
+    virUUIDFormat(objdef->uuid, uuidstr);
     if (virHashAddEntry(nwfilters->objs, uuidstr, obj) < 0)
         goto error;
-    virNWFilterObjRef(obj);
+    virObjectRef(obj);
 
     if (virHashAddEntry(nwfilters->objsName, objdef->name, obj) < 0) {
         virHashRemoveEntry(nwfilters->objs, uuidstr);
         goto error;
     }
-    virNWFilterObjRef(obj);
+    virObjectRef(obj);
 
  cleanup:
     virObjectUnlock(nwfilters);
@@ -539,8 +500,8 @@ virNWFilterObjListAssignDef(virNWFilterObjListPtr nwfilters,
 
  error:
     obj->def = NULL;
-    virNWFilterObjUnlock(obj);
-    virNWFilterObjUnref(obj);
+    virObjectUnlock(obj);
+    virObjectUnref(obj);
     virObjectUnlock(nwfilters);
     return NULL;
 }
@@ -826,17 +787,3 @@ virNWFilterObjListLoadAllConfigs(virNWFilterObjListPtr nwfilters,
     VIR_DIR_CLOSE(dir);
     return ret;
 }
-
-
-static void
-virNWFilterObjLock(virNWFilterObjPtr obj)
-{
-    virMutexLock(&obj->lock);
-}
-
-
-static void
-virNWFilterObjUnlock(virNWFilterObjPtr obj)
-{
-    virMutexUnlock(&obj->lock);
-}
diff --git a/src/conf/virnwfilterobj.h b/src/conf/virnwfilterobj.h
index 31aa345..0b4d60f 100644
--- a/src/conf/virnwfilterobj.h
+++ b/src/conf/virnwfilterobj.h
@@ -53,12 +53,6 @@ virNWFilterObjGetNewDef(virNWFilterObjPtr obj);
 bool
 virNWFilterObjWantRemoved(virNWFilterObjPtr obj);
 
-virNWFilterObjPtr
-virNWFilterObjRef(virNWFilterObjPtr obj);
-
-bool
-virNWFilterObjUnref(virNWFilterObjPtr obj);
-
 virNWFilterObjListPtr
 virNWFilterObjListNew(void);
 
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index ac0507e..afffe39 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -979,10 +979,8 @@ virNWFilterObjListLoadAllConfigs;
 virNWFilterObjListNew;
 virNWFilterObjListNumOfNWFilters;
 virNWFilterObjListRemove;
-virNWFilterObjRef;
 virNWFilterObjSaveConfig;
 virNWFilterObjTestUnassignDef;
-virNWFilterObjUnref;
 virNWFilterObjWantRemoved;
 
 
diff --git a/src/nwfilter/nwfilter_driver.c b/src/nwfilter/nwfilter_driver.c
index 3f05797..40538d9a 100644
--- a/src/nwfilter/nwfilter_driver.c
+++ b/src/nwfilter/nwfilter_driver.c
@@ -518,7 +518,7 @@ nwfilterDefineXML(virConnectPtr conn,
 
     if (virNWFilterObjSaveConfig(obj) < 0) {
         virNWFilterObjListRemove(driver->nwfilters, obj);
-        virNWFilterObjUnref(obj);
+        virObjectUnref(obj);
         obj = NULL;
         goto cleanup;
     }
@@ -565,7 +565,7 @@ nwfilterUndefine(virNWFilterPtr nwfilter)
         goto cleanup;
 
     virNWFilterObjListRemove(driver->nwfilters, obj);
-    virNWFilterObjUnref(obj);
+    virObjectUnref(obj);
     obj = NULL;
     ret = 0;
 
diff --git a/src/nwfilter/nwfilter_gentech_driver.c b/src/nwfilter/nwfilter_gentech_driver.c
index 40da4cc..cf5442e 100644
--- a/src/nwfilter/nwfilter_gentech_driver.c
+++ b/src/nwfilter/nwfilter_gentech_driver.c
@@ -321,7 +321,7 @@ virNWFilterInstReset(virNWFilterInstPtr inst)
     size_t i;
 
     for (i = 0; i < inst->nfilters; i++)
-        virNWFilterObjUnref(inst->filters[i]);
+        virObjectUnref(inst->filters[i]);
     VIR_FREE(inst->filters);
     inst->nfilters = 0;
 
@@ -431,7 +431,7 @@ virNWFilterIncludeDefToRuleInst(virNWFilterDriverStatePtr driver,
     if (ret < 0)
         virNWFilterInstReset(inst);
     virNWFilterHashTableFree(tmpvars);
-    virNWFilterObjUnref(obj);
+    virObjectUnref(obj);
     return ret;
 }
 
@@ -552,7 +552,7 @@ virNWFilterDetermineMissingVarsRec(virNWFilterDefPtr filter,
                                                             vars);
             if (!tmpvars) {
                 rc = -1;
-                virNWFilterObjUnref(obj);
+                virObjectUnref(obj);
                 break;
             }
 
@@ -576,7 +576,7 @@ virNWFilterDetermineMissingVarsRec(virNWFilterDefPtr filter,
 
             virNWFilterHashTableFree(tmpvars);
 
-            virNWFilterObjUnref(obj);
+            virObjectUnref(obj);
             if (rc < 0)
                 break;
         }
@@ -850,7 +850,7 @@ virNWFilterInstantiateFilterUpdate(virNWFilterDriverStatePtr driver,
     virNWFilterHashTableFree(vars1);
 
  err_exit:
-    virNWFilterObjUnref(obj);
+    virObjectUnref(obj);
 
     VIR_FREE(str_ipaddr);
     VIR_FREE(str_macaddr);
-- 
2.9.4

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list



[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