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

[libvirt] [PATCH v4 08/17] util: Introduce virObjectLookupHashSearch[Locked]



A common object API wrapper to use the virHashSearch API to
search the LookupHash for specific data defined in the @opaque
parameter. Once data is found, the search would end and the
LookupKeys object that is represented is returned locked with
it's reference count incremented.

The virObjectLookupHashSearchLocked is the workhorse, but similar
to the virObjectLookupFind* APIs may be required an Add or AssignDef
caller has the Write lock already to ensure no other thread will
grab the lock and add the same or competing object.

It is up to the caller to unlock and lower the refcnt once done
with the object and of course handle a NULL return indicating
no object found.

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

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 3978106..df3f246 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -2337,6 +2337,8 @@ virObjectLookupHashFindLocked;
 virObjectLookupHashForEach;
 virObjectLookupHashNew;
 virObjectLookupHashRemove;
+virObjectLookupHashSearch;
+virObjectLookupHashSearchLocked;
 virObjectLookupKeysIsActive;
 virObjectLookupKeysNew;
 virObjectLookupKeysSetActive;
diff --git a/src/util/virobject.c b/src/util/virobject.c
index a76f1cb..17ea4e6 100644
--- a/src/util/virobject.c
+++ b/src/util/virobject.c
@@ -1093,3 +1093,77 @@ virObjectLookupHashForEach(void *anyobj,
     }
     return -1;
 }
+
+
+static virObjectLookupKeysPtr
+virObjectLookupHashSearchInternal(virObjectLookupHashPtr hashObj,
+                                  virHashSearcher callback,
+                                  void *opaque)
+{
+    virObjectLookupKeysPtr obj;
+
+    obj = virHashSearch(hashObj->objsKey1, callback, opaque, NULL);
+    virObjectRef(obj);
+
+    if (obj)
+        virObjectLock(obj);
+
+    return obj;
+}
+
+
+/**
+ * virObjectLookupHashSearchLocked
+ * @anyobj: LookupHash object
+ * @callback: callback function to handle the object specific checks
+ * @opaque: callback data
+ *
+ * Search the hash table objsKey1 table calling the specified @callback
+ * routine with an object and @opaque data in order to determine whether
+ * the object is represented by the @opaque data.
+ *
+ * NB: Caller assumes the responsibility for locking LookupHash
+ *
+ * Returns locked/refcnt incremented object on success, NULL on failure
+ */
+virObjectLookupKeysPtr
+virObjectLookupHashSearchLocked(void *anyobj,
+                                virHashSearcher callback,
+                                void *opaque)
+{
+    virObjectLookupHashPtr hashObj = virObjectGetLookupHashObj(anyobj);
+
+    if (!hashObj)
+        return NULL;
+
+    return virObjectLookupHashSearchInternal(hashObj, callback, opaque);
+}
+
+
+/**
+ * virObjectLookupHashSearch
+ * @anyobj: LookupHash object
+ * @callback: callback function to handle the object specific checks
+ * @opaque: callback data
+ *
+ * Call virObjectLookupHashSearchLocked with a locked hash table
+ *
+ * Returns @obj from virObjectLookupHashSearchLocked
+ */
+virObjectLookupKeysPtr
+virObjectLookupHashSearch(void *anyobj,
+                          virHashSearcher callback,
+                          void *opaque)
+{
+    virObjectLookupHashPtr hashObj = virObjectGetLookupHashObj(anyobj);
+    virObjectLookupKeysPtr obj;
+
+    if (!hashObj)
+        return NULL;
+
+    virObjectRWLockRead(hashObj);
+    obj = virObjectLookupHashSearchInternal(hashObj, callback, opaque);
+    virObjectRWUnlock(hashObj);
+
+    return obj;
+}
diff --git a/src/util/virobject.h b/src/util/virobject.h
index ddebf6c..d4bc3c3 100644
--- a/src/util/virobject.h
+++ b/src/util/virobject.h
@@ -240,4 +240,16 @@ virObjectLookupHashForEach(void *anyobj,
                            virObjectLookupHashForEachDataPtr data)
     ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3);
 
+virObjectLookupKeysPtr
+virObjectLookupHashSearchLocked(void *anyobj,
+                                virHashSearcher callback,
+                                void *opaque)
+    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
+
+virObjectLookupKeysPtr
+virObjectLookupHashSearch(void *anyobj,
+                          virHashSearcher callback,
+                          void *opaque)
+    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
+
 #endif /* __VIR_OBJECT_H */
-- 
2.9.4


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