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_int.h"
16 #include "qemu/timer.h"
17 #include "block/aio.h"
19 static const char *qemu_co_sleep_ns__scheduled
= "qemu_co_sleep_ns";
21 void qemu_co_sleep_wake(QemuCoSleep
*w
)
28 /* Write of schedule protected by barrier write in aio_co_schedule */
29 const char *scheduled
= qatomic_cmpxchg(&co
->scheduled
,
30 qemu_co_sleep_ns__scheduled
, NULL
);
32 assert(scheduled
== qemu_co_sleep_ns__scheduled
);
37 static void co_sleep_cb(void *opaque
)
39 QemuCoSleep
*w
= opaque
;
40 qemu_co_sleep_wake(w
);
43 void coroutine_fn
qemu_co_sleep(QemuCoSleep
*w
)
45 Coroutine
*co
= qemu_coroutine_self();
47 const char *scheduled
= qatomic_cmpxchg(&co
->scheduled
, NULL
,
48 qemu_co_sleep_ns__scheduled
);
51 "%s: Co-routine was already scheduled in '%s'\n",
57 qemu_coroutine_yield();
59 /* w->to_wake is cleared before resuming this coroutine. */
60 assert(w
->to_wake
== NULL
);
63 void coroutine_fn
qemu_co_sleep_ns_wakeable(QemuCoSleep
*w
,
64 QEMUClockType type
, int64_t ns
)
66 AioContext
*ctx
= qemu_get_current_aio_context();
69 aio_timer_init(ctx
, &ts
, type
, SCALE_NS
, co_sleep_cb
, w
);
70 timer_mod(&ts
, qemu_clock_get_ns(type
) + ns
);
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.