[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH v7 01/31] main-loop.h: introduce qemu_in_main_thread()
From: |
Emanuele Giuseppe Esposito |
Subject: |
[PATCH v7 01/31] main-loop.h: introduce qemu_in_main_thread() |
Date: |
Fri, 11 Feb 2022 09:51:23 -0500 |
When invoked from the main loop, this function is the same
as qemu_mutex_iothread_locked, and returns true if the BQL is held.
When invoked from iothreads or tests, it returns true only
if the current AioContext is the Main Loop.
This essentially just extends qemu_mutex_iothread_locked to work
also in unit tests or other users like storage-daemon, that run
in the Main Loop but end up using the implementation in
stubs/iothread-lock.c.
Using qemu_mutex_iothread_locked in unit tests defaults to false
because they use the implementation in stubs/iothread-lock,
making all assertions added in next patches fail despite the
AioContext is still the main loop.
See the comment in the function header for more information.
Signed-off-by: Emanuele Giuseppe Esposito <eesposit@redhat.com>
---
include/qemu/main-loop.h | 24 ++++++++++++++++++++++++
softmmu/cpus.c | 5 +++++
stubs/iothread-lock.c | 5 +++++
3 files changed, 34 insertions(+)
diff --git a/include/qemu/main-loop.h b/include/qemu/main-loop.h
index 8dbc6fcb89..bc42b5939d 100644
--- a/include/qemu/main-loop.h
+++ b/include/qemu/main-loop.h
@@ -242,9 +242,33 @@ AioContext *iohandler_get_aio_context(void);
* must always be taken outside other locks. This function helps
* functions take different paths depending on whether the current
* thread is running within the main loop mutex.
+ *
+ * This function should never be used in the block layer, because
+ * unit tests, block layer tools and qemu-storage-daemon do not
+ * have a BQL.
+ * Please instead refer to qemu_in_main_thread().
*/
bool qemu_mutex_iothread_locked(void);
+/**
+ * qemu_in_main_thread: return whether it's possible to safely access
+ * the global state of the block layer.
+ *
+ * Global state of the block layer is not accessible from I/O threads
+ * or worker threads; only from threads that "own" the default
+ * AioContext that qemu_get_aio_context() returns. For tests, block
+ * layer tools and qemu-storage-daemon there is a designated thread that
+ * runs the event loop for qemu_get_aio_context(), and that is the
+ * main thread.
+ *
+ * For emulators, however, any thread that holds the BQL can act
+ * as the block layer main thread; this will be any of the actual
+ * main thread, the vCPU threads or the RCU thread.
+ *
+ * For clarity, do not use this function outside the block layer.
+ */
+bool qemu_in_main_thread(void);
+
/**
* qemu_mutex_lock_iothread: Lock the main loop mutex.
*
diff --git a/softmmu/cpus.c b/softmmu/cpus.c
index 23bca46b07..fd4e139289 100644
--- a/softmmu/cpus.c
+++ b/softmmu/cpus.c
@@ -485,6 +485,11 @@ bool qemu_mutex_iothread_locked(void)
return iothread_locked;
}
+bool qemu_in_main_thread(void)
+{
+ return qemu_mutex_iothread_locked();
+}
+
/*
* The BQL is taken from so many places that it is worth profiling the
* callers directly, instead of funneling them all through a single function.
diff --git a/stubs/iothread-lock.c b/stubs/iothread-lock.c
index 5b45b7fc8b..ff7386e42c 100644
--- a/stubs/iothread-lock.c
+++ b/stubs/iothread-lock.c
@@ -6,6 +6,11 @@ bool qemu_mutex_iothread_locked(void)
return false;
}
+bool qemu_in_main_thread(void)
+{
+ return qemu_get_current_aio_context() == qemu_get_aio_context();
+}
+
void qemu_mutex_lock_iothread_impl(const char *file, int line)
{
}
--
2.31.1
- [PATCH v7 00/31] block layer: split block APIs in global state and I/O, Emanuele Giuseppe Esposito, 2022/02/11
- [PATCH v7 01/31] main-loop.h: introduce qemu_in_main_thread(),
Emanuele Giuseppe Esposito <=
- [PATCH v7 02/31] main loop: macros to mark GS and I/O functions, Emanuele Giuseppe Esposito, 2022/02/11
- [PATCH v7 12/31] assertions for block_int global state API, Emanuele Giuseppe Esposito, 2022/02/11
- [PATCH v7 15/31] include/block/blockjob_int.h: split header into I/O and GS API, Emanuele Giuseppe Esposito, 2022/02/11
- [PATCH v7 16/31] GS and IO CODE macros for blockjob_int.h, Emanuele Giuseppe Esposito, 2022/02/11
- [PATCH v7 10/31] block.c: assertions to the block layer permissions API, Emanuele Giuseppe Esposito, 2022/02/11
- [PATCH v7 07/31] include/sysemu/block-backend: split header into I/O and global state (GS) API, Emanuele Giuseppe Esposito, 2022/02/11
- [PATCH v7 06/31] block/export/fuse.c: allow writable exports to take RESIZE permission, Emanuele Giuseppe Esposito, 2022/02/11
- [PATCH v7 04/31] assertions for block global state API, Emanuele Giuseppe Esposito, 2022/02/11
- [PATCH v7 08/31] block/block-backend.c: assertions for block-backend, Emanuele Giuseppe Esposito, 2022/02/11
- [PATCH v7 05/31] IO_CODE and IO_OR_GS_CODE for block I/O API, Emanuele Giuseppe Esposito, 2022/02/11