Monitor code now can be run in more than one thread. Let it be thread safe when accessing suspend_cnt counter.
Signed-off-by: Peter Xu <pet...@redhat.com> --- monitor.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/monitor.c b/monitor.c index 35d8925636..6ac1b2065d 100644 --- a/monitor.c +++ b/monitor.c @@ -3874,7 +3874,7 @@ static int monitor_can_read(void *opaque) { Monitor *mon = opaque; - return (mon->suspend_cnt == 0) ? 1 : 0; + return (atomic_read(&mon->suspend_cnt) == 0) ? 1 : 0; } /* @@ -4003,7 +4003,7 @@ int monitor_suspend(Monitor *mon) { if (!mon->rs) return -ENOTTY; - mon->suspend_cnt++; + atomic_inc(&mon->suspend_cnt); return 0; } @@ -4011,8 +4011,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(void) @@ -4078,19 +4079,19 @@ static void monitor_event(void *opaque, int event) monitor_resume(mon); monitor_flush(mon); } else { - mon->suspend_cnt = 0; + atomic_set(&mon->suspend_cnt, 0); } break; case CHR_EVENT_MUX_OUT: if (mon->reset_seen) { - if (mon->suspend_cnt == 0) { + if (atomic_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