On a Wednesday in 2021, Tim Wiederhake wrote:
Modeled after "WITH_QEMU_LOCK_GUARD" (see qemu's include/qemu/lockable.h).
Uses "__LINE__" instead of "__COUNTER__", as the latter is a GNU extension.


We mandate gnu99 in meson.build. But it seems both would work.

See comment for typical usage.

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

diff --git a/src/util/virthread.h b/src/util/virthread.h
index b394dbd226..00a9987a79 100644
--- a/src/util/virthread.h
+++ b/src/util/virthread.h
@@ -209,3 +209,23 @@ int virThreadLocalSet(virThreadLocal *l, void*) 
G_GNUC_WARN_UNUSED_RESULT;
        return 0; \
    } \
    struct classname ## EatSemicolon
+
+/**
+ * WITH_VIR_MUTEX_LOCK_GUARD:

I'd prefer the VIR_ prefix at the start.

Also, VIR_MUTEX_WITH should be enough to describe its functionality.

Jano

+ *
+ * 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 = ...;
+ *
+ *     WITH_VIR_MUTEX_LOCK_GUARD(mutex) {
+ *         // `mutex` is locked, and released automatically on scope exit
+ *         ...
+ *     }
+ */
+#define WITH_VIR_MUTEX_LOCK_GUARD(m) \
+    for (g_autoptr(virLockGuard) CONCAT(var, __LINE__) = virLockGuardNew(m); \
+         CONCAT(var, __LINE__); \
+         CONCAT(var, __LINE__) = (virLockGuardFree(CONCAT(var, __LINE__)), 
NULL))
--
2.31.1

Attachment: signature.asc
Description: PGP signature

Reply via email to