util: Introduce virObjectGetRWLockableObj

Introduce a helper to handle the error path more cleanly. The same
as virObjectGetLockableObj in order to essentially follow the original
logic of commit 'b545f65d' to ensure that the input argument at least
has some validity before using.

Signed-off-by: John Ferlan <jferlan@redhat.com>
This commit is contained in:
John Ferlan 2017-07-28 10:27:11 -04:00
parent 8b03a609dd
commit bf09f002b1

View File

@ -383,6 +383,22 @@ virObjectGetLockableObj(void *anyobj)
}
static virObjectRWLockablePtr
virObjectGetRWLockableObj(void *anyobj)
{
virObjectPtr obj;
if (virObjectIsClass(anyobj, virObjectRWLockableClass))
return anyobj;
obj = anyobj;
VIR_WARN("Object %p (%s) is not a virObjectRWLockable instance",
anyobj, obj ? obj->klass->name : "(unknown)");
return NULL;
}
/**
* virObjectLock:
* @anyobj: any instance of virObjectLockable or virObjectRWLockable
@ -427,14 +443,12 @@ virObjectLock(void *anyobj)
void
virObjectRWLockRead(void *anyobj)
{
if (virObjectIsClass(anyobj, virObjectRWLockableClass)) {
virObjectRWLockablePtr obj = anyobj;
virObjectRWLockablePtr obj = virObjectGetRWLockableObj(anyobj);
if (!obj)
return;
virRWLockRead(&obj->lock);
} else {
virObjectPtr obj = anyobj;
VIR_WARN("Object %p (%s) is not a virObjectRWLockable instance",
anyobj, obj ? obj->klass->name : "(unknown)");
}
}
@ -458,14 +472,12 @@ virObjectRWLockRead(void *anyobj)
void
virObjectRWLockWrite(void *anyobj)
{
if (virObjectIsClass(anyobj, virObjectRWLockableClass)) {
virObjectRWLockablePtr obj = anyobj;
virObjectRWLockablePtr obj = virObjectGetRWLockableObj(anyobj);
if (!obj)
return;
virRWLockWrite(&obj->lock);
} else {
virObjectPtr obj = anyobj;
VIR_WARN("Object %p (%s) is not a virObjectRWLockable instance",
anyobj, obj ? obj->klass->name : "(unknown)");
}
}