On 11/18/22 05:38, Alex Bennée wrote:
Richard Henderson <richard.hender...@linaro.org> writes:
Create a wrapper for locking/unlocking the iothread lock.
Signed-off-by: Richard Henderson <richard.hender...@linaro.org>
---
Cc: Paolo Bonzini <pbonz...@redhat.com> (maintainer:Main loop)
---
include/qemu/main-loop.h | 29 +++++++++++++++++++++++++++++
1 file changed, 29 insertions(+)
diff --git a/include/qemu/main-loop.h b/include/qemu/main-loop.h
index 3c9a9a982d..c25f390696 100644
--- a/include/qemu/main-loop.h
+++ b/include/qemu/main-loop.h
@@ -343,6 +343,35 @@ void qemu_mutex_lock_iothread_impl(const char *file, int
line);
*/
void qemu_mutex_unlock_iothread(void);
+/**
+ * QEMU_IOTHREAD_LOCK_GUARD
+ *
+ * Wrap a block of code in a conditional qemu_mutex_{lock,unlock}_iothread.
+ */
+typedef struct IOThreadLockAuto IOThreadLockAuto;
+
+static inline IOThreadLockAuto *qemu_iothread_auto_lock(const char *file,
+ int line)
+{
+ if (qemu_mutex_iothread_locked()) {
+ return NULL;
+ }
+ qemu_mutex_lock_iothread_impl(file, line);
+ /* Anything non-NULL causes the cleanup function to be called */
+ return (IOThreadLockAuto *)(uintptr_t)1;
Oh hang on, what black magic is this. Does the compiler do a NULL check
before calling the cleanup?
Not the compiler, but...
+G_DEFINE_AUTOPTR_CLEANUP_FUNC(IOThreadLockAuto, qemu_iothread_auto_unlock)
... this does. Follow the macros down and you get
static G_GNUC_UNUSED inline void _GLIB_AUTOPTR_CLEAR_FUNC_NAME(TypeName)
(TypeName *_ptr)
{ if (_ptr) (cleanup) ((ParentName *) _ptr); }
r~