blob: 8fa2f3bfb80fad75ef6614a46f2afaa6cc895dfb [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);
177}
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);
Markus Armbruster7a309cc2020-07-14 18:02:00 +0200184 char *thread_name;
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
Peter Xub506e0f2019-03-06 19:55:29 +0800193 /*
194 * Init one GMainContext for the iothread unconditionally, even if
195 * it's not used
196 */
197 iothread_init_gcontext(iothread);
198
Nicolas Saenz Julienne7d5983e2022-04-25 09:57:21 +0200199 iothread_set_aio_context_params(base, &local_error);
Stefan Hajnoczi0d9d86f2016-12-01 19:26:45 +0000200 if (local_error) {
201 error_propagate(errp, local_error);
202 aio_context_unref(iothread->ctx);
203 iothread->ctx = NULL;
204 return;
205 }
206
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +0100207 /* This assumes we are called from a thread with useful CPU affinity for us
208 * to inherit.
209 */
Markus Armbruster7a309cc2020-07-14 18:02:00 +0200210 thread_name = g_strdup_printf("IO %s",
Nicolas Saenz Julienne7d5983e2022-04-25 09:57:21 +0200211 object_get_canonical_path_component(OBJECT(base)));
Paolo Bonzinid21e8772015-11-24 14:46:44 +0100212 qemu_thread_create(&iothread->thread, thread_name, iothread_run,
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +0100213 iothread, QEMU_THREAD_JOINABLE);
Paolo Bonzinid21e8772015-11-24 14:46:44 +0100214 g_free(thread_name);
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
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +0100295static void iothread_class_init(ObjectClass *klass, void *class_data)
296{
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{
407 return qemu_get_current_aio_context() == qemu_get_aio_context() ?
408 false : true;
409}