qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Paolo Bonzini <pbonzini@redhat.com>
To: qemu-devel@nongnu.org
Cc: kwolf@redhat.com, famz@redhat.com, kraxel@redhat.com,
	stefanha@redhat.com, lcapitulino@redhat.com
Subject: [Qemu-devel] [PATCH 5/6] monitor: protect outbuf with mutex
Date: Tue,  3 Jun 2014 18:39:10 +0200	[thread overview]
Message-ID: <1401813551-6667-6-git-send-email-pbonzini@redhat.com> (raw)
In-Reply-To: <1401813551-6667-1-git-send-email-pbonzini@redhat.com>

This lets the block layer emit QMP events from outside the I/O thread.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 monitor.c | 35 ++++++++++++++++++++++++++++-------
 1 file changed, 28 insertions(+), 7 deletions(-)

diff --git a/monitor.c b/monitor.c
index 342e83b..ebc66fb 100644
--- a/monitor.c
+++ b/monitor.c
@@ -191,8 +191,11 @@ struct Monitor {
     int flags;
     int suspend_cnt;
     bool skip_flush;
+
+    QemuMutex out_lock;
     QString *outbuf;
-    guint watch;
+    guint out_watch;
+
     ReadLineState *rs;
     MonitorControl *mc;
     CPUState *mon_cpu;
@@ -265,17 +268,22 @@ int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
     }
 }
 
+static void do_monitor_flush(Monitor *mon);
+
 static gboolean monitor_unblocked(GIOChannel *chan, GIOCondition cond,
                                   void *opaque)
 {
     Monitor *mon = opaque;
 
-    mon->watch = 0;
+    qemu_mutex_lock(&mon->out_lock);
+    mon->out_watch = 0;
     monitor_flush(mon);
+    qemu_mutex_unlock(&mon->out_lock);
     return FALSE;
 }
 
-void monitor_flush(Monitor *mon)
+/* Called with mon->out_lock held.  */
+static void do_monitor_flush(Monitor *mon)
 {
     int rc;
     size_t len;
@@ -302,18 +310,26 @@ void monitor_flush(Monitor *mon)
             QDECREF(mon->outbuf);
             mon->outbuf = tmp;
         }
-        if (mon->watch == 0) {
-            mon->watch = qemu_chr_fe_add_watch(mon->chr, G_IO_OUT,
-                                               monitor_unblocked, mon);
+        if (mon->out_watch == 0) {
+            mon->out_watch = qemu_chr_fe_add_watch(mon->chr, G_IO_OUT,
+                                                   monitor_unblocked, mon);
         }
     }
 }
 
+void monitor_flush(Monitor *mon)
+{
+    qemu_mutex_lock(&mon->out_lock);
+    do_monitor_flush(mon);
+    qemu_mutex_unlock(&mon->out_lock);
+}
+
 /* flush at every end of line */
 static void monitor_puts(Monitor *mon, const char *str)
 {
     char c;
 
+    qemu_mutex_lock(&mon->out_lock);
     for(;;) {
         c = *str++;
         if (c == '\0')
@@ -323,9 +339,10 @@ static void monitor_puts(Monitor *mon, const char *str)
         }
         qstring_append_chr(mon->outbuf, c);
         if (c == '\n') {
-            monitor_flush(mon);
+            do_monitor_flush(mon);
         }
     }
+    qemu_mutex_unlock(&mon->out_lock);
 }
 
 void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
@@ -690,6 +707,7 @@ static void handle_user_command(Monitor *mon, const char *cmdline);
 static void monitor_data_init(Monitor *mon)
 {
     memset(mon, 0, sizeof(Monitor));
+    qemu_mutex_init(&mon->out_lock);
     mon->outbuf = qstring_new();
     /* Use *mon_cmds by default. */
     mon->cmd_table = mon_cmds;
@@ -698,6 +716,7 @@ static void monitor_data_init(Monitor *mon)
 static void monitor_data_destroy(Monitor *mon)
 {
     QDECREF(mon->outbuf);
+    qemu_mutex_destroy(&mon->out_lock);
 }
 
 char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index,
@@ -725,11 +744,13 @@ char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index,
     handle_user_command(&hmp, command_line);
     cur_mon = old_mon;
 
+    qemu_mutex_lock(&hmp.out_lock);
     if (qstring_get_length(hmp.outbuf) > 0) {
         output = g_strdup(qstring_get_str(hmp.outbuf));
     } else {
         output = g_strdup("");
     }
+    qemu_mutex_unlock(&hmp.out_lock);
 
 out:
     monitor_data_destroy(&hmp);
-- 
1.8.3.1

  parent reply	other threads:[~2014-06-03 16:39 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-06-03 16:39 [Qemu-devel] [PATCH 0/5] qemu-char/monitor: make monitor_puts thread safe Paolo Bonzini
2014-06-03 16:39 ` [Qemu-devel] [PATCH 1/6] qemu-char: introduce qemu_chr_alloc Paolo Bonzini
2014-06-11  6:28   ` Fam Zheng
2014-06-03 16:39 ` [Qemu-devel] [PATCH 2/6] qemu-char: do not call chr_write directly Paolo Bonzini
2014-06-11  6:30   ` Fam Zheng
2014-06-03 16:39 ` [Qemu-devel] [PATCH 3/6] qemu-char: move pty_chr_update_read_handler around Paolo Bonzini
2014-06-11  6:32   ` Fam Zheng
2014-06-03 16:39 ` [Qemu-devel] [PATCH 4/6] qemu-char: make writes thread-safe Paolo Bonzini
2014-06-11  6:59   ` Fam Zheng
2014-06-11  8:16     ` Paolo Bonzini
2014-06-03 16:39 ` Paolo Bonzini [this message]
2014-06-10 14:10   ` [Qemu-devel] [PATCH 5/6] monitor: protect outbuf with mutex Luiz Capitulino
2014-06-10 14:24     ` Paolo Bonzini
2014-06-10 14:28       ` Luiz Capitulino
2014-06-03 16:39 ` [Qemu-devel] [PATCH 6/6] monitor: protect event emission Paolo Bonzini
2014-06-10 13:33   ` Luiz Capitulino
2014-06-27  9:43 ` [Qemu-devel] [PATCH 0/5] qemu-char/monitor: make monitor_puts thread safe Stefan Hajnoczi
2014-06-27 12:33   ` Luiz Capitulino

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=1401813551-6667-6-git-send-email-pbonzini@redhat.com \
    --to=pbonzini@redhat.com \
    --cc=famz@redhat.com \
    --cc=kraxel@redhat.com \
    --cc=kwolf@redhat.com \
    --cc=lcapitulino@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).