]> git.proxmox.com Git - mirror_qemu.git/blame - util/qemu-coroutine-sleep.c
include/exec: Use vaddr in DisasContextBase for virtual addresses
[mirror_qemu.git] / util / qemu-coroutine-sleep.c
CommitLineData
7e624667
SH
1/*
2 * QEMU coroutine sleep
3 *
4 * Copyright IBM, Corp. 2011
5 *
6 * Authors:
7 * Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
8 *
9 * This work is licensed under the terms of the GNU LGPL, version 2 or later.
10 * See the COPYING.LIB file in the top-level directory.
11 *
12 */
13
aafd7584 14#include "qemu/osdep.h"
6133b39f 15#include "qemu/coroutine_int.h"
1de7afc9 16#include "qemu/timer.h"
3ab7bd19 17#include "block/aio.h"
7e624667 18
3d692649
VSO
19static const char *qemu_co_sleep_ns__scheduled = "qemu_co_sleep_ns";
20
29a6ea24
PB
21void qemu_co_sleep_wake(QemuCoSleep *w)
22{
3d692649 23 Coroutine *co;
7e624667 24
29a6ea24
PB
25 co = w->to_wake;
26 w->to_wake = NULL;
27 if (co) {
eaee0720 28 /* Write of schedule protected by barrier write in aio_co_schedule */
29a6ea24 29 const char *scheduled = qatomic_cmpxchg(&co->scheduled,
eaee0720 30 qemu_co_sleep_ns__scheduled, NULL);
3d692649 31
eaee0720 32 assert(scheduled == qemu_co_sleep_ns__scheduled);
29a6ea24 33 aio_co_wake(co);
eaee0720 34 }
7e624667
SH
35}
36
3d692649
VSO
37static void co_sleep_cb(void *opaque)
38{
29a6ea24
PB
39 QemuCoSleep *w = opaque;
40 qemu_co_sleep_wake(w);
3d692649
VSO
41}
42
0a6f0c76 43void coroutine_fn qemu_co_sleep(QemuCoSleep *w)
3ab7bd19 44{
29a6ea24 45 Coroutine *co = qemu_coroutine_self();
6133b39f 46
29a6ea24
PB
47 const char *scheduled = qatomic_cmpxchg(&co->scheduled, NULL,
48 qemu_co_sleep_ns__scheduled);
6133b39f
JC
49 if (scheduled) {
50 fprintf(stderr,
51 "%s: Co-routine was already scheduled in '%s'\n",
52 __func__, scheduled);
53 abort();
54 }
3d692649 55
29a6ea24 56 w->to_wake = co;
3ab7bd19 57 qemu_coroutine_yield();
fb74a286 58
29a6ea24
PB
59 /* w->to_wake is cleared before resuming this coroutine. */
60 assert(w->to_wake == NULL);
3ab7bd19 61}
0a6f0c76
PB
62
63void coroutine_fn qemu_co_sleep_ns_wakeable(QemuCoSleep *w,
64 QEMUClockType type, int64_t ns)
65{
66 AioContext *ctx = qemu_get_current_aio_context();
67 QEMUTimer ts;
68
69 aio_timer_init(ctx, &ts, type, SCALE_NS, co_sleep_cb, w);
70 timer_mod(&ts, qemu_clock_get_ns(type) + ns);
71
72 /*
73 * The timer will fire in the current AiOContext, so the callback
74 * must happen after qemu_co_sleep yields and there is no race
75 * between timer_mod and qemu_co_sleep.
76 */
77 qemu_co_sleep(w);
78 timer_del(&ts);
79}