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 void co_sleep_cb(void *opaque
)
22 Coroutine
*co
= opaque
;
24 /* Write of schedule protected by barrier write in aio_co_schedule */
25 atomic_set(&co
->scheduled
, NULL
);
29 void coroutine_fn
qemu_co_sleep_ns(QEMUClockType type
, int64_t ns
)
31 AioContext
*ctx
= qemu_get_current_aio_context();
33 Coroutine
*co
= qemu_coroutine_self();
35 const char *scheduled
= atomic_cmpxchg(&co
->scheduled
, NULL
, __func__
);
38 "%s: Co-routine was already scheduled in '%s'\n",
42 ts
= aio_timer_new(ctx
, type
, SCALE_NS
, co_sleep_cb
, co
);
43 timer_mod(ts
, qemu_clock_get_ns(type
) + ns
);
44 qemu_coroutine_yield();