Typical usage:
    void foobar(virObjectLockable *obj)
    {
        VIR_LOCK_GUARD lock = virObjectLockGuard(obj);
        /* `obj` is locked, and released automatically on scope exit */

        ...
    }

Signed-off-by: Tim Wiederhake <twied...@redhat.com>
---
 src/libvirt_private.syms |  1 +
 src/util/virobject.c     | 16 ++++++++++++++++
 src/util/virobject.h     |  4 ++++
 3 files changed, 21 insertions(+)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index f41674781d..733e40384e 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -2947,6 +2947,7 @@ virObjectListFree;
 virObjectListFreeCount;
 virObjectLock;
 virObjectLockableNew;
+virObjectLockGuard;
 virObjectNew;
 virObjectRef;
 virObjectRWLockableNew;
diff --git a/src/util/virobject.c b/src/util/virobject.c
index 3412985b79..4812b79d56 100644
--- a/src/util/virobject.c
+++ b/src/util/virobject.c
@@ -426,6 +426,22 @@ virObjectGetRWLockableObj(void *anyobj)
 }
 
 
+/**
+ * virObjectLockGuard:
+ * @anyobj: any instance of virObjectLockable
+ *
+ * Acquire a lock on @anyobj that will be managed by the virLockGuard object
+ * returned to the caller.
+ */
+virLockGuard *
+virObjectLockGuard(void *anyobj)
+{
+    virObjectLockable *obj = virObjectGetLockableObj(anyobj);
+
+    return virLockGuardNew(&obj->lock);
+}
+
+
 /**
  * virObjectLock:
  * @anyobj: any instance of virObjectLockable
diff --git a/src/util/virobject.h b/src/util/virobject.h
index 1e34c77744..0c9cbea726 100644
--- a/src/util/virobject.h
+++ b/src/util/virobject.h
@@ -118,6 +118,10 @@ void *
 virObjectRWLockableNew(virClass *klass)
     ATTRIBUTE_NONNULL(1);
 
+virLockGuard *
+virObjectLockGuard(void *lockableobj)
+    ATTRIBUTE_NONNULL(1);
+
 void
 virObjectLock(void *lockableobj)
     ATTRIBUTE_NONNULL(1);
-- 
2.31.1

Reply via email to