blob: 8810376dcea13cef3f704677fb8c3d06641c9885 [file] [log] [blame]
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +01001/*
2 * Event loop thread
3 *
Eric Blakec3033fd2021-01-13 16:10:12 -06004 * Copyright Red Hat Inc., 2013, 2020
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +01005 *
6 * Authors:
7 * Stefan Hajnoczi <stefanha@redhat.com>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
11 *
12 */
13
Peter Maydelld38ea872016-01-29 17:50:05 +000014#include "qemu/osdep.h"
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +010015#include "qom/object.h"
16#include "qom/object_interfaces.h"
17#include "qemu/module.h"
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +010018#include "block/aio.h"
Paolo Bonzinid16341f2016-10-27 12:49:00 +020019#include "block/block.h"
Philippe Mathieu-Daudé32cad1f2024-12-03 15:20:13 +010020#include "system/event-loop-base.h"
21#include "system/iothread.h"
Markus Armbrustere688df62018-02-01 12:18:31 +010022#include "qapi/error.h"
Markus Armbruster112ed242018-02-26 17:13:27 -060023#include "qapi/qapi-commands-misc.h"
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +030024#include "qemu/error-report.h"
Paolo Bonziniab28bd22015-07-09 08:55:38 +020025#include "qemu/rcu.h"
Paolo Bonzinie4370162016-10-27 12:48:59 +020026#include "qemu/main-loop.h"
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +010027
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +010028
Peter Xu90c558b2018-03-22 16:56:30 +080029#ifdef CONFIG_POSIX
Stefan Hajnoczicdd7abf2017-01-26 17:01:19 +000030/* Benchmark results from 2016 on NVMe SSD drives show max polling times around
31 * 16-32 microseconds yield IOPS improvements for both iodepth=1 and iodepth=32
32 * workloads.
33 */
34#define IOTHREAD_POLL_MAX_NS_DEFAULT 32768ULL
Peter Xu90c558b2018-03-22 16:56:30 +080035#else
36#define IOTHREAD_POLL_MAX_NS_DEFAULT 0ULL
37#endif
Stefan Hajnoczicdd7abf2017-01-26 17:01:19 +000038
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +010039static void *iothread_run(void *opaque)
40{
41 IOThread *iothread = opaque;
42
Paolo Bonziniab28bd22015-07-09 08:55:38 +020043 rcu_register_thread();
Peter Xub60ec762019-03-06 19:55:31 +080044 /*
45 * g_main_context_push_thread_default() must be called before anything
46 * in this new thread uses glib.
47 */
48 g_main_context_push_thread_default(iothread->worker_context);
Paolo Bonzini5f50be92021-06-09 14:22:34 +020049 qemu_set_current_aio_context(iothread->ctx);
Stefan Hajnoczi88eb7c22014-02-27 11:48:41 +010050 iothread->thread_id = qemu_get_thread_id();
Peter Xu21c4d152019-03-06 19:55:28 +080051 qemu_sem_post(&iothread->init_done_sem);
Stefan Hajnoczi88eb7c22014-02-27 11:48:41 +010052
Stefan Hajnoczi2362a282017-12-07 20:13:19 +000053 while (iothread->running) {
Peter Xu6ca20622019-03-06 19:55:32 +080054 /*
55 * Note: from functional-wise the g_main_loop_run() below can
56 * already cover the aio_poll() events, but we can't run the
57 * main loop unconditionally because explicit aio_poll() here
58 * is faster than g_main_loop_run() when we do not need the
59 * gcontext at all (e.g., pure block layer iothreads). In
60 * other words, when we want to run the gcontext with the
61 * iothread we need to pay some performance for functionality.
62 */
Paolo Bonzini65c1b5b2016-10-27 12:49:06 +020063 aio_poll(iothread->ctx, true);
Wang Yong329163c2017-08-29 15:22:37 +080064
Peter Xu6c953632019-01-29 13:14:32 +080065 /*
66 * We must check the running state again in case it was
67 * changed in previous aio_poll()
68 */
Stefan Hajnoczid73415a2020-09-23 11:56:46 +010069 if (iothread->running && qatomic_read(&iothread->run_gcontext)) {
Wang Yong329163c2017-08-29 15:22:37 +080070 g_main_loop_run(iothread->main_loop);
Wang Yong329163c2017-08-29 15:22:37 +080071 }
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +010072 }
Paolo Bonziniab28bd22015-07-09 08:55:38 +020073
Peter Xub60ec762019-03-06 19:55:31 +080074 g_main_context_pop_thread_default(iothread->worker_context);
Paolo Bonziniab28bd22015-07-09 08:55:38 +020075 rcu_unregister_thread();
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +010076 return NULL;
77}
78
Stefan Hajnoczi2362a282017-12-07 20:13:19 +000079/* Runs in iothread_run() thread */
80static void iothread_stop_bh(void *opaque)
81{
82 IOThread *iothread = opaque;
83
84 iothread->running = false; /* stop iothread_run() */
85
86 if (iothread->main_loop) {
87 g_main_loop_quit(iothread->main_loop);
88 }
89}
90
Peter Xu82d90702017-09-28 10:59:56 +080091void iothread_stop(IOThread *iothread)
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +010092{
Peter Xu82d90702017-09-28 10:59:56 +080093 if (!iothread->ctx || iothread->stopping) {
94 return;
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +030095 }
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +010096 iothread->stopping = true;
Stefan Hajnoczi2362a282017-12-07 20:13:19 +000097 aio_bh_schedule_oneshot(iothread->ctx, iothread_stop_bh, iothread);
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +010098 qemu_thread_join(&iothread->thread);
Peter Xu82d90702017-09-28 10:59:56 +080099}
100
Stefan Hajnoczicdd7abf2017-01-26 17:01:19 +0000101static void iothread_instance_init(Object *obj)
102{
103 IOThread *iothread = IOTHREAD(obj);
104
105 iothread->poll_max_ns = IOTHREAD_POLL_MAX_NS_DEFAULT;
Marc-André Lureau14a2d112018-08-21 12:07:16 +0200106 iothread->thread_id = -1;
Peter Xu21c4d152019-03-06 19:55:28 +0800107 qemu_sem_init(&iothread->init_done_sem, 0);
Peter Xub506e0f2019-03-06 19:55:29 +0800108 /* By default, we don't run gcontext */
Stefan Hajnoczid73415a2020-09-23 11:56:46 +0100109 qatomic_set(&iothread->run_gcontext, 0);
Stefan Hajnoczicdd7abf2017-01-26 17:01:19 +0000110}
111
Fam Zhengdce89212016-09-08 17:28:51 +0800112static void iothread_instance_finalize(Object *obj)
113{
114 IOThread *iothread = IOTHREAD(obj);
115
Peter Xu82d90702017-09-28 10:59:56 +0800116 iothread_stop(iothread);
Marc-André Lureau14a2d112018-08-21 12:07:16 +0200117
Peter Xu15544342018-04-09 16:39:56 +0800118 /*
119 * Before glib2 2.33.10, there is a glib2 bug that GSource context
120 * pointer may not be cleared even if the context has already been
121 * destroyed (while it should). Here let's free the AIO context
122 * earlier to bypass that glib bug.
123 *
124 * We can remove this comment after the minimum supported glib2
125 * version boosts to 2.33.10. Before that, let's free the
126 * GSources first before destroying any GMainContext.
127 */
128 if (iothread->ctx) {
129 aio_context_unref(iothread->ctx);
130 iothread->ctx = NULL;
131 }
Peter Xu5b3ac232017-09-28 10:59:57 +0800132 if (iothread->worker_context) {
133 g_main_context_unref(iothread->worker_context);
134 iothread->worker_context = NULL;
Peter Xu0bd2d232019-03-06 19:55:30 +0800135 g_main_loop_unref(iothread->main_loop);
136 iothread->main_loop = NULL;
Peter Xu5b3ac232017-09-28 10:59:57 +0800137 }
Peter Xu21c4d152019-03-06 19:55:28 +0800138 qemu_sem_destroy(&iothread->init_done_sem);
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +0100139}
140
Fabiano Rosas1f14c912023-09-05 15:03:59 -0300141static void iothread_init_gcontext(IOThread *iothread, const char *thread_name)
Peter Xub506e0f2019-03-06 19:55:29 +0800142{
143 GSource *source;
Fabiano Rosas1f14c912023-09-05 15:03:59 -0300144 g_autofree char *name = g_strdup_printf("%s aio-context", thread_name);
Peter Xub506e0f2019-03-06 19:55:29 +0800145
146 iothread->worker_context = g_main_context_new();
147 source = aio_get_g_source(iothread_get_aio_context(iothread));
Fabiano Rosas1f14c912023-09-05 15:03:59 -0300148 g_source_set_name(source, name);
Peter Xub506e0f2019-03-06 19:55:29 +0800149 g_source_attach(source, iothread->worker_context);
150 g_source_unref(source);
Peter Xu0bd2d232019-03-06 19:55:30 +0800151 iothread->main_loop = g_main_loop_new(iothread->worker_context, TRUE);
Peter Xub506e0f2019-03-06 19:55:29 +0800152}
153
Nicolas Saenz Julienne7d5983e2022-04-25 09:57:21 +0200154static void iothread_set_aio_context_params(EventLoopBase *base, Error **errp)
Stefano Garzarella1793ad02021-07-21 11:42:10 +0200155{
156 ERRP_GUARD();
Markus Armbruster05e385d2022-11-21 09:50:47 +0100157 IOThread *iothread = IOTHREAD(base);
Stefano Garzarella1793ad02021-07-21 11:42:10 +0200158
Nicolas Saenz Julienne7d5983e2022-04-25 09:57:21 +0200159 if (!iothread->ctx) {
160 return;
161 }
162
Stefano Garzarella1793ad02021-07-21 11:42:10 +0200163 aio_context_set_poll_params(iothread->ctx,
164 iothread->poll_max_ns,
165 iothread->poll_grow,
166 iothread->poll_shrink,
167 errp);
168 if (*errp) {
169 return;
170 }
171
172 aio_context_set_aio_params(iothread->ctx,
Philippe Mathieu-Daudé897a06c2023-11-20 18:18:06 +0100173 iothread->parent_obj.aio_max_batch);
Nicolas Saenz Julienne71ad4712022-04-25 09:57:23 +0200174
175 aio_context_set_thread_pool_params(iothread->ctx, base->thread_pool_min,
176 base->thread_pool_max, errp);
Stefano Garzarella1793ad02021-07-21 11:42:10 +0200177}
178
Nicolas Saenz Julienne7d5983e2022-04-25 09:57:21 +0200179
180static void iothread_init(EventLoopBase *base, Error **errp)
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +0100181{
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300182 Error *local_error = NULL;
Nicolas Saenz Julienne7d5983e2022-04-25 09:57:21 +0200183 IOThread *iothread = IOTHREAD(base);
Fabiano Rosas1f14c912023-09-05 15:03:59 -0300184 g_autofree char *thread_name = NULL;
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +0100185
186 iothread->stopping = false;
Stefan Hajnoczi2362a282017-12-07 20:13:19 +0000187 iothread->running = true;
Markus Armbruster668f62e2020-07-07 18:06:02 +0200188 iothread->ctx = aio_context_new(errp);
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300189 if (!iothread->ctx) {
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300190 return;
191 }
Stefan Hajnoczi88eb7c22014-02-27 11:48:41 +0100192
Fabiano Rosas1f14c912023-09-05 15:03:59 -0300193 thread_name = g_strdup_printf("IO %s",
194 object_get_canonical_path_component(OBJECT(base)));
195
Peter Xub506e0f2019-03-06 19:55:29 +0800196 /*
197 * Init one GMainContext for the iothread unconditionally, even if
198 * it's not used
199 */
Fabiano Rosas1f14c912023-09-05 15:03:59 -0300200 iothread_init_gcontext(iothread, thread_name);
Peter Xub506e0f2019-03-06 19:55:29 +0800201
Nicolas Saenz Julienne7d5983e2022-04-25 09:57:21 +0200202 iothread_set_aio_context_params(base, &local_error);
Stefan Hajnoczi0d9d86f2016-12-01 19:26:45 +0000203 if (local_error) {
204 error_propagate(errp, local_error);
205 aio_context_unref(iothread->ctx);
206 iothread->ctx = NULL;
207 return;
208 }
209
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +0100210 /* This assumes we are called from a thread with useful CPU affinity for us
211 * to inherit.
212 */
Paolo Bonzinid21e8772015-11-24 14:46:44 +0100213 qemu_thread_create(&iothread->thread, thread_name, iothread_run,
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +0100214 iothread, QEMU_THREAD_JOINABLE);
Stefan Hajnoczi88eb7c22014-02-27 11:48:41 +0100215
216 /* Wait for initialization to complete */
Stefan Hajnoczi88eb7c22014-02-27 11:48:41 +0100217 while (iothread->thread_id == -1) {
Peter Xu21c4d152019-03-06 19:55:28 +0800218 qemu_sem_wait(&iothread->init_done_sem);
Stefan Hajnoczi88eb7c22014-02-27 11:48:41 +0100219 }
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +0100220}
221
Stefan Hajnoczi5e5db492016-12-01 19:26:52 +0000222typedef struct {
223 const char *name;
224 ptrdiff_t offset; /* field's byte offset in IOThread struct */
Stefano Garzarellaf0ed36a2021-07-27 16:59:35 +0200225} IOThreadParamInfo;
Stefan Hajnoczi5e5db492016-12-01 19:26:52 +0000226
Stefano Garzarellaf0ed36a2021-07-27 16:59:35 +0200227static IOThreadParamInfo poll_max_ns_info = {
Stefan Hajnoczi5e5db492016-12-01 19:26:52 +0000228 "poll-max-ns", offsetof(IOThread, poll_max_ns),
229};
Stefano Garzarellaf0ed36a2021-07-27 16:59:35 +0200230static IOThreadParamInfo poll_grow_info = {
Stefan Hajnoczi5e5db492016-12-01 19:26:52 +0000231 "poll-grow", offsetof(IOThread, poll_grow),
232};
Stefano Garzarellaf0ed36a2021-07-27 16:59:35 +0200233static IOThreadParamInfo poll_shrink_info = {
Stefan Hajnoczi5e5db492016-12-01 19:26:52 +0000234 "poll-shrink", offsetof(IOThread, poll_shrink),
235};
236
Stefano Garzarella04454092021-07-21 11:42:09 +0200237static void iothread_get_param(Object *obj, Visitor *v,
Stefano Garzarella1cc7ead2021-07-27 16:59:36 +0200238 const char *name, IOThreadParamInfo *info, Error **errp)
Stefan Hajnoczi0d9d86f2016-12-01 19:26:45 +0000239{
240 IOThread *iothread = IOTHREAD(obj);
Stefan Hajnoczi5e5db492016-12-01 19:26:52 +0000241 int64_t *field = (void *)iothread + info->offset;
Stefan Hajnoczi0d9d86f2016-12-01 19:26:45 +0000242
Stefan Hajnoczi5e5db492016-12-01 19:26:52 +0000243 visit_type_int64(v, name, field, errp);
Stefan Hajnoczi0d9d86f2016-12-01 19:26:45 +0000244}
245
Stefano Garzarella04454092021-07-21 11:42:09 +0200246static bool iothread_set_param(Object *obj, Visitor *v,
Stefano Garzarella1cc7ead2021-07-27 16:59:36 +0200247 const char *name, IOThreadParamInfo *info, Error **errp)
Stefan Hajnoczi0d9d86f2016-12-01 19:26:45 +0000248{
249 IOThread *iothread = IOTHREAD(obj);
Stefan Hajnoczi5e5db492016-12-01 19:26:52 +0000250 int64_t *field = (void *)iothread + info->offset;
Stefan Hajnoczi0d9d86f2016-12-01 19:26:45 +0000251 int64_t value;
252
Markus Armbruster668f62e2020-07-07 18:06:02 +0200253 if (!visit_type_int64(v, name, &value, errp)) {
Stefano Garzarella04454092021-07-21 11:42:09 +0200254 return false;
Stefan Hajnoczi0d9d86f2016-12-01 19:26:45 +0000255 }
256
257 if (value < 0) {
Markus Armbrusterdcfe4802020-07-07 18:06:01 +0200258 error_setg(errp, "%s value must be in range [0, %" PRId64 "]",
Stefan Hajnoczi5e5db492016-12-01 19:26:52 +0000259 info->name, INT64_MAX);
Stefano Garzarella04454092021-07-21 11:42:09 +0200260 return false;
Stefan Hajnoczi0d9d86f2016-12-01 19:26:45 +0000261 }
262
Stefan Hajnoczi5e5db492016-12-01 19:26:52 +0000263 *field = value;
Stefan Hajnoczi0d9d86f2016-12-01 19:26:45 +0000264
Stefano Garzarella04454092021-07-21 11:42:09 +0200265 return true;
266}
267
268static void iothread_get_poll_param(Object *obj, Visitor *v,
269 const char *name, void *opaque, Error **errp)
270{
Stefano Garzarella1cc7ead2021-07-27 16:59:36 +0200271 IOThreadParamInfo *info = opaque;
Stefano Garzarella04454092021-07-21 11:42:09 +0200272
Stefano Garzarella1cc7ead2021-07-27 16:59:36 +0200273 iothread_get_param(obj, v, name, info, errp);
Stefano Garzarella04454092021-07-21 11:42:09 +0200274}
275
276static void iothread_set_poll_param(Object *obj, Visitor *v,
277 const char *name, void *opaque, Error **errp)
278{
279 IOThread *iothread = IOTHREAD(obj);
Stefano Garzarella1cc7ead2021-07-27 16:59:36 +0200280 IOThreadParamInfo *info = opaque;
Stefano Garzarella04454092021-07-21 11:42:09 +0200281
Stefano Garzarella1cc7ead2021-07-27 16:59:36 +0200282 if (!iothread_set_param(obj, v, name, info, errp)) {
Stefano Garzarella04454092021-07-21 11:42:09 +0200283 return;
284 }
285
Stefan Hajnoczi0d9d86f2016-12-01 19:26:45 +0000286 if (iothread->ctx) {
Stefan Hajnoczi5e5db492016-12-01 19:26:52 +0000287 aio_context_set_poll_params(iothread->ctx,
288 iothread->poll_max_ns,
289 iothread->poll_grow,
290 iothread->poll_shrink,
Markus Armbrusterdcfe4802020-07-07 18:06:01 +0200291 errp);
Stefan Hajnoczi0d9d86f2016-12-01 19:26:45 +0000292 }
Stefan Hajnoczi0d9d86f2016-12-01 19:26:45 +0000293}
294
Philippe Mathieu-Daudé12d1a762025-02-09 23:47:35 +0100295static void iothread_class_init(ObjectClass *klass, const void *class_data)
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +0100296{
Nicolas Saenz Julienne7d5983e2022-04-25 09:57:21 +0200297 EventLoopBaseClass *bc = EVENT_LOOP_BASE_CLASS(klass);
298
299 bc->init = iothread_init;
300 bc->update_params = iothread_set_aio_context_params;
Stefan Hajnoczi0d9d86f2016-12-01 19:26:45 +0000301
302 object_class_property_add(klass, "poll-max-ns", "int",
Stefan Hajnoczi5e5db492016-12-01 19:26:52 +0000303 iothread_get_poll_param,
304 iothread_set_poll_param,
Markus Armbrusterd2623122020-05-05 17:29:22 +0200305 NULL, &poll_max_ns_info);
Stefan Hajnoczi5e5db492016-12-01 19:26:52 +0000306 object_class_property_add(klass, "poll-grow", "int",
307 iothread_get_poll_param,
308 iothread_set_poll_param,
Markus Armbrusterd2623122020-05-05 17:29:22 +0200309 NULL, &poll_grow_info);
Stefan Hajnoczi5e5db492016-12-01 19:26:52 +0000310 object_class_property_add(klass, "poll-shrink", "int",
311 iothread_get_poll_param,
312 iothread_set_poll_param,
Markus Armbrusterd2623122020-05-05 17:29:22 +0200313 NULL, &poll_shrink_info);
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +0100314}
315
316static const TypeInfo iothread_info = {
317 .name = TYPE_IOTHREAD,
Nicolas Saenz Julienne7d5983e2022-04-25 09:57:21 +0200318 .parent = TYPE_EVENT_LOOP_BASE,
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +0100319 .class_init = iothread_class_init,
320 .instance_size = sizeof(IOThread),
Stefan Hajnoczicdd7abf2017-01-26 17:01:19 +0000321 .instance_init = iothread_instance_init,
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +0100322 .instance_finalize = iothread_instance_finalize,
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +0100323};
324
325static void iothread_register_types(void)
326{
327 type_register_static(&iothread_info);
328}
329
330type_init(iothread_register_types)
331
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +0100332char *iothread_get_id(IOThread *iothread)
333{
Markus Armbruster7a309cc2020-07-14 18:02:00 +0200334 return g_strdup(object_get_canonical_path_component(OBJECT(iothread)));
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +0100335}
336
337AioContext *iothread_get_aio_context(IOThread *iothread)
338{
339 return iothread->ctx;
340}
Stefan Hajnoczidc3dd0d2014-02-27 11:48:42 +0100341
342static int query_one_iothread(Object *object, void *opaque)
343{
Eric Blakec3033fd2021-01-13 16:10:12 -0600344 IOThreadInfoList ***tail = opaque;
Stefan Hajnoczidc3dd0d2014-02-27 11:48:42 +0100345 IOThreadInfo *info;
346 IOThread *iothread;
347
348 iothread = (IOThread *)object_dynamic_cast(object, TYPE_IOTHREAD);
349 if (!iothread) {
350 return 0;
351 }
352
353 info = g_new0(IOThreadInfo, 1);
354 info->id = iothread_get_id(iothread);
355 info->thread_id = iothread->thread_id;
Pavel Hrdina5fc00482017-02-10 10:41:17 +0100356 info->poll_max_ns = iothread->poll_max_ns;
357 info->poll_grow = iothread->poll_grow;
358 info->poll_shrink = iothread->poll_shrink;
Nicolas Saenz Julienne7d5983e2022-04-25 09:57:21 +0200359 info->aio_max_batch = iothread->parent_obj.aio_max_batch;
Stefan Hajnoczidc3dd0d2014-02-27 11:48:42 +0100360
Eric Blakec3033fd2021-01-13 16:10:12 -0600361 QAPI_LIST_APPEND(*tail, info);
Stefan Hajnoczidc3dd0d2014-02-27 11:48:42 +0100362 return 0;
363}
364
365IOThreadInfoList *qmp_query_iothreads(Error **errp)
366{
367 IOThreadInfoList *head = NULL;
368 IOThreadInfoList **prev = &head;
Daniel P. Berrangebc2256c2015-05-13 17:14:05 +0100369 Object *container = object_get_objects_root();
Stefan Hajnoczidc3dd0d2014-02-27 11:48:42 +0100370
371 object_child_foreach(container, query_one_iothread, &prev);
372 return head;
373}
Fam Zhengdce89212016-09-08 17:28:51 +0800374
Wang Yong329163c2017-08-29 15:22:37 +0800375GMainContext *iothread_get_g_main_context(IOThread *iothread)
376{
Stefan Hajnoczid73415a2020-09-23 11:56:46 +0100377 qatomic_set(&iothread->run_gcontext, 1);
Peter Xub506e0f2019-03-06 19:55:29 +0800378 aio_notify(iothread->ctx);
Wang Yong329163c2017-08-29 15:22:37 +0800379 return iothread->worker_context;
380}
Peter Xu0173e212017-09-28 10:59:55 +0800381
382IOThread *iothread_create(const char *id, Error **errp)
383{
384 Object *obj;
385
386 obj = object_new_with_props(TYPE_IOTHREAD,
387 object_get_internal_root(),
388 id, errp, NULL);
389
390 return IOTHREAD(obj);
391}
392
393void iothread_destroy(IOThread *iothread)
394{
395 object_unparent(OBJECT(iothread));
396}
Stefan Hajnoczifbcc6922017-12-06 14:45:48 +0000397
398/* Lookup IOThread by its id. Only finds user-created objects, not internal
399 * iothread_create() objects. */
400IOThread *iothread_by_id(const char *id)
401{
402 return IOTHREAD(object_resolve_path_type(id, TYPE_IOTHREAD, NULL));
403}
Elena Ufimtsevaad22c302021-01-29 11:46:10 -0500404
405bool qemu_in_iothread(void)
406{
Kevin Wolf69422642024-02-08 11:16:57 +0100407 return qemu_get_current_aio_context() != qemu_get_aio_context();
Elena Ufimtsevaad22c302021-01-29 11:46:10 -0500408}