[PATCH v2 1/7] qemu/main-loop: Introduce QEMU_IOTHREAD_LOCK_GUARD, WITH_QEMU_IOTHREAD_LOCK

Richard Henderson posted 7 patches 3 years, 2 months ago
Maintainers: Richard Henderson <richard.henderson@linaro.org>, Paolo Bonzini <pbonzini@redhat.com>, "Philippe Mathieu-Daudé" <philmd@linaro.org>, Daniel Henrique Barboza <danielhb413@gmail.com>, "Cédric Le Goater" <clg@kaod.org>, David Gibson <david@gibson.dropbear.id.au>, Greg Kurz <groug@kaod.org>, Palmer Dabbelt <palmer@dabbelt.com>, Alistair Francis <alistair.francis@wdc.com>, Bin Meng <bin.meng@windriver.com>
[PATCH v2 1/7] qemu/main-loop: Introduce QEMU_IOTHREAD_LOCK_GUARD, WITH_QEMU_IOTHREAD_LOCK
Posted by Richard Henderson 3 years, 2 months ago
Create a couple of wrappers for locking/unlocking the iothread lock.

Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
---
 include/qemu/main-loop.h | 39 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 39 insertions(+)

diff --git a/include/qemu/main-loop.h b/include/qemu/main-loop.h
index 3c9a9a982d..73c60a9af4 100644
--- a/include/qemu/main-loop.h
+++ b/include/qemu/main-loop.h
@@ -343,6 +343,45 @@ void qemu_mutex_lock_iothread_impl(const char *file, int line);
  */
 void qemu_mutex_unlock_iothread(void);
 
+/**
+ * QEMU_IOTHREAD_LOCK_GUARD
+ * WITH_QEMU_IOTHREAD_LOCK
+ *
+ * Wrap a block of code in a conditional qemu_mutex_{lock,unlock}_iothread.
+ */
+typedef struct IOThreadLockAuto {
+    bool locked;
+    bool iterate;
+} IOThreadLockAuto;
+
+static inline IOThreadLockAuto qemu_iothread_auto_lock(const char *file,
+                                                       int line)
+{
+    bool need = !qemu_mutex_iothread_locked();
+    if (need) {
+        qemu_mutex_lock_iothread_impl(file, line);
+    }
+    return (IOThreadLockAuto){ .locked = need, .iterate = true };
+}
+
+static inline void qemu_iothread_auto_unlock(IOThreadLockAuto *l)
+{
+    if (l->locked) {
+        qemu_mutex_unlock_iothread();
+    }
+}
+
+G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(IOThreadLockAuto, qemu_iothread_auto_unlock)
+
+#define QEMU_IOTHREAD_LOCK_GUARD()                              \
+    g_auto(IOThreadLockAuto) _iothread_lock_auto                \
+        = qemu_iothread_auto_lock(__FILE__, __LINE__)           \
+
+#define WITH_QEMU_IOTHREAD_LOCK()                               \
+    for (QEMU_IOTHREAD_LOCK_GUARD();                            \
+         _iothread_lock_auto.iterate;                           \
+         _iothread_lock_auto.iterate = false)
+
 /*
  * qemu_cond_wait_iothread: Wait on condition for the main loop mutex
  *
-- 
2.34.1
Re: [PATCH v2 1/7] qemu/main-loop: Introduce QEMU_IOTHREAD_LOCK_GUARD, WITH_QEMU_IOTHREAD_LOCK
Posted by Richard Henderson 3 years, 2 months ago
On 11/22/22 12:57, Richard Henderson wrote:
> +#define QEMU_IOTHREAD_LOCK_GUARD()                              \
> +    g_auto(IOThreadLockAuto) _iothread_lock_auto                \
> +        = qemu_iothread_auto_lock(__FILE__, __LINE__)           \

Need an extra G_GNUC_UNUSED for (some versions of?) clang.
Which is really a bug, because it's always used via the cleanup.


r~
Re: [PATCH v2 1/7] qemu/main-loop: Introduce QEMU_IOTHREAD_LOCK_GUARD, WITH_QEMU_IOTHREAD_LOCK
Posted by Philippe Mathieu-Daudé 3 years, 2 months ago
On 22/11/22 21:57, Richard Henderson wrote:
> Create a couple of wrappers for locking/unlocking the iothread lock.
> 
> Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
> ---
>   include/qemu/main-loop.h | 39 +++++++++++++++++++++++++++++++++++++++
>   1 file changed, 39 insertions(+)


> +#define QEMU_IOTHREAD_LOCK_GUARD()                              \
> +    g_auto(IOThreadLockAuto) _iothread_lock_auto                \
> +        = qemu_iothread_auto_lock(__FILE__, __LINE__)           \
> +
> +#define WITH_QEMU_IOTHREAD_LOCK()                               \
> +    for (QEMU_IOTHREAD_LOCK_GUARD();                            \
> +         _iothread_lock_auto.iterate;                           \
> +         _iothread_lock_auto.iterate = false)

Nice, thanks!

Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>