From nobody Sun Apr 28 19:21:43 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 1523513573695731.8721161492047; Wed, 11 Apr 2018 23:12:53 -0700 (PDT) Received: from localhost ([::1]:38443 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1f6VTV-0001Bx-NW for importer@patchew.org; Thu, 12 Apr 2018 02:12:49 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:53673) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1f6VSG-0000Rq-7z for qemu-devel@nongnu.org; Thu, 12 Apr 2018 02:11:33 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1f6VSC-0006d5-8k for qemu-devel@nongnu.org; Thu, 12 Apr 2018 02:11:32 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:52606 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 1f6VSC-0006bI-36 for qemu-devel@nongnu.org; Thu, 12 Apr 2018 02:11:28 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id D4AC58DC39 for ; Thu, 12 Apr 2018 06:11:23 +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 424CF10B2B5E; Thu, 12 Apr 2018 06:11:09 +0000 (UTC) From: Peter Xu To: qemu-devel@nongnu.org Date: Thu, 12 Apr 2018 14:11:08 +0800 Message-Id: <20180412061108.10875-1-peterx@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.3 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.2]); Thu, 12 Apr 2018 06:11:23 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.2]); Thu, 12 Apr 2018 06:11:23 +0000 (UTC) for IP:'10.11.54.3' DOMAIN:'int-mx03.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 v3] monitor: 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: "Dr . David Alan Gilbert" , peterx@redhat.com, Markus Armbruster , Stefan Hajnoczi , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , 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" In the future the monitor iothread may be accessing the cur_mon as well (via monitor_qmp_dispatch_one()). Before we introduce a real Out-Of-Band command, let's convert the cur_mon variable to be a per-thread variable to make sure there won't be a race between threads. Note that thread variables are not initialized to a valid value when new thread is created. However for our case we don't need to set it up, since the cur_mon variable is only used in such a pattern: old_mon =3D cur_mon; cur_mon =3D xxx; (do something, read cur_mon if necessary in the stack) cur_mon =3D old_mon; It plays a role as stack variable, so no need to be initialized at all. We only need to make sure the variable won't be changed unexpectedly by other threads. Signed-off-by: Peter Xu Reviewed-by: Eric Blake Reviewed-by: Marc-Andr=C3=A9 Lureau Reviewed-by: Stefan Hajnoczi --- v3: - fix code style warning from patchew v2: - drop qemu-thread changes --- include/monitor/monitor.h | 2 +- monitor.c | 2 +- stubs/monitor.c | 2 +- tests/test-util-sockets.c | 2 +- 4 files changed, 4 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/monitor.c b/monitor.c index 39f8ee17ba..c90c31b6b3 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 --=20 2.14.3