[PATCH v2 16/24] network_conf: Introduce locked versions of lookup functions

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

 



This is going to be needed later, when some functions needs to avoid
calling multiple times at once. It will work like this:

1) gain the object list mutex
2) find the object to work on
3) do the work
4) release the mutex

As an example of such function is virNetworkAssignDef(). The other
use case might be in virNetworkObjListForEach() callback.

Signed-off-by: Michal Privoznik <mprivozn@xxxxxxxxxx>
---
 src/conf/network_conf.c  | 45 ++++++++++++++++++++++++++++++++++-----------
 src/conf/network_conf.h  |  4 ++++
 src/libvirt_private.syms |  2 ++
 3 files changed, 40 insertions(+), 11 deletions(-)

diff --git a/src/conf/network_conf.c b/src/conf/network_conf.c
index a821f6c..8cf9ffd 100644
--- a/src/conf/network_conf.c
+++ b/src/conf/network_conf.c
@@ -153,34 +153,57 @@ virNetworkObjListPtr virNetworkObjListNew(void)
     return nets;
 }
 
+virNetworkObjPtr
+virNetworkObjFindByUUIDLocked(virNetworkObjListPtr nets,
+                              const unsigned char *uuid)
+{
+    size_t i;
+
+    for (i = 0; i < nets->count; i++) {
+        virObjectLock(nets->objs[i]);
+        if (!memcmp(nets->objs[i]->def->uuid, uuid, VIR_UUID_BUFLEN))
+            return nets->objs[i];
+        virObjectUnlock(nets->objs[i]);
+    }
+
+    return NULL;
+}
+
 virNetworkObjPtr virNetworkObjFindByUUID(virNetworkObjListPtr nets,
                                          const unsigned char *uuid)
 {
+    virNetworkObjPtr ret;
+
+    virObjectLock(nets);
+    ret = virNetworkObjFindByUUIDLocked(nets, uuid);
+    virObjectUnlock(nets);
+    return ret;
+}
+
+virNetworkObjPtr
+virNetworkObjFindByNameLocked(virNetworkObjListPtr nets,
+                              const char *name)
+{
     size_t i;
 
     for (i = 0; i < nets->count; i++) {
         virObjectLock(nets->objs[i]);
-        if (!memcmp(nets->objs[i]->def->uuid, uuid, VIR_UUID_BUFLEN))
+        if (STREQ(nets->objs[i]->def->name, name))
             return nets->objs[i];
         virObjectUnlock(nets->objs[i]);
     }
 
     return NULL;
 }
-
 virNetworkObjPtr virNetworkObjFindByName(virNetworkObjListPtr nets,
                                          const char *name)
 {
-    size_t i;
+    virNetworkObjPtr ret;
 
-    for (i = 0; i < nets->count; i++) {
-        virObjectLock(nets->objs[i]);
-        if (STREQ(nets->objs[i]->def->name, name))
-            return nets->objs[i];
-        virObjectUnlock(nets->objs[i]);
-    }
-
-    return NULL;
+    virObjectLock(nets);
+    ret = virNetworkObjFindByNameLocked(nets, name);
+    virObjectUnlock(nets);
+    return ret;
 }
 
 bool
diff --git a/src/conf/network_conf.h b/src/conf/network_conf.h
index e0ed714..3e926f7 100644
--- a/src/conf/network_conf.h
+++ b/src/conf/network_conf.h
@@ -300,8 +300,12 @@ virNetworkObjIsActive(const virNetworkObj *net)
 
 virNetworkObjListPtr virNetworkObjListNew(void);
 
+virNetworkObjPtr virNetworkObjFindByUUIDLocked(virNetworkObjListPtr nets,
+                                               const unsigned char *uuid);
 virNetworkObjPtr virNetworkObjFindByUUID(virNetworkObjListPtr nets,
                                          const unsigned char *uuid);
+virNetworkObjPtr virNetworkObjFindByNameLocked(virNetworkObjListPtr nets,
+                                               const char *name);
 virNetworkObjPtr virNetworkObjFindByName(virNetworkObjListPtr nets,
                                          const char *name);
 bool virNetworkObjTaint(virNetworkObjPtr obj,
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index c770177..c678dd8 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -565,7 +565,9 @@ virNetworkLoadAllState;
 virNetworkObjAssignDef;
 virNetworkObjEndAPI;
 virNetworkObjFindByName;
+virNetworkObjFindByNameLocked;
 virNetworkObjFindByUUID;
+virNetworkObjFindByUUIDLocked;
 virNetworkObjGetPersistentDef;
 virNetworkObjIsDuplicate;
 virNetworkObjListExport;
-- 
2.0.5

--
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]