1
2
3
4
5
6
7
8
9
10
11
12
13
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"
19
20static void co_sleep_cb(void *opaque)
21{
22 Coroutine *co = opaque;
23
24
25 atomic_set(&co->scheduled, NULL);
26 aio_co_wake(co);
27}
28
29void coroutine_fn qemu_co_sleep_ns(QEMUClockType type, int64_t ns)
30{
31 AioContext *ctx = qemu_get_current_aio_context();
32 QEMUTimer *ts;
33 Coroutine *co = qemu_coroutine_self();
34
35 const char *scheduled = atomic_cmpxchg(&co->scheduled, NULL, __func__);
36 if (scheduled) {
37 fprintf(stderr,
38 "%s: Co-routine was already scheduled in '%s'\n",
39 __func__, scheduled);
40 abort();
41 }
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();
45 timer_del(ts);
46 timer_free(ts);
47}
48