]>
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 | 19 | |
3d692649 VSO |
20 | static const char *qemu_co_sleep_ns__scheduled = "qemu_co_sleep_ns"; |
21 | ||
22 | struct QemuCoSleepState { | |
23 | Coroutine *co; | |
24 | QEMUTimer *ts; | |
25 | QemuCoSleepState **user_state_pointer; | |
26 | }; | |
7e624667 | 27 | |
3d692649 VSO |
28 | void qemu_co_sleep_wake(QemuCoSleepState *sleep_state) |
29 | { | |
6133b39f | 30 | /* Write of schedule protected by barrier write in aio_co_schedule */ |
d73415a3 | 31 | const char *scheduled = qatomic_cmpxchg(&sleep_state->co->scheduled, |
3d692649 VSO |
32 | qemu_co_sleep_ns__scheduled, NULL); |
33 | ||
34 | assert(scheduled == qemu_co_sleep_ns__scheduled); | |
35 | if (sleep_state->user_state_pointer) { | |
36 | *sleep_state->user_state_pointer = NULL; | |
37 | } | |
38 | timer_del(sleep_state->ts); | |
39 | aio_co_wake(sleep_state->co); | |
7e624667 SH |
40 | } |
41 | ||
3d692649 VSO |
42 | static void co_sleep_cb(void *opaque) |
43 | { | |
44 | qemu_co_sleep_wake(opaque); | |
45 | } | |
46 | ||
47 | void coroutine_fn qemu_co_sleep_ns_wakeable(QEMUClockType type, int64_t ns, | |
48 | QemuCoSleepState **sleep_state) | |
3ab7bd19 | 49 | { |
78f1d3d6 | 50 | AioContext *ctx = qemu_get_current_aio_context(); |
3d692649 VSO |
51 | QemuCoSleepState state = { |
52 | .co = qemu_coroutine_self(), | |
53 | .ts = aio_timer_new(ctx, type, SCALE_NS, co_sleep_cb, &state), | |
54 | .user_state_pointer = sleep_state, | |
55 | }; | |
6133b39f | 56 | |
d73415a3 | 57 | const char *scheduled = qatomic_cmpxchg(&state.co->scheduled, NULL, |
3d692649 | 58 | qemu_co_sleep_ns__scheduled); |
6133b39f JC |
59 | if (scheduled) { |
60 | fprintf(stderr, | |
61 | "%s: Co-routine was already scheduled in '%s'\n", | |
62 | __func__, scheduled); | |
63 | abort(); | |
64 | } | |
3d692649 VSO |
65 | |
66 | if (sleep_state) { | |
67 | *sleep_state = &state; | |
68 | } | |
69 | timer_mod(state.ts, qemu_clock_get_ns(type) + ns); | |
3ab7bd19 | 70 | qemu_coroutine_yield(); |
f61ffad5 EB |
71 | if (sleep_state) { |
72 | /* | |
73 | * Note that *sleep_state is cleared during qemu_co_sleep_wake | |
74 | * before resuming this coroutine. | |
75 | */ | |
76 | assert(*sleep_state == NULL); | |
77 | } | |
3d692649 | 78 | timer_free(state.ts); |
3ab7bd19 | 79 | } |