Blame SOURCES/kvm-qemu-iothread-IOThread-supports-the-GMainContext-eve.patch

4a2fec
From 79000a2843af86fcd026ba8eb899dfeb1da0e04d Mon Sep 17 00:00:00 2001
4a2fec
From: Stefan Hajnoczi <stefanha@redhat.com>
4a2fec
Date: Fri, 22 Dec 2017 11:08:43 +0100
4a2fec
Subject: [PATCH 25/42] qemu-iothread: IOThread supports the GMainContext event
4a2fec
 loop
4a2fec
4a2fec
RH-Author: Stefan Hajnoczi <stefanha@redhat.com>
4a2fec
Message-id: <20171222110900.24813-4-stefanha@redhat.com>
4a2fec
Patchwork-id: 78485
4a2fec
O-Subject: [RHV7.5 qemu-kvm-rhev PATCH 03/20] qemu-iothread: IOThread supports the GMainContext event loop
4a2fec
Bugzilla: 1519721
4a2fec
RH-Acked-by: Paolo Bonzini <pbonzini@redhat.com>
4a2fec
RH-Acked-by: Jeffrey Cody <jcody@redhat.com>
4a2fec
RH-Acked-by: Miroslav Rezanina <mrezanin@redhat.com>
4a2fec
4a2fec
From: Wang Yong <wang.yong155@zte.com.cn>
4a2fec
4a2fec
IOThread uses AioContext event loop and does not run a GMainContext.
4a2fec
Therefore,chardev cannot work in IOThread,such as the chardev is
4a2fec
used for colo-compare packets reception.
4a2fec
4a2fec
This patch makes the IOThread run the GMainContext event loop,
4a2fec
chardev and IOThread can work together.
4a2fec
4a2fec
Reviewed-by: Fam Zheng <famz@redhat.com>
4a2fec
Signed-off-by: Wang Yong <wang.yong155@zte.com.cn>
4a2fec
Signed-off-by: Wang Guang <wang.guang55@zte.com.cn>
4a2fec
Signed-off-by: Jason Wang <jasowang@redhat.com>
4a2fec
(cherry picked from commit 329163cbe64a615b4edf6c40f2fff8c79dbc8fb4)
4a2fec
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
4a2fec
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
4a2fec
---
4a2fec
 include/sysemu/iothread.h |  4 ++++
4a2fec
 iothread.c                | 45 +++++++++++++++++++++++++++++++++++++++++++++
4a2fec
 2 files changed, 49 insertions(+)
4a2fec
4a2fec
diff --git a/include/sysemu/iothread.h b/include/sysemu/iothread.h
4a2fec
index e6da1a4..d2985b3 100644
4a2fec
--- a/include/sysemu/iothread.h
4a2fec
+++ b/include/sysemu/iothread.h
4a2fec
@@ -24,6 +24,9 @@ typedef struct {
4a2fec
 
4a2fec
     QemuThread thread;
4a2fec
     AioContext *ctx;
4a2fec
+    GMainContext *worker_context;
4a2fec
+    GMainLoop *main_loop;
4a2fec
+    GOnce once;
4a2fec
     QemuMutex init_done_lock;
4a2fec
     QemuCond init_done_cond;    /* is thread initialization done? */
4a2fec
     bool stopping;
4a2fec
@@ -41,5 +44,6 @@ typedef struct {
4a2fec
 char *iothread_get_id(IOThread *iothread);
4a2fec
 AioContext *iothread_get_aio_context(IOThread *iothread);
4a2fec
 void iothread_stop_all(void);
4a2fec
+GMainContext *iothread_get_g_main_context(IOThread *iothread);
4a2fec
 
4a2fec
 #endif /* IOTHREAD_H */
4a2fec
diff --git a/iothread.c b/iothread.c
4a2fec
index d67bdd4..59d0850 100644
4a2fec
--- a/iothread.c
4a2fec
+++ b/iothread.c
4a2fec
@@ -57,6 +57,23 @@ static void *iothread_run(void *opaque)
4a2fec
 
4a2fec
     while (!atomic_read(&iothread->stopping)) {
4a2fec
         aio_poll(iothread->ctx, true);
4a2fec
+
4a2fec
+        if (atomic_read(&iothread->worker_context)) {
4a2fec
+            GMainLoop *loop;
4a2fec
+
4a2fec
+            g_main_context_push_thread_default(iothread->worker_context);
4a2fec
+            iothread->main_loop =
4a2fec
+                g_main_loop_new(iothread->worker_context, TRUE);
4a2fec
+            loop = iothread->main_loop;
4a2fec
+
4a2fec
+            g_main_loop_run(iothread->main_loop);
4a2fec
+            iothread->main_loop = NULL;
4a2fec
+            g_main_loop_unref(loop);
4a2fec
+
4a2fec
+            g_main_context_pop_thread_default(iothread->worker_context);
4a2fec
+            g_main_context_unref(iothread->worker_context);
4a2fec
+            iothread->worker_context = NULL;
4a2fec
+        }
4a2fec
     }
4a2fec
 
4a2fec
     rcu_unregister_thread();
4a2fec
@@ -73,6 +90,9 @@ static int iothread_stop(Object *object, void *opaque)
4a2fec
     }
4a2fec
     iothread->stopping = true;
4a2fec
     aio_notify(iothread->ctx);
4a2fec
+    if (atomic_read(&iothread->main_loop)) {
4a2fec
+        g_main_loop_quit(iothread->main_loop);
4a2fec
+    }
4a2fec
     qemu_thread_join(&iothread->thread);
4a2fec
     return 0;
4a2fec
 }
4a2fec
@@ -125,6 +145,7 @@ static void iothread_complete(UserCreatable *obj, Error **errp)
4a2fec
 
4a2fec
     qemu_mutex_init(&iothread->init_done_lock);
4a2fec
     qemu_cond_init(&iothread->init_done_cond);
4a2fec
+    iothread->once = (GOnce) G_ONCE_INIT;
4a2fec
 
4a2fec
     /* This assumes we are called from a thread with useful CPU affinity for us
4a2fec
      * to inherit.
4a2fec
@@ -309,3 +330,27 @@ void iothread_stop_all(void)
4a2fec
 
4a2fec
     object_child_foreach(container, iothread_stop, NULL);
4a2fec
 }
4a2fec
+
4a2fec
+static gpointer iothread_g_main_context_init(gpointer opaque)
4a2fec
+{
4a2fec
+    AioContext *ctx;
4a2fec
+    IOThread *iothread = opaque;
4a2fec
+    GSource *source;
4a2fec
+
4a2fec
+    iothread->worker_context = g_main_context_new();
4a2fec
+
4a2fec
+    ctx = iothread_get_aio_context(iothread);
4a2fec
+    source = aio_get_g_source(ctx);
4a2fec
+    g_source_attach(source, iothread->worker_context);
4a2fec
+    g_source_unref(source);
4a2fec
+
4a2fec
+    aio_notify(iothread->ctx);
4a2fec
+    return NULL;
4a2fec
+}
4a2fec
+
4a2fec
+GMainContext *iothread_get_g_main_context(IOThread *iothread)
4a2fec
+{
4a2fec
+    g_once(&iothread->once, iothread_g_main_context_init, iothread);
4a2fec
+
4a2fec
+    return iothread->worker_context;
4a2fec
+}
4a2fec
-- 
4a2fec
1.8.3.1
4a2fec