[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH 14/14] block/nbd: drop thr->state
From: |
Vladimir Sementsov-Ogievskiy |
Subject: |
[PATCH 14/14] block/nbd: drop thr->state |
Date: |
Wed, 7 Apr 2021 13:46:37 +0300 |
thr->state variable mostly duplicates information that is already
obvious from the other fields: thr->bs=NULL means DETACHED,
thr->sioc!=NULL means SUCCESS. The only bit of information we need is
"is thread running now or not". So, drop state and add simple boolean
instead. It simplifies the logic a lot.
Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
---
block/nbd.c | 122 +++++++++++++++-------------------------------------
1 file changed, 34 insertions(+), 88 deletions(-)
diff --git a/block/nbd.c b/block/nbd.c
index 9cee5b6650..5320a359f6 100644
--- a/block/nbd.c
+++ b/block/nbd.c
@@ -66,31 +66,16 @@ typedef enum NBDClientState {
NBD_CLIENT_QUIT
} NBDClientState;
-typedef enum NBDConnectThreadState {
- /* No thread, no pending results */
- CONNECT_THREAD_NONE,
-
- /* Thread is running, no results for now */
- CONNECT_THREAD_RUNNING,
-
+typedef struct NBDConnectCB {
/*
- * Thread is running, but requestor exited. Thread should close
- * the new socket and free the connect state on exit.
+ * Result of last attempt. Set in connect_thread_cb() on success. Should
be
+ * set to NULL before starting the thread.
*/
- CONNECT_THREAD_RUNNING_DETACHED,
-
- /* Thread finished, results are stored in a state */
- CONNECT_THREAD_FAIL,
- CONNECT_THREAD_SUCCESS
-} NBDConnectThreadState;
-
-typedef struct NBDConnectCB {
- /* Result of last attempt. Valid in FAIL and SUCCESS states. */
QIOChannelSocket *sioc;
QemuMutex mutex;
/* All further fields are protected by mutex */
- NBDConnectThreadState state; /* current state of the thread */
+ bool running; /* thread is running now */
/* Link to NBD BDS. If NULL thread is detached, BDS is probably closed. */
BlockDriverState *bs;
@@ -354,10 +339,7 @@ static void nbd_init_connect_thread(BlockDriverState *bs)
s->connect_thread = g_new(NBDConnectCB, 1);
- *s->connect_thread = (NBDConnectCB) {
- .state = CONNECT_THREAD_NONE,
- .bs = bs,
- };
+ *s->connect_thread = (NBDConnectCB) { .bs = bs };
qemu_mutex_init(&s->connect_thread->mutex);
}
@@ -374,22 +356,21 @@ static void connect_thread_cb(QIOChannelSocket *sioc, int
ret, void *opaque)
bool do_wake = false;
BDRVNBDState *s = thr->bs ? thr->bs->opaque : NULL;
+ /* We are in context of connect thread ! */
+
qemu_mutex_lock(&thr->mutex);
+ assert(thr->running);
+ assert(thr->sioc == NULL);
+ assert(thr->bs || !thr->wait_connect);
+
+ thr->running = false;
thr->sioc = sioc;
- switch (thr->state) {
- case CONNECT_THREAD_RUNNING:
- thr->state = ret < 0 ? CONNECT_THREAD_FAIL : CONNECT_THREAD_SUCCESS;
- do_wake = thr->wait_connect;
- thr->wait_connect = false;
- break;
- case CONNECT_THREAD_RUNNING_DETACHED:
- do_free = true;
- break;
- default:
- abort();
- }
+ do_wake = thr->wait_connect;
+ thr->wait_connect = false;
+
+ do_free = !thr->bs; /* detached */
qemu_mutex_unlock(&thr->mutex);
@@ -416,25 +397,21 @@ nbd_co_establish_connection(BlockDriverState *bs)
BDRVNBDState *s = bs->opaque;
NBDConnectCB *thr = s->connect_thread;
+ assert(!s->sioc);
+
qemu_mutex_lock(&thr->mutex);
- switch (thr->state) {
- case CONNECT_THREAD_FAIL:
- case CONNECT_THREAD_NONE:
- thr->state = CONNECT_THREAD_RUNNING;
- nbd_connect_thread_start(s->saddr, connect_thread_cb, thr);
- break;
- case CONNECT_THREAD_SUCCESS:
+ if (thr->sioc) {
/* Previous attempt finally succeeded in background */
- thr->state = CONNECT_THREAD_NONE;
+ assert(!thr->running);
s->sioc = thr->sioc;
thr->sioc = NULL;
goto out;
- case CONNECT_THREAD_RUNNING:
- /* Already running, will wait */
- break;
- default:
- abort();
+ }
+
+ if (!thr->running) {
+ thr->running = true;
+ nbd_connect_thread_start(s->saddr, connect_thread_cb, thr);
}
thr->wait_connect = true;
@@ -449,32 +426,8 @@ nbd_co_establish_connection(BlockDriverState *bs)
qemu_mutex_lock(&thr->mutex);
- switch (thr->state) {
- case CONNECT_THREAD_SUCCESS:
- case CONNECT_THREAD_FAIL:
- thr->state = CONNECT_THREAD_NONE;
- s->sioc = thr->sioc;
- thr->sioc = NULL;
- break;
- case CONNECT_THREAD_RUNNING:
- case CONNECT_THREAD_RUNNING_DETACHED:
- /*
- * Obviously, drained section wants to start. Report the attempt as
- * failed. Still connect thread is executing in background, and its
- * result may be used for next connection attempt.
- */
- break;
-
- case CONNECT_THREAD_NONE:
- /*
- * Impossible. We've seen this thread running. So it should be
- * running or at least give some results.
- */
- abort();
-
- default:
- abort();
- }
+ s->sioc = thr->sioc;
+ thr->sioc = NULL;
out:
qemu_mutex_unlock(&thr->mutex);
@@ -506,26 +459,19 @@ static void
nbd_co_establish_connection_cancel(BlockDriverState *bs,
qemu_mutex_lock(&thr->mutex);
- if (thr->state == CONNECT_THREAD_RUNNING) {
- /* We can cancel only in running state, when bh is not yet scheduled */
- if (thr->wait_connect) {
- thr->wait_connect = false;
- do_wake = true;
- }
- if (detach) {
- thr->bs = NULL;
- thr->state = CONNECT_THREAD_RUNNING_DETACHED;
- s->connect_thread = NULL;
- }
- } else if (detach) {
- do_free = true;
+ do_wake = thr->wait_connect;
+ thr->wait_connect = false;
+
+ if (detach) {
+ s->connect_thread = NULL;
+ thr->bs = NULL;
+ do_free = !thr->running;
}
qemu_mutex_unlock(&thr->mutex);
if (do_free) {
g_free(thr);
- s->connect_thread = NULL;
}
if (do_wake) {
--
2.29.2
- [PATCH 06/14] block/nbd: further segregation of connect-thread, (continued)
- [PATCH 06/14] block/nbd: further segregation of connect-thread, Vladimir Sementsov-Ogievskiy, 2021/04/07
- [PATCH 05/14] block/nbd: rename NBDConnectThread to NBDConnectCB, Vladimir Sementsov-Ogievskiy, 2021/04/07
- [PATCH 08/14] block/nbd: move nbd connect-thread to nbd/client-connect.c, Vladimir Sementsov-Ogievskiy, 2021/04/07
- [PATCH 07/14] block/nbd: drop nbd_free_connect_thread(), Vladimir Sementsov-Ogievskiy, 2021/04/07
- [PATCH 09/14] block/nbd: NBDConnectCB: drop bh_* fields, Vladimir Sementsov-Ogievskiy, 2021/04/07
- [PATCH 10/14] block/nbd: move wait_connect field under mutex protection, Vladimir Sementsov-Ogievskiy, 2021/04/07
- [PATCH 11/14] block/nbd: refactor connect_bh(), Vladimir Sementsov-Ogievskiy, 2021/04/07
- [PATCH 12/14] block/nbd: refactor nbd_co_establish_connection, Vladimir Sementsov-Ogievskiy, 2021/04/07
- [PATCH 13/14] block/nbd: nbd_co_establish_connection_cancel(): rename wake to do_wake, Vladimir Sementsov-Ogievskiy, 2021/04/07
- [PATCH 14/14] block/nbd: drop thr->state,
Vladimir Sementsov-Ogievskiy <=
- DROP THIS Re: [PATCH 00/14] nbd: move reconnect-thread to separate file, Vladimir Sementsov-Ogievskiy, 2021/04/08