[libvirt] [PATCH v4 06/17] util: Introduce virObjectLookupHashFind[Locked]

John Ferlan jferlan at redhat.com
Fri Aug 18 21:50:30 UTC 2017


These API's will use the virHashLookup in order to find the object in
the hash table 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 at redhat.com>
---
 src/libvirt_private.syms |  2 ++
 src/util/virobject.c     | 69 ++++++++++++++++++++++++++++++++++++++++++++++++
 src/util/virobject.h     | 10 +++++++
 3 files changed, 81 insertions(+)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index c6d22d7..b6ab173 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -2332,6 +2332,8 @@ virObjectListFreeCount;
 virObjectLock;
 virObjectLockableNew;
 virObjectLookupHashAdd;
+virObjectLookupHashFind;
+virObjectLookupHashFindLocked;
 virObjectLookupHashNew;
 virObjectLookupHashRemove;
 virObjectLookupKeysIsActive;
diff --git a/src/util/virobject.c b/src/util/virobject.c
index 59c8ac6..7dbfd1b 100644
--- a/src/util/virobject.c
+++ b/src/util/virobject.c
@@ -950,3 +950,72 @@ virObjectLookupHashRemove(void *anyobj,
     virObjectUnref(obj);
     virObjectRWUnlock(hashObj);
 }
+
+
+static virObjectLookupKeysPtr
+virObjectLookupHashFindInternal(virObjectLookupHashPtr hashObj,
+                                const char *key)
+{
+    virObjectLookupKeysPtr obj;
+
+    if ((obj = virHashLookup(hashObj->objsKey1, key)))
+        return virObjectRef(obj);
+
+    if (hashObj->objsKey2)
+        obj = virHashLookup(hashObj->objsKey2, key);
+
+    return virObjectRef(obj);
+}
+
+
+/**
+ * virObjectLookupHashFindLocked:
+ * @anyobj: LookupHash object
+ * @key: Key to use for lookup
+ *
+ * Search through the hash tables looking for the key. The key may be
+ * either key1 or key2 - 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
+ */
+virObjectLookupKeysPtr
+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
+ */
+virObjectLookupKeysPtr
+virObjectLookupHashFind(void *anyobj,
+                        const char *key)
+{
+    virObjectLookupHashPtr hashObj = virObjectGetLookupHashObj(anyobj);
+    virObjectLookupKeysPtr 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 bb02781..dc668ca 100644
--- a/src/util/virobject.h
+++ b/src/util/virobject.h
@@ -209,4 +209,14 @@ virObjectLookupHashRemove(void *anyobj,
                           virObjectLookupKeysPtr obj)
     ATTRIBUTE_NONNULL(1);
 
+virObjectLookupKeysPtr
+virObjectLookupHashFindLocked(void *anyobj,
+                              const char *key)
+    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
+
+virObjectLookupKeysPtr
+virObjectLookupHashFind(void *anyobj,
+                        const char *key)
+    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
+
 #endif /* __VIR_OBJECT_H */
-- 
2.9.4




More information about the libvir-list mailing list