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>
This commit is contained in:
Paolo Bonzini 2021-05-17 12:05:47 +02:00 committed by Stefan Hajnoczi
parent 1485f0c24c
commit 29a6ea24eb
4 changed files with 40 additions and 44 deletions

View file

@ -19,42 +19,37 @@
static const char *qemu_co_sleep_ns__scheduled = "qemu_co_sleep_ns";
struct QemuCoSleepState {
Coroutine *co;
QemuCoSleepState **user_state_pointer;
};
void qemu_co_sleep_wake(QemuCoSleepState *sleep_state)
void qemu_co_sleep_wake(QemuCoSleep *w)
{
if (sleep_state) {
Coroutine *co;
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);
}