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

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