blob: b41c305bd967a174155beacce56d8e43882b7ea7 [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
Peter Xub506e0f2019-03-06 19:55:29 +0800141static void iothread_init_gcontext(IOThread *iothread)
142{
143 GSource *source;
144
145 iothread->worker_context = g_main_context_new();
146 source = aio_get_g_source(iothread_get_aio_context(iothread));
147 g_source_attach(source, iothread->worker_context);
148 g_source_unref(source);
Peter Xu0bd2d232019-03-06 19:55:30 +0800149 iothread->main_loop = g_main_loop_new(iothread->worker_context, TRUE);
Peter Xub506e0f2019-03-06 19:55:29 +0800150}
151
Nicolas Saenz Julienne7d5983e2022-04-25 09:57:21 +0200152static void iothread_set_aio_context_params(EventLoopBase *base, Error **errp)
Stefano Garzarella1793ad02021-07-21 11:42:10 +0200153{
154 ERRP_GUARD();
Markus Armbruster05e385d2022-11-21 09:50:47 +0100155 IOThread *iothread = IOTHREAD(base);
Stefano Garzarella1793ad02021-07-21 11:42:10 +0200156
Nicolas Saenz Julienne7d5983e2022-04-25 09:57:21 +0200157 if (!iothread->ctx) {
158 return;
159 }
160
Stefano Garzarella1793ad02021-07-21 11:42:10 +0200161 aio_context_set_poll_params(iothread->ctx,
162 iothread->poll_max_ns,
163 iothread->poll_grow,
164 iothread->poll_shrink,
165 errp);
166 if (*errp) {
167 return;
168 }
169
170 aio_context_set_aio_params(iothread->ctx,
Nicolas Saenz Julienne7d5983e2022-04-25 09:57:21 +0200171 iothread->parent_obj.aio_max_batch,
Stefano Garzarella1793ad02021-07-21 11:42:10 +0200172 errp);
Nicolas Saenz Julienne71ad4712022-04-25 09:57:23 +0200173
174 aio_context_set_thread_pool_params(iothread->ctx, base->thread_pool_min,
175 base->thread_pool_max, errp);
Stefano Garzarella1793ad02021-07-21 11:42:10 +0200176}
177
Nicolas Saenz Julienne7d5983e2022-04-25 09:57:21 +0200178
179static void iothread_init(EventLoopBase *base, Error **errp)
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +0100180{
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300181 Error *local_error = NULL;
Nicolas Saenz Julienne7d5983e2022-04-25 09:57:21 +0200182 IOThread *iothread = IOTHREAD(base);
Markus Armbruster7a309cc2020-07-14 18:02:00 +0200183 char *thread_name;
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +0100184
185 iothread->stopping = false;
Stefan Hajnoczi2362a282017-12-07 20:13:19 +0000186 iothread->running = true;
Markus Armbruster668f62e2020-07-07 18:06:02 +0200187 iothread->ctx = aio_context_new(errp);
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300188 if (!iothread->ctx) {
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300189 return;
190 }
Stefan Hajnoczi88eb7c22014-02-27 11:48:41 +0100191
Peter Xub506e0f2019-03-06 19:55:29 +0800192 /*
193 * Init one GMainContext for the iothread unconditionally, even if
194 * it's not used
195 */
196 iothread_init_gcontext(iothread);
197
Nicolas Saenz Julienne7d5983e2022-04-25 09:57:21 +0200198 iothread_set_aio_context_params(base, &local_error);
Stefan Hajnoczi0d9d86f2016-12-01 19:26:45 +0000199 if (local_error) {
200 error_propagate(errp, local_error);
201 aio_context_unref(iothread->ctx);
202 iothread->ctx = NULL;
203 return;
204 }
205
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +0100206 /* This assumes we are called from a thread with useful CPU affinity for us
207 * to inherit.
208 */
Markus Armbruster7a309cc2020-07-14 18:02:00 +0200209 thread_name = g_strdup_printf("IO %s",
Nicolas Saenz Julienne7d5983e2022-04-25 09:57:21 +0200210 object_get_canonical_path_component(OBJECT(base)));
Paolo Bonzinid21e8772015-11-24 14:46:44 +0100211 qemu_thread_create(&iothread->thread, thread_name, iothread_run,
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +0100212 iothread, QEMU_THREAD_JOINABLE);
Paolo Bonzinid21e8772015-11-24 14:46:44 +0100213 g_free(thread_name);
Stefan Hajnoczi88eb7c22014-02-27 11:48:41 +0100214
215 /* Wait for initialization to complete */
Stefan Hajnoczi88eb7c22014-02-27 11:48:41 +0100216 while (iothread->thread_id == -1) {
Peter Xu21c4d152019-03-06 19:55:28 +0800217 qemu_sem_wait(&iothread->init_done_sem);
Stefan Hajnoczi88eb7c22014-02-27 11:48:41 +0100218 }
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +0100219}
220
Stefan Hajnoczi5e5db492016-12-01 19:26:52 +0000221typedef struct {
222 const char *name;
223 ptrdiff_t offset; /* field's byte offset in IOThread struct */
Stefano Garzarellaf0ed36a2021-07-27 16:59:35 +0200224} IOThreadParamInfo;
Stefan Hajnoczi5e5db492016-12-01 19:26:52 +0000225
Stefano Garzarellaf0ed36a2021-07-27 16:59:35 +0200226static IOThreadParamInfo poll_max_ns_info = {
Stefan Hajnoczi5e5db492016-12-01 19:26:52 +0000227 "poll-max-ns", offsetof(IOThread, poll_max_ns),
228};
Stefano Garzarellaf0ed36a2021-07-27 16:59:35 +0200229static IOThreadParamInfo poll_grow_info = {
Stefan Hajnoczi5e5db492016-12-01 19:26:52 +0000230 "poll-grow", offsetof(IOThread, poll_grow),
231};
Stefano Garzarellaf0ed36a2021-07-27 16:59:35 +0200232static IOThreadParamInfo poll_shrink_info = {
Stefan Hajnoczi5e5db492016-12-01 19:26:52 +0000233 "poll-shrink", offsetof(IOThread, poll_shrink),
234};
235
Stefano Garzarella04454092021-07-21 11:42:09 +0200236static void iothread_get_param(Object *obj, Visitor *v,
Stefano Garzarella1cc7ead2021-07-27 16:59:36 +0200237 const char *name, IOThreadParamInfo *info, Error **errp)
Stefan Hajnoczi0d9d86f2016-12-01 19:26:45 +0000238{
239 IOThread *iothread = IOTHREAD(obj);
Stefan Hajnoczi5e5db492016-12-01 19:26:52 +0000240 int64_t *field = (void *)iothread + info->offset;
Stefan Hajnoczi0d9d86f2016-12-01 19:26:45 +0000241
Stefan Hajnoczi5e5db492016-12-01 19:26:52 +0000242 visit_type_int64(v, name, field, errp);
Stefan Hajnoczi0d9d86f2016-12-01 19:26:45 +0000243}
244
Stefano Garzarella04454092021-07-21 11:42:09 +0200245static bool iothread_set_param(Object *obj, Visitor *v,
Stefano Garzarella1cc7ead2021-07-27 16:59:36 +0200246 const char *name, IOThreadParamInfo *info, Error **errp)
Stefan Hajnoczi0d9d86f2016-12-01 19:26:45 +0000247{
248 IOThread *iothread = IOTHREAD(obj);
Stefan Hajnoczi5e5db492016-12-01 19:26:52 +0000249 int64_t *field = (void *)iothread + info->offset;
Stefan Hajnoczi0d9d86f2016-12-01 19:26:45 +0000250 int64_t value;
251
Markus Armbruster668f62e2020-07-07 18:06:02 +0200252 if (!visit_type_int64(v, name, &value, errp)) {
Stefano Garzarella04454092021-07-21 11:42:09 +0200253 return false;
Stefan Hajnoczi0d9d86f2016-12-01 19:26:45 +0000254 }
255
256 if (value < 0) {
Markus Armbrusterdcfe4802020-07-07 18:06:01 +0200257 error_setg(errp, "%s value must be in range [0, %" PRId64 "]",
Stefan Hajnoczi5e5db492016-12-01 19:26:52 +0000258 info->name, INT64_MAX);
Stefano Garzarella04454092021-07-21 11:42:09 +0200259 return false;
Stefan Hajnoczi0d9d86f2016-12-01 19:26:45 +0000260 }
261
Stefan Hajnoczi5e5db492016-12-01 19:26:52 +0000262 *field = value;
Stefan Hajnoczi0d9d86f2016-12-01 19:26:45 +0000263
Stefano Garzarella04454092021-07-21 11:42:09 +0200264 return true;
265}
266
267static void iothread_get_poll_param(Object *obj, Visitor *v,
268 const char *name, void *opaque, Error **errp)
269{
Stefano Garzarella1cc7ead2021-07-27 16:59:36 +0200270 IOThreadParamInfo *info = opaque;
Stefano Garzarella04454092021-07-21 11:42:09 +0200271
Stefano Garzarella1cc7ead2021-07-27 16:59:36 +0200272 iothread_get_param(obj, v, name, info, errp);
Stefano Garzarella04454092021-07-21 11:42:09 +0200273}
274
275static void iothread_set_poll_param(Object *obj, Visitor *v,
276 const char *name, void *opaque, Error **errp)
277{
278 IOThread *iothread = IOTHREAD(obj);
Stefano Garzarella1cc7ead2021-07-27 16:59:36 +0200279 IOThreadParamInfo *info = opaque;
Stefano Garzarella04454092021-07-21 11:42:09 +0200280
Stefano Garzarella1cc7ead2021-07-27 16:59:36 +0200281 if (!iothread_set_param(obj, v, name, info, errp)) {
Stefano Garzarella04454092021-07-21 11:42:09 +0200282 return;
283 }
284
Stefan Hajnoczi0d9d86f2016-12-01 19:26:45 +0000285 if (iothread->ctx) {
Stefan Hajnoczi5e5db492016-12-01 19:26:52 +0000286 aio_context_set_poll_params(iothread->ctx,
287 iothread->poll_max_ns,
288 iothread->poll_grow,
289 iothread->poll_shrink,
Markus Armbrusterdcfe4802020-07-07 18:06:01 +0200290 errp);
Stefan Hajnoczi0d9d86f2016-12-01 19:26:45 +0000291 }
Stefan Hajnoczi0d9d86f2016-12-01 19:26:45 +0000292}
293
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +0100294static void iothread_class_init(ObjectClass *klass, void *class_data)
295{
Nicolas Saenz Julienne7d5983e2022-04-25 09:57:21 +0200296 EventLoopBaseClass *bc = EVENT_LOOP_BASE_CLASS(klass);
297
298 bc->init = iothread_init;
299 bc->update_params = iothread_set_aio_context_params;
Stefan Hajnoczi0d9d86f2016-12-01 19:26:45 +0000300
301 object_class_property_add(klass, "poll-max-ns", "int",
Stefan Hajnoczi5e5db492016-12-01 19:26:52 +0000302 iothread_get_poll_param,
303 iothread_set_poll_param,
Markus Armbrusterd2623122020-05-05 17:29:22 +0200304 NULL, &poll_max_ns_info);
Stefan Hajnoczi5e5db492016-12-01 19:26:52 +0000305 object_class_property_add(klass, "poll-grow", "int",
306 iothread_get_poll_param,
307 iothread_set_poll_param,
Markus Armbrusterd2623122020-05-05 17:29:22 +0200308 NULL, &poll_grow_info);
Stefan Hajnoczi5e5db492016-12-01 19:26:52 +0000309 object_class_property_add(klass, "poll-shrink", "int",
310 iothread_get_poll_param,
311 iothread_set_poll_param,
Markus Armbrusterd2623122020-05-05 17:29:22 +0200312 NULL, &poll_shrink_info);
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +0100313}
314
315static const TypeInfo iothread_info = {
316 .name = TYPE_IOTHREAD,
Nicolas Saenz Julienne7d5983e2022-04-25 09:57:21 +0200317 .parent = TYPE_EVENT_LOOP_BASE,
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +0100318 .class_init = iothread_class_init,
319 .instance_size = sizeof(IOThread),
Stefan Hajnoczicdd7abf2017-01-26 17:01:19 +0000320 .instance_init = iothread_instance_init,
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +0100321 .instance_finalize = iothread_instance_finalize,
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +0100322};
323
324static void iothread_register_types(void)
325{
326 type_register_static(&iothread_info);
327}
328
329type_init(iothread_register_types)
330
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +0100331char *iothread_get_id(IOThread *iothread)
332{
Markus Armbruster7a309cc2020-07-14 18:02:00 +0200333 return g_strdup(object_get_canonical_path_component(OBJECT(iothread)));
Stefan Hajnoczibe8d8532014-03-03 11:30:05 +0100334}
335
336AioContext *iothread_get_aio_context(IOThread *iothread)
337{
338 return iothread->ctx;
339}
Stefan Hajnoczidc3dd0d2014-02-27 11:48:42 +0100340
341static int query_one_iothread(Object *object, void *opaque)
342{
Eric Blakec3033fd2021-01-13 16:10:12 -0600343 IOThreadInfoList ***tail = opaque;
Stefan Hajnoczidc3dd0d2014-02-27 11:48:42 +0100344 IOThreadInfo *info;
345 IOThread *iothread;
346
347 iothread = (IOThread *)object_dynamic_cast(object, TYPE_IOTHREAD);
348 if (!iothread) {
349 return 0;
350 }
351
352 info = g_new0(IOThreadInfo, 1);
353 info->id = iothread_get_id(iothread);
354 info->thread_id = iothread->thread_id;
Pavel Hrdina5fc00482017-02-10 10:41:17 +0100355 info->poll_max_ns = iothread->poll_max_ns;
356 info->poll_grow = iothread->poll_grow;
357 info->poll_shrink = iothread->poll_shrink;
Nicolas Saenz Julienne7d5983e2022-04-25 09:57:21 +0200358 info->aio_max_batch = iothread->parent_obj.aio_max_batch;
Stefan Hajnoczidc3dd0d2014-02-27 11:48:42 +0100359
Eric Blakec3033fd2021-01-13 16:10:12 -0600360 QAPI_LIST_APPEND(*tail, info);
Stefan Hajnoczidc3dd0d2014-02-27 11:48:42 +0100361 return 0;
362}
363
364IOThreadInfoList *qmp_query_iothreads(Error **errp)
365{
366 IOThreadInfoList *head = NULL;
367 IOThreadInfoList **prev = &head;
Daniel P. Berrangebc2256c2015-05-13 17:14:05 +0100368 Object *container = object_get_objects_root();
Stefan Hajnoczidc3dd0d2014-02-27 11:48:42 +0100369
370 object_child_foreach(container, query_one_iothread, &prev);
371 return head;
372}
Fam Zhengdce89212016-09-08 17:28:51 +0800373
Wang Yong329163c2017-08-29 15:22:37 +0800374GMainContext *iothread_get_g_main_context(IOThread *iothread)
375{
Stefan Hajnoczid73415a2020-09-23 11:56:46 +0100376 qatomic_set(&iothread->run_gcontext, 1);
Peter Xub506e0f2019-03-06 19:55:29 +0800377 aio_notify(iothread->ctx);
Wang Yong329163c2017-08-29 15:22:37 +0800378 return iothread->worker_context;
379}
Peter Xu0173e212017-09-28 10:59:55 +0800380
381IOThread *iothread_create(const char *id, Error **errp)
382{
383 Object *obj;
384
385 obj = object_new_with_props(TYPE_IOTHREAD,
386 object_get_internal_root(),
387 id, errp, NULL);
388
389 return IOTHREAD(obj);
390}
391
392void iothread_destroy(IOThread *iothread)
393{
394 object_unparent(OBJECT(iothread));
395}
Stefan Hajnoczifbcc6922017-12-06 14:45:48 +0000396
397/* Lookup IOThread by its id. Only finds user-created objects, not internal
398 * iothread_create() objects. */
399IOThread *iothread_by_id(const char *id)
400{
401 return IOTHREAD(object_resolve_path_type(id, TYPE_IOTHREAD, NULL));
402}
Elena Ufimtsevaad22c302021-01-29 11:46:10 -0500403
404bool qemu_in_iothread(void)
405{
406 return qemu_get_current_aio_context() == qemu_get_aio_context() ?
407 false : true;
408}