]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
virobject: Introduce VIR_WITH_OBJECT_LOCK_GUARD
authorTim Wiederhake <twiederh@redhat.com>
Mon, 23 Aug 2021 12:47:25 +0000 (14:47 +0200)
committerTim Wiederhake <twiederh@redhat.com>
Tue, 1 Feb 2022 16:19:44 +0000 (17:19 +0100)
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>
Reviewed-by: Martin Kletzander <mkletzan@redhat.com>
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
src/util/virobject.h

index dc1ce66a4f2179522328bcd99dca3204c4e3a314..a1e16aee773fb1899cfe62f4799458a4e37d9494 100644 (file)
@@ -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__))