From: Peter Xu <peterx@redhat.com>
To: qemu-devel@nongnu.org
Cc: peterx@redhat.com
Subject: [Qemu-devel] [PATCH 2/3] monitor: take mon_lock where proper
Date: Wed, 18 Apr 2018 16:58:30 +0800 [thread overview]
Message-ID: <20180418085831.12058-3-peterx@redhat.com> (raw)
In-Reply-To: <20180418085831.12058-1-peterx@redhat.com>
Went through all the montior.h APIs to make sure existing Monitor
related APIs will always take the new monitor lock when necessary.
Signed-off-by: Peter Xu <peterx@redhat.com>
---
monitor.c | 33 +++++++++++++++++++++++++++------
1 file changed, 27 insertions(+), 6 deletions(-)
diff --git a/monitor.c b/monitor.c
index c93aa4e22b..f4951cafbc 100644
--- a/monitor.c
+++ b/monitor.c
@@ -306,16 +306,20 @@ void monitor_read_command(Monitor *mon, int show_prompt)
if (!mon->rs)
return;
+ qemu_mutex_lock(&mon->mon_lock);
readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
if (show_prompt)
readline_show_prompt(mon->rs);
+ qemu_mutex_unlock(&mon->mon_lock);
}
int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
void *opaque)
{
if (mon->rs) {
+ qemu_mutex_lock(&mon->mon_lock);
readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
+ qemu_mutex_unlock(&mon->mon_lock);
/* prompt is printed on return from the command handler */
return 0;
} else {
@@ -1308,8 +1312,7 @@ void qmp_qmp_capabilities(bool has_enable, QMPCapabilityList *enable,
cur_mon->qmp.commands = &qmp_commands;
}
-/* set the current CPU defined by the user */
-int monitor_set_cpu(int cpu_index)
+static int monitor_set_cpu_locked(Monitor *mon, int cpu_index)
{
CPUState *cpu;
@@ -1317,15 +1320,28 @@ int monitor_set_cpu(int cpu_index)
if (cpu == NULL) {
return -1;
}
- g_free(cur_mon->mon_cpu_path);
- cur_mon->mon_cpu_path = object_get_canonical_path(OBJECT(cpu));
+ g_free(mon->mon_cpu_path);
+ mon->mon_cpu_path = object_get_canonical_path(OBJECT(cpu));
return 0;
}
+/* set the current CPU defined by the user */
+int monitor_set_cpu(int cpu_index)
+{
+ int ret;
+
+ qemu_mutex_lock(&cur_mon->mon_lock);
+ ret = monitor_set_cpu_locked(cur_mon, cpu_index);
+ qemu_mutex_unlock(&cur_mon->mon_lock);
+
+ return ret;
+}
+
static CPUState *mon_get_cpu_sync(bool synchronize)
{
CPUState *cpu;
+ qemu_mutex_lock(&cur_mon->mon_lock);
if (cur_mon->mon_cpu_path) {
cpu = (CPUState *) object_resolve_path_type(cur_mon->mon_cpu_path,
TYPE_CPU, NULL);
@@ -1336,11 +1352,14 @@ static CPUState *mon_get_cpu_sync(bool synchronize)
}
if (!cur_mon->mon_cpu_path) {
if (!first_cpu) {
+ qemu_mutex_unlock(&cur_mon->mon_lock);
return NULL;
}
- monitor_set_cpu(first_cpu->cpu_index);
+ monitor_set_cpu_locked(cur_mon, first_cpu->cpu_index);
cpu = first_cpu;
}
+ qemu_mutex_unlock(&cur_mon->mon_lock);
+
if (synchronize) {
cpu_synchronize_state(cpu);
}
@@ -2239,6 +2258,7 @@ int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
{
mon_fd_t *monfd;
+ qemu_mutex_lock(&mon->mon_lock);
QLIST_FOREACH(monfd, &mon->fds, next) {
int fd;
@@ -2252,9 +2272,10 @@ int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
QLIST_REMOVE(monfd, next);
g_free(monfd->name);
g_free(monfd);
-
+ qemu_mutex_unlock(&mon->mon_lock);
return fd;
}
+ qemu_mutex_unlock(&mon->mon_lock);
error_setg(errp, "File descriptor named '%s' has not been found", fdname);
return -1;
--
2.14.3
next prev parent reply other threads:[~2018-04-18 8:58 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-04-18 8:58 [Qemu-devel] [PATCH 0/3] monitor: let Monitor be thread safe Peter Xu
2018-04-18 8:58 ` [Qemu-devel] [PATCH 1/3] monitor: rename out_lock to mon_lock Peter Xu
2018-04-18 8:58 ` Peter Xu [this message]
2018-04-18 8:58 ` [Qemu-devel] [PATCH 3/3] monitor: add lock to protect mon_fdsets 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=20180418085831.12058-3-peterx@redhat.com \
--to=peterx@redhat.com \
--cc=qemu-devel@nongnu.org \
/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).