conf: Rework/rename virDomainObjListFindByIDRef

Rework the code such that virDomainObjListFindByID will always
return a locked/ref counted object so that the callers can
always do the same cleanup logic to call virDomainObjEndAPI.
Makes accessing the objects much more consistent.

NB:
There were 2 callers (lxcDomainLookupByID and qemuDomainLookupByID)
that were already using the ByID name, but not virDomainObjEndAPI -
these were changed as well in this update/patch.

Signed-off-by: John Ferlan <jferlan@redhat.com>
Reviewed-by: Jim Fehlig <jfehlig@suse.com>
This commit is contained in:
John Ferlan 2018-03-09 10:59:28 -05:00
parent fd9ef3b31e
commit 868136624f
13 changed files with 26 additions and 48 deletions

View File

@ -844,7 +844,7 @@ bhyveDomainLookupByID(virConnectPtr conn,
virDomainObjPtr vm;
virDomainPtr dom = NULL;
vm = virDomainObjListFindByIDRef(privconn->domains, id);
vm = virDomainObjListFindByID(privconn->domains, id);
if (!vm) {
virReportError(VIR_ERR_NO_DOMAIN,

View File

@ -109,44 +109,27 @@ static int virDomainObjListSearchID(const void *payload,
return want;
}
static virDomainObjPtr
virDomainObjListFindByIDInternal(virDomainObjListPtr doms,
int id,
bool ref)
{
virDomainObjPtr obj;
virObjectRWLockRead(doms);
obj = virHashSearch(doms->objs, virDomainObjListSearchID, &id, NULL);
if (ref) {
virObjectRef(obj);
virObjectRWUnlock(doms);
}
if (obj) {
virObjectLock(obj);
if (obj->removing) {
virObjectUnlock(obj);
if (ref)
virObjectUnref(obj);
obj = NULL;
}
}
if (!ref)
virObjectRWUnlock(doms);
return obj;
}
virDomainObjPtr
virDomainObjListFindByID(virDomainObjListPtr doms,
int id)
{
return virDomainObjListFindByIDInternal(doms, id, false);
virDomainObjPtr obj;
virObjectRWLockRead(doms);
obj = virHashSearch(doms->objs, virDomainObjListSearchID, &id, NULL);
virObjectRef(obj);
virObjectRWUnlock(doms);
if (obj) {
virObjectLock(obj);
if (obj->removing) {
virObjectUnlock(obj);
virObjectUnref(obj);
obj = NULL;
}
}
virDomainObjPtr
virDomainObjListFindByIDRef(virDomainObjListPtr doms,
int id)
{
return virDomainObjListFindByIDInternal(doms, id, true);
return obj;
}

View File

@ -34,8 +34,6 @@ virDomainObjListPtr virDomainObjListNew(void);
virDomainObjPtr virDomainObjListFindByID(virDomainObjListPtr doms,
int id);
virDomainObjPtr virDomainObjListFindByIDRef(virDomainObjListPtr doms,
int id);
virDomainObjPtr virDomainObjListFindByUUID(virDomainObjListPtr doms,
const unsigned char *uuid);
virDomainObjPtr virDomainObjListFindByName(virDomainObjListPtr doms,

View File

@ -952,7 +952,6 @@ virDomainObjListCollect;
virDomainObjListConvert;
virDomainObjListExport;
virDomainObjListFindByID;
virDomainObjListFindByIDRef;
virDomainObjListFindByName;
virDomainObjListFindByUUID;
virDomainObjListForEach;

View File

@ -450,7 +450,7 @@ libxlDomainShutdownThread(void *opaque)
cfg = libxlDriverConfigGet(driver);
vm = virDomainObjListFindByIDRef(driver->domains, ev->domid);
vm = virDomainObjListFindByID(driver->domains, ev->domid);
if (!vm) {
VIR_INFO("Received event for unknown domain ID %d", ev->domid);
goto cleanup;

View File

@ -1082,7 +1082,7 @@ libxlDomainLookupByID(virConnectPtr conn, int id)
virDomainObjPtr vm;
virDomainPtr dom = NULL;
vm = virDomainObjListFindByIDRef(driver->domains, id);
vm = virDomainObjListFindByID(driver->domains, id);
if (!vm) {
virReportError(VIR_ERR_NO_DOMAIN, NULL);
goto cleanup;

View File

@ -262,8 +262,7 @@ static virDomainPtr lxcDomainLookupByID(virConnectPtr conn,
dom = virGetDomain(conn, vm->def->name, vm->def->uuid, vm->def->id);
cleanup:
if (vm)
virObjectUnlock(vm);
virDomainObjEndAPI(&vm);
return dom;
}

View File

@ -342,7 +342,7 @@ static virDomainPtr openvzDomainLookupByID(virConnectPtr conn,
virDomainPtr dom = NULL;
openvzDriverLock(driver);
vm = virDomainObjListFindByIDRef(driver->domains, id);
vm = virDomainObjListFindByID(driver->domains, id);
openvzDriverUnlock(driver);
if (!vm) {

View File

@ -1543,8 +1543,7 @@ static virDomainPtr qemuDomainLookupByID(virConnectPtr conn,
dom = virGetDomain(conn, vm->def->name, vm->def->uuid, vm->def->id);
cleanup:
if (vm)
virObjectUnlock(vm);
virDomainObjEndAPI(&vm);
return dom;
}

View File

@ -1708,7 +1708,7 @@ static virDomainPtr testDomainLookupByID(virConnectPtr conn,
virDomainPtr ret = NULL;
virDomainObjPtr dom;
if (!(dom = virDomainObjListFindByIDRef(privconn->domains, id))) {
if (!(dom = virDomainObjListFindByID(privconn->domains, id))) {
virReportError(VIR_ERR_NO_DOMAIN, NULL);
return NULL;
}

View File

@ -1379,7 +1379,7 @@ static virDomainPtr umlDomainLookupByID(virConnectPtr conn,
virDomainPtr dom = NULL;
umlDriverLock(driver);
vm = virDomainObjListFindByIDRef(driver->domains, id);
vm = virDomainObjListFindByID(driver->domains, id);
umlDriverUnlock(driver);
if (!vm) {

View File

@ -825,7 +825,7 @@ vmwareDomainLookupByID(virConnectPtr conn, int id)
virDomainPtr dom = NULL;
vmwareDriverLock(driver);
vm = virDomainObjListFindByIDRef(driver->domains, id);
vm = virDomainObjListFindByID(driver->domains, id);
vmwareDriverUnlock(driver);
if (!vm) {

View File

@ -555,7 +555,7 @@ vzDomainLookupByID(virConnectPtr conn, int id)
virDomainPtr ret = NULL;
virDomainObjPtr dom;
dom = virDomainObjListFindByIDRef(privconn->driver->domains, id);
dom = virDomainObjListFindByID(privconn->driver->domains, id);
if (dom == NULL) {
virReportError(VIR_ERR_NO_DOMAIN, NULL);