util: Introduce and use virObjectRWUnlock

Rather than overload virObjectUnlock as commit id '77f4593b' has
done, create a separate virObjectRWUnlock API that will force the
consumers to make the proper decision regarding unlocking the
RWLock's. Similar to the RWLockRead and RWLockWrite, use the
virObjectGetRWLockableObj helper. This restores the virObjectUnlock
code to using the virObjectGetLockableObj.

Signed-off-by: John Ferlan <jferlan@redhat.com>
This commit is contained in:
John Ferlan 2017-07-28 12:03:50 -04:00
parent bf09f002b1
commit 045d712c4b
4 changed files with 52 additions and 34 deletions

View File

@ -122,7 +122,7 @@ virDomainObjListFindByIDInternal(virDomainObjListPtr doms,
obj = virHashSearch(doms->objs, virDomainObjListSearchID, &id, NULL); obj = virHashSearch(doms->objs, virDomainObjListSearchID, &id, NULL);
if (ref) { if (ref) {
virObjectRef(obj); virObjectRef(obj);
virObjectUnlock(doms); virObjectRWUnlock(doms);
} }
if (obj) { if (obj) {
virObjectLock(obj); virObjectLock(obj);
@ -134,7 +134,7 @@ virDomainObjListFindByIDInternal(virDomainObjListPtr doms,
} }
} }
if (!ref) if (!ref)
virObjectUnlock(doms); virObjectRWUnlock(doms);
return obj; return obj;
} }
@ -166,7 +166,7 @@ virDomainObjListFindByUUIDInternal(virDomainObjListPtr doms,
obj = virHashLookup(doms->objs, uuidstr); obj = virHashLookup(doms->objs, uuidstr);
if (ref) { if (ref) {
virObjectRef(obj); virObjectRef(obj);
virObjectUnlock(doms); virObjectRWUnlock(doms);
} }
if (obj) { if (obj) {
virObjectLock(obj); virObjectLock(obj);
@ -178,7 +178,7 @@ virDomainObjListFindByUUIDInternal(virDomainObjListPtr doms,
} }
} }
if (!ref) if (!ref)
virObjectUnlock(doms); virObjectRWUnlock(doms);
return obj; return obj;
} }
@ -207,7 +207,7 @@ virDomainObjPtr virDomainObjListFindByName(virDomainObjListPtr doms,
virObjectRWLockRead(doms); virObjectRWLockRead(doms);
obj = virHashLookup(doms->objsName, name); obj = virHashLookup(doms->objsName, name);
virObjectRef(obj); virObjectRef(obj);
virObjectUnlock(doms); virObjectRWUnlock(doms);
if (obj) { if (obj) {
virObjectLock(obj); virObjectLock(obj);
if (obj->removing) { if (obj->removing) {
@ -329,7 +329,7 @@ virDomainObjPtr virDomainObjListAdd(virDomainObjListPtr doms,
virObjectRWLockWrite(doms); virObjectRWLockWrite(doms);
ret = virDomainObjListAddLocked(doms, def, xmlopt, flags, oldDef); ret = virDomainObjListAddLocked(doms, def, xmlopt, flags, oldDef);
virObjectUnlock(doms); virObjectRWUnlock(doms);
return ret; return ret;
} }
@ -355,7 +355,7 @@ void virDomainObjListRemove(virDomainObjListPtr doms,
virHashRemoveEntry(doms->objsName, dom->def->name); virHashRemoveEntry(doms->objsName, dom->def->name);
virObjectUnlock(dom); virObjectUnlock(dom);
virObjectUnref(dom); virObjectUnref(dom);
virObjectUnlock(doms); virObjectRWUnlock(doms);
} }
@ -420,7 +420,7 @@ virDomainObjListRename(virDomainObjListPtr doms,
ret = 0; ret = 0;
cleanup: cleanup:
virObjectUnlock(doms); virObjectRWUnlock(doms);
VIR_FREE(old_name); VIR_FREE(old_name);
return ret; return ret;
} }
@ -609,7 +609,7 @@ virDomainObjListLoadAllConfigs(virDomainObjListPtr doms,
} }
VIR_DIR_CLOSE(dir); VIR_DIR_CLOSE(dir);
virObjectUnlock(doms); virObjectRWUnlock(doms);
return ret; return ret;
} }
@ -655,7 +655,7 @@ virDomainObjListNumOfDomains(virDomainObjListPtr doms,
struct virDomainObjListData data = { filter, conn, active, 0 }; struct virDomainObjListData data = { filter, conn, active, 0 };
virObjectRWLockRead(doms); virObjectRWLockRead(doms);
virHashForEach(doms->objs, virDomainObjListCount, &data); virHashForEach(doms->objs, virDomainObjListCount, &data);
virObjectUnlock(doms); virObjectRWUnlock(doms);
return data.count; return data.count;
} }
@ -699,7 +699,7 @@ virDomainObjListGetActiveIDs(virDomainObjListPtr doms,
0, maxids, ids }; 0, maxids, ids };
virObjectRWLockRead(doms); virObjectRWLockRead(doms);
virHashForEach(doms->objs, virDomainObjListCopyActiveIDs, &data); virHashForEach(doms->objs, virDomainObjListCopyActiveIDs, &data);
virObjectUnlock(doms); virObjectRWUnlock(doms);
return data.numids; return data.numids;
} }
@ -753,7 +753,7 @@ virDomainObjListGetInactiveNames(virDomainObjListPtr doms,
size_t i; size_t i;
virObjectRWLockRead(doms); virObjectRWLockRead(doms);
virHashForEach(doms->objs, virDomainObjListCopyInactiveNames, &data); virHashForEach(doms->objs, virDomainObjListCopyInactiveNames, &data);
virObjectUnlock(doms); virObjectRWUnlock(doms);
if (data.oom) { if (data.oom) {
for (i = 0; i < data.numnames; i++) for (i = 0; i < data.numnames; i++)
VIR_FREE(data.names[i]); VIR_FREE(data.names[i]);
@ -794,7 +794,7 @@ virDomainObjListForEach(virDomainObjListPtr doms,
}; };
virObjectRWLockRead(doms); virObjectRWLockRead(doms);
virHashForEach(doms->objs, virDomainObjListHelper, &data); virHashForEach(doms->objs, virDomainObjListHelper, &data);
virObjectUnlock(doms); virObjectRWUnlock(doms);
return data.ret; return data.ret;
} }
@ -928,12 +928,12 @@ virDomainObjListCollect(virDomainObjListPtr domlist,
virObjectRWLockRead(domlist); virObjectRWLockRead(domlist);
sa_assert(domlist->objs); sa_assert(domlist->objs);
if (VIR_ALLOC_N(data.vms, virHashSize(domlist->objs)) < 0) { if (VIR_ALLOC_N(data.vms, virHashSize(domlist->objs)) < 0) {
virObjectUnlock(domlist); virObjectRWUnlock(domlist);
return -1; return -1;
} }
virHashForEach(domlist->objs, virDomainObjListCollectIterator, &data); virHashForEach(domlist->objs, virDomainObjListCollectIterator, &data);
virObjectUnlock(domlist); virObjectRWUnlock(domlist);
virDomainObjListFilter(&data.vms, &data.nvms, conn, filter, flags); virDomainObjListFilter(&data.vms, &data.nvms, conn, filter, flags);
@ -972,7 +972,7 @@ virDomainObjListConvert(virDomainObjListPtr domlist,
if (skip_missing) if (skip_missing)
continue; continue;
virObjectUnlock(domlist); virObjectRWUnlock(domlist);
virReportError(VIR_ERR_NO_DOMAIN, virReportError(VIR_ERR_NO_DOMAIN,
_("no domain with matching uuid '%s' (%s)"), _("no domain with matching uuid '%s' (%s)"),
uuidstr, dom->name); uuidstr, dom->name);
@ -982,12 +982,12 @@ virDomainObjListConvert(virDomainObjListPtr domlist,
virObjectRef(vm); virObjectRef(vm);
if (VIR_APPEND_ELEMENT(*vms, *nvms, vm) < 0) { if (VIR_APPEND_ELEMENT(*vms, *nvms, vm) < 0) {
virObjectUnlock(domlist); virObjectRWUnlock(domlist);
virObjectUnref(vm); virObjectUnref(vm);
goto error; goto error;
} }
} }
virObjectUnlock(domlist); virObjectRWUnlock(domlist);
sa_assert(*vms); sa_assert(*vms);
virDomainObjListFilter(vms, nvms, conn, filter, flags); virDomainObjListFilter(vms, nvms, conn, filter, flags);

View File

@ -2313,6 +2313,7 @@ virObjectRef;
virObjectRWLockableNew; virObjectRWLockableNew;
virObjectRWLockRead; virObjectRWLockRead;
virObjectRWLockWrite; virObjectRWLockWrite;
virObjectRWUnlock;
virObjectUnlock; virObjectUnlock;
virObjectUnref; virObjectUnref;

View File

@ -428,7 +428,7 @@ virObjectLock(void *anyobj)
* @anyobj: any instance of virObjectRWLockable * @anyobj: any instance of virObjectRWLockable
* *
* Acquire a read lock on @anyobj. The lock must be * Acquire a read lock on @anyobj. The lock must be
* released by virObjectUnlock. * released by virObjectRWUnlock.
* *
* The caller is expected to have acquired a reference * The caller is expected to have acquired a reference
* on the object before locking it (eg virObjectRef). * on the object before locking it (eg virObjectRef).
@ -457,7 +457,7 @@ virObjectRWLockRead(void *anyobj)
* @anyobj: any instance of virObjectRWLockable * @anyobj: any instance of virObjectRWLockable
* *
* Acquire a write lock on @anyobj. The lock must be * Acquire a write lock on @anyobj. The lock must be
* released by virObjectUnlock. * released by virObjectRWUnlock.
* *
* The caller is expected to have acquired a reference * The caller is expected to have acquired a reference
* on the object before locking it (eg virObjectRef). * on the object before locking it (eg virObjectRef).
@ -483,26 +483,39 @@ virObjectRWLockWrite(void *anyobj)
/** /**
* virObjectUnlock: * virObjectUnlock:
* @anyobj: any instance of virObjectLockable or virObjectRWLockable * @anyobj: any instance of virObjectLockable
* *
* Release a lock on @anyobj. The lock must have been acquired by * Release a lock on @anyobj. The lock must have been acquired by
* virObjectLock, virObjectRWLockRead, or virObjectRWLockWrite. * virObjectLock.
*/ */
void void
virObjectUnlock(void *anyobj) virObjectUnlock(void *anyobj)
{ {
if (virObjectIsClass(anyobj, virObjectLockableClass)) { virObjectLockablePtr obj = virObjectGetLockableObj(anyobj);
virObjectLockablePtr obj = anyobj;
virMutexUnlock(&obj->lock); if (!obj)
} else if (virObjectIsClass(anyobj, virObjectRWLockableClass)) { return;
virObjectRWLockablePtr obj = anyobj;
virRWLockUnlock(&obj->lock); virMutexUnlock(&obj->lock);
} else { }
virObjectPtr obj = anyobj;
VIR_WARN("Object %p (%s) is not a virObjectLockable "
"nor virObjectRWLockable instance", /**
anyobj, obj ? obj->klass->name : "(unknown)"); * virObjectRWUnlock:
} * @anyobj: any instance of virObjectRWLockable
*
* Release a lock on @anyobj. The lock must have been acquired by
* virObjectRWLockRead or virObjectRWLockWrite.
*/
void
virObjectRWUnlock(void *anyobj)
{
virObjectRWLockablePtr obj = virObjectGetRWLockableObj(anyobj);
if (!obj)
return;
virRWLockUnlock(&obj->lock);
} }

View File

@ -136,6 +136,10 @@ void
virObjectUnlock(void *lockableobj) virObjectUnlock(void *lockableobj)
ATTRIBUTE_NONNULL(1); ATTRIBUTE_NONNULL(1);
void
virObjectRWUnlock(void *lockableobj)
ATTRIBUTE_NONNULL(1);
void void
virObjectListFree(void *list); virObjectListFree(void *list);