summaryrefslogtreecommitdiff
path: root/monitor.c
diff options
context:
space:
mode:
authorPeter Xu <peterx@redhat.com>2018-03-09 16:59:55 +0800
committerEric Blake <eblake@redhat.com>2018-03-19 14:58:37 -0500
commitdf152fb950e3e00d0186cbdc412dc8ddcc618621 (patch)
treec57d0301cf69e0d82dc34581ebc8864b3a4727e1 /monitor.c
parent546aa56674305a4ab45bea4f36956651fd892b04 (diff)
downloadqemu-df152fb950e3e00d0186cbdc412dc8ddcc618621.tar.gz
monitor: let suspend_cnt be thread safe
Monitor code now can be run in more than one thread. Let it be thread safe when accessing suspend_cnt counter. Reviewed-by: Eric Blake <eblake@redhat.com> Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com> Signed-off-by: Peter Xu <peterx@redhat.com> Message-Id: <20180309090006.10018-13-peterx@redhat.com> Signed-off-by: Eric Blake <eblake@redhat.com>
Diffstat (limited to 'monitor.c')
-rw-r--r--monitor.c15
1 files changed, 8 insertions, 7 deletions
diff --git a/monitor.c b/monitor.c
index ec209c9141..00de3c37ab 100644
--- a/monitor.c
+++ b/monitor.c
@@ -194,7 +194,7 @@ struct Monitor {
CharBackend chr;
int reset_seen;
int flags;
- int suspend_cnt;
+ int suspend_cnt; /* Needs to be accessed atomically */
bool skip_flush;
bool use_io_thr;
@@ -3819,7 +3819,7 @@ static int monitor_can_read(void *opaque)
{
Monitor *mon = opaque;
- return (mon->suspend_cnt == 0) ? 1 : 0;
+ return !atomic_mb_read(&mon->suspend_cnt);
}
/*
@@ -3951,7 +3951,7 @@ int monitor_suspend(Monitor *mon)
{
if (!mon->rs)
return -ENOTTY;
- mon->suspend_cnt++;
+ atomic_inc(&mon->suspend_cnt);
return 0;
}
@@ -3959,8 +3959,9 @@ void monitor_resume(Monitor *mon)
{
if (!mon->rs)
return;
- if (--mon->suspend_cnt == 0)
+ if (atomic_dec_fetch(&mon->suspend_cnt) == 0) {
readline_show_prompt(mon->rs);
+ }
}
static QObject *get_qmp_greeting(Monitor *mon)
@@ -4025,19 +4026,19 @@ static void monitor_event(void *opaque, int event)
monitor_resume(mon);
monitor_flush(mon);
} else {
- mon->suspend_cnt = 0;
+ atomic_mb_set(&mon->suspend_cnt, 0);
}
break;
case CHR_EVENT_MUX_OUT:
if (mon->reset_seen) {
- if (mon->suspend_cnt == 0) {
+ if (atomic_mb_read(&mon->suspend_cnt) == 0) {
monitor_printf(mon, "\n");
}
monitor_flush(mon);
monitor_suspend(mon);
} else {
- mon->suspend_cnt++;
+ atomic_inc(&mon->suspend_cnt);
}
qemu_mutex_lock(&mon->out_lock);
mon->mux_out = 1;