]>
Commit | Line | Data |
---|---|---|
7e624667 SH |
1 | /* |
2 | * QEMU coroutine sleep | |
3 | * | |
4 | * Copyright IBM, Corp. 2011 | |
5 | * | |
6 | * Authors: | |
7 | * Stefan Hajnoczi <[email protected]> | |
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" |
10817bf0 | 15 | #include "qemu/coroutine.h" |
6133b39f | 16 | #include "qemu/coroutine_int.h" |
1de7afc9 | 17 | #include "qemu/timer.h" |
3ab7bd19 | 18 | #include "block/aio.h" |
7e624667 SH |
19 | |
20 | typedef struct CoSleepCB { | |
21 | QEMUTimer *ts; | |
22 | Coroutine *co; | |
23 | } CoSleepCB; | |
24 | ||
25 | static void co_sleep_cb(void *opaque) | |
26 | { | |
27 | CoSleepCB *sleep_cb = opaque; | |
28 | ||
6133b39f JC |
29 | /* Write of schedule protected by barrier write in aio_co_schedule */ |
30 | atomic_set(&sleep_cb->co->scheduled, NULL); | |
2f47da5f | 31 | aio_co_wake(sleep_cb->co); |
7e624667 SH |
32 | } |
33 | ||
78f1d3d6 | 34 | void coroutine_fn qemu_co_sleep_ns(QEMUClockType type, int64_t ns) |
3ab7bd19 | 35 | { |
78f1d3d6 | 36 | AioContext *ctx = qemu_get_current_aio_context(); |
3ab7bd19 MK |
37 | CoSleepCB sleep_cb = { |
38 | .co = qemu_coroutine_self(), | |
39 | }; | |
6133b39f JC |
40 | |
41 | const char *scheduled = atomic_cmpxchg(&sleep_cb.co->scheduled, NULL, | |
42 | __func__); | |
43 | if (scheduled) { | |
44 | fprintf(stderr, | |
45 | "%s: Co-routine was already scheduled in '%s'\n", | |
46 | __func__, scheduled); | |
47 | abort(); | |
48 | } | |
3ab7bd19 MK |
49 | sleep_cb.ts = aio_timer_new(ctx, type, SCALE_NS, co_sleep_cb, &sleep_cb); |
50 | timer_mod(sleep_cb.ts, qemu_clock_get_ns(type) + ns); | |
51 | qemu_coroutine_yield(); | |
52 | timer_del(sleep_cb.ts); | |
53 | timer_free(sleep_cb.ts); | |
54 | } |