From nobody Fri May 3 19:38:28 2024 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 Return-Path: Received: from lists.gnu.org (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 1506070722992360.74808396380706; Fri, 22 Sep 2017 01:58:42 -0700 (PDT) Received: from localhost ([::1]:57417 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dvJn7-0005BG-Pf for importer@patchew.org; Fri, 22 Sep 2017 04:58:33 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:55670) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dvJl6-000436-J7 for qemu-devel@nongnu.org; Fri, 22 Sep 2017 04:56:29 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dvJl5-0004ww-N3 for qemu-devel@nongnu.org; Fri, 22 Sep 2017 04:56:28 -0400 Received: from mx1.redhat.com ([209.132.183.28]:38906) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dvJl5-0004wN-HQ for qemu-devel@nongnu.org; Fri, 22 Sep 2017 04:56:27 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 68076C0587C6 for ; Fri, 22 Sep 2017 08:56:26 +0000 (UTC) Received: from pxdev.xzpeter.org.com (ovpn-12-133.pek2.redhat.com [10.72.12.133]) by smtp.corp.redhat.com (Postfix) with ESMTP id 133C617AB6; Fri, 22 Sep 2017 08:56:23 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 68076C0587C6 Authentication-Results: ext-mx08.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx08.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=peterx@redhat.com From: Peter Xu To: qemu-devel@nongnu.org Date: Fri, 22 Sep 2017 16:56:10 +0800 Message-Id: <1506070572-7549-2-git-send-email-peterx@redhat.com> In-Reply-To: <1506070572-7549-1-git-send-email-peterx@redhat.com> References: <1506070572-7549-1-git-send-email-peterx@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.32]); Fri, 22 Sep 2017 08:56:26 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH 1/3] iothread: provide helpers for internal use 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: Fam Zheng , peterx@redhat.com, "Dr . David Alan Gilbert" , Stefan Hajnoczi , Paolo Bonzini 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" IOThread is a general framework that contains IO loop environment and a real thread behind. It's also good to be used internally inside qemu. Provide some helpers for it to create iothreads to be used internally. Signed-off-by: Peter Xu --- include/sysemu/iothread.h | 8 ++++++++ iothread.c | 21 +++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/include/sysemu/iothread.h b/include/sysemu/iothread.h index d2985b3..b07663f 100644 --- a/include/sysemu/iothread.h +++ b/include/sysemu/iothread.h @@ -46,4 +46,12 @@ AioContext *iothread_get_aio_context(IOThread *iothread); void iothread_stop_all(void); GMainContext *iothread_get_g_main_context(IOThread *iothread); =20 +/* + * Helpers used to allocate iothreads for internal use. These + * iothreads will not be seen by monitor clients when query using + * "query-iothreads". + */ +IOThread *iothread_create(const char *id, Error **errp); +void iothread_destroy(IOThread *iothread); + #endif /* IOTHREAD_H */ diff --git a/iothread.c b/iothread.c index 44c8944..74e400c 100644 --- a/iothread.c +++ b/iothread.c @@ -354,3 +354,24 @@ GMainContext *iothread_get_g_main_context(IOThread *io= thread) =20 return iothread->worker_context; } + +static Object *iothread_get_internal_parent(void) +{ + return container_get(object_get_root(), "/internal-iothreads"); +} + +IOThread *iothread_create(const char *id, Error **errp) +{ + Object *obj; + + obj =3D object_new_with_props(TYPE_IOTHREAD, + iothread_get_internal_parent(), + id, errp, NULL); + + return IOTHREAD(obj); +} + +void iothread_destroy(IOThread *iothread) +{ + object_unparent(OBJECT(iothread)); +} --=20 2.7.4 From nobody Fri May 3 19:38:28 2024 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 Return-Path: Received: from lists.gnu.org (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 1506070722993533.7091975878931; Fri, 22 Sep 2017 01:58:42 -0700 (PDT) Received: from localhost ([::1]:57419 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dvJnB-0005F7-94 for importer@patchew.org; Fri, 22 Sep 2017 04:58:37 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:55725) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dvJlA-000454-L9 for qemu-devel@nongnu.org; Fri, 22 Sep 2017 04:56:34 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dvJl9-0004zw-Lb for qemu-devel@nongnu.org; Fri, 22 Sep 2017 04:56:32 -0400 Received: from mx1.redhat.com ([209.132.183.28]:55168) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dvJl9-0004ys-D1 for qemu-devel@nongnu.org; Fri, 22 Sep 2017 04:56:31 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 5A6844E4D1 for ; Fri, 22 Sep 2017 08:56:30 +0000 (UTC) Received: from pxdev.xzpeter.org.com (ovpn-12-133.pek2.redhat.com [10.72.12.133]) by smtp.corp.redhat.com (Postfix) with ESMTP id ECB1760A98; Fri, 22 Sep 2017 08:56:26 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 5A6844E4D1 Authentication-Results: ext-mx09.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx09.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=peterx@redhat.com From: Peter Xu To: qemu-devel@nongnu.org Date: Fri, 22 Sep 2017 16:56:11 +0800 Message-Id: <1506070572-7549-3-git-send-email-peterx@redhat.com> In-Reply-To: <1506070572-7549-1-git-send-email-peterx@redhat.com> References: <1506070572-7549-1-git-send-email-peterx@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.38]); Fri, 22 Sep 2017 08:56:30 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH 2/3] iothread: export iothread_stop() 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: Fam Zheng , peterx@redhat.com, "Dr . David Alan Gilbert" , Stefan Hajnoczi , Paolo Bonzini 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" So that internal iothread users can explicitly stop one iothread without destroying it. Since at it, fix iothread_stop() to allow re-entrance. Before this patch we may call iothread_stop() twice on single iothread, while that may not be correct since qemu_thread_join() is not allowed to run twice. From manual of pthread_join(): Joining with a thread that has previously been joined results in undefined behavior. Signed-off-by: Peter Xu --- include/sysemu/iothread.h | 1 + iothread.c | 24 ++++++++++++++++-------- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/include/sysemu/iothread.h b/include/sysemu/iothread.h index b07663f..110329b 100644 --- a/include/sysemu/iothread.h +++ b/include/sysemu/iothread.h @@ -52,6 +52,7 @@ GMainContext *iothread_get_g_main_context(IOThread *iothr= ead); * "query-iothreads". */ IOThread *iothread_create(const char *id, Error **errp); +void iothread_stop(IOThread *iothread); void iothread_destroy(IOThread *iothread); =20 #endif /* IOTHREAD_H */ diff --git a/iothread.c b/iothread.c index 74e400c..894756b 100644 --- a/iothread.c +++ b/iothread.c @@ -80,13 +80,10 @@ static void *iothread_run(void *opaque) return NULL; } =20 -static int iothread_stop(Object *object, void *opaque) +void iothread_stop(IOThread *iothread) { - IOThread *iothread; - - iothread =3D (IOThread *)object_dynamic_cast(object, TYPE_IOTHREAD); - if (!iothread || !iothread->ctx) { - return 0; + if (iothread->stopping) { + return; } iothread->stopping =3D true; aio_notify(iothread->ctx); @@ -94,6 +91,17 @@ static int iothread_stop(Object *object, void *opaque) g_main_loop_quit(iothread->main_loop); } qemu_thread_join(&iothread->thread); +} + +static int iothread_stop_iter(Object *object, void *opaque) +{ + IOThread *iothread; + + iothread =3D (IOThread *)object_dynamic_cast(object, TYPE_IOTHREAD); + if (!iothread || !iothread->ctx) { + return 0; + } + iothread_stop(iothread); return 0; } =20 @@ -108,7 +116,7 @@ static void iothread_instance_finalize(Object *obj) { IOThread *iothread =3D IOTHREAD(obj); =20 - iothread_stop(obj, NULL); + iothread_stop(iothread); qemu_cond_destroy(&iothread->init_done_cond); qemu_mutex_destroy(&iothread->init_done_lock); if (!iothread->ctx) { @@ -328,7 +336,7 @@ void iothread_stop_all(void) aio_context_release(ctx); } =20 - object_child_foreach(container, iothread_stop, NULL); + object_child_foreach(container, iothread_stop_iter, NULL); } =20 static gpointer iothread_g_main_context_init(gpointer opaque) --=20 2.7.4 From nobody Fri May 3 19:38:28 2024 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1506070737016290.531458671069; Fri, 22 Sep 2017 01:58:57 -0700 (PDT) Received: from localhost ([::1]:57420 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dvJnM-0005Ll-O6 for importer@patchew.org; Fri, 22 Sep 2017 04:58:48 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:55779) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dvJlH-00049M-AX for qemu-devel@nongnu.org; Fri, 22 Sep 2017 04:56:40 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dvJlC-00053O-Fj for qemu-devel@nongnu.org; Fri, 22 Sep 2017 04:56:39 -0400 Received: from mx1.redhat.com ([209.132.183.28]:48910) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dvJlC-00052Q-9c for qemu-devel@nongnu.org; Fri, 22 Sep 2017 04:56:34 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 3F418267DB for ; Fri, 22 Sep 2017 08:56:33 +0000 (UTC) Received: from pxdev.xzpeter.org.com (ovpn-12-133.pek2.redhat.com [10.72.12.133]) by smtp.corp.redhat.com (Postfix) with ESMTP id DB1AC6060D; Fri, 22 Sep 2017 08:56:30 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 3F418267DB Authentication-Results: ext-mx06.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx06.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=peterx@redhat.com From: Peter Xu To: qemu-devel@nongnu.org Date: Fri, 22 Sep 2017 16:56:12 +0800 Message-Id: <1506070572-7549-4-git-send-email-peterx@redhat.com> In-Reply-To: <1506070572-7549-1-git-send-email-peterx@redhat.com> References: <1506070572-7549-1-git-send-email-peterx@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Fri, 22 Sep 2017 08:56:33 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH 3/3] iothread: delay the context release to finalize 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: Fam Zheng , peterx@redhat.com, "Dr . David Alan Gilbert" , Stefan Hajnoczi , Paolo Bonzini 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" When gcontext is used with iothread, the context will be destroyed during iothread_stop(). That's not good since sometimes we would like to keep the resources until iothread is destroyed, but we may want to stop the thread before that point. Delay the destruction of gcontext to iothread finalize. Then we can do: iothread_stop(thread); some_cleanup_on_resources(); iothread_destroy(thread); Signed-off-by: Peter Xu Reviewed-by: Fam Zheng --- iothread.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/iothread.c b/iothread.c index 894756b..b95369b 100644 --- a/iothread.c +++ b/iothread.c @@ -71,8 +71,6 @@ static void *iothread_run(void *opaque) g_main_loop_unref(loop); =20 g_main_context_pop_thread_default(iothread->worker_context); - g_main_context_unref(iothread->worker_context); - iothread->worker_context =3D NULL; } } =20 @@ -117,6 +115,10 @@ static void iothread_instance_finalize(Object *obj) IOThread *iothread =3D IOTHREAD(obj); =20 iothread_stop(iothread); + if (iothread->worker_context) { + g_main_context_unref(iothread->worker_context); + iothread->worker_context =3D NULL; + } qemu_cond_destroy(&iothread->init_done_cond); qemu_mutex_destroy(&iothread->init_done_lock); if (!iothread->ctx) { --=20 2.7.4