diff mbox series

[2/2] monitor: delay monitor iothread creation

Message ID 20180921082856.1000-2-w.bumiller@proxmox.com
State New
Headers show
Series [1/2] monitor: guard iothread access by mon->use_io_thread | expand

Commit Message

Wolfgang Bumiller Sept. 21, 2018, 8:28 a.m. UTC
Commit d32749deb615 moved the call to monitor_init_globals()
to before os_daemonize(), making it an unsuitable place to
spawn the monitor iothread as it won't be inherited over the
fork() in os_daemonize().

We now spawn the thread the first time we instantiate a
monitor which actually has use_io_thread == true.
Instantiation of monitors happens only after os_daemonize().
We still need to create the qmp_dispatcher_bh when not using
iothreads, so this now still happens in
monitor_init_globals().

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
Fixes: d32749deb615 ("monitor: move init global earlier")
---
 monitor.c | 38 +++++++++++++++++++++++---------------
 1 file changed, 23 insertions(+), 15 deletions(-)

Comments

Peter Xu Sept. 25, 2018, 4:14 a.m. UTC | #1
On Fri, Sep 21, 2018 at 10:28:56AM +0200, Wolfgang Bumiller wrote:

[...]

> @@ -4738,11 +4744,13 @@ void monitor_cleanup(void)
>      /* QEMUBHs needs to be deleted before destroying the I/O thread */
>      qemu_bh_delete(qmp_dispatcher_bh);
>      qmp_dispatcher_bh = NULL;
> -    qemu_bh_delete(qmp_respond_bh);
> -    qmp_respond_bh = NULL;
> +    if (mon_iothread) {
> +        qemu_bh_delete(qmp_respond_bh);

qmp_respond_bh should have been removed.  Please rebase with master
and repost.  When do, feel free to add a cover letter as Eric
suggested.

Otherwise it looks good to me.

Thanks,

> +        qmp_respond_bh = NULL;
>  
> -    iothread_destroy(mon_iothread);
> -    mon_iothread = NULL;
> +        iothread_destroy(mon_iothread);
> +        mon_iothread = NULL;
> +    }
>  }
diff mbox series

Patch

diff --git a/monitor.c b/monitor.c
index 4802148c82..e9d44b443b 100644
--- a/monitor.c
+++ b/monitor.c
@@ -806,9 +806,14 @@  static void monitor_qapi_event_init(void)
 
 static void handle_hmp_command(Monitor *mon, const char *cmdline);
 
+static void monitor_iothread_init(void);
+
 static void monitor_data_init(Monitor *mon, bool skip_flush,
                               bool use_io_thread)
 {
+    if (use_io_thread && !mon_iothread) {
+        monitor_iothread_init();
+    }
     memset(mon, 0, sizeof(Monitor));
     qemu_mutex_init(&mon->mon_lock);
     qemu_mutex_init(&mon->qmp.qmp_queue_lock);
@@ -4551,15 +4556,6 @@  static void monitor_iothread_init(void)
     mon_iothread = iothread_create("mon_iothread", &error_abort);
 
     /*
-     * The dispatcher BH must run in the main loop thread, since we
-     * have commands assuming that context.  It would be nice to get
-     * rid of those assumptions.
-     */
-    qmp_dispatcher_bh = aio_bh_new(iohandler_get_aio_context(),
-                                   monitor_qmp_bh_dispatcher,
-                                   NULL);
-
-    /*
      * The responder BH must be run in the monitor I/O thread, so that
      * monitors that are using the I/O thread have their output
      * written by the I/O thread.
@@ -4576,7 +4572,15 @@  void monitor_init_globals(void)
     sortcmdlist();
     qemu_mutex_init(&monitor_lock);
     qemu_mutex_init(&mon_fdsets_lock);
-    monitor_iothread_init();
+
+    /*
+     * The dispatcher BH must run in the main loop thread, since we
+     * have commands assuming that context.  It would be nice to get
+     * rid of those assumptions.
+     */
+    qmp_dispatcher_bh = aio_bh_new(iohandler_get_aio_context(),
+                                   monitor_qmp_bh_dispatcher,
+                                   NULL);
 }
 
 /* These functions just adapt the readline interface in a typesafe way.  We
@@ -4717,7 +4721,9 @@  void monitor_cleanup(void)
      * we need to unregister from chardev below in
      * monitor_data_destroy(), and chardev is not thread-safe yet
      */
-    iothread_stop(mon_iothread);
+    if (mon_iothread) {
+        iothread_stop(mon_iothread);
+    }
 
     /*
      * Flush all response queues.  Note that even after this flush,
@@ -4738,11 +4744,13 @@  void monitor_cleanup(void)
     /* QEMUBHs needs to be deleted before destroying the I/O thread */
     qemu_bh_delete(qmp_dispatcher_bh);
     qmp_dispatcher_bh = NULL;
-    qemu_bh_delete(qmp_respond_bh);
-    qmp_respond_bh = NULL;
+    if (mon_iothread) {
+        qemu_bh_delete(qmp_respond_bh);
+        qmp_respond_bh = NULL;
 
-    iothread_destroy(mon_iothread);
-    mon_iothread = NULL;
+        iothread_destroy(mon_iothread);
+        mon_iothread = NULL;
+    }
 }
 
 QemuOptsList qemu_mon_opts = {