diff mbox series

[RFC,7/7] migration: Further unify paths for multifd normal or sync requests

Message ID 20231022201211.452861-8-peterx@redhat.com (mailing list archive)
State New, archived
Headers show
Series migration/multifd: quit unitifications and separate sync packet | expand

Commit Message

Peter Xu Oct. 22, 2023, 8:12 p.m. UTC
Provide multifd_send_execute() for merging duplicated codes.

The trick here is multifd_send_execute() will conditionally hold the mutex
when returned, depending on the retval.

Signed-off-by: Peter Xu <peterx@redhat.com>
---
 migration/multifd.c | 51 ++++++++++++++++++++++-----------------------
 1 file changed, 25 insertions(+), 26 deletions(-)
diff mbox series

Patch

diff --git a/migration/multifd.c b/migration/multifd.c
index fe8d746ff9..0052e5daee 100644
--- a/migration/multifd.c
+++ b/migration/multifd.c
@@ -717,6 +717,29 @@  static bool multifd_do_send(MultiFDSendParams *p, Error **errp)
     return true;
 }
 
+/*
+ * When succeed: returns true, mutex held.
+ * When failed:  returns false, mutex released.
+ */
+static bool multifd_send_execute(MultiFDSendParams *p, Error **errp)
+{
+    if (!multifd_send_prepare(p, errp)) {
+        qemu_mutex_unlock(&p->mutex);
+        assert(*errp);
+        return false;
+    }
+
+    /* Send the packets without mutex */
+    qemu_mutex_unlock(&p->mutex);
+    if (!multifd_do_send(p, errp)) {
+        assert(*errp);
+        return false;
+    }
+    qemu_mutex_lock(&p->mutex);
+
+    return true;
+}
+
 static void *multifd_send_thread(void *opaque)
 {
     MultiFDSendParams *p = opaque;
@@ -746,40 +769,16 @@  static void *multifd_send_thread(void *opaque)
 
         qemu_mutex_lock(&p->mutex);
         if (p->pending_job) {
-            if (!multifd_send_prepare(p, &local_err)) {
-                assert(local_err);
-                qemu_mutex_unlock(&p->mutex);
+            if (!multifd_send_execute(p, &local_err)) {
                 goto out;
             }
-
-            /* Send the packets without mutex */
-            qemu_mutex_unlock(&p->mutex);
-            if (!multifd_do_send(p, &local_err)) {
-                assert(local_err);
-                goto out;
-            }
-            qemu_mutex_lock(&p->mutex);
-
-            /* Send successful, mark the task completed */
             p->pending_job = false;
 
         } else if (p->pending_sync) {
             p->flags |= MULTIFD_FLAG_SYNC;
-
-            if (!multifd_send_prepare(p, &local_err)) {
-                assert(local_err);
-                qemu_mutex_unlock(&p->mutex);
-                goto out;
-            }
-
-            /* Send the packets without mutex */
-            qemu_mutex_unlock(&p->mutex);
-            if (!multifd_do_send(p, &local_err)) {
-                assert(local_err);
+            if (!multifd_send_execute(p, &local_err)) {
                 goto out;
             }
-            qemu_mutex_lock(&p->mutex);
-
             qemu_sem_post(&p->sem_sync);
             p->pending_sync = false;
         }