All of lore.kernel.org
 help / color / mirror / Atom feed
From: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
To: qemu-block@nongnu.org
Cc: qemu-devel@nongnu.org, mreitz@redhat.com, kwolf@redhat.com,
	vsementsov@virtuozzo.com, eblake@redhat.com,
	rvkagan@yandex-team.ru, den@openvz.org
Subject: [PATCH 14/14] block/nbd: drop thr->state
Date: Wed,  7 Apr 2021 13:46:37 +0300	[thread overview]
Message-ID: <20210407104637.36033-15-vsementsov@virtuozzo.com> (raw)
In-Reply-To: <20210407104637.36033-1-vsementsov@virtuozzo.com>

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



  parent reply	other threads:[~2021-04-07 10:54 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-07 10:46 [PATCH 00/14] nbd: move reconnect-thread to separate file Vladimir Sementsov-Ogievskiy
2021-04-07 10:46 ` [PATCH 01/14] block/nbd: BDRVNBDState: drop unused connect_err Vladimir Sementsov-Ogievskiy
2021-04-07 11:13   ` Roman Kagan
2021-04-07 10:46 ` [PATCH 02/14] block/nbd: nbd_co_establish_connection(): drop unused errp Vladimir Sementsov-Ogievskiy
2021-04-07 11:28   ` Roman Kagan
2021-04-07 10:46 ` [PATCH 03/14] block/nbd: drop unused NBDConnectThread::err field Vladimir Sementsov-Ogievskiy
2021-04-07 11:42   ` Roman Kagan
2021-04-07 11:55     ` Vladimir Sementsov-Ogievskiy
2021-04-07 10:46 ` [PATCH 04/14] block/nbd: split connect_thread_cb() out of connect_thread_func() Vladimir Sementsov-Ogievskiy
2021-04-07 10:46 ` [PATCH 05/14] block/nbd: rename NBDConnectThread to NBDConnectCB Vladimir Sementsov-Ogievskiy
2021-04-07 10:46 ` [PATCH 06/14] block/nbd: further segregation of connect-thread Vladimir Sementsov-Ogievskiy
2021-04-08 10:44   ` Roman Kagan
2021-04-07 10:46 ` [PATCH 07/14] block/nbd: drop nbd_free_connect_thread() Vladimir Sementsov-Ogievskiy
2021-04-07 10:46 ` [PATCH 08/14] block/nbd: move nbd connect-thread to nbd/client-connect.c Vladimir Sementsov-Ogievskiy
2021-04-07 10:46 ` [PATCH 09/14] block/nbd: NBDConnectCB: drop bh_* fields Vladimir Sementsov-Ogievskiy
2021-04-07 10:46 ` [PATCH 10/14] block/nbd: move wait_connect field under mutex protection Vladimir Sementsov-Ogievskiy
2021-04-07 10:46 ` [PATCH 11/14] block/nbd: refactor connect_bh() Vladimir Sementsov-Ogievskiy
2021-04-07 10:46 ` [PATCH 12/14] block/nbd: refactor nbd_co_establish_connection Vladimir Sementsov-Ogievskiy
2021-04-07 10:46 ` [PATCH 13/14] block/nbd: nbd_co_establish_connection_cancel(): rename wake to do_wake Vladimir Sementsov-Ogievskiy
2021-04-07 10:46 ` Vladimir Sementsov-Ogievskiy [this message]
2021-04-08 10:03 ` DROP THIS Re: [PATCH 00/14] nbd: move reconnect-thread to separate file Vladimir Sementsov-Ogievskiy

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210407104637.36033-15-vsementsov@virtuozzo.com \
    --to=vsementsov@virtuozzo.com \
    --cc=den@openvz.org \
    --cc=eblake@redhat.com \
    --cc=kwolf@redhat.com \
    --cc=mreitz@redhat.com \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    --cc=rvkagan@yandex-team.ru \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.