[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH v4 18/32] nbd/client-connection: use QEMU_LOCK_GUARD
From: |
Vladimir Sementsov-Ogievskiy |
Subject: |
[PATCH v4 18/32] nbd/client-connection: use QEMU_LOCK_GUARD |
Date: |
Thu, 10 Jun 2021 13:07:48 +0300 |
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>
---
nbd/client-connection.c | 99 +++++++++++++++++++----------------------
1 file changed, 45 insertions(+), 54 deletions(-)
diff --git a/nbd/client-connection.c b/nbd/client-connection.c
index 31a129bf11..a18842a5a6 100644
--- a/nbd/client-connection.c
+++ b/nbd/client-connection.c
@@ -124,14 +124,14 @@ void nbd_client_connection_release(NBDClientConnection
*conn)
return;
}
- qemu_mutex_lock(&conn->mutex);
- assert(!conn->detached);
- if (conn->running) {
- conn->detached = true;
- } else {
- do_free = true;
+ WITH_QEMU_LOCK_GUARD(&conn->mutex) {
+ assert(!conn->detached);
+ if (conn->running) {
+ conn->detached = true;
+ } else {
+ do_free = true;
+ }
}
- qemu_mutex_unlock(&conn->mutex);
if (do_free) {
nbd_client_connection_do_free(conn);
@@ -149,62 +149,55 @@ void nbd_client_connection_release(NBDClientConnection
*conn)
QIOChannelSocket *coroutine_fn
nbd_co_establish_connection(NBDClientConnection *conn, Error **errp)
{
- QIOChannelSocket *sioc = NULL;
QemuThread thread;
- qemu_mutex_lock(&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) {
- if (conn->sioc) {
- /* Previous attempt finally succeeded in background */
- sioc = g_steal_pointer(&conn->sioc);
- qemu_mutex_unlock(&conn->mutex);
-
- return sioc;
+ 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) {
+ if (conn->sioc) {
+ /* Previous attempt finally succeeded in background */
+ return g_steal_pointer(&conn->sioc);
+ }
+
+ conn->running = true;
+ error_free(conn->err);
+ conn->err = NULL;
+ qemu_thread_create(&thread, "nbd-connect",
+ connect_thread_func, conn,
QEMU_THREAD_DETACHED);
}
- conn->running = true;
- 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();
}
- conn->wait_co = qemu_coroutine_self();
-
- qemu_mutex_unlock(&conn->mutex);
-
/*
* We are going to wait for connect-thread finish, but
* nbd_co_establish_connection_cancel() can interrupt.
*/
qemu_coroutine_yield();
- qemu_mutex_lock(&conn->mutex);
-
- if (conn->running) {
- /*
- * The connection attempt was canceled and the coroutine resumed
- * before the connection thread finished its job. Report the
- * attempt as failed, but leave the connection thread running,
- * to reuse it for the next connection attempt.
- */
- error_setg(errp, "Connection attempt cancelled by other operation");
- } else {
- error_propagate(errp, conn->err);
- conn->err = NULL;
- sioc = g_steal_pointer(&conn->sioc);
+ WITH_QEMU_LOCK_GUARD(&conn->mutex) {
+ if (conn->running) {
+ /*
+ * The connection attempt was canceled and the coroutine resumed
+ * before the connection thread finished its job. Report the
+ * attempt as failed, but leave the connection thread running,
+ * to reuse it for the next connection attempt.
+ */
+ error_setg(errp, "Connection attempt cancelled by other
operation");
+ return NULL;
+ } else {
+ error_propagate(errp, conn->err);
+ conn->err = NULL;
+ return g_steal_pointer(&conn->sioc);
+ }
}
- qemu_mutex_unlock(&conn->mutex);
-
- return sioc;
+ abort(); /* unreachable */
}
/*
@@ -219,11 +212,9 @@ void
nbd_co_establish_connection_cancel(NBDClientConnection *conn)
{
Coroutine *wait_co;
- qemu_mutex_lock(&conn->mutex);
-
- wait_co = g_steal_pointer(&conn->wait_co);
-
- qemu_mutex_unlock(&conn->mutex);
+ WITH_QEMU_LOCK_GUARD(&conn->mutex) {
+ wait_co = g_steal_pointer(&conn->wait_co);
+ }
if (wait_co) {
aio_co_wake(wait_co);
--
2.29.2
- [PATCH v4 03/32] block/nbd: fix how state is cleared on nbd_open() failure paths, (continued)
- [PATCH v4 03/32] block/nbd: fix how state is cleared on nbd_open() failure paths, Vladimir Sementsov-Ogievskiy, 2021/06/10
- [PATCH v4 08/32] block/nbd: nbd_client_handshake(): fix leak of s->ioc, Vladimir Sementsov-Ogievskiy, 2021/06/10
- [PATCH v4 09/32] block/nbd: BDRVNBDState: drop unused connect_err and connect_status, Vladimir Sementsov-Ogievskiy, 2021/06/10
- [PATCH v4 04/32] block/nbd: connect_thread_func(): do qio_channel_set_delay(false), Vladimir Sementsov-Ogievskiy, 2021/06/10
- [PATCH v4 10/32] block/nbd: simplify waking of nbd_co_establish_connection(), Vladimir Sementsov-Ogievskiy, 2021/06/10
- [PATCH v4 11/32] block/nbd: drop thr->state, Vladimir Sementsov-Ogievskiy, 2021/06/10
- [PATCH v4 17/32] nbd: move connection code from block/nbd to nbd/client-connection, Vladimir Sementsov-Ogievskiy, 2021/06/10
- [PATCH v4 18/32] nbd/client-connection: use QEMU_LOCK_GUARD,
Vladimir Sementsov-Ogievskiy <=
- [PATCH v4 05/32] qemu-sockets: introduce socket_address_parse_named_fd(), Vladimir Sementsov-Ogievskiy, 2021/06/10
- [PATCH v4 15/32] block/nbd: introduce nbd_client_connection_new(), Vladimir Sementsov-Ogievskiy, 2021/06/10
- [PATCH v4 20/32] nbd/client-connection: implement connection retry, Vladimir Sementsov-Ogievskiy, 2021/06/10
- [PATCH v4 12/32] block/nbd: bs-independent interface for nbd_co_establish_connection(), Vladimir Sementsov-Ogievskiy, 2021/06/10
- [PATCH v4 13/32] block/nbd: make nbd_co_establish_connection_cancel() bs-independent, Vladimir Sementsov-Ogievskiy, 2021/06/10
- [PATCH v4 19/32] nbd/client-connection: add possibility of negotiation, Vladimir Sementsov-Ogievskiy, 2021/06/10