[libvirt PATCH v5 5/9] virobject: Introduce VIR_WITH_OBJECT_LOCK_GUARD

Tim Wiederhake posted 9 patches 4 years ago
[libvirt PATCH v5 5/9] virobject: Introduce VIR_WITH_OBJECT_LOCK_GUARD
Posted by Tim Wiederhake 4 years ago
Modeled after "WITH_QEMU_LOCK_GUARD" (see qemu's include/qemu/lockable.h).

See comment for typical usage.

Signed-off-by: Tim Wiederhake <twiederh@redhat.com>
---
 src/util/virobject.h | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)

diff --git a/src/util/virobject.h b/src/util/virobject.h
index dc1ce66a4f..a1e16aee77 100644
--- a/src/util/virobject.h
+++ b/src/util/virobject.h
@@ -148,3 +148,25 @@ virObjectListFree(void *list);
 void
 virObjectListFreeCount(void *list,
                        size_t count);
+
+#define VIR_WITH_OBJECT_LOCK_GUARD_(o, name) \
+    for (g_auto(virLockGuard) name = virObjectLockGuard(o); name.mutex; \
+         name.mutex = (virLockGuardUnlock(&name), NULL))
+
+/**
+ * VIR_WITH_OBJECT_LOCK_GUARD:
+ *
+ * This macro defines a lock scope such that entering the scope takes the lock
+ * and leaving the scope releases the lock. Return statements are allowed
+ * within the scope and release the lock. Break and continue statements leave
+ * the scope early and release the lock.
+ *
+ *     virObjectLockable *lockable = ...;
+ *
+ *     VIR_WITH_OBJECT_LOCK_GUARD(lockable) {
+ *         // `lockable` is locked, and released automatically on scope exit
+ *         ...
+ *     }
+ */
+#define VIR_WITH_OBJECT_LOCK_GUARD(o) \
+    VIR_WITH_OBJECT_LOCK_GUARD_(o, CONCAT(var, __COUNTER__))
-- 
2.31.1

Re: [libvirt PATCH v5 5/9] virobject: Introduce VIR_WITH_OBJECT_LOCK_GUARD
Posted by Daniel P. Berrangé 4 years ago
On Tue, Feb 01, 2022 at 02:20:13PM +0100, Tim Wiederhake wrote:
> Modeled after "WITH_QEMU_LOCK_GUARD" (see qemu's include/qemu/lockable.h).
> 
> See comment for typical usage.
> 
> Signed-off-by: Tim Wiederhake <twiederh@redhat.com>
> ---
>  src/util/virobject.h | 22 ++++++++++++++++++++++
>  1 file changed, 22 insertions(+)

Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>


Regards,
Daniel
-- 
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|