From: Peter Xu <peterx@redhat.com>
To: qemu-devel@nongnu.org
Cc: "Paolo Bonzini" <pbonzini@redhat.com>,
"Alex Bennée" <alex.bennee@linaro.org>,
"Fam Zheng" <famz@redhat.com>,
peterx@redhat.com, "Eric Blake" <eblake@redhat.com>,
"Marc-André Lureau" <marcandre.lureau@redhat.com>,
"Markus Armbruster" <armbru@redhat.com>,
"Stefan Hajnoczi" <stefanha@redhat.com>,
"Dr . David Alan Gilbert" <dgilbert@redhat.com>
Subject: [Qemu-devel] [PATCH 2/2] qemu-thread: let cur_mon be per-thread
Date: Tue, 10 Apr 2018 20:49:13 +0800 [thread overview]
Message-ID: <20180410124913.10832-3-peterx@redhat.com> (raw)
In-Reply-To: <20180410124913.10832-1-peterx@redhat.com>
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 <peterx@redhat.com>
---
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"
-extern Monitor *cur_mon;
+extern __thread Monitor *cur_mon;
/* 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;
};
/* 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[];
QmpCommandList qmp_commands, qmp_cap_negotiation_commands;
-Monitor *cur_mon;
+__thread Monitor *cur_mon;
static QEMUClockType event_clock_type = QEMU_CLOCK_REALTIME;
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"
-Monitor *cur_mon = NULL;
+__thread Monitor *cur_mon;
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, Error **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) {}
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"
static bool name_threads;
@@ -486,6 +487,7 @@ typedef struct {
void *(*start_routine)(void *);
void *arg;
char *name;
+ Monitor *current_monitor;
} QemuThreadArgs;
static void *qemu_thread_start(void *args)
@@ -494,6 +496,9 @@ static void *qemu_thread_start(void *args)
void *(*start_routine)(void *) = qemu_thread_args->start_routine;
void *arg = qemu_thread_args->arg;
+ /* Inherit the cur_mon pointer from father thread */
+ cur_mon = 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 = g_strdup(name);
qemu_thread_args->start_routine = start_routine;
qemu_thread_args->arg = arg;
+ qemu_thread_args->current_monitor = cur_mon;
err = 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 <process.h>
@@ -298,6 +299,7 @@ struct QemuThreadData {
void *arg;
short mode;
NotifierList exit;
+ Monitor *current_monitor;
/* Only used for joinable threads. */
bool exited;
@@ -339,6 +341,9 @@ static unsigned __stdcall win32_start_routine(void *arg)
void *(*start_routine)(void *) = data->start_routine;
void *thread_arg = data->arg;
+ /* Inherit the cur_mon pointer from father thread */
+ cur_mon = data->current_monitor;
+
qemu_thread_data = data;
qemu_thread_exit(start_routine(thread_arg));
abort();
@@ -401,6 +406,7 @@ void qemu_thread_create(QemuThread *thread, const char *name,
data->arg = arg;
data->mode = mode;
data->exited = false;
+ data->current_monitor = cur_mon;
notifier_list_init(&data->exit);
if (data->mode != QEMU_THREAD_DETACHED) {
--
2.14.3
next prev parent reply other threads:[~2018-04-10 12:49 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-04-10 12:49 [Qemu-devel] [PATCH 0/2] qemu-thread: allow cur_mon be per thread Peter Xu
2018-04-10 12:49 ` [Qemu-devel] [PATCH 1/2] qemu-thread: always keep the posix wrapper layer Peter Xu
2018-04-10 13:35 ` Eric Blake
2018-04-11 3:18 ` Peter Xu
2018-04-10 12:49 ` Peter Xu [this message]
2018-04-10 13:54 ` [Qemu-devel] [PATCH 2/2] qemu-thread: let cur_mon be per-thread Eric Blake
2018-04-11 3:31 ` Peter Xu
2018-04-11 1:45 ` Stefan Hajnoczi
2018-04-11 3:49 ` Peter Xu
2018-04-11 9:23 ` Paolo Bonzini
2018-04-11 9:35 ` Peter Xu
2018-04-11 9:38 ` Paolo Bonzini
2018-04-11 9:48 ` Peter Xu
2018-04-11 13:06 ` Eric Blake
2018-04-12 5:24 ` Peter Xu
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20180410124913.10832-3-peterx@redhat.com \
--to=peterx@redhat.com \
--cc=alex.bennee@linaro.org \
--cc=armbru@redhat.com \
--cc=dgilbert@redhat.com \
--cc=eblake@redhat.com \
--cc=famz@redhat.com \
--cc=marcandre.lureau@redhat.com \
--cc=pbonzini@redhat.com \
--cc=qemu-devel@nongnu.org \
--cc=stefanha@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).