blob: b753286414a7ec2f3c4de0e20f1478ac35224091 [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"
Nicolas Saenz Julienne7d5983e2022-04-25 09:57:21 +020020#include "sysemu/event-loop-base.h"
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +010021#include "sysemu/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,
Nicolas Saenz Julienne7d5983e2022-04-25 09:57:21 +0200173 iothread->parent_obj.aio_max_batch,
Stefano Garzarella1793ad02021-07-21 11:42:10 +0200174 errp);
Nicolas Saenz Julienne71ad4712022-04-25 09:57:23 +0200175
176 aio_context_set_thread_pool_params(iothread->ctx, base->thread_pool_min,
177 base->thread_pool_max, errp);
Stefano Garzarella1793ad02021-07-21 11:42:10 +0200178}
179
Nicolas Saenz Julienne7d5983e2022-04-25 09:57:21 +0200180
181static void iothread_init(EventLoopBase *base, Error **errp)
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +0100182{
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300183 Error *local_error = NULL;
Nicolas Saenz Julienne7d5983e2022-04-25 09:57:21 +0200184 IOThread *iothread = IOTHREAD(base);
Fabiano Rosas1f14c912023-09-05 15:03:59 -0300185 g_autofree char *thread_name = NULL;
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +0100186
187 iothread->stopping = false;
Stefan Hajnoczi2362a282017-12-07 20:13:19 +0000188 iothread->running = true;
Markus Armbruster668f62e2020-07-07 18:06:02 +0200189 iothread->ctx = aio_context_new(errp);
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300190 if (!iothread->ctx) {
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300191 return;
192 }
Stefan Hajnoczi88eb7c22014-02-27 11:48:41 +0100193
Fabiano Rosas1f14c912023-09-05 15:03:59 -0300194 thread_name = g_strdup_printf("IO %s",
195 object_get_canonical_path_component(OBJECT(base)));
196
Peter Xub506e0f2019-03-06 19:55:29 +0800197 /*
198 * Init one GMainContext for the iothread unconditionally, even if
199 * it's not used
200 */
Fabiano Rosas1f14c912023-09-05 15:03:59 -0300201 iothread_init_gcontext(iothread, thread_name);
Peter Xub506e0f2019-03-06 19:55:29 +0800202
Nicolas Saenz Julienne7d5983e2022-04-25 09:57:21 +0200203 iothread_set_aio_context_params(base, &local_error);
Stefan Hajnoczi0d9d86f2016-12-01 19:26:45 +0000204 if (local_error) {
205 error_propagate(errp, local_error);
206 aio_context_unref(iothread->ctx);
207 iothread->ctx = NULL;
208 return;
209 }
210
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +0100211 /* This assumes we are called from a thread with useful CPU affinity for us
212 * to inherit.
213 */
Paolo Bonzinid21e8772015-11-24 14:46:44 +0100214 qemu_thread_create(&iothread->thread, thread_name, iothread_run,
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +0100215 iothread, QEMU_THREAD_JOINABLE);
Stefan Hajnoczi88eb7c22014-02-27 11:48:41 +0100216
217 /* Wait for initialization to complete */
Stefan Hajnoczi88eb7c22014-02-27 11:48:41 +0100218 while (iothread->thread_id == -1) {
Peter Xu21c4d152019-03-06 19:55:28 +0800219 qemu_sem_wait(&iothread->init_done_sem);
Stefan Hajnoczi88eb7c22014-02-27 11:48:41 +0100220 }
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +0100221}
222
Stefan Hajnoczi5e5db492016-12-01 19:26:52 +0000223typedef struct {
224 const char *name;
225 ptrdiff_t offset; /* field's byte offset in IOThread struct */
Stefano Garzarellaf0ed36a2021-07-27 16:59:35 +0200226} IOThreadParamInfo;
Stefan Hajnoczi5e5db492016-12-01 19:26:52 +0000227
Stefano Garzarellaf0ed36a2021-07-27 16:59:35 +0200228static IOThreadParamInfo poll_max_ns_info = {
Stefan Hajnoczi5e5db492016-12-01 19:26:52 +0000229 "poll-max-ns", offsetof(IOThread, poll_max_ns),
230};
Stefano Garzarellaf0ed36a2021-07-27 16:59:35 +0200231static IOThreadParamInfo poll_grow_info = {
Stefan Hajnoczi5e5db492016-12-01 19:26:52 +0000232 "poll-grow", offsetof(IOThread, poll_grow),
233};
Stefano Garzarellaf0ed36a2021-07-27 16:59:35 +0200234static IOThreadParamInfo poll_shrink_info = {
Stefan Hajnoczi5e5db492016-12-01 19:26:52 +0000235 "poll-shrink", offsetof(IOThread, poll_shrink),
236};
237
Stefano Garzarella04454092021-07-21 11:42:09 +0200238static void iothread_get_param(Object *obj, Visitor *v,
Stefano Garzarella1cc7ead2021-07-27 16:59:36 +0200239 const char *name, IOThreadParamInfo *info, Error **errp)
Stefan Hajnoczi0d9d86f2016-12-01 19:26:45 +0000240{
241 IOThread *iothread = IOTHREAD(obj);
Stefan Hajnoczi5e5db492016-12-01 19:26:52 +0000242 int64_t *field = (void *)iothread + info->offset;
Stefan Hajnoczi0d9d86f2016-12-01 19:26:45 +0000243
Stefan Hajnoczi5e5db492016-12-01 19:26:52 +0000244 visit_type_int64(v, name, field, errp);
Stefan Hajnoczi0d9d86f2016-12-01 19:26:45 +0000245}
246
Stefano Garzarella04454092021-07-21 11:42:09 +0200247static bool iothread_set_param(Object *obj, Visitor *v,
Stefano Garzarella1cc7ead2021-07-27 16:59:36 +0200248 const char *name, IOThreadParamInfo *info, Error **errp)
Stefan Hajnoczi0d9d86f2016-12-01 19:26:45 +0000249{
250 IOThread *iothread = IOTHREAD(obj);
Stefan Hajnoczi5e5db492016-12-01 19:26:52 +0000251 int64_t *field = (void *)iothread + info->offset;
Stefan Hajnoczi0d9d86f2016-12-01 19:26:45 +0000252 int64_t value;
253
Markus Armbruster668f62e2020-07-07 18:06:02 +0200254 if (!visit_type_int64(v, name, &value, errp)) {
Stefano Garzarella04454092021-07-21 11:42:09 +0200255 return false;
Stefan Hajnoczi0d9d86f2016-12-01 19:26:45 +0000256 }
257
258 if (value < 0) {
Markus Armbrusterdcfe4802020-07-07 18:06:01 +0200259 error_setg(errp, "%s value must be in range [0, %" PRId64 "]",
Stefan Hajnoczi5e5db492016-12-01 19:26:52 +0000260 info->name, INT64_MAX);
Stefano Garzarella04454092021-07-21 11:42:09 +0200261 return false;
Stefan Hajnoczi0d9d86f2016-12-01 19:26:45 +0000262 }
263
Stefan Hajnoczi5e5db492016-12-01 19:26:52 +0000264 *field = value;
Stefan Hajnoczi0d9d86f2016-12-01 19:26:45 +0000265
Stefano Garzarella04454092021-07-21 11:42:09 +0200266 return true;
267}
268
269static void iothread_get_poll_param(Object *obj, Visitor *v,
270 const char *name, void *opaque, Error **errp)
271{
Stefano Garzarella1cc7ead2021-07-27 16:59:36 +0200272 IOThreadParamInfo *info = opaque;
Stefano Garzarella04454092021-07-21 11:42:09 +0200273
Stefano Garzarella1cc7ead2021-07-27 16:59:36 +0200274 iothread_get_param(obj, v, name, info, errp);
Stefano Garzarella04454092021-07-21 11:42:09 +0200275}
276
277static void iothread_set_poll_param(Object *obj, Visitor *v,
278 const char *name, void *opaque, Error **errp)
279{
280 IOThread *iothread = IOTHREAD(obj);
Stefano Garzarella1cc7ead2021-07-27 16:59:36 +0200281 IOThreadParamInfo *info = opaque;
Stefano Garzarella04454092021-07-21 11:42:09 +0200282
Stefano Garzarella1cc7ead2021-07-27 16:59:36 +0200283 if (!iothread_set_param(obj, v, name, info, errp)) {
Stefano Garzarella04454092021-07-21 11:42:09 +0200284 return;
285 }
286
Stefan Hajnoczi0d9d86f2016-12-01 19:26:45 +0000287 if (iothread->ctx) {
Stefan Hajnoczi5e5db492016-12-01 19:26:52 +0000288 aio_context_set_poll_params(iothread->ctx,
289 iothread->poll_max_ns,
290 iothread->poll_grow,
291 iothread->poll_shrink,
Markus Armbrusterdcfe4802020-07-07 18:06:01 +0200292 errp);
Stefan Hajnoczi0d9d86f2016-12-01 19:26:45 +0000293 }
Stefan Hajnoczi0d9d86f2016-12-01 19:26:45 +0000294}
295
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +0100296static void iothread_class_init(ObjectClass *klass, void *class_data)
297{
Nicolas Saenz Julienne7d5983e2022-04-25 09:57:21 +0200298 EventLoopBaseClass *bc = EVENT_LOOP_BASE_CLASS(klass);
299
300 bc->init = iothread_init;
301 bc->update_params = iothread_set_aio_context_params;
Stefan Hajnoczi0d9d86f2016-12-01 19:26:45 +0000302
303 object_class_property_add(klass, "poll-max-ns", "int",
Stefan Hajnoczi5e5db492016-12-01 19:26:52 +0000304 iothread_get_poll_param,
305 iothread_set_poll_param,
Markus Armbrusterd2623122020-05-05 17:29:22 +0200306 NULL, &poll_max_ns_info);
Stefan Hajnoczi5e5db492016-12-01 19:26:52 +0000307 object_class_property_add(klass, "poll-grow", "int",
308 iothread_get_poll_param,
309 iothread_set_poll_param,
Markus Armbrusterd2623122020-05-05 17:29:22 +0200310 NULL, &poll_grow_info);
Stefan Hajnoczi5e5db492016-12-01 19:26:52 +0000311 object_class_property_add(klass, "poll-shrink", "int",
312 iothread_get_poll_param,
313 iothread_set_poll_param,
Markus Armbrusterd2623122020-05-05 17:29:22 +0200314 NULL, &poll_shrink_info);
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +0100315}
316
317static const TypeInfo iothread_info = {
318 .name = TYPE_IOTHREAD,
Nicolas Saenz Julienne7d5983e2022-04-25 09:57:21 +0200319 .parent = TYPE_EVENT_LOOP_BASE,
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +0100320 .class_init = iothread_class_init,
321 .instance_size = sizeof(IOThread),
Stefan Hajnoczicdd7abf2017-01-26 17:01:19 +0000322 .instance_init = iothread_instance_init,
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +0100323 .instance_finalize = iothread_instance_finalize,
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +0100324};
325
326static void iothread_register_types(void)
327{
328 type_register_static(&iothread_info);
329}
330
331type_init(iothread_register_types)
332
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +0100333char *iothread_get_id(IOThread *iothread)
334{
Markus Armbruster7a309cc2020-07-14 18:02:00 +0200335 return g_strdup(object_get_canonical_path_component(OBJECT(iothread)));
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +0100336}
337
338AioContext *iothread_get_aio_context(IOThread *iothread)
339{
340 return iothread->ctx;
341}
Stefan Hajnoczidc3dd0d2014-02-27 11:48:42 +0100342
343static int query_one_iothread(Object *object, void *opaque)
344{
Eric Blakec3033fd2021-01-13 16:10:12 -0600345 IOThreadInfoList ***tail = opaque;
Stefan Hajnoczidc3dd0d2014-02-27 11:48:42 +0100346 IOThreadInfo *info;
347 IOThread *iothread;
348
349 iothread = (IOThread *)object_dynamic_cast(object, TYPE_IOTHREAD);
350 if (!iothread) {
351 return 0;
352 }
353
354 info = g_new0(IOThreadInfo, 1);
355 info->id = iothread_get_id(iothread);
356 info->thread_id = iothread->thread_id;
Pavel Hrdina5fc00482017-02-10 10:41:17 +0100357 info->poll_max_ns = iothread->poll_max_ns;
358 info->poll_grow = iothread->poll_grow;
359 info->poll_shrink = iothread->poll_shrink;
Nicolas Saenz Julienne7d5983e2022-04-25 09:57:21 +0200360 info->aio_max_batch = iothread->parent_obj.aio_max_batch;
Stefan Hajnoczidc3dd0d2014-02-27 11:48:42 +0100361
Eric Blakec3033fd2021-01-13 16:10:12 -0600362 QAPI_LIST_APPEND(*tail, info);
Stefan Hajnoczidc3dd0d2014-02-27 11:48:42 +0100363 return 0;
364}
365
366IOThreadInfoList *qmp_query_iothreads(Error **errp)
367{
368 IOThreadInfoList *head = NULL;
369 IOThreadInfoList **prev = &head;
Daniel P. Berrangebc2256c2015-05-13 17:14:05 +0100370 Object *container = object_get_objects_root();
Stefan Hajnoczidc3dd0d2014-02-27 11:48:42 +0100371
372 object_child_foreach(container, query_one_iothread, &prev);
373 return head;
374}
Fam Zhengdce89212016-09-08 17:28:51 +0800375
Wang Yong329163c2017-08-29 15:22:37 +0800376GMainContext *iothread_get_g_main_context(IOThread *iothread)
377{
Stefan Hajnoczid73415a2020-09-23 11:56:46 +0100378 qatomic_set(&iothread->run_gcontext, 1);
Peter Xub506e0f2019-03-06 19:55:29 +0800379 aio_notify(iothread->ctx);
Wang Yong329163c2017-08-29 15:22:37 +0800380 return iothread->worker_context;
381}
Peter Xu0173e212017-09-28 10:59:55 +0800382
383IOThread *iothread_create(const char *id, Error **errp)
384{
385 Object *obj;
386
387 obj = object_new_with_props(TYPE_IOTHREAD,
388 object_get_internal_root(),
389 id, errp, NULL);
390
391 return IOTHREAD(obj);
392}
393
394void iothread_destroy(IOThread *iothread)
395{
396 object_unparent(OBJECT(iothread));
397}
Stefan Hajnoczifbcc6922017-12-06 14:45:48 +0000398
399/* Lookup IOThread by its id. Only finds user-created objects, not internal
400 * iothread_create() objects. */
401IOThread *iothread_by_id(const char *id)
402{
403 return IOTHREAD(object_resolve_path_type(id, TYPE_IOTHREAD, NULL));
404}
Elena Ufimtsevaad22c302021-01-29 11:46:10 -0500405
406bool qemu_in_iothread(void)
407{
408 return qemu_get_current_aio_context() == qemu_get_aio_context() ?
409 false : true;
410}