Introduce iothread_get_aio_context() (In this patch named "iothread_ref_and_get_aio_context" for build, will change the name in the last patch) with a 'holder' argument and its counterpart iothread_put_aio_context().
Previously, users of an IOThread's AioContext did not explicitly record their identity, making it difficult to debug which devices or subsystems were pinning an IOThread. This patch enhances the reference counting mechanism by: 1. Automatically incrementing the object reference count when a context is retrieved. 2. Tracking holders using iothread_ref() and iothread_unref(). Signed-off-by: Zhang Chen <[email protected]> --- include/system/iothread.h | 8 ++++++++ iothread.c | 23 ++++++++++++++++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/include/system/iothread.h b/include/system/iothread.h index ef0b2f9648..86f02c5a7d 100644 --- a/include/system/iothread.h +++ b/include/system/iothread.h @@ -18,6 +18,7 @@ #include "qemu/thread.h" #include "qom/object.h" #include "system/event-loop-base.h" +#include "qapi/qapi-types-misc.h" #define TYPE_IOTHREAD "iothread" @@ -72,7 +73,14 @@ DECLARE_INSTANCE_CHECKER(IOThread, IOTHREAD, char *iothread_get_id(IOThread *iothread); IOThread *iothread_by_id(const char *id); +/* + * The iothread_get_aio_context() and iothread_put_aio_context() are not + * thread-safe and must be called under the Big QEMU Lock (BQL). + */ AioContext *iothread_get_aio_context(IOThread *iothread); +AioContext *iothread_ref_and_get_aio_context(IOThread *iothread, + const IOThreadHolder *holder); +void iothread_put_aio_context(IOThread *iothread, const IOThreadHolder *holder); GMainContext *iothread_get_g_main_context(IOThread *iothread); /* diff --git a/iothread.c b/iothread.c index 38f273c0e9..f5d4708b25 100644 --- a/iothread.c +++ b/iothread.c @@ -39,6 +39,11 @@ void iothread_ref(IOThread *iothread, const IOThreadHolder *holder) assert(holder); QAPI_LIST_PREPEND(iothread->holders, QAPI_CLONE(IOThreadHolder, holder)); + /* + * This guarantees that the IOThread and its AioContext remain alive + * as long as there is a holder. + */ + object_ref(OBJECT(iothread)); } static int iothread_holder_compare(const IOThreadHolder *holder_a, @@ -85,6 +90,7 @@ void iothread_unref(IOThread *iothread, const IOThreadHolder *holder) *prev = curr->next; curr->next = NULL; qapi_free_IOThreadHolderList(curr); + object_unref(OBJECT(iothread)); return; } prev = &curr->next; @@ -206,7 +212,7 @@ static void iothread_init_gcontext(IOThread *iothread, const char *thread_name) g_autofree char *name = g_strdup_printf("%s aio-context", thread_name); iothread->worker_context = g_main_context_new(); - source = aio_get_g_source(iothread_get_aio_context(iothread)); + source = aio_get_g_source(iothread->ctx); g_source_set_name(source, name); g_source_attach(source, iothread->worker_context); g_source_unref(source); @@ -428,6 +434,21 @@ AioContext *iothread_get_aio_context(IOThread *iothread) return iothread->ctx; } +AioContext *iothread_ref_and_get_aio_context(IOThread *iothread, + const IOThreadHolder *holder) +{ + /* Add IOThreadHolder to the list */ + iothread_ref(iothread, holder); + + return iothread->ctx; + } + +void iothread_put_aio_context(IOThread *iothread, const IOThreadHolder *holder) +{ + /* Delete IOThreadHolder from the list */ + iothread_unref(iothread, holder); +} + static int query_one_iothread(Object *object, void *opaque) { IOThreadInfoList ***tail = opaque; -- 2.49.0
