@@ -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;
}
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(-)