]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
virthread: Introduce VIR_WITH_MUTEX_LOCK_GUARD
authorTim Wiederhake <twiederh@redhat.com>
Wed, 25 Aug 2021 08:25:32 +0000 (10:25 +0200)
committerTim Wiederhake <twiederh@redhat.com>
Tue, 1 Feb 2022 16:19:32 +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/virthread.h

index 6cdaf2820e02be4aa152903da1e10f68da00707e..23abe0b6c92a39cbd4118e85cedb35ba3687da62 100644 (file)
@@ -209,3 +209,24 @@ int virThreadLocalSet(virThreadLocal *l, void*) G_GNUC_WARN_UNUSED_RESULT;
         return 0; \
     } \
     struct classname ## EatSemicolon
+
+#define VIR_WITH_MUTEX_LOCK_GUARD_(m, name) \
+    for (g_auto(virLockGuard) name = virLockGuardLock(m); name.mutex; \
+        name.mutex = (virLockGuardUnlock(&name), NULL))
+/**
+ * VIR_WITH_MUTEX_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.
+ *
+ *     virMutex *mutex = ...;
+ *
+ *     VIR_WITH_MUTEX_LOCK_GUARD(mutex) {
+ *         // `mutex` is locked, and released automatically on scope exit
+ *         ...
+ *     }
+ */
+#define VIR_WITH_MUTEX_LOCK_GUARD(m) \
+    VIR_WITH_MUTEX_LOCK_GUARD_(m, CONCAT(var, __COUNTER__))