This is a wrapper function that: (1) Attempts to take a lock on the object. (2) gracefully returns if the object is already locked. Signed-off-by: Prerna Saxena <saxenap.ltc@xxxxxxxxx> --- src/libvirt_private.syms | 1 + src/util/virobject.c | 26 ++++++++++++++++++++++++++ src/util/virobject.h | 4 ++++ src/util/virthread.c | 5 +++++ src/util/virthread.h | 1 + 5 files changed, 37 insertions(+) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 9243c55..c0ab8b5 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2362,6 +2362,7 @@ virObjectRWLockableNew; virObjectRWLockRead; virObjectRWLockWrite; virObjectRWUnlock; +virObjectTrylock; virObjectUnlock; virObjectUnref; diff --git a/src/util/virobject.c b/src/util/virobject.c index cfa821c..796ea06 100644 --- a/src/util/virobject.c +++ b/src/util/virobject.c @@ -495,6 +495,32 @@ virObjectRWLockWrite(void *anyobj) /** + * virObjectTrylock: + * @anyobj: any instance of virObjectLockable or virObjectRWLockable + * + * Attempt to acquire a lock on @anyobj. The lock must be released by + * virObjectUnlock. + * Returns: + * 0: If the lock was successfully taken. + * errno : Indicates error. + * + * The caller is expected to have acquired a reference + * on the object before locking it (eg virObjectRef). + * The object must be unlocked before releasing this + * reference. + */ +int +virObjectTrylock(void *anyobj) +{ + virObjectLockablePtr obj = virObjectGetLockableObj(anyobj); + + if (!obj) + return -1; + + return virMutexTrylock(&obj->lock); +} + +/** * virObjectUnlock: * @anyobj: any instance of virObjectLockable * diff --git a/src/util/virobject.h b/src/util/virobject.h index ac6cf22..402ea32 100644 --- a/src/util/virobject.h +++ b/src/util/virobject.h @@ -124,6 +124,10 @@ void virObjectLock(void *lockableobj) ATTRIBUTE_NONNULL(1); +int +virObjectTrylock(void *lockableobj) + ATTRIBUTE_NONNULL(1); + void virObjectRWLockRead(void *lockableobj) ATTRIBUTE_NONNULL(1); diff --git a/src/util/virthread.c b/src/util/virthread.c index 6c49515..07b7a3f 100644 --- a/src/util/virthread.c +++ b/src/util/virthread.c @@ -89,6 +89,11 @@ void virMutexLock(virMutexPtr m) pthread_mutex_lock(&m->lock); } +int virMutexTrylock(virMutexPtr m) +{ + return pthread_mutex_trylock(&m->lock); +} + void virMutexUnlock(virMutexPtr m) { pthread_mutex_unlock(&m->lock); diff --git a/src/util/virthread.h b/src/util/virthread.h index e466d9b..8e3da2c 100644 --- a/src/util/virthread.h +++ b/src/util/virthread.h @@ -132,6 +132,7 @@ int virMutexInitRecursive(virMutexPtr m) ATTRIBUTE_RETURN_CHECK; void virMutexDestroy(virMutexPtr m); void virMutexLock(virMutexPtr m); +int virMutexTrylock(virMutexPtr m); void virMutexUnlock(virMutexPtr m); -- 2.9.5 -- libvir-list mailing list libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list