nbd/client-connection: use QEMU_LOCK_GUARD

We don't update connect_thread_func() to use QEMU_LOCK_GUARD, as it
will get more complex critical sections logic in further commit, where
QEMU_LOCK_GUARD doesn't help.

Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
Message-Id: <20210610100802.5888-19-vsementsov@virtuozzo.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Eric Blake <eblake@redhat.com>
This commit is contained in:
Vladimir Sementsov-Ogievskiy 2021-06-10 13:07:48 +03:00 committed by Eric Blake
parent 5276c87c12
commit e70da5ff64

View File

@ -125,14 +125,14 @@ void nbd_client_connection_release(NBDClientConnection *conn)
return; return;
} }
qemu_mutex_lock(&conn->mutex); WITH_QEMU_LOCK_GUARD(&conn->mutex) {
assert(!conn->detached); assert(!conn->detached);
if (conn->running) { if (conn->running) {
conn->detached = true; conn->detached = true;
} else { } else {
do_free = true; do_free = true;
}
} }
qemu_mutex_unlock(&conn->mutex);
if (do_free) { if (do_free) {
nbd_client_connection_do_free(conn); nbd_client_connection_do_free(conn);
@ -150,62 +150,55 @@ void nbd_client_connection_release(NBDClientConnection *conn)
QIOChannelSocket *coroutine_fn QIOChannelSocket *coroutine_fn
nbd_co_establish_connection(NBDClientConnection *conn, Error **errp) nbd_co_establish_connection(NBDClientConnection *conn, Error **errp)
{ {
QIOChannelSocket *sioc = NULL;
QemuThread thread; QemuThread thread;
qemu_mutex_lock(&conn->mutex); WITH_QEMU_LOCK_GUARD(&conn->mutex) {
/*
* Don't call nbd_co_establish_connection() in several coroutines in
* parallel. Only one call at once is supported.
*/
assert(!conn->wait_co);
/* if (!conn->running) {
* Don't call nbd_co_establish_connection() in several coroutines in if (conn->sioc) {
* parallel. Only one call at once is supported. /* Previous attempt finally succeeded in background */
*/ return g_steal_pointer(&conn->sioc);
assert(!conn->wait_co); }
if (!conn->running) { conn->running = true;
if (conn->sioc) { error_free(conn->err);
/* Previous attempt finally succeeded in background */ conn->err = NULL;
sioc = g_steal_pointer(&conn->sioc); qemu_thread_create(&thread, "nbd-connect",
qemu_mutex_unlock(&conn->mutex); connect_thread_func, conn, QEMU_THREAD_DETACHED);
return sioc;
} }
conn->running = true; conn->wait_co = qemu_coroutine_self();
error_free(conn->err);
conn->err = NULL;
qemu_thread_create(&thread, "nbd-connect",
connect_thread_func, conn, QEMU_THREAD_DETACHED);
} }
conn->wait_co = qemu_coroutine_self();
qemu_mutex_unlock(&conn->mutex);
/* /*
* We are going to wait for connect-thread finish, but * We are going to wait for connect-thread finish, but
* nbd_co_establish_connection_cancel() can interrupt. * nbd_co_establish_connection_cancel() can interrupt.
*/ */
qemu_coroutine_yield(); qemu_coroutine_yield();
qemu_mutex_lock(&conn->mutex); WITH_QEMU_LOCK_GUARD(&conn->mutex) {
if (conn->running) {
if (conn->running) { /*
/* * The connection attempt was canceled and the coroutine resumed
* The connection attempt was canceled and the coroutine resumed * before the connection thread finished its job. Report the
* before the connection thread finished its job. Report the * attempt as failed, but leave the connection thread running,
* attempt as failed, but leave the connection thread running, * to reuse it for the next connection attempt.
* to reuse it for the next connection attempt. */
*/ error_setg(errp, "Connection attempt cancelled by other operation");
error_setg(errp, "Connection attempt cancelled by other operation"); return NULL;
} else { } else {
error_propagate(errp, conn->err); error_propagate(errp, conn->err);
conn->err = NULL; conn->err = NULL;
sioc = g_steal_pointer(&conn->sioc); return g_steal_pointer(&conn->sioc);
}
} }
qemu_mutex_unlock(&conn->mutex); abort(); /* unreachable */
return sioc;
} }
/* /*
@ -220,11 +213,9 @@ void nbd_co_establish_connection_cancel(NBDClientConnection *conn)
{ {
Coroutine *wait_co; Coroutine *wait_co;
qemu_mutex_lock(&conn->mutex); WITH_QEMU_LOCK_GUARD(&conn->mutex) {
wait_co = g_steal_pointer(&conn->wait_co);
wait_co = g_steal_pointer(&conn->wait_co); }
qemu_mutex_unlock(&conn->mutex);
if (wait_co) { if (wait_co) {
aio_co_wake(wait_co); aio_co_wake(wait_co);