]> git.proxmox.com Git - mirror_qemu.git/blame - util/async.c
tls: add macros for coroutine-safe TLS variables
[mirror_qemu.git] / util / async.c
CommitLineData
4f999d05 1/*
c2b38b27 2 * Data plane event loop
4f999d05
KW
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
c2b38b27 5 * Copyright (c) 2009-2017 QEMU contributors
4f999d05
KW
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
24 */
25
d38ea87a 26#include "qemu/osdep.h"
da34e65c 27#include "qapi/error.h"
737e150e 28#include "block/aio.h"
9b34277d 29#include "block/thread-pool.h"
1de7afc9 30#include "qemu/main-loop.h"
0ceb849b 31#include "qemu/atomic.h"
8c6b0356 32#include "qemu/rcu_queue.h"
0187f5c9 33#include "block/raw-aio.h"
0c330a73
PB
34#include "qemu/coroutine_int.h"
35#include "trace.h"
9a1e9481 36
4f999d05
KW
37/***********************************************************/
38/* bottom halves (can be seen as timers which expire ASAP) */
39
8c6b0356
SH
40/* QEMUBH::flags values */
41enum {
42 /* Already enqueued and waiting for aio_bh_poll() */
43 BH_PENDING = (1 << 0),
44
45 /* Invoke the callback */
46 BH_SCHEDULED = (1 << 1),
47
48 /* Delete without invoking callback */
49 BH_DELETED = (1 << 2),
50
51 /* Delete after invoking callback */
52 BH_ONESHOT = (1 << 3),
53
54 /* Schedule periodically when the event loop is idle */
55 BH_IDLE = (1 << 4),
56};
57
4f999d05 58struct QEMUBH {
2f4dc3c1 59 AioContext *ctx;
0f08586c 60 const char *name;
4f999d05
KW
61 QEMUBHFunc *cb;
62 void *opaque;
8c6b0356
SH
63 QSLIST_ENTRY(QEMUBH) next;
64 unsigned flags;
4f999d05
KW
65};
66
8c6b0356
SH
67/* Called concurrently from any thread */
68static void aio_bh_enqueue(QEMUBH *bh, unsigned new_flags)
69{
70 AioContext *ctx = bh->ctx;
71 unsigned old_flags;
72
73 /*
d73415a3 74 * The memory barrier implicit in qatomic_fetch_or makes sure that:
8c6b0356
SH
75 * 1. idle & any writes needed by the callback are done before the
76 * locations are read in the aio_bh_poll.
77 * 2. ctx is loaded before the callback has a chance to execute and bh
78 * could be freed.
79 */
d73415a3 80 old_flags = qatomic_fetch_or(&bh->flags, BH_PENDING | new_flags);
8c6b0356
SH
81 if (!(old_flags & BH_PENDING)) {
82 QSLIST_INSERT_HEAD_ATOMIC(&ctx->bh_list, bh, next);
83 }
84
85 aio_notify(ctx);
86}
87
88/* Only called from aio_bh_poll() and aio_ctx_finalize() */
89static QEMUBH *aio_bh_dequeue(BHList *head, unsigned *flags)
90{
91 QEMUBH *bh = QSLIST_FIRST_RCU(head);
92
93 if (!bh) {
94 return NULL;
95 }
96
97 QSLIST_REMOVE_HEAD(head, next);
98
99 /*
d73415a3 100 * The qatomic_and is paired with aio_bh_enqueue(). The implicit memory
8c6b0356
SH
101 * barrier ensures that the callback sees all writes done by the scheduling
102 * thread. It also ensures that the scheduling thread sees the cleared
103 * flag before bh->cb has run, and thus will call aio_notify again if
104 * necessary.
105 */
d73415a3 106 *flags = qatomic_fetch_and(&bh->flags,
8c6b0356
SH
107 ~(BH_PENDING | BH_SCHEDULED | BH_IDLE));
108 return bh;
109}
110
0f08586c
SH
111void aio_bh_schedule_oneshot_full(AioContext *ctx, QEMUBHFunc *cb,
112 void *opaque, const char *name)
5b8bb359
PB
113{
114 QEMUBH *bh;
115 bh = g_new(QEMUBH, 1);
116 *bh = (QEMUBH){
117 .ctx = ctx,
118 .cb = cb,
119 .opaque = opaque,
0f08586c 120 .name = name,
5b8bb359 121 };
8c6b0356 122 aio_bh_enqueue(bh, BH_SCHEDULED | BH_ONESHOT);
5b8bb359
PB
123}
124
0f08586c
SH
125QEMUBH *aio_bh_new_full(AioContext *ctx, QEMUBHFunc *cb, void *opaque,
126 const char *name)
4f999d05
KW
127{
128 QEMUBH *bh;
ee82310f
PB
129 bh = g_new(QEMUBH, 1);
130 *bh = (QEMUBH){
131 .ctx = ctx,
132 .cb = cb,
133 .opaque = opaque,
0f08586c 134 .name = name,
ee82310f 135 };
4f999d05
KW
136 return bh;
137}
138
df281b80
PD
139void aio_bh_call(QEMUBH *bh)
140{
141 bh->cb(bh->opaque);
142}
143
8c6b0356 144/* Multiple occurrences of aio_bh_poll cannot be called concurrently. */
f627aab1 145int aio_bh_poll(AioContext *ctx)
4f999d05 146{
8c6b0356
SH
147 BHListSlice slice;
148 BHListSlice *s;
149 int ret = 0;
150
151 QSLIST_MOVE_ATOMIC(&slice.bh_list, &ctx->bh_list);
152 QSIMPLEQ_INSERT_TAIL(&ctx->bh_slice_list, &slice, next);
153
154 while ((s = QSIMPLEQ_FIRST(&ctx->bh_slice_list))) {
155 QEMUBH *bh;
156 unsigned flags;
157
158 bh = aio_bh_dequeue(&s->bh_list, &flags);
159 if (!bh) {
160 QSIMPLEQ_REMOVE_HEAD(&ctx->bh_slice_list, next);
161 continue;
162 }
163
164 if ((flags & (BH_SCHEDULED | BH_DELETED)) == BH_SCHEDULED) {
65c1b5b6 165 /* Idle BHs don't count as progress */
8c6b0356 166 if (!(flags & BH_IDLE)) {
4f999d05 167 ret = 1;
ca96ac44 168 }
df281b80 169 aio_bh_call(bh);
4f999d05 170 }
8c6b0356
SH
171 if (flags & (BH_DELETED | BH_ONESHOT)) {
172 g_free(bh);
7d506c90 173 }
4f999d05
KW
174 }
175
4f999d05
KW
176 return ret;
177}
178
179void qemu_bh_schedule_idle(QEMUBH *bh)
180{
8c6b0356 181 aio_bh_enqueue(bh, BH_SCHEDULED | BH_IDLE);
4f999d05
KW
182}
183
184void qemu_bh_schedule(QEMUBH *bh)
185{
8c6b0356 186 aio_bh_enqueue(bh, BH_SCHEDULED);
4f999d05
KW
187}
188
dcc772e2
LPF
189/* This func is async.
190 */
4f999d05
KW
191void qemu_bh_cancel(QEMUBH *bh)
192{
d73415a3 193 qatomic_and(&bh->flags, ~BH_SCHEDULED);
4f999d05
KW
194}
195
dcc772e2
LPF
196/* This func is async.The bottom half will do the delete action at the finial
197 * end.
198 */
4f999d05
KW
199void qemu_bh_delete(QEMUBH *bh)
200{
8c6b0356 201 aio_bh_enqueue(bh, BH_DELETED);
4f999d05
KW
202}
203
8c6b0356 204static int64_t aio_compute_bh_timeout(BHList *head, int timeout)
4f999d05
KW
205{
206 QEMUBH *bh;
207
8c6b0356
SH
208 QSLIST_FOREACH_RCU(bh, head, next) {
209 if ((bh->flags & (BH_SCHEDULED | BH_DELETED)) == BH_SCHEDULED) {
210 if (bh->flags & BH_IDLE) {
4f999d05
KW
211 /* idle bottom halves will be polled at least
212 * every 10ms */
845ca10d 213 timeout = 10000000;
4f999d05
KW
214 } else {
215 /* non-idle bottom halves will be executed
216 * immediately */
845ca10d 217 return 0;
4f999d05
KW
218 }
219 }
220 }
e3713e00 221
8c6b0356
SH
222 return timeout;
223}
224
225int64_t
226aio_compute_timeout(AioContext *ctx)
227{
228 BHListSlice *s;
229 int64_t deadline;
230 int timeout = -1;
231
232 timeout = aio_compute_bh_timeout(&ctx->bh_list, timeout);
233 if (timeout == 0) {
234 return 0;
235 }
236
237 QSIMPLEQ_FOREACH(s, &ctx->bh_slice_list, next) {
238 timeout = aio_compute_bh_timeout(&s->bh_list, timeout);
239 if (timeout == 0) {
240 return 0;
241 }
242 }
243
845ca10d 244 deadline = timerlistgroup_deadline_ns(&ctx->tlg);
533a8cf3 245 if (deadline == 0) {
845ca10d 246 return 0;
533a8cf3 247 } else {
845ca10d 248 return qemu_soonest_timeout(timeout, deadline);
533a8cf3 249 }
845ca10d 250}
533a8cf3 251
845ca10d
PB
252static gboolean
253aio_ctx_prepare(GSource *source, gint *timeout)
254{
255 AioContext *ctx = (AioContext *) source;
256
d73415a3 257 qatomic_set(&ctx->notify_me, qatomic_read(&ctx->notify_me) | 1);
5710a3e0
PB
258
259 /*
260 * Write ctx->notify_me before computing the timeout
261 * (reading bottom half flags, etc.). Pairs with
262 * smp_mb in aio_notify().
263 */
264 smp_mb();
eabc9779 265
845ca10d
PB
266 /* We assume there is no timeout already supplied */
267 *timeout = qemu_timeout_ns_to_ms(aio_compute_timeout(ctx));
a3462c65
PB
268
269 if (aio_prepare(ctx)) {
270 *timeout = 0;
271 }
272
845ca10d 273 return *timeout == 0;
e3713e00
PB
274}
275
276static gboolean
277aio_ctx_check(GSource *source)
278{
279 AioContext *ctx = (AioContext *) source;
280 QEMUBH *bh;
8c6b0356 281 BHListSlice *s;
e3713e00 282
5710a3e0 283 /* Finish computing the timeout before clearing the flag. */
d73415a3 284 qatomic_store_release(&ctx->notify_me, qatomic_read(&ctx->notify_me) & ~1);
05e514b1 285 aio_notify_accept(ctx);
21a03d17 286
8c6b0356
SH
287 QSLIST_FOREACH_RCU(bh, &ctx->bh_list, next) {
288 if ((bh->flags & (BH_SCHEDULED | BH_DELETED)) == BH_SCHEDULED) {
e3713e00 289 return true;
6977d901 290 }
e3713e00 291 }
8c6b0356
SH
292
293 QSIMPLEQ_FOREACH(s, &ctx->bh_slice_list, next) {
294 QSLIST_FOREACH_RCU(bh, &s->bh_list, next) {
295 if ((bh->flags & (BH_SCHEDULED | BH_DELETED)) == BH_SCHEDULED) {
296 return true;
297 }
298 }
299 }
533a8cf3 300 return aio_pending(ctx) || (timerlistgroup_deadline_ns(&ctx->tlg) == 0);
e3713e00
PB
301}
302
303static gboolean
304aio_ctx_dispatch(GSource *source,
305 GSourceFunc callback,
306 gpointer user_data)
307{
308 AioContext *ctx = (AioContext *) source;
309
310 assert(callback == NULL);
a153bf52 311 aio_dispatch(ctx);
e3713e00
PB
312 return true;
313}
314
2f4dc3c1
PB
315static void
316aio_ctx_finalize(GSource *source)
317{
318 AioContext *ctx = (AioContext *) source;
8c6b0356
SH
319 QEMUBH *bh;
320 unsigned flags;
2f4dc3c1 321
9b34277d 322 thread_pool_free(ctx->thread_pool);
a076972a 323
0187f5c9
PB
324#ifdef CONFIG_LINUX_AIO
325 if (ctx->linux_aio) {
326 laio_detach_aio_context(ctx->linux_aio, ctx);
327 laio_cleanup(ctx->linux_aio);
328 ctx->linux_aio = NULL;
329 }
330#endif
331
fcb7a4a4
AM
332#ifdef CONFIG_LINUX_IO_URING
333 if (ctx->linux_io_uring) {
334 luring_detach_aio_context(ctx->linux_io_uring, ctx);
335 luring_cleanup(ctx->linux_io_uring);
336 ctx->linux_io_uring = NULL;
337 }
338#endif
339
0c330a73
PB
340 assert(QSLIST_EMPTY(&ctx->scheduled_coroutines));
341 qemu_bh_delete(ctx->co_schedule_bh);
342
8c6b0356
SH
343 /* There must be no aio_bh_poll() calls going on */
344 assert(QSIMPLEQ_EMPTY(&ctx->bh_slice_list));
a076972a 345
8c6b0356 346 while ((bh = aio_bh_dequeue(&ctx->bh_list, &flags))) {
023ca420
SH
347 /*
348 * qemu_bh_delete() must have been called on BHs in this AioContext. In
349 * many cases memory leaks, hangs, or inconsistent state occur when a
350 * BH is leaked because something still expects it to run.
351 *
352 * If you hit this, fix the lifecycle of the BH so that
353 * qemu_bh_delete() and any associated cleanup is called before the
354 * AioContext is finalized.
355 */
356 if (unlikely(!(flags & BH_DELETED))) {
357 fprintf(stderr, "%s: BH '%s' leaked, aborting...\n",
358 __func__, bh->name);
359 abort();
360 }
a076972a 361
8c6b0356 362 g_free(bh);
a076972a 363 }
a076972a 364
826cc324 365 aio_set_event_notifier(ctx, &ctx->notifier, false, NULL, NULL, NULL);
2f4dc3c1 366 event_notifier_cleanup(&ctx->notifier);
3fe71223 367 qemu_rec_mutex_destroy(&ctx->lock);
d7c99a12 368 qemu_lockcnt_destroy(&ctx->list_lock);
dae21b98 369 timerlistgroup_deinit(&ctx->tlg);
cd0a6d2b 370 aio_context_destroy(ctx);
2f4dc3c1
PB
371}
372
e3713e00
PB
373static GSourceFuncs aio_source_funcs = {
374 aio_ctx_prepare,
375 aio_ctx_check,
376 aio_ctx_dispatch,
2f4dc3c1 377 aio_ctx_finalize
e3713e00
PB
378};
379
380GSource *aio_get_g_source(AioContext *ctx)
381{
ba607ca8 382 aio_context_use_g_source(ctx);
e3713e00
PB
383 g_source_ref(&ctx->source);
384 return &ctx->source;
385}
a915f4bc 386
9b34277d
SH
387ThreadPool *aio_get_thread_pool(AioContext *ctx)
388{
389 if (!ctx->thread_pool) {
390 ctx->thread_pool = thread_pool_new(ctx);
391 }
392 return ctx->thread_pool;
393}
394
0187f5c9 395#ifdef CONFIG_LINUX_AIO
ed6e2161 396LinuxAioState *aio_setup_linux_aio(AioContext *ctx, Error **errp)
0187f5c9
PB
397{
398 if (!ctx->linux_aio) {
ed6e2161
NA
399 ctx->linux_aio = laio_init(errp);
400 if (ctx->linux_aio) {
401 laio_attach_aio_context(ctx->linux_aio, ctx);
402 }
0187f5c9
PB
403 }
404 return ctx->linux_aio;
405}
ed6e2161
NA
406
407LinuxAioState *aio_get_linux_aio(AioContext *ctx)
408{
409 assert(ctx->linux_aio);
410 return ctx->linux_aio;
411}
0187f5c9
PB
412#endif
413
fcb7a4a4
AM
414#ifdef CONFIG_LINUX_IO_URING
415LuringState *aio_setup_linux_io_uring(AioContext *ctx, Error **errp)
416{
417 if (ctx->linux_io_uring) {
418 return ctx->linux_io_uring;
419 }
420
421 ctx->linux_io_uring = luring_init(errp);
422 if (!ctx->linux_io_uring) {
423 return NULL;
424 }
425
426 luring_attach_aio_context(ctx->linux_io_uring, ctx);
427 return ctx->linux_io_uring;
428}
429
430LuringState *aio_get_linux_io_uring(AioContext *ctx)
431{
432 assert(ctx->linux_io_uring);
433 return ctx->linux_io_uring;
434}
435#endif
436
2f4dc3c1
PB
437void aio_notify(AioContext *ctx)
438{
601829f8
SH
439 /*
440 * Write e.g. bh->flags before writing ctx->notified. Pairs with smp_mb in
441 * aio_notify_accept.
442 */
443 smp_wmb();
d73415a3 444 qatomic_set(&ctx->notified, true);
601829f8
SH
445
446 /*
447 * Write ctx->notified before reading ctx->notify_me. Pairs
5710a3e0 448 * with smp_mb in aio_ctx_prepare or aio_poll.
eabc9779 449 */
0ceb849b 450 smp_mb();
d73415a3 451 if (qatomic_read(&ctx->notify_me)) {
0ceb849b 452 event_notifier_set(&ctx->notifier);
05e514b1
PB
453 }
454}
455
456void aio_notify_accept(AioContext *ctx)
457{
d73415a3 458 qatomic_set(&ctx->notified, false);
601829f8
SH
459
460 /*
461 * Write ctx->notified before reading e.g. bh->flags. Pairs with smp_wmb
462 * in aio_notify.
463 */
464 smp_mb();
2f4dc3c1
PB
465}
466
3f53bc61 467static void aio_timerlist_notify(void *opaque, QEMUClockType type)
d5541d86
AB
468{
469 aio_notify(opaque);
470}
471
601829f8 472static void aio_context_notifier_cb(EventNotifier *e)
21a03d17 473{
601829f8
SH
474 AioContext *ctx = container_of(e, AioContext, notifier);
475
476 event_notifier_test_and_clear(&ctx->notifier);
21a03d17
PB
477}
478
4a1cba38 479/* Returns true if aio_notify() was called (e.g. a BH was scheduled) */
c13be5a1 480static bool aio_context_notifier_poll(void *opaque)
4a1cba38
SH
481{
482 EventNotifier *e = opaque;
483 AioContext *ctx = container_of(e, AioContext, notifier);
484
d73415a3 485 return qatomic_read(&ctx->notified);
4a1cba38
SH
486}
487
826cc324
SH
488static void aio_context_notifier_poll_ready(EventNotifier *e)
489{
490 /* Do nothing, we just wanted to kick the event loop */
491}
492
0c330a73
PB
493static void co_schedule_bh_cb(void *opaque)
494{
495 AioContext *ctx = opaque;
496 QSLIST_HEAD(, Coroutine) straight, reversed;
497
498 QSLIST_MOVE_ATOMIC(&reversed, &ctx->scheduled_coroutines);
499 QSLIST_INIT(&straight);
500
501 while (!QSLIST_EMPTY(&reversed)) {
502 Coroutine *co = QSLIST_FIRST(&reversed);
503 QSLIST_REMOVE_HEAD(&reversed, co_scheduled_next);
504 QSLIST_INSERT_HEAD(&straight, co, co_scheduled_next);
505 }
506
507 while (!QSLIST_EMPTY(&straight)) {
508 Coroutine *co = QSLIST_FIRST(&straight);
509 QSLIST_REMOVE_HEAD(&straight, co_scheduled_next);
510 trace_aio_co_schedule_bh_cb(ctx, co);
1919631e 511 aio_context_acquire(ctx);
6133b39f
JC
512
513 /* Protected by write barrier in qemu_aio_coroutine_enter */
d73415a3 514 qatomic_set(&co->scheduled, NULL);
6808ae04 515 qemu_aio_coroutine_enter(ctx, co);
1919631e 516 aio_context_release(ctx);
0c330a73
PB
517 }
518}
519
2f78e491 520AioContext *aio_context_new(Error **errp)
f627aab1 521{
2f78e491 522 int ret;
2f4dc3c1 523 AioContext *ctx;
37fcee5d 524
2f4dc3c1 525 ctx = (AioContext *) g_source_new(&aio_source_funcs, sizeof(AioContext));
8c6b0356
SH
526 QSLIST_INIT(&ctx->bh_list);
527 QSIMPLEQ_INIT(&ctx->bh_slice_list);
7e003465
C
528 aio_context_setup(ctx);
529
2f78e491
CN
530 ret = event_notifier_init(&ctx->notifier, false);
531 if (ret < 0) {
2f78e491 532 error_setg_errno(errp, -ret, "Failed to initialize event notifier");
37fcee5d 533 goto fail;
2f78e491 534 }
fcf5def1 535 g_source_set_can_recurse(&ctx->source, true);
d7c99a12 536 qemu_lockcnt_init(&ctx->list_lock);
0c330a73
PB
537
538 ctx->co_schedule_bh = aio_bh_new(ctx, co_schedule_bh_cb, ctx);
539 QSLIST_INIT(&ctx->scheduled_coroutines);
540
2f78e491 541 aio_set_event_notifier(ctx, &ctx->notifier,
dca21ef2 542 false,
601829f8 543 aio_context_notifier_cb,
826cc324
SH
544 aio_context_notifier_poll,
545 aio_context_notifier_poll_ready);
0187f5c9
PB
546#ifdef CONFIG_LINUX_AIO
547 ctx->linux_aio = NULL;
548#endif
fcb7a4a4
AM
549
550#ifdef CONFIG_LINUX_IO_URING
551 ctx->linux_io_uring = NULL;
552#endif
553
9b34277d 554 ctx->thread_pool = NULL;
3fe71223 555 qemu_rec_mutex_init(&ctx->lock);
d5541d86 556 timerlistgroup_init(&ctx->tlg, aio_timerlist_notify, ctx);
2f4dc3c1 557
82a41186 558 ctx->poll_ns = 0;
4a1cba38 559 ctx->poll_max_ns = 0;
82a41186
SH
560 ctx->poll_grow = 0;
561 ctx->poll_shrink = 0;
4a1cba38 562
1793ad02
SG
563 ctx->aio_max_batch = 0;
564
2f4dc3c1 565 return ctx;
37fcee5d
FZ
566fail:
567 g_source_destroy(&ctx->source);
568 return NULL;
e3713e00
PB
569}
570
0c330a73
PB
571void aio_co_schedule(AioContext *ctx, Coroutine *co)
572{
573 trace_aio_co_schedule(ctx, co);
d73415a3 574 const char *scheduled = qatomic_cmpxchg(&co->scheduled, NULL,
6133b39f
JC
575 __func__);
576
577 if (scheduled) {
578 fprintf(stderr,
579 "%s: Co-routine was already scheduled in '%s'\n",
580 __func__, scheduled);
581 abort();
582 }
583
f0f81002
SH
584 /* The coroutine might run and release the last ctx reference before we
585 * invoke qemu_bh_schedule(). Take a reference to keep ctx alive until
586 * we're done.
587 */
588 aio_context_ref(ctx);
589
0c330a73
PB
590 QSLIST_INSERT_HEAD_ATOMIC(&ctx->scheduled_coroutines,
591 co, co_scheduled_next);
592 qemu_bh_schedule(ctx->co_schedule_bh);
f0f81002
SH
593
594 aio_context_unref(ctx);
0c330a73
PB
595}
596
26b0b698
KW
597typedef struct AioCoRescheduleSelf {
598 Coroutine *co;
599 AioContext *new_ctx;
600} AioCoRescheduleSelf;
601
602static void aio_co_reschedule_self_bh(void *opaque)
603{
604 AioCoRescheduleSelf *data = opaque;
605 aio_co_schedule(data->new_ctx, data->co);
606}
607
608void coroutine_fn aio_co_reschedule_self(AioContext *new_ctx)
609{
610 AioContext *old_ctx = qemu_get_current_aio_context();
611
612 if (old_ctx != new_ctx) {
613 AioCoRescheduleSelf data = {
614 .co = qemu_coroutine_self(),
615 .new_ctx = new_ctx,
616 };
617 /*
618 * We can't directly schedule the coroutine in the target context
619 * because this would be racy: The other thread could try to enter the
620 * coroutine before it has yielded in this one.
621 */
622 aio_bh_schedule_oneshot(old_ctx, aio_co_reschedule_self_bh, &data);
623 qemu_coroutine_yield();
624 }
625}
626
0c330a73
PB
627void aio_co_wake(struct Coroutine *co)
628{
629 AioContext *ctx;
630
631 /* Read coroutine before co->ctx. Matches smp_wmb in
632 * qemu_coroutine_enter.
633 */
634 smp_read_barrier_depends();
d73415a3 635 ctx = qatomic_read(&co->ctx);
0c330a73 636
8865852e
FZ
637 aio_co_enter(ctx, co);
638}
639
640void aio_co_enter(AioContext *ctx, struct Coroutine *co)
641{
0c330a73
PB
642 if (ctx != qemu_get_current_aio_context()) {
643 aio_co_schedule(ctx, co);
644 return;
645 }
646
647 if (qemu_in_coroutine()) {
648 Coroutine *self = qemu_coroutine_self();
649 assert(self != co);
650 QSIMPLEQ_INSERT_TAIL(&self->co_queue_wakeup, co, co_queue_next);
651 } else {
652 aio_context_acquire(ctx);
8865852e 653 qemu_aio_coroutine_enter(ctx, co);
0c330a73
PB
654 aio_context_release(ctx);
655 }
656}
657
e3713e00
PB
658void aio_context_ref(AioContext *ctx)
659{
660 g_source_ref(&ctx->source);
661}
662
663void aio_context_unref(AioContext *ctx)
664{
665 g_source_unref(&ctx->source);
f627aab1 666}
98563fc3
SH
667
668void aio_context_acquire(AioContext *ctx)
669{
3fe71223 670 qemu_rec_mutex_lock(&ctx->lock);
98563fc3
SH
671}
672
673void aio_context_release(AioContext *ctx)
674{
3fe71223 675 qemu_rec_mutex_unlock(&ctx->lock);
98563fc3 676}
5f50be9b
PB
677
678static __thread AioContext *my_aiocontext;
679
680AioContext *qemu_get_current_aio_context(void)
681{
682 if (my_aiocontext) {
683 return my_aiocontext;
684 }
685 if (qemu_mutex_iothread_locked()) {
686 /* Possibly in a vCPU thread. */
687 return qemu_get_aio_context();
688 }
689 return NULL;
690}
691
692void qemu_set_current_aio_context(AioContext *ctx)
693{
694 assert(!my_aiocontext);
695 my_aiocontext = ctx;
696}