diff mbox series

[RFC,v3,10/27] monitor: create monitor dedicate iothread

Message ID 20171106094643.14881-11-peterx@redhat.com
State New
Headers show
Series [RFC,v3,01/27] char-io: fix possible race on IOWatchPoll | expand

Commit Message

Peter Xu Nov. 6, 2017, 9:46 a.m. UTC
Create one IOThread for the monitors, prepared to handle all the
input/output IOs using existing iothread framework.

Signed-off-by: Peter Xu <peterx@redhat.com>
---
 monitor.c | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)

Comments

Fam Zheng Nov. 7, 2017, 7:11 a.m. UTC | #1
On Mon, 11/06 17:46, Peter Xu wrote:
> +static GMainContext *monitor_io_context_get(void)
> +{
> +    return iothread_get_g_main_context(mon_global.mon_iothread);
> +}
> +
> +static void monitor_iothread_init(void)
> +{
> +    mon_global.mon_iothread = iothread_create("monitor_iothread",
> +                                              &error_abort);
> +    /*
> +     * GContext in iothread is using lazy init - the first time we
> +     * fetch the context we'll have that initialized.
> +     */
> +    monitor_io_context_get();

Why do you need an eager init here?

Fam
Peter Xu Nov. 8, 2017, 7:25 a.m. UTC | #2
On Tue, Nov 07, 2017 at 03:11:31PM +0800, Fam Zheng wrote:
> On Mon, 11/06 17:46, Peter Xu wrote:
> > +static GMainContext *monitor_io_context_get(void)
> > +{
> > +    return iothread_get_g_main_context(mon_global.mon_iothread);
> > +}
> > +
> > +static void monitor_iothread_init(void)
> > +{
> > +    mon_global.mon_iothread = iothread_create("monitor_iothread",
> > +                                              &error_abort);
> > +    /*
> > +     * GContext in iothread is using lazy init - the first time we
> > +     * fetch the context we'll have that initialized.
> > +     */
> > +    monitor_io_context_get();
> 
> Why do you need an eager init here?

I thought putting this line with IOThread creation will make it more
clear that the IOThread is using GMainContext polling.  I am not that
strong to keep it, but I would prefer.
Fam Zheng Nov. 8, 2017, 11:18 a.m. UTC | #3
On Wed, 11/08 15:25, Peter Xu wrote:
> On Tue, Nov 07, 2017 at 03:11:31PM +0800, Fam Zheng wrote:
> > On Mon, 11/06 17:46, Peter Xu wrote:
> > > +static GMainContext *monitor_io_context_get(void)
> > > +{
> > > +    return iothread_get_g_main_context(mon_global.mon_iothread);
> > > +}
> > > +
> > > +static void monitor_iothread_init(void)
> > > +{
> > > +    mon_global.mon_iothread = iothread_create("monitor_iothread",
> > > +                                              &error_abort);
> > > +    /*
> > > +     * GContext in iothread is using lazy init - the first time we
> > > +     * fetch the context we'll have that initialized.
> > > +     */
> > > +    monitor_io_context_get();
> > 
> > Why do you need an eager init here?
> 
> I thought putting this line with IOThread creation will make it more
> clear that the IOThread is using GMainContext polling.  I am not that
> strong to keep it, but I would prefer.

The "iothread only starts using GMainContext polling after the lazy init
happens" is completely opaque to its user, so I don't think there is anything
but confusion to have this.

Fam
Peter Xu Nov. 8, 2017, 11:35 a.m. UTC | #4
On Wed, Nov 08, 2017 at 07:18:44PM +0800, Fam Zheng wrote:
> On Wed, 11/08 15:25, Peter Xu wrote:
> > On Tue, Nov 07, 2017 at 03:11:31PM +0800, Fam Zheng wrote:
> > > On Mon, 11/06 17:46, Peter Xu wrote:
> > > > +static GMainContext *monitor_io_context_get(void)
> > > > +{
> > > > +    return iothread_get_g_main_context(mon_global.mon_iothread);
> > > > +}
> > > > +
> > > > +static void monitor_iothread_init(void)
> > > > +{
> > > > +    mon_global.mon_iothread = iothread_create("monitor_iothread",
> > > > +                                              &error_abort);
> > > > +    /*
> > > > +     * GContext in iothread is using lazy init - the first time we
> > > > +     * fetch the context we'll have that initialized.
> > > > +     */
> > > > +    monitor_io_context_get();
> > > 
> > > Why do you need an eager init here?
> > 
> > I thought putting this line with IOThread creation will make it more
> > clear that the IOThread is using GMainContext polling.  I am not that
> > strong to keep it, but I would prefer.
> 
> The "iothread only starts using GMainContext polling after the lazy init
> happens" is completely opaque to its user, so I don't think there is anything
> but confusion to have this.

Makes sense.  Let me remove it.
diff mbox series

Patch

diff --git a/monitor.c b/monitor.c
index a70ab5606b..df1ec8d037 100644
--- a/monitor.c
+++ b/monitor.c
@@ -76,6 +76,7 @@ 
 #include "qmp-introspect.h"
 #include "sysemu/qtest.h"
 #include "sysemu/cpus.h"
+#include "sysemu/iothread.h"
 #include "qemu/cutils.h"
 #include "qapi/qmp/dispatch.h"
 
@@ -208,6 +209,12 @@  struct Monitor {
     QTAILQ_ENTRY(Monitor) entry;
 };
 
+struct MonitorGlobal {
+    IOThread *mon_iothread;
+};
+
+static struct MonitorGlobal mon_global;
+
 /* QMP checker flags */
 #define QMP_ACCEPT_UNKNOWNS 1
 
@@ -4034,12 +4041,29 @@  static void sortcmdlist(void)
     qsort((void *)info_cmds, array_num, elem_size, compare_mon_cmd);
 }
 
+static GMainContext *monitor_io_context_get(void)
+{
+    return iothread_get_g_main_context(mon_global.mon_iothread);
+}
+
+static void monitor_iothread_init(void)
+{
+    mon_global.mon_iothread = iothread_create("monitor_iothread",
+                                              &error_abort);
+    /*
+     * GContext in iothread is using lazy init - the first time we
+     * fetch the context we'll have that initialized.
+     */
+    monitor_io_context_get();
+}
+
 void monitor_init_globals(void)
 {
     monitor_init_qmp_commands();
     monitor_qapi_event_init();
     sortcmdlist();
     qemu_mutex_init(&monitor_lock);
+    monitor_iothread_init();
 }
 
 /* These functions just adapt the readline interface in a typesafe way.  We
@@ -4117,6 +4141,13 @@  void monitor_cleanup(void)
 {
     Monitor *mon, *next;
 
+    /*
+     * We need to explicitly stop the iothread (but not destroy it),
+     * cleanup the monitor resources, then destroy the iothread.  See
+     * again on the glib bug mentioned in 2b316774f6 for a reason.
+     */
+    iothread_stop(mon_global.mon_iothread);
+
     qemu_mutex_lock(&monitor_lock);
     QTAILQ_FOREACH_SAFE(mon, &mon_list, entry, next) {
         QTAILQ_REMOVE(&mon_list, mon, entry);
@@ -4124,6 +4155,9 @@  void monitor_cleanup(void)
         g_free(mon);
     }
     qemu_mutex_unlock(&monitor_lock);
+
+    iothread_destroy(mon_global.mon_iothread);
+    mon_global.mon_iothread = NULL;
 }
 
 QemuOptsList qemu_mon_opts = {