diff mbox series

[RFC,3/3] monitor: switch to use chardev's iothread

Message ID 20180824090826.21370-4-peterx@redhat.com
State New
Headers show
Series chardev: introduce chardev contexts | expand

Commit Message

Peter Xu Aug. 24, 2018, 9:08 a.m. UTC
Now after we have chardev maintained iothread, we switch to that one by
adding a parsing phase for monitors even before chardevs start to
initialize.  Then chardevs will use a constant gcontext since the
creation of the object and it should never change.

Remove the context setup in qemu_chr_be_update_read_handlers(), instead
assert it in qemu_chr_fe_set_handlers() properly to make sure the
context to be setup is always the one provided when chardev creates.

Note that now we kept all the context parameters in misc functions,
however it should be always setting the same context, otherwise a
programming bug.  In the future we may remove all these parameters.

Signed-off-by: Peter Xu <peterx@redhat.com>
---
 chardev/char-fe.c |  6 ++++++
 chardev/char.c    |  1 -
 monitor.c         |  4 ++--
 vl.c              | 31 +++++++++++++++++++++++++++++++
 4 files changed, 39 insertions(+), 3 deletions(-)
diff mbox series

Patch

diff --git a/chardev/char-fe.c b/chardev/char-fe.c
index b1f228e8b5..fcb232c9b6 100644
--- a/chardev/char-fe.c
+++ b/chardev/char-fe.c
@@ -267,6 +267,12 @@  void qemu_chr_fe_set_handlers(CharBackend *b,
         remove_fd_in_watch(s);
     } else {
         fe_open = 1;
+        /*
+         * Currently we don't allow to change context after chardev is
+         * created.  TODO: either remove these context parameters, or
+         * re-activate dynamic context switch for chardevs.
+         */
+        assert(context == s->gcontext);
     }
     b->chr_can_read = fd_can_read;
     b->chr_read = fd_read;
diff --git a/chardev/char.c b/chardev/char.c
index c98d245ef7..da8ca9082b 100644
--- a/chardev/char.c
+++ b/chardev/char.c
@@ -200,7 +200,6 @@  void qemu_chr_be_update_read_handlers(Chardev *s,
 {
     ChardevClass *cc = CHARDEV_GET_CLASS(s);
 
-    s->gcontext = context;
     if (cc->chr_update_read_handler) {
         cc->chr_update_read_handler(s);
     }
diff --git a/monitor.c b/monitor.c
index a1999e396c..1e45f3466e 100644
--- a/monitor.c
+++ b/monitor.c
@@ -4543,7 +4543,7 @@  static AioContext *monitor_get_aio_context(void)
 
 static void monitor_iothread_init(void)
 {
-    mon_iothread = iothread_create("mon_iothread", &error_abort);
+    mon_iothread = qemu_chr_iothread_get(CHR_CONTEXT_MONITOR);
 
     /*
      * The dispatcher BH must run in the main loop thread, since we
@@ -4735,7 +4735,7 @@  void monitor_cleanup(void)
     qemu_bh_delete(qmp_respond_bh);
     qmp_respond_bh = NULL;
 
-    iothread_destroy(mon_iothread);
+    /* It'll be cleaned up by chardev */
     mon_iothread = NULL;
 }
 
diff --git a/vl.c b/vl.c
index 25e156c020..5bc0946987 100644
--- a/vl.c
+++ b/vl.c
@@ -2349,6 +2349,32 @@  static int mon_init_func(void *opaque, QemuOpts *opts, Error **errp)
     return 0;
 }
 
+/*
+ * This setup the backends properly, it really has nothing to do with
+ * monitor itself yet.
+ */
+static int mon_setup_func(void *opaque, QemuOpts *opts, Error **errp)
+{
+    const char *chardev;
+    int flags;
+    QemuOpts *chr_opts;
+
+    flags = mon_parse_flags(opts);
+    chardev = qemu_opt_get(opts, "chardev");
+
+    /*
+     * If out-of-band is enabled on the monitor, choose the correct
+     * context for the backend before it initializes.
+     */
+    if (flags & MONITOR_USE_OOB) {
+        chr_opts = qemu_opts_find(qemu_find_opts("chardev"), chardev);
+        qemu_opt_set_number(chr_opts, "context", CHR_CONTEXT_MONITOR,
+                            &error_abort);
+    }
+
+    return 0;
+}
+
 static void monitor_parse(const char *optarg, const char *mode, bool pretty)
 {
     static int monitor_device_index = 0;
@@ -4278,6 +4304,11 @@  int main(int argc, char **argv, char **envp)
         exit(1);
     }
 
+    if (qemu_opts_foreach(qemu_find_opts("mon"),
+                          mon_setup_func, NULL, NULL)) {
+        exit(1);
+    }
+
     if (qemu_opts_foreach(qemu_find_opts("chardev"),
                           chardev_init_func, NULL, NULL)) {
         exit(1);