From nobody Thu May 2 04:54:33 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; 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 1523364658102646.1044909491925; Tue, 10 Apr 2018 05:50:58 -0700 (PDT) Received: from localhost ([::1]:42654 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1f5sjh-0005Lg-Cn for importer@patchew.org; Tue, 10 Apr 2018 08:50:57 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:43348) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1f5siC-0004ES-Fn for qemu-devel@nongnu.org; Tue, 10 Apr 2018 08:49:27 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1f5siB-0000k5-EM for qemu-devel@nongnu.org; Tue, 10 Apr 2018 08:49:24 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:56082 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 1f5siB-0000jl-9Y for qemu-devel@nongnu.org; Tue, 10 Apr 2018 08:49:23 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id D0B557C3B3; Tue, 10 Apr 2018 12:49:22 +0000 (UTC) Received: from xz-mi.nay.redhat.com (dhcp-14-151.nay.redhat.com [10.66.14.151]) by smtp.corp.redhat.com (Postfix) with ESMTP id 4BB152023227; Tue, 10 Apr 2018 12:49:19 +0000 (UTC) From: Peter Xu To: qemu-devel@nongnu.org Date: Tue, 10 Apr 2018 20:49:12 +0800 Message-Id: <20180410124913.10832-2-peterx@redhat.com> In-Reply-To: <20180410124913.10832-1-peterx@redhat.com> References: <20180410124913.10832-1-peterx@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.2]); Tue, 10 Apr 2018 12:49:22 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.2]); Tue, 10 Apr 2018 12:49:22 +0000 (UTC) for IP:'10.11.54.4' DOMAIN:'int-mx04.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'peterx@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 1/2] qemu-thread: always keep the posix wrapper layer 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 , Markus Armbruster , peterx@redhat.com, "Dr . David Alan Gilbert" , Stefan Hajnoczi , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , Paolo Bonzini , =?UTF-8?q?Alex=20Benn=C3=A9e?= 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" We will conditionally have a wrapper layer depending on whether the host has the PTHREAD_SETNAME capability. It complicates stuff. Let's just keep the wrapper there, meanwhile we opt out the pthread_setname_np() call only. The layer can be helpful in future patches to pass data from the parent thread to the child thread. Signed-off-by: Peter Xu --- util/qemu-thread-posix.c | 33 +++++++++++++-------------------- 1 file changed, 13 insertions(+), 20 deletions(-) diff --git a/util/qemu-thread-posix.c b/util/qemu-thread-posix.c index b789cf32e9..3ae96210d6 100644 --- a/util/qemu-thread-posix.c +++ b/util/qemu-thread-posix.c @@ -482,7 +482,6 @@ static void __attribute__((constructor)) qemu_thread_at= exit_init(void) } =20 =20 -#ifdef CONFIG_PTHREAD_SETNAME_NP typedef struct { void *(*start_routine)(void *); void *arg; @@ -498,13 +497,15 @@ static void *qemu_thread_start(void *args) /* Attempt to set the threads name; note that this is for debug, so * we're not going to fail if we can't set it. */ - pthread_setname_np(pthread_self(), qemu_thread_args->name); +#ifdef CONFIG_PTHREAD_SETNAME_NP + if (qemu_thread_args->name) { + pthread_setname_np(pthread_self(), qemu_thread_args->name); + } +#endif g_free(qemu_thread_args->name); g_free(qemu_thread_args); return start_routine(arg); } -#endif - =20 void qemu_thread_create(QemuThread *thread, const char *name, void *(*start_routine)(void*), @@ -513,6 +514,7 @@ void qemu_thread_create(QemuThread *thread, const char = *name, sigset_t set, oldset; int err; pthread_attr_t attr; + QemuThreadArgs *qemu_thread_args; =20 err =3D pthread_attr_init(&attr); if (err) { @@ -527,22 +529,13 @@ void qemu_thread_create(QemuThread *thread, const cha= r *name, sigfillset(&set); pthread_sigmask(SIG_SETMASK, &set, &oldset); =20 -#ifdef CONFIG_PTHREAD_SETNAME_NP - if (name_threads) { - QemuThreadArgs *qemu_thread_args; - qemu_thread_args =3D g_new0(QemuThreadArgs, 1); - qemu_thread_args->name =3D g_strdup(name); - qemu_thread_args->start_routine =3D start_routine; - qemu_thread_args->arg =3D arg; - - err =3D pthread_create(&thread->thread, &attr, - qemu_thread_start, qemu_thread_args); - } else -#endif - { - err =3D pthread_create(&thread->thread, &attr, - start_routine, arg); - } + qemu_thread_args =3D g_new0(QemuThreadArgs, 1); + qemu_thread_args->name =3D g_strdup(name); + qemu_thread_args->start_routine =3D start_routine; + qemu_thread_args->arg =3D arg; + + err =3D pthread_create(&thread->thread, &attr, + qemu_thread_start, qemu_thread_args); =20 if (err) error_exit(err, __func__); --=20 2.14.3 From nobody Thu May 2 04:54:33 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; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 1523364836175945.5059183768732; Tue, 10 Apr 2018 05:53:56 -0700 (PDT) Received: from localhost ([::1]:42797 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1f5smU-0007Yr-4j for importer@patchew.org; Tue, 10 Apr 2018 08:53:50 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:43460) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1f5siG-0004IE-QW for qemu-devel@nongnu.org; Tue, 10 Apr 2018 08:49:32 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1f5siF-0000n5-LA for qemu-devel@nongnu.org; Tue, 10 Apr 2018 08:49:28 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:56088 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 1f5siF-0000mf-GW for qemu-devel@nongnu.org; Tue, 10 Apr 2018 08:49:27 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 10B5A7C3B3; Tue, 10 Apr 2018 12:49:27 +0000 (UTC) Received: from xz-mi.nay.redhat.com (dhcp-14-151.nay.redhat.com [10.66.14.151]) by smtp.corp.redhat.com (Postfix) with ESMTP id 6732C2023227; Tue, 10 Apr 2018 12:49:23 +0000 (UTC) From: Peter Xu To: qemu-devel@nongnu.org Date: Tue, 10 Apr 2018 20:49:13 +0800 Message-Id: <20180410124913.10832-3-peterx@redhat.com> In-Reply-To: <20180410124913.10832-1-peterx@redhat.com> References: <20180410124913.10832-1-peterx@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.2]); Tue, 10 Apr 2018 12:49:27 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.2]); Tue, 10 Apr 2018 12:49:27 +0000 (UTC) for IP:'10.11.54.4' DOMAIN:'int-mx04.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'peterx@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 2/2] qemu-thread: let cur_mon be per-thread 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 , Markus Armbruster , peterx@redhat.com, "Dr . David Alan Gilbert" , Stefan Hajnoczi , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , Paolo Bonzini , =?UTF-8?q?Alex=20Benn=C3=A9e?= 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" cur_mon was only used in main loop so we don't really need that to be per-thread variable. Now it's possible that we have more than one thread to operate on it. Let's start to let it be per-thread variable. In case we'll create threads within a valid cur_mon setup, we'd better let the child threads to inherit the cur_mon from parent thread too. Do that for both posix and win32 threads. Signed-off-by: Peter Xu --- include/monitor/monitor.h | 2 +- include/qemu/thread-win32.h | 1 + monitor.c | 2 +- stubs/monitor.c | 2 +- tests/test-util-sockets.c | 2 +- util/qemu-thread-posix.c | 6 ++++++ util/qemu-thread-win32.c | 6 ++++++ 7 files changed, 17 insertions(+), 4 deletions(-) diff --git a/include/monitor/monitor.h b/include/monitor/monitor.h index d6ab70cae2..2ef5e04b37 100644 --- a/include/monitor/monitor.h +++ b/include/monitor/monitor.h @@ -6,7 +6,7 @@ #include "qapi/qapi-types-misc.h" #include "qemu/readline.h" =20 -extern Monitor *cur_mon; +extern __thread Monitor *cur_mon; =20 /* flags for monitor_init */ /* 0x01 unused */ diff --git a/include/qemu/thread-win32.h b/include/qemu/thread-win32.h index 3a05e3b3aa..f4d4cd96a1 100644 --- a/include/qemu/thread-win32.h +++ b/include/qemu/thread-win32.h @@ -39,6 +39,7 @@ typedef struct QemuThreadData QemuThreadData; struct QemuThread { QemuThreadData *data; unsigned tid; + Monitor *current_monitor; }; =20 /* Only valid for joinable threads. */ diff --git a/monitor.c b/monitor.c index 51f4cf480f..5035e42364 100644 --- a/monitor.c +++ b/monitor.c @@ -266,7 +266,7 @@ static mon_cmd_t info_cmds[]; =20 QmpCommandList qmp_commands, qmp_cap_negotiation_commands; =20 -Monitor *cur_mon; +__thread Monitor *cur_mon; =20 static QEMUClockType event_clock_type =3D QEMU_CLOCK_REALTIME; =20 diff --git a/stubs/monitor.c b/stubs/monitor.c index e018c8f594..3890771bb5 100644 --- a/stubs/monitor.c +++ b/stubs/monitor.c @@ -3,7 +3,7 @@ #include "qemu-common.h" #include "monitor/monitor.h" =20 -Monitor *cur_mon =3D NULL; +__thread Monitor *cur_mon; =20 int monitor_get_fd(Monitor *mon, const char *name, Error **errp) { diff --git a/tests/test-util-sockets.c b/tests/test-util-sockets.c index acadd85e8f..6195a3ac36 100644 --- a/tests/test-util-sockets.c +++ b/tests/test-util-sockets.c @@ -69,7 +69,7 @@ int monitor_get_fd(Monitor *mon, const char *fdname, Erro= r **errp) * stubs/monitor.c is defined, to make sure monitor.o is discarded * otherwise we get duplicate syms at link time. */ -Monitor *cur_mon; +__thread Monitor *cur_mon; void monitor_init(Chardev *chr, int flags) {} =20 =20 diff --git a/util/qemu-thread-posix.c b/util/qemu-thread-posix.c index 3ae96210d6..8d13da1b09 100644 --- a/util/qemu-thread-posix.c +++ b/util/qemu-thread-posix.c @@ -14,6 +14,7 @@ #include "qemu/thread.h" #include "qemu/atomic.h" #include "qemu/notify.h" +#include "monitor/monitor.h" #include "trace.h" =20 static bool name_threads; @@ -486,6 +487,7 @@ typedef struct { void *(*start_routine)(void *); void *arg; char *name; + Monitor *current_monitor; } QemuThreadArgs; =20 static void *qemu_thread_start(void *args) @@ -494,6 +496,9 @@ static void *qemu_thread_start(void *args) void *(*start_routine)(void *) =3D qemu_thread_args->start_routine; void *arg =3D qemu_thread_args->arg; =20 + /* Inherit the cur_mon pointer from father thread */ + cur_mon =3D qemu_thread_args->current_monitor; + /* Attempt to set the threads name; note that this is for debug, so * we're not going to fail if we can't set it. */ @@ -533,6 +538,7 @@ void qemu_thread_create(QemuThread *thread, const char = *name, qemu_thread_args->name =3D g_strdup(name); qemu_thread_args->start_routine =3D start_routine; qemu_thread_args->arg =3D arg; + qemu_thread_args->current_monitor =3D cur_mon; =20 err =3D pthread_create(&thread->thread, &attr, qemu_thread_start, qemu_thread_args); diff --git a/util/qemu-thread-win32.c b/util/qemu-thread-win32.c index ab60c0d557..b5197dbc78 100644 --- a/util/qemu-thread-win32.c +++ b/util/qemu-thread-win32.c @@ -19,6 +19,7 @@ #include "qemu-common.h" #include "qemu/thread.h" #include "qemu/notify.h" +#include "monitor/monitor.h" #include "trace.h" #include =20 @@ -298,6 +299,7 @@ struct QemuThreadData { void *arg; short mode; NotifierList exit; + Monitor *current_monitor; =20 /* Only used for joinable threads. */ bool exited; @@ -339,6 +341,9 @@ static unsigned __stdcall win32_start_routine(void *arg) void *(*start_routine)(void *) =3D data->start_routine; void *thread_arg =3D data->arg; =20 + /* Inherit the cur_mon pointer from father thread */ + cur_mon =3D data->current_monitor; + qemu_thread_data =3D data; qemu_thread_exit(start_routine(thread_arg)); abort(); @@ -401,6 +406,7 @@ void qemu_thread_create(QemuThread *thread, const char = *name, data->arg =3D arg; data->mode =3D mode; data->exited =3D false; + data->current_monitor =3D cur_mon; notifier_list_init(&data->exit); =20 if (data->mode !=3D QEMU_THREAD_DETACHED) { --=20 2.14.3