[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PULL 23/36] monitor: let suspend_cnt be thread safe
From: |
Eric Blake |
Subject: |
[Qemu-devel] [PULL 23/36] monitor: let suspend_cnt be thread safe |
Date: |
Mon, 12 Mar 2018 13:36:14 -0500 |
From: Peter Xu <address@hidden>
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 <address@hidden>
Reviewed-by: Stefan Hajnoczi <address@hidden>
Signed-off-by: Peter Xu <address@hidden>
Message-Id: <address@hidden>
Signed-off-by: Eric Blake <address@hidden>
---
monitor.c | 15 ++++++++-------
1 file changed, 8 insertions(+), 7 deletions(-)
diff --git a/monitor.c b/monitor.c
index a3412359ca8..608faf35c25 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;
@@ -3816,7 +3816,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);
}
/*
@@ -3948,7 +3948,7 @@ int monitor_suspend(Monitor *mon)
{
if (!mon->rs)
return -ENOTTY;
- mon->suspend_cnt++;
+ atomic_inc(&mon->suspend_cnt);
return 0;
}
@@ -3956,8 +3956,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)
@@ -4022,19 +4023,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;
--
2.14.3
- [Qemu-devel] [PULL 22/36] monitor: introduce monitor_qmp_respond(), (continued)
- [Qemu-devel] [PULL 22/36] monitor: introduce monitor_qmp_respond(), Eric Blake, 2018/03/12
- [Qemu-devel] [PULL 20/36] monitor: allow using IO thread for parsing, Eric Blake, 2018/03/12
- [Qemu-devel] [PULL 24/36] monitor: let suspend/resume work even with QMPs, Eric Blake, 2018/03/12
- [Qemu-devel] [PULL 29/36] qmp: support out-of-band (oob) execution, Eric Blake, 2018/03/12
- [Qemu-devel] [PULL 32/36] qmp: add command "x-oob-test", Eric Blake, 2018/03/12
- [Qemu-devel] [PULL 34/36] tests: qmp-test: add oob test, Eric Blake, 2018/03/12
- [Qemu-devel] [PULL 28/36] qapi: introduce new cmd option "allow-oob", Eric Blake, 2018/03/12
- [Qemu-devel] [PULL 21/36] qmp: introduce QMPCapability, Eric Blake, 2018/03/12
- [Qemu-devel] [PULL 30/36] qmp: isolate responses into io thread, Eric Blake, 2018/03/12
- [Qemu-devel] [PULL 15/36] qobject: let object_property_get_str() use new API, Eric Blake, 2018/03/12
- [Qemu-devel] [PULL 23/36] monitor: let suspend_cnt be thread safe,
Eric Blake <=
- [Qemu-devel] [PULL 27/36] monitor: send event when command queue full, Eric Blake, 2018/03/12
- [Qemu-devel] [PULL 25/36] monitor: separate QMP parser and dispatcher, Eric Blake, 2018/03/12
- [Qemu-devel] [PULL 31/36] monitor: enable IO thread for (qmp & !mux) typed, Eric Blake, 2018/03/12
- [Qemu-devel] [PULL 33/36] tests: qmp-test: verify command batching, Eric Blake, 2018/03/12
- [Qemu-devel] [PULL 35/36] block/accounting: introduce latency histogram, Eric Blake, 2018/03/12