[libvirt PATCH 06/11] virthread: Introduce WITH_VIR_MUTEX_LOCK_GUARD

Ján Tomko jtomko at redhat.com
Wed Aug 25 15:10:53 UTC 2021


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 <twiederh at 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
>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 488 bytes
Desc: not available
URL: <http://listman.redhat.com/archives/libvir-list/attachments/20210825/d7ae64d2/attachment-0001.sig>


More information about the libvir-list mailing list