On Thu, Feb 26, 2015 at 15:17:36 +0100, Michal Privoznik wrote:
Every API that touches internal structure of the object must lock
the object first. Not every API that has the object as an
argument needs to do that though. Some APIs just pass the object
to lower layers, which, however, must lock the object then. Look
at the code, you'll get my meaning soon.
Signed-off-by: Michal Privoznik <mprivozn(a)redhat.com>
---
src/conf/network_conf.c | 39 +++++++++++++++++++++++++++++++++------
1 file changed, 33 insertions(+), 6 deletions(-)
diff --git a/src/conf/network_conf.c b/src/conf/network_conf.c
index 95d8b4d..5b0f36f 100644
--- a/src/conf/network_conf.c
+++ b/src/conf/network_conf.c
@@ -156,31 +156,41 @@ virNetworkObjListPtr virNetworkObjListNew(void)
virNetworkObjPtr virNetworkObjFindByUUID(virNetworkObjListPtr nets,
const unsigned char *uuid)
{
+ virNetworkObjPtr ret = NULL;
size_t i;
+ virObjectLock(nets);
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];
+ if (!memcmp(nets->objs[i]->def->uuid, uuid, VIR_UUID_BUFLEN)) {
+ ret = nets->objs[i];
+ break;
+ }
virObjectUnlock(nets->objs[i]);
}
+ virObjectUnlock(nets);
- return NULL;
+ return ret;
}
virNetworkObjPtr virNetworkObjFindByName(virNetworkObjListPtr nets,
const char *name)
{
+ virNetworkObjPtr ret = NULL;
size_t i;
+ virObjectLock(nets);
for (i = 0; i < nets->count; i++) {
virObjectLock(nets->objs[i]);
- if (STREQ(nets->objs[i]->def->name, name))
- return nets->objs[i];
+ if (STREQ(nets->objs[i]->def->name, name)) {
+ ret = nets->objs[i];
+ break;
+ }
virObjectUnlock(nets->objs[i]);
}
+ virObjectUnlock(nets);
- return NULL;
+ return ret;
}
bool
Later review showed that making the two functions above self locking
won't be ideal.
Peter