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

[libvirt] [PATCH v5 04/15] util: Introduce virObjectLookupHashFind[Locked]



These API's will use the virHashLookup in order to find the object in
the lookup hash object by the specified @key. If two hash tables exist
in the hash table object, then both will be searched for the key. Both
API's will call an virObjectLookupHashFindInternal in order in handle
the fetch.

The virObjectLookupHashFindLocked is the primary workhorse and should
only be called externally if the caller has taken the proper RW LookupHash
lock. This is necessary in some paths, such as during Add/Assign processing
where getting the RW Write lock early is required to ensure no other thread
attempts to create/add the same object

Signed-off-by: John Ferlan <jferlan redhat com>
---
 src/libvirt_private.syms |  2 ++
 src/util/virobject.c     | 73 ++++++++++++++++++++++++++++++++++++++++++++++++
 src/util/virobject.h     | 10 +++++++
 3 files changed, 85 insertions(+)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index e98646c..8ad7223 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -2331,6 +2331,8 @@ virObjectListFreeCount;
 virObjectLock;
 virObjectLockableNew;
 virObjectLookupHashAdd;
+virObjectLookupHashFind;
+virObjectLookupHashFindLocked;
 virObjectLookupHashNew;
 virObjectLookupHashRemove;
 virObjectNew;
diff --git a/src/util/virobject.c b/src/util/virobject.c
index 76bf1bf..63205d3 100644
--- a/src/util/virobject.c
+++ b/src/util/virobject.c
@@ -857,3 +857,76 @@ virObjectLookupHashRemove(void *anyobj,
     virObjectUnref(obj);
     virObjectRWUnlock(hashObj);
 }
+
+
+static void *
+virObjectLookupHashFindInternal(virObjectLookupHashPtr hashObj,
+                                const char *key)
+{
+    virObjectLockablePtr obj;
+
+    if (hashObj->objsUUID) {
+        if ((obj = virHashLookup(hashObj->objsUUID, key)))
+            return virObjectRef(obj);
+    }
+
+    if (hashObj->objsName) {
+        obj = virHashLookup(hashObj->objsName, key);
+            return virObjectRef(obj);
+    }
+
+    return NULL;
+}
+
+
+/**
+ * virObjectLookupHashFindLocked:
+ * @anyobj: LookupHash object
+ * @key: Key to use for lookup
+ *
+ * Search through the hash tables looking for the @key. The @key may be
+ * either UUID or Name - both tables if they exist will be searched.
+ *
+ * NB: Assumes that the LookupHash has already been locked
+ *
+ * Returns a pointer to the entry with refcnt incremented or NULL on failure
+ */
+void *
+virObjectLookupHashFindLocked(void *anyobj,
+                              const char *key)
+{
+    virObjectLookupHashPtr hashObj = virObjectGetLookupHashObj(anyobj);
+
+    if (!hashObj)
+        return NULL;
+
+    return virObjectLookupHashFindInternal(anyobj, key);
+
+}
+
+
+/**
+ * virObjectLookupHashFind:
+ * @anyobj: LookupHash object
+ * @key: Key to use for lookup
+ *
+ * Call virObjectLookupHashFindLocked after locking the LookupHash
+ *
+ * Returns a pointer to the entry with refcnt incremented or NULL on failure
+ */
+void *
+virObjectLookupHashFind(void *anyobj,
+                        const char *key)
+{
+    virObjectLookupHashPtr hashObj = virObjectGetLookupHashObj(anyobj);
+    void *obj;
+
+    if (!hashObj)
+        return NULL;
+
+    virObjectRWLockRead(hashObj);
+    obj = virObjectLookupHashFindInternal(hashObj, key);
+    virObjectRWUnlock(hashObj);
+
+    return obj;
+}
diff --git a/src/util/virobject.h b/src/util/virobject.h
index d149f30..e5596e6 100644
--- a/src/util/virobject.h
+++ b/src/util/virobject.h
@@ -195,4 +195,14 @@ virObjectLookupHashRemove(void *anyobj,
                           const char *name)
     ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
 
+void *
+virObjectLookupHashFindLocked(void *anyobj,
+                              const char *key)
+    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
+
+void *
+virObjectLookupHashFind(void *anyobj,
+                        const char *key)
+    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
+
 #endif /* __VIR_OBJECT_H */
-- 
2.9.5


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