From nobody Sat Oct 25 08:55:14 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1520421073373875.1857895049375; Wed, 7 Mar 2018 03:11:13 -0800 (PST) Received: from localhost ([::1]:60785 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1etWyW-0005dy-J9 for importer@patchew.org; Wed, 07 Mar 2018 06:11:12 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:59612) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1etWoE-0004sm-S1 for qemu-devel@nongnu.org; Wed, 07 Mar 2018 06:00:36 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1etWoD-0006BU-QY for qemu-devel@nongnu.org; Wed, 07 Mar 2018 06:00:34 -0500 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:44646 helo=mx1.redhat.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1etWoD-0006B1-Lt for qemu-devel@nongnu.org; Wed, 07 Mar 2018 06:00:33 -0500 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 2F364EBFE6 for ; Wed, 7 Mar 2018 11:00:33 +0000 (UTC) Received: from secure.mitica (ovpn-116-34.ams2.redhat.com [10.36.116.34]) by smtp.corp.redhat.com (Postfix) with ESMTP id 15859215CDA7; Wed, 7 Mar 2018 11:00:31 +0000 (UTC) From: Juan Quintela To: qemu-devel@nongnu.org Date: Wed, 7 Mar 2018 11:59:57 +0100 Message-Id: <20180307110010.2205-12-quintela@redhat.com> In-Reply-To: <20180307110010.2205-1-quintela@redhat.com> References: <20180307110010.2205-1-quintela@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.6 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.1]); Wed, 07 Mar 2018 11:00:33 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.1]); Wed, 07 Mar 2018 11:00:33 +0000 (UTC) for IP:'10.11.54.6' DOMAIN:'int-mx06.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'quintela@redhat.com' RCPT:'' X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 66.187.233.73 Subject: [Qemu-devel] [PATCH v10 11/24] migration: terminate_* can be called for other threads X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: lvivier@redhat.com, dgilbert@redhat.com, peterx@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Once there, make count field to always be accessed with atomic operations. To make blocking operations, we need to know that the thread is running, so create a bool to indicate that. Signed-off-by: Juan Quintela --- migration/ram.c | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/migration/ram.c b/migration/ram.c index 4a56a85d53..977e675f46 100644 --- a/migration/ram.c +++ b/migration/ram.c @@ -400,6 +400,7 @@ struct MultiFDSendParams { QemuThread thread; QemuSemaphore sem; QemuMutex mutex; + bool running; bool quit; }; typedef struct MultiFDSendParams MultiFDSendParams; @@ -424,7 +425,7 @@ static void terminate_multifd_send_threads(Error *errp) } } =20 - for (i =3D 0; i < multifd_send_state->count; i++) { + for (i =3D 0; i < migrate_multifd_channels(); i++) { MultiFDSendParams *p =3D &multifd_send_state->params[i]; =20 qemu_mutex_lock(&p->mutex); @@ -443,10 +444,13 @@ int multifd_save_cleanup(Error **errp) return 0; } terminate_multifd_send_threads(NULL); - for (i =3D 0; i < multifd_send_state->count; i++) { + for (i =3D 0; i < migrate_multifd_channels(); i++) { MultiFDSendParams *p =3D &multifd_send_state->params[i]; =20 - qemu_thread_join(&p->thread); + if (p->running) { + qemu_thread_join(&p->thread); + p->running =3D false; + } qemu_mutex_destroy(&p->mutex); qemu_sem_destroy(&p->sem); g_free(p->name); @@ -487,7 +491,7 @@ int multifd_save_setup(void) thread_count =3D migrate_multifd_channels(); multifd_send_state =3D g_malloc0(sizeof(*multifd_send_state)); multifd_send_state->params =3D g_new0(MultiFDSendParams, thread_count); - multifd_send_state->count =3D 0; + atomic_set(&multifd_send_state->count, 0); for (i =3D 0; i < thread_count; i++) { MultiFDSendParams *p =3D &multifd_send_state->params[i]; =20 @@ -496,10 +500,11 @@ int multifd_save_setup(void) p->quit =3D false; p->id =3D i; p->name =3D g_strdup_printf("multifdsend_%d", i); + p->running =3D true; qemu_thread_create(&p->thread, p->name, multifd_send_thread, p, QEMU_THREAD_JOINABLE); =20 - multifd_send_state->count++; + atomic_inc(&multifd_send_state->count); } return 0; } @@ -510,6 +515,7 @@ struct MultiFDRecvParams { QemuThread thread; QemuSemaphore sem; QemuMutex mutex; + bool running; bool quit; }; typedef struct MultiFDRecvParams MultiFDRecvParams; @@ -534,7 +540,7 @@ static void terminate_multifd_recv_threads(Error *errp) } } =20 - for (i =3D 0; i < multifd_recv_state->count; i++) { + for (i =3D 0; i < migrate_multifd_channels(); i++) { MultiFDRecvParams *p =3D &multifd_recv_state->params[i]; =20 qemu_mutex_lock(&p->mutex); @@ -553,10 +559,13 @@ int multifd_load_cleanup(Error **errp) return 0; } terminate_multifd_recv_threads(NULL); - for (i =3D 0; i < multifd_recv_state->count; i++) { + for (i =3D 0; i < migrate_multifd_channels(); i++) { MultiFDRecvParams *p =3D &multifd_recv_state->params[i]; =20 - qemu_thread_join(&p->thread); + if (p->running) { + qemu_thread_join(&p->thread); + p->running =3D false; + } qemu_mutex_destroy(&p->mutex); qemu_sem_destroy(&p->sem); g_free(p->name); @@ -598,7 +607,7 @@ int multifd_load_setup(void) thread_count =3D migrate_multifd_channels(); multifd_recv_state =3D g_malloc0(sizeof(*multifd_recv_state)); multifd_recv_state->params =3D g_new0(MultiFDRecvParams, thread_count); - multifd_recv_state->count =3D 0; + atomic_set(&multifd_recv_state->count, 0); for (i =3D 0; i < thread_count; i++) { MultiFDRecvParams *p =3D &multifd_recv_state->params[i]; =20 @@ -607,9 +616,10 @@ int multifd_load_setup(void) p->quit =3D false; p->id =3D i; p->name =3D g_strdup_printf("multifdrecv_%d", i); + p->running =3D true; qemu_thread_create(&p->thread, p->name, multifd_recv_thread, p, QEMU_THREAD_JOINABLE); - multifd_recv_state->count++; + atomic_inc(&multifd_recv_state->count); } return 0; } --=20 2.14.3