aboutsummaryrefslogtreecommitdiff
path: root/util/qemu-coroutine-sleep.c
diff options
context:
space:
mode:
authorPaolo Bonzini <pbonzini@redhat.com>2021-05-17 12:05:47 +0200
committerStefan Hajnoczi <stefanha@redhat.com>2021-05-21 18:22:33 +0100
commit29a6ea24eb85d8400df1607a9e11d0ef9ec5e88d (patch)
tree86dbfa59f1ceb9324ee7937f457ac906e6b81069 /util/qemu-coroutine-sleep.c
parent1485f0c24cfbef7a542ce13d49e9f68e285c57d8 (diff)
coroutine-sleep: replace QemuCoSleepState pointer with struct in the API
Right now, users of qemu_co_sleep_ns_wakeable are simply passing a pointer to QemuCoSleepState by reference to the function. But QemuCoSleepState really is just a Coroutine*; making the content of the struct public is just as efficient and lets us skip the user_state_pointer indirection. Since the usage is changed, take the occasion to rename the struct to QemuCoSleep. Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com> Message-id: 20210517100548.28806-6-pbonzini@redhat.com Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
Diffstat (limited to 'util/qemu-coroutine-sleep.c')
-rw-r--r--util/qemu-coroutine-sleep.c41
1 files changed, 18 insertions, 23 deletions
diff --git a/util/qemu-coroutine-sleep.c b/util/qemu-coroutine-sleep.c
index 1d25019620..89c3b758c5 100644
--- a/util/qemu-coroutine-sleep.c
+++ b/util/qemu-coroutine-sleep.c
@@ -19,42 +19,37 @@
static const char *qemu_co_sleep_ns__scheduled = "qemu_co_sleep_ns";
-struct QemuCoSleepState {
+void qemu_co_sleep_wake(QemuCoSleep *w)
+{
Coroutine *co;
- QemuCoSleepState **user_state_pointer;
-};
-void qemu_co_sleep_wake(QemuCoSleepState *sleep_state)
-{
- if (sleep_state) {
+ co = w->to_wake;
+ w->to_wake = NULL;
+ if (co) {
/* Write of schedule protected by barrier write in aio_co_schedule */
- const char *scheduled = qatomic_cmpxchg(&sleep_state->co->scheduled,
+ const char *scheduled = qatomic_cmpxchg(&co->scheduled,
qemu_co_sleep_ns__scheduled, NULL);
assert(scheduled == qemu_co_sleep_ns__scheduled);
- *sleep_state->user_state_pointer = NULL;
- aio_co_wake(sleep_state->co);
+ aio_co_wake(co);
}
}
static void co_sleep_cb(void *opaque)
{
- QemuCoSleepState **sleep_state = opaque;
- qemu_co_sleep_wake(*sleep_state);
+ QemuCoSleep *w = opaque;
+ qemu_co_sleep_wake(w);
}
-void coroutine_fn qemu_co_sleep_ns_wakeable(QEMUClockType type, int64_t ns,
- QemuCoSleepState **sleep_state)
+void coroutine_fn qemu_co_sleep_ns_wakeable(QemuCoSleep *w,
+ QEMUClockType type, int64_t ns)
{
+ Coroutine *co = qemu_coroutine_self();
AioContext *ctx = qemu_get_current_aio_context();
QEMUTimer ts;
- QemuCoSleepState state = {
- .co = qemu_coroutine_self(),
- .user_state_pointer = sleep_state,
- };
- const char *scheduled = qatomic_cmpxchg(&state.co->scheduled, NULL,
- qemu_co_sleep_ns__scheduled);
+ const char *scheduled = qatomic_cmpxchg(&co->scheduled, NULL,
+ qemu_co_sleep_ns__scheduled);
if (scheduled) {
fprintf(stderr,
"%s: Co-routine was already scheduled in '%s'\n",
@@ -62,12 +57,12 @@ void coroutine_fn qemu_co_sleep_ns_wakeable(QEMUClockType type, int64_t ns,
abort();
}
- aio_timer_init(ctx, &ts, type, SCALE_NS, co_sleep_cb, sleep_state);
- *sleep_state = &state;
+ w->to_wake = co;
+ aio_timer_init(ctx, &ts, type, SCALE_NS, co_sleep_cb, w),
timer_mod(&ts, qemu_clock_get_ns(type) + ns);
qemu_coroutine_yield();
timer_del(&ts);
- /* qemu_co_sleep_wake clears *sleep_state before resuming this coroutine. */
- assert(*sleep_state == NULL);
+ /* w->to_wake is cleared before resuming this coroutine. */
+ assert(w->to_wake == NULL);
}