4 * Copyright IBM, Corp. 2011
7 * Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
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.
14 #include "qemu/osdep.h"
15 #include "qemu/coroutine.h"
16 #include "qemu/coroutine_int.h"
17 #include "qemu/timer.h"
18 #include "block/aio.h"
20 static const char *qemu_co_sleep_ns__scheduled
= "qemu_co_sleep_ns";
22 void qemu_co_sleep_wake(QemuCoSleep
*w
)
29 /* Write of schedule protected by barrier write in aio_co_schedule */
30 const char *scheduled
= qatomic_cmpxchg(&co
->scheduled
,
31 qemu_co_sleep_ns__scheduled
, NULL
);
33 assert(scheduled
== qemu_co_sleep_ns__scheduled
);
38 static void co_sleep_cb(void *opaque
)
40 QemuCoSleep
*w
= opaque
;
41 qemu_co_sleep_wake(w
);
44 void coroutine_fn
qemu_co_sleep(QemuCoSleep
*w
)
46 Coroutine
*co
= qemu_coroutine_self();
48 const char *scheduled
= qatomic_cmpxchg(&co
->scheduled
, NULL
,
49 qemu_co_sleep_ns__scheduled
);
52 "%s: Co-routine was already scheduled in '%s'\n",
58 qemu_coroutine_yield();
60 /* w->to_wake is cleared before resuming this coroutine. */
61 assert(w
->to_wake
== NULL
);
64 void coroutine_fn
qemu_co_sleep_ns_wakeable(QemuCoSleep
*w
,
65 QEMUClockType type
, int64_t ns
)
67 AioContext
*ctx
= qemu_get_current_aio_context();
70 aio_timer_init(ctx
, &ts
, type
, SCALE_NS
, co_sleep_cb
, w
);
71 timer_mod(&ts
, qemu_clock_get_ns(type
) + ns
);
74 * The timer will fire in the current AiOContext, so the callback
75 * must happen after qemu_co_sleep yields and there is no race
76 * between timer_mod and qemu_co_sleep.