]>
Commit | Line | Data |
---|---|---|
7e624667 SH |
1 | /* |
2 | * QEMU coroutine sleep | |
3 | * | |
4 | * Copyright IBM, Corp. 2011 | |
5 | * | |
6 | * Authors: | |
7 | * Stefan Hajnoczi <stefanha@linux.vnet.ibm.com> | |
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 | 19 | |
7e624667 SH |
20 | static void co_sleep_cb(void *opaque) |
21 | { | |
85956859 | 22 | Coroutine *co = opaque; |
7e624667 | 23 | |
6133b39f | 24 | /* Write of schedule protected by barrier write in aio_co_schedule */ |
85956859 VSO |
25 | atomic_set(&co->scheduled, NULL); |
26 | aio_co_wake(co); | |
7e624667 SH |
27 | } |
28 | ||
78f1d3d6 | 29 | void coroutine_fn qemu_co_sleep_ns(QEMUClockType type, int64_t ns) |
3ab7bd19 | 30 | { |
78f1d3d6 | 31 | AioContext *ctx = qemu_get_current_aio_context(); |
85956859 VSO |
32 | QEMUTimer *ts; |
33 | Coroutine *co = qemu_coroutine_self(); | |
6133b39f | 34 | |
85956859 | 35 | const char *scheduled = atomic_cmpxchg(&co->scheduled, NULL, __func__); |
6133b39f JC |
36 | if (scheduled) { |
37 | fprintf(stderr, | |
38 | "%s: Co-routine was already scheduled in '%s'\n", | |
39 | __func__, scheduled); | |
40 | abort(); | |
41 | } | |
85956859 VSO |
42 | ts = aio_timer_new(ctx, type, SCALE_NS, co_sleep_cb, co); |
43 | timer_mod(ts, qemu_clock_get_ns(type) + ns); | |
3ab7bd19 | 44 | qemu_coroutine_yield(); |
85956859 VSO |
45 | timer_del(ts); |
46 | timer_free(ts); | |
3ab7bd19 | 47 | } |