From 29a6ea24eb85d8400df1607a9e11d0ef9ec5e88d Mon Sep 17 00:00:00 2001 From: Paolo Bonzini Date: Mon, 17 May 2021 12:05:47 +0200 Subject: 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 Signed-off-by: Paolo Bonzini Message-id: 20210517100548.28806-6-pbonzini@redhat.com Signed-off-by: Stefan Hajnoczi --- util/qemu-coroutine-sleep.c | 41 ++++++++++++++++++----------------------- 1 file changed, 18 insertions(+), 23 deletions(-) (limited to 'util') diff --git a/util/qemu-coroutine-sleep.c b/util/qemu-coroutine-sleep.c index 1d25019..89c3b75 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); } -- cgit v1.1