blob: 529194a566d3ced2f7902f6541c2007ab69512e7 [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 +010028typedef ObjectClass IOThreadClass;
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +010029
Eduardo Habkost8110fa12020-08-31 17:07:33 -040030DECLARE_CLASS_CHECKERS(IOThreadClass, IOTHREAD,
31 TYPE_IOTHREAD)
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +010032
Peter Xu90c558b2018-03-22 16:56:30 +080033#ifdef CONFIG_POSIX
Stefan Hajnoczicdd7abf2017-01-26 17:01:19 +000034/* Benchmark results from 2016 on NVMe SSD drives show max polling times around
35 * 16-32 microseconds yield IOPS improvements for both iodepth=1 and iodepth=32
36 * workloads.
37 */
38#define IOTHREAD_POLL_MAX_NS_DEFAULT 32768ULL
Peter Xu90c558b2018-03-22 16:56:30 +080039#else
40#define IOTHREAD_POLL_MAX_NS_DEFAULT 0ULL
41#endif
Stefan Hajnoczicdd7abf2017-01-26 17:01:19 +000042
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +010043static void *iothread_run(void *opaque)
44{
45 IOThread *iothread = opaque;
46
Paolo Bonziniab28bd22015-07-09 08:55:38 +020047 rcu_register_thread();
Peter Xub60ec762019-03-06 19:55:31 +080048 /*
49 * g_main_context_push_thread_default() must be called before anything
50 * in this new thread uses glib.
51 */
52 g_main_context_push_thread_default(iothread->worker_context);
Paolo Bonzini5f50be92021-06-09 14:22:34 +020053 qemu_set_current_aio_context(iothread->ctx);
Stefan Hajnoczi88eb7c22014-02-27 11:48:41 +010054 iothread->thread_id = qemu_get_thread_id();
Peter Xu21c4d152019-03-06 19:55:28 +080055 qemu_sem_post(&iothread->init_done_sem);
Stefan Hajnoczi88eb7c22014-02-27 11:48:41 +010056
Stefan Hajnoczi2362a282017-12-07 20:13:19 +000057 while (iothread->running) {
Peter Xu6ca20622019-03-06 19:55:32 +080058 /*
59 * Note: from functional-wise the g_main_loop_run() below can
60 * already cover the aio_poll() events, but we can't run the
61 * main loop unconditionally because explicit aio_poll() here
62 * is faster than g_main_loop_run() when we do not need the
63 * gcontext at all (e.g., pure block layer iothreads). In
64 * other words, when we want to run the gcontext with the
65 * iothread we need to pay some performance for functionality.
66 */
Paolo Bonzini65c1b5b2016-10-27 12:49:06 +020067 aio_poll(iothread->ctx, true);
Wang Yong329163c2017-08-29 15:22:37 +080068
Peter Xu6c953632019-01-29 13:14:32 +080069 /*
70 * We must check the running state again in case it was
71 * changed in previous aio_poll()
72 */
Stefan Hajnoczid73415a2020-09-23 11:56:46 +010073 if (iothread->running && qatomic_read(&iothread->run_gcontext)) {
Wang Yong329163c2017-08-29 15:22:37 +080074 g_main_loop_run(iothread->main_loop);
Wang Yong329163c2017-08-29 15:22:37 +080075 }
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +010076 }
Paolo Bonziniab28bd22015-07-09 08:55:38 +020077
Peter Xub60ec762019-03-06 19:55:31 +080078 g_main_context_pop_thread_default(iothread->worker_context);
Paolo Bonziniab28bd22015-07-09 08:55:38 +020079 rcu_unregister_thread();
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +010080 return NULL;
81}
82
Stefan Hajnoczi2362a282017-12-07 20:13:19 +000083/* Runs in iothread_run() thread */
84static void iothread_stop_bh(void *opaque)
85{
86 IOThread *iothread = opaque;
87
88 iothread->running = false; /* stop iothread_run() */
89
90 if (iothread->main_loop) {
91 g_main_loop_quit(iothread->main_loop);
92 }
93}
94
Peter Xu82d90702017-09-28 10:59:56 +080095void iothread_stop(IOThread *iothread)
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +010096{
Peter Xu82d90702017-09-28 10:59:56 +080097 if (!iothread->ctx || iothread->stopping) {
98 return;
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +030099 }
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +0100100 iothread->stopping = true;
Stefan Hajnoczi2362a282017-12-07 20:13:19 +0000101 aio_bh_schedule_oneshot(iothread->ctx, iothread_stop_bh, iothread);
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +0100102 qemu_thread_join(&iothread->thread);
Peter Xu82d90702017-09-28 10:59:56 +0800103}
104
Stefan Hajnoczicdd7abf2017-01-26 17:01:19 +0000105static void iothread_instance_init(Object *obj)
106{
107 IOThread *iothread = IOTHREAD(obj);
108
109 iothread->poll_max_ns = IOTHREAD_POLL_MAX_NS_DEFAULT;
Marc-André Lureau14a2d112018-08-21 12:07:16 +0200110 iothread->thread_id = -1;
Peter Xu21c4d152019-03-06 19:55:28 +0800111 qemu_sem_init(&iothread->init_done_sem, 0);
Peter Xub506e0f2019-03-06 19:55:29 +0800112 /* By default, we don't run gcontext */
Stefan Hajnoczid73415a2020-09-23 11:56:46 +0100113 qatomic_set(&iothread->run_gcontext, 0);
Stefan Hajnoczicdd7abf2017-01-26 17:01:19 +0000114}
115
Fam Zhengdce89212016-09-08 17:28:51 +0800116static void iothread_instance_finalize(Object *obj)
117{
118 IOThread *iothread = IOTHREAD(obj);
119
Peter Xu82d90702017-09-28 10:59:56 +0800120 iothread_stop(iothread);
Marc-André Lureau14a2d112018-08-21 12:07:16 +0200121
Peter Xu15544342018-04-09 16:39:56 +0800122 /*
123 * Before glib2 2.33.10, there is a glib2 bug that GSource context
124 * pointer may not be cleared even if the context has already been
125 * destroyed (while it should). Here let's free the AIO context
126 * earlier to bypass that glib bug.
127 *
128 * We can remove this comment after the minimum supported glib2
129 * version boosts to 2.33.10. Before that, let's free the
130 * GSources first before destroying any GMainContext.
131 */
132 if (iothread->ctx) {
133 aio_context_unref(iothread->ctx);
134 iothread->ctx = NULL;
135 }
Peter Xu5b3ac232017-09-28 10:59:57 +0800136 if (iothread->worker_context) {
137 g_main_context_unref(iothread->worker_context);
138 iothread->worker_context = NULL;
Peter Xu0bd2d232019-03-06 19:55:30 +0800139 g_main_loop_unref(iothread->main_loop);
140 iothread->main_loop = NULL;
Peter Xu5b3ac232017-09-28 10:59:57 +0800141 }
Peter Xu21c4d152019-03-06 19:55:28 +0800142 qemu_sem_destroy(&iothread->init_done_sem);
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +0100143}
144
Peter Xub506e0f2019-03-06 19:55:29 +0800145static void iothread_init_gcontext(IOThread *iothread)
146{
147 GSource *source;
148
149 iothread->worker_context = g_main_context_new();
150 source = aio_get_g_source(iothread_get_aio_context(iothread));
151 g_source_attach(source, iothread->worker_context);
152 g_source_unref(source);
Peter Xu0bd2d232019-03-06 19:55:30 +0800153 iothread->main_loop = g_main_loop_new(iothread->worker_context, TRUE);
Peter Xub506e0f2019-03-06 19:55:29 +0800154}
155
Nicolas Saenz Julienne7d5983e2022-04-25 09:57:21 +0200156static void iothread_set_aio_context_params(EventLoopBase *base, Error **errp)
Stefano Garzarella1793ad02021-07-21 11:42:10 +0200157{
Nicolas Saenz Julienne7d5983e2022-04-25 09:57:21 +0200158 IOThread *iothread = IOTHREAD(base);
Stefano Garzarella1793ad02021-07-21 11:42:10 +0200159 ERRP_GUARD();
160
Nicolas Saenz Julienne7d5983e2022-04-25 09:57:21 +0200161 if (!iothread->ctx) {
162 return;
163 }
164
Stefano Garzarella1793ad02021-07-21 11:42:10 +0200165 aio_context_set_poll_params(iothread->ctx,
166 iothread->poll_max_ns,
167 iothread->poll_grow,
168 iothread->poll_shrink,
169 errp);
170 if (*errp) {
171 return;
172 }
173
174 aio_context_set_aio_params(iothread->ctx,
Nicolas Saenz Julienne7d5983e2022-04-25 09:57:21 +0200175 iothread->parent_obj.aio_max_batch,
Stefano Garzarella1793ad02021-07-21 11:42:10 +0200176 errp);
Nicolas Saenz Julienne71ad4712022-04-25 09:57:23 +0200177
178 aio_context_set_thread_pool_params(iothread->ctx, base->thread_pool_min,
179 base->thread_pool_max, errp);
Stefano Garzarella1793ad02021-07-21 11:42:10 +0200180}
181
Nicolas Saenz Julienne7d5983e2022-04-25 09:57:21 +0200182
183static void iothread_init(EventLoopBase *base, Error **errp)
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +0100184{
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300185 Error *local_error = NULL;
Nicolas Saenz Julienne7d5983e2022-04-25 09:57:21 +0200186 IOThread *iothread = IOTHREAD(base);
Markus Armbruster7a309cc2020-07-14 18:02:00 +0200187 char *thread_name;
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +0100188
189 iothread->stopping = false;
Stefan Hajnoczi2362a282017-12-07 20:13:19 +0000190 iothread->running = true;
Markus Armbruster668f62e2020-07-07 18:06:02 +0200191 iothread->ctx = aio_context_new(errp);
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300192 if (!iothread->ctx) {
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300193 return;
194 }
Stefan Hajnoczi88eb7c22014-02-27 11:48:41 +0100195
Peter Xub506e0f2019-03-06 19:55:29 +0800196 /*
197 * Init one GMainContext for the iothread unconditionally, even if
198 * it's not used
199 */
200 iothread_init_gcontext(iothread);
201
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 */
Markus Armbruster7a309cc2020-07-14 18:02:00 +0200213 thread_name = g_strdup_printf("IO %s",
Nicolas Saenz Julienne7d5983e2022-04-25 09:57:21 +0200214 object_get_canonical_path_component(OBJECT(base)));
Paolo Bonzinid21e8772015-11-24 14:46:44 +0100215 qemu_thread_create(&iothread->thread, thread_name, iothread_run,
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +0100216 iothread, QEMU_THREAD_JOINABLE);
Paolo Bonzinid21e8772015-11-24 14:46:44 +0100217 g_free(thread_name);
Stefan Hajnoczi88eb7c22014-02-27 11:48:41 +0100218
219 /* Wait for initialization to complete */
Stefan Hajnoczi88eb7c22014-02-27 11:48:41 +0100220 while (iothread->thread_id == -1) {
Peter Xu21c4d152019-03-06 19:55:28 +0800221 qemu_sem_wait(&iothread->init_done_sem);
Stefan Hajnoczi88eb7c22014-02-27 11:48:41 +0100222 }
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +0100223}
224
Stefan Hajnoczi5e5db492016-12-01 19:26:52 +0000225typedef struct {
226 const char *name;
227 ptrdiff_t offset; /* field's byte offset in IOThread struct */
Stefano Garzarellaf0ed36a2021-07-27 16:59:35 +0200228} IOThreadParamInfo;
Stefan Hajnoczi5e5db492016-12-01 19:26:52 +0000229
Stefano Garzarellaf0ed36a2021-07-27 16:59:35 +0200230static IOThreadParamInfo poll_max_ns_info = {
Stefan Hajnoczi5e5db492016-12-01 19:26:52 +0000231 "poll-max-ns", offsetof(IOThread, poll_max_ns),
232};
Stefano Garzarellaf0ed36a2021-07-27 16:59:35 +0200233static IOThreadParamInfo poll_grow_info = {
Stefan Hajnoczi5e5db492016-12-01 19:26:52 +0000234 "poll-grow", offsetof(IOThread, poll_grow),
235};
Stefano Garzarellaf0ed36a2021-07-27 16:59:35 +0200236static IOThreadParamInfo poll_shrink_info = {
Stefan Hajnoczi5e5db492016-12-01 19:26:52 +0000237 "poll-shrink", offsetof(IOThread, poll_shrink),
238};
239
Stefano Garzarella04454092021-07-21 11:42:09 +0200240static void iothread_get_param(Object *obj, Visitor *v,
Stefano Garzarella1cc7ead2021-07-27 16:59:36 +0200241 const char *name, IOThreadParamInfo *info, Error **errp)
Stefan Hajnoczi0d9d86f2016-12-01 19:26:45 +0000242{
243 IOThread *iothread = IOTHREAD(obj);
Stefan Hajnoczi5e5db492016-12-01 19:26:52 +0000244 int64_t *field = (void *)iothread + info->offset;
Stefan Hajnoczi0d9d86f2016-12-01 19:26:45 +0000245
Stefan Hajnoczi5e5db492016-12-01 19:26:52 +0000246 visit_type_int64(v, name, field, errp);
Stefan Hajnoczi0d9d86f2016-12-01 19:26:45 +0000247}
248
Stefano Garzarella04454092021-07-21 11:42:09 +0200249static bool iothread_set_param(Object *obj, Visitor *v,
Stefano Garzarella1cc7ead2021-07-27 16:59:36 +0200250 const char *name, IOThreadParamInfo *info, Error **errp)
Stefan Hajnoczi0d9d86f2016-12-01 19:26:45 +0000251{
252 IOThread *iothread = IOTHREAD(obj);
Stefan Hajnoczi5e5db492016-12-01 19:26:52 +0000253 int64_t *field = (void *)iothread + info->offset;
Stefan Hajnoczi0d9d86f2016-12-01 19:26:45 +0000254 int64_t value;
255
Markus Armbruster668f62e2020-07-07 18:06:02 +0200256 if (!visit_type_int64(v, name, &value, errp)) {
Stefano Garzarella04454092021-07-21 11:42:09 +0200257 return false;
Stefan Hajnoczi0d9d86f2016-12-01 19:26:45 +0000258 }
259
260 if (value < 0) {
Markus Armbrusterdcfe4802020-07-07 18:06:01 +0200261 error_setg(errp, "%s value must be in range [0, %" PRId64 "]",
Stefan Hajnoczi5e5db492016-12-01 19:26:52 +0000262 info->name, INT64_MAX);
Stefano Garzarella04454092021-07-21 11:42:09 +0200263 return false;
Stefan Hajnoczi0d9d86f2016-12-01 19:26:45 +0000264 }
265
Stefan Hajnoczi5e5db492016-12-01 19:26:52 +0000266 *field = value;
Stefan Hajnoczi0d9d86f2016-12-01 19:26:45 +0000267
Stefano Garzarella04454092021-07-21 11:42:09 +0200268 return true;
269}
270
271static void iothread_get_poll_param(Object *obj, Visitor *v,
272 const char *name, void *opaque, Error **errp)
273{
Stefano Garzarella1cc7ead2021-07-27 16:59:36 +0200274 IOThreadParamInfo *info = opaque;
Stefano Garzarella04454092021-07-21 11:42:09 +0200275
Stefano Garzarella1cc7ead2021-07-27 16:59:36 +0200276 iothread_get_param(obj, v, name, info, errp);
Stefano Garzarella04454092021-07-21 11:42:09 +0200277}
278
279static void iothread_set_poll_param(Object *obj, Visitor *v,
280 const char *name, void *opaque, Error **errp)
281{
282 IOThread *iothread = IOTHREAD(obj);
Stefano Garzarella1cc7ead2021-07-27 16:59:36 +0200283 IOThreadParamInfo *info = opaque;
Stefano Garzarella04454092021-07-21 11:42:09 +0200284
Stefano Garzarella1cc7ead2021-07-27 16:59:36 +0200285 if (!iothread_set_param(obj, v, name, info, errp)) {
Stefano Garzarella04454092021-07-21 11:42:09 +0200286 return;
287 }
288
Stefan Hajnoczi0d9d86f2016-12-01 19:26:45 +0000289 if (iothread->ctx) {
Stefan Hajnoczi5e5db492016-12-01 19:26:52 +0000290 aio_context_set_poll_params(iothread->ctx,
291 iothread->poll_max_ns,
292 iothread->poll_grow,
293 iothread->poll_shrink,
Markus Armbrusterdcfe4802020-07-07 18:06:01 +0200294 errp);
Stefan Hajnoczi0d9d86f2016-12-01 19:26:45 +0000295 }
Stefan Hajnoczi0d9d86f2016-12-01 19:26:45 +0000296}
297
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +0100298static void iothread_class_init(ObjectClass *klass, void *class_data)
299{
Nicolas Saenz Julienne7d5983e2022-04-25 09:57:21 +0200300 EventLoopBaseClass *bc = EVENT_LOOP_BASE_CLASS(klass);
301
302 bc->init = iothread_init;
303 bc->update_params = iothread_set_aio_context_params;
Stefan Hajnoczi0d9d86f2016-12-01 19:26:45 +0000304
305 object_class_property_add(klass, "poll-max-ns", "int",
Stefan Hajnoczi5e5db492016-12-01 19:26:52 +0000306 iothread_get_poll_param,
307 iothread_set_poll_param,
Markus Armbrusterd2623122020-05-05 17:29:22 +0200308 NULL, &poll_max_ns_info);
Stefan Hajnoczi5e5db492016-12-01 19:26:52 +0000309 object_class_property_add(klass, "poll-grow", "int",
310 iothread_get_poll_param,
311 iothread_set_poll_param,
Markus Armbrusterd2623122020-05-05 17:29:22 +0200312 NULL, &poll_grow_info);
Stefan Hajnoczi5e5db492016-12-01 19:26:52 +0000313 object_class_property_add(klass, "poll-shrink", "int",
314 iothread_get_poll_param,
315 iothread_set_poll_param,
Markus Armbrusterd2623122020-05-05 17:29:22 +0200316 NULL, &poll_shrink_info);
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +0100317}
318
319static const TypeInfo iothread_info = {
320 .name = TYPE_IOTHREAD,
Nicolas Saenz Julienne7d5983e2022-04-25 09:57:21 +0200321 .parent = TYPE_EVENT_LOOP_BASE,
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +0100322 .class_init = iothread_class_init,
323 .instance_size = sizeof(IOThread),
Stefan Hajnoczicdd7abf2017-01-26 17:01:19 +0000324 .instance_init = iothread_instance_init,
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +0100325 .instance_finalize = iothread_instance_finalize,
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +0100326};
327
328static void iothread_register_types(void)
329{
330 type_register_static(&iothread_info);
331}
332
333type_init(iothread_register_types)
334
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +0100335char *iothread_get_id(IOThread *iothread)
336{
Markus Armbruster7a309cc2020-07-14 18:02:00 +0200337 return g_strdup(object_get_canonical_path_component(OBJECT(iothread)));
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +0100338}
339
340AioContext *iothread_get_aio_context(IOThread *iothread)
341{
342 return iothread->ctx;
343}
Stefan Hajnoczidc3dd0d2014-02-27 11:48:42 +0100344
345static int query_one_iothread(Object *object, void *opaque)
346{
Eric Blakec3033fd2021-01-13 16:10:12 -0600347 IOThreadInfoList ***tail = opaque;
Stefan Hajnoczidc3dd0d2014-02-27 11:48:42 +0100348 IOThreadInfo *info;
349 IOThread *iothread;
350
351 iothread = (IOThread *)object_dynamic_cast(object, TYPE_IOTHREAD);
352 if (!iothread) {
353 return 0;
354 }
355
356 info = g_new0(IOThreadInfo, 1);
357 info->id = iothread_get_id(iothread);
358 info->thread_id = iothread->thread_id;
Pavel Hrdina5fc00482017-02-10 10:41:17 +0100359 info->poll_max_ns = iothread->poll_max_ns;
360 info->poll_grow = iothread->poll_grow;
361 info->poll_shrink = iothread->poll_shrink;
Nicolas Saenz Julienne7d5983e2022-04-25 09:57:21 +0200362 info->aio_max_batch = iothread->parent_obj.aio_max_batch;
Stefan Hajnoczidc3dd0d2014-02-27 11:48:42 +0100363
Eric Blakec3033fd2021-01-13 16:10:12 -0600364 QAPI_LIST_APPEND(*tail, info);
Stefan Hajnoczidc3dd0d2014-02-27 11:48:42 +0100365 return 0;
366}
367
368IOThreadInfoList *qmp_query_iothreads(Error **errp)
369{
370 IOThreadInfoList *head = NULL;
371 IOThreadInfoList **prev = &head;
Daniel P. Berrangebc2256c2015-05-13 17:14:05 +0100372 Object *container = object_get_objects_root();
Stefan Hajnoczidc3dd0d2014-02-27 11:48:42 +0100373
374 object_child_foreach(container, query_one_iothread, &prev);
375 return head;
376}
Fam Zhengdce89212016-09-08 17:28:51 +0800377
Wang Yong329163c2017-08-29 15:22:37 +0800378GMainContext *iothread_get_g_main_context(IOThread *iothread)
379{
Stefan Hajnoczid73415a2020-09-23 11:56:46 +0100380 qatomic_set(&iothread->run_gcontext, 1);
Peter Xub506e0f2019-03-06 19:55:29 +0800381 aio_notify(iothread->ctx);
Wang Yong329163c2017-08-29 15:22:37 +0800382 return iothread->worker_context;
383}
Peter Xu0173e212017-09-28 10:59:55 +0800384
385IOThread *iothread_create(const char *id, Error **errp)
386{
387 Object *obj;
388
389 obj = object_new_with_props(TYPE_IOTHREAD,
390 object_get_internal_root(),
391 id, errp, NULL);
392
393 return IOTHREAD(obj);
394}
395
396void iothread_destroy(IOThread *iothread)
397{
398 object_unparent(OBJECT(iothread));
399}
Stefan Hajnoczifbcc6922017-12-06 14:45:48 +0000400
401/* Lookup IOThread by its id. Only finds user-created objects, not internal
402 * iothread_create() objects. */
403IOThread *iothread_by_id(const char *id)
404{
405 return IOTHREAD(object_resolve_path_type(id, TYPE_IOTHREAD, NULL));
406}
Elena Ufimtsevaad22c302021-01-29 11:46:10 -0500407
408bool qemu_in_iothread(void)
409{
410 return qemu_get_current_aio_context() == qemu_get_aio_context() ?
411 false : true;
412}