Skip to contentSkip to conversation

qemu-iothread: IOThread supports the GMainContext event loop

PATCH Superseded Wang yong 3 participants 5 messages Aug 10, 2017

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Wang yong

author
#0 lore raw

PATCH 1/1 qemu-iothread: IOThread supports the GMainContext event loop

From: Wang Yong<[email protected]>

IOThread uses AioContext event loop and does not run a GMainContext. Therefore,chardev cannot work in IOThread,such as the chardev is used for colo-compare packets reception.

This patch makes the IOThread run the GMainContext event loop, chardev and IOThread can work together.

---
 include/sysemu/iothread.h |  1 +
 iothread.c                | 13 +++++++++++++
 2 files changed, 14 insertions(+)
include/sysemu/iothread.h +1 -0
index e6da1a4..ffe4e8a 100644
@@ -24,6 +24,7 @@ typedef struct {
 
     QemuThread thread;
     AioContext *ctx;
+    GMainLoop *loop;
     QemuMutex init_done_lock;
     QemuCond init_done_cond;    /* is thread initialization done? */
     bool stopping;
iothread.c +13 -0
index beeb870..b6f3c3c 100644
@@ -46,6 +46,7 @@ AioContext *qemu_get_current_aio_context(void)
 static void *iothread_run(void *opaque)
 {
     IOThread *iothread = opaque;
+    GMainContext *context;
 
     rcu_register_thread();
 
@@ -57,6 +58,15 @@ static void *iothread_run(void *opaque)
 
     while (!atomic_read(&iothread->stopping)) {
         aio_poll(iothread->ctx, true);
+
+        context = iothread->ctx->source.context;
+        if (context) {
+            iothread->loop = g_main_loop_new(context, TRUE);
+            g_main_loop_run(iothread->loop);
+
+            g_main_loop_unref(iothread->loop);
+            g_main_context_unref(context);
+        }
     }
 
     rcu_unregister_thread();
@@ -72,6 +82,9 @@ static int iothread_stop(Object *object, void *opaque)
         return 0;
     }
     iothread->stopping = true;
+    if (iothread->loop) {
+        g_main_loop_quit(iothread->loop);
+    }
     aio_notify(iothread->ctx);
     qemu_thread_join(&iothread->thread);
     return 0;
Signature · 1 line
1.8.3.1
#1 lore raw

----- Original Message -----

Quoted 14 lines

From: "Wang yong" <[email protected]>
To: [email protected], [email protected], [email protected], [email protected]
Cc: "wang yong155" <[email protected]>, "wang guang55" <[email protected]>, "zhangchen fnst" <[email protected]>, "zhang zhanghailiang" <[email protected]>, [email protected], [email protected]
Sent: Friday, August 11, 2017 2:29:15 AM
Subject: [PATCH] qemu-iothread: IOThread supports the GMainContext event loop

From: Wang Yong<[email protected]>

IOThread uses AioContext event loop and does not run a GMainContext. Therefore,chardev cannot work in IOThread,such as the chardev is used for colo-compare packets reception.

Quoted 5 lines

This patch makes the IOThread run the GMainContext event loop, chardev and IOThread can work together.

Nice. Just a note, I think an iothread should have its own (optional) GMainContext accessed with iothread_get_g_main_context(iothread). When you call it for the first time, the iothread:

1) creates a GMainContext

2) adds the AioContext as a GSource in the GMainContext

3) asks iothread_run to switch from the AioContext loop to the GMainContext loop

To simplify thread-safety:

1) the GMainContext can be wrapped with a GOnce

2) the GOnce callback can leave steps 2 and 3 to a bottom half.

Paolo

Quoted 60 lines
---
 include/sysemu/iothread.h |  1 +
 iothread.c                | 13 +++++++++++++
 2 files changed, 14 insertions(+)
include/sysemu/iothread.h +1 -0
index e6da1a4..ffe4e8a 100644
@@ -24,6 +24,7 @@ typedef struct {
 
     QemuThread thread;
     AioContext *ctx;
+    GMainLoop *loop;
     QemuMutex init_done_lock;
     QemuCond init_done_cond;    /* is thread initialization done? */
     bool stopping;
iothread.c +13 -1
index beeb870..b6f3c3c 100644
@@ -46,6 +46,7 @@ AioContext *qemu_get_current_aio_context(void)
 static void *iothread_run(void *opaque)
 {
     IOThread *iothread = opaque;
+    GMainContext *context;
 
     rcu_register_thread();
 
@@ -57,6 +58,15 @@ static void *iothread_run(void *opaque)
 
     while (!atomic_read(&iothread->stopping)) {
         aio_poll(iothread->ctx, true);
+
+        context = iothread->ctx->source.context;
+        if (context) {
+            iothread->loop = g_main_loop_new(context, TRUE);
+            g_main_loop_run(iothread->loop);
+
+            g_main_loop_unref(iothread->loop);
+            g_main_context_unref(context);
+        }
     }
 
     rcu_unregister_thread();
@@ -72,6 +82,9 @@ static int iothread_stop(Object *object, void *opaque)
         return 0;
     }
     iothread->stopping = true;
+    if (iothread->loop) {
+        g_main_loop_quit(iothread->loop);
+    }
     aio_notify(iothread->ctx);
     qemu_thread_join(&iothread->thread);
     return 0;
--

1.8.3.1

#2 lore raw

[Qemu-devel] 答复: Re: [PATCH] qemu-iothread: IOThread supports the GMainContext eventloop

Quoted 28 lines

Nice. Just a note, I think an iothread should have its own (optional)

GMainContext accessed with iothread_get_g_main_context(iothread). When

you call it for the first time, the iothread:

1) creates a GMainContext

2) adds the AioContext as a GSource in the GMainContext

3) asks iothread_run to switch from the AioContext loop to the

GMainContext loop

To simplify thread-safety:

Quoted 7 lines

1) the GMainContext can be wrapped with a GOnce

2) the GOnce callback can leave steps 2 and 3 to a bottom half.

Thanks, I will submit a patch v2.

WangYong

原始邮件

发件人: <[email protected]>
收件人:王勇10170530
抄送人: <[email protected]> <[email protected]> <[email protected]>王广10165992 <[email protected]> <[email protected]> <[email protected]> <[email protected]> 日 期 :2017年08月10日 16:51
主 题 :Re: [PATCH] qemu-iothread: IOThread supports the GMainContext eventloop

----- Original Message -----

Quoted 14 lines

From: "Wang yong" <[email protected]>
To: [email protected], [email protected], [email protected], [email protected]
Cc: "wang yong155" <[email protected]>, "wang guang55" <[email protected]>, "zhangchen fnst" <[email protected]>, "zhang zhanghailiang" <[email protected]>, [email protected], [email protected]
Sent: Friday, August 11, 2017 2:29:15 AM
Subject: [PATCH] qemu-iothread: IOThread supports the GMainContext event loop

From: Wang Yong<[email protected]>

IOThread uses AioContext event loop and does not run a GMainContext. Therefore,chardev cannot work in IOThread,such as the chardev is used for colo-compare packets reception.

Quoted 5 lines

This patch makes the IOThread run the GMainContext event loop, chardev and IOThread can work together.

Nice. Just a note, I think an iothread should have its own (optional) GMainContext accessed with iothread_get_g_main_context(iothread). When you call it for the first time, the iothread:

1) creates a GMainContext

2) adds the AioContext as a GSource in the GMainContext

3) asks iothread_run to switch from the AioContext loop to the GMainContext loop

To simplify thread-safety:

1) the GMainContext can be wrapped with a GOnce

2) the GOnce callback can leave steps 2 and 3 to a bottom half.

Paolo

Quoted 60 lines
---
 include/sysemu/iothread.h |  1 +
 iothread.c                | 13 +++++++++++++
 2 files changed, 14 insertions(+)
include/sysemu/iothread.h +1 -0
index e6da1a4..ffe4e8a 100644
@@ -24,6 +24,7 @@ typedef struct {
 
     QemuThread thread
     AioContext *ctx
+    GMainLoop *loop
     QemuMutex init_done_lock
     QemuCond init_done_cond    /* is thread initialization done? */
     bool stopping
iothread.c +13 -1
index beeb870..b6f3c3c 100644
@@ -46,6 +46,7 @@ AioContext *qemu_get_current_aio_context(void)
 static void *iothread_run(void *opaque)
 {
     IOThread *iothread = opaque
+    GMainContext *context
 
     rcu_register_thread()
 
@@ -57,6 +58,15 @@ static void *iothread_run(void *opaque)
 
     while (!atomic_read(&iothread->stopping)) {
         aio_poll(iothread->ctx, true)
+
+        context = iothread->ctx->source.context
+        if (context) {
+            iothread->loop = g_main_loop_new(context, TRUE)
+            g_main_loop_run(iothread->loop)
+
+            g_main_loop_unref(iothread->loop)
+            g_main_context_unref(context)
+        }
     }
 
     rcu_unregister_thread()
@@ -72,6 +82,9 @@ static int iothread_stop(Object *object, void *opaque)
         return 0
     }
     iothread->stopping = true
+    if (iothread->loop) {
+        g_main_loop_quit(iothread->loop)
+    }
     aio_notify(iothread->ctx)
     qemu_thread_join(&iothread->thread)
     return 0
--

1.8.3.1

#3 lore raw

Re: [Qemu-devel] 答复: Re: [PATCH] qemu-iothread: IOThread supports the GMainContext eventloop

Quoted 97 lines · On 10/08/2017 11:17, [email protected] wrote:
Quoted 28 lines

Nice. Just a note, I think an iothread should have its own (optional)

GMainContext accessed with iothread_get_g_main_context(iothread). When

you call it for the first time, the iothread:

1) creates a GMainContext

2) adds the AioContext as a GSource in the GMainContext

3) asks iothread_run to switch from the AioContext loop to the

GMainContext loop

To simplify thread-safety:

Quoted 7 lines

1) the GMainContext can be wrapped with a GOnce

2) the GOnce callback can leave steps 2 and 3 to a bottom half.

Thanks, I will submit a patch v2.

WangYong

原始邮件

发件人: <[email protected]>
收件人:王勇10170530
抄送人: <[email protected]> <[email protected]> <[email protected]>王广10165992 <[email protected]> <[email protected]> <[email protected]> <[email protected]> 日 期 :2017年08月10日 16:51
主 题 :Re: [PATCH] qemu-iothread: IOThread supports the GMainContext eventloop

----- Original Message -----

Quoted 14 lines

From: "Wang yong" <[email protected]>
To: [email protected], [email protected], [email protected], [email protected]
Cc: "wang yong155" <[email protected]>, "wang guang55" <[email protected]>, "zhangchen fnst" <[email protected]>, "zhang zhanghailiang" <[email protected]>, [email protected], [email protected]
Sent: Friday, August 11, 2017 2:29:15 AM
Subject: [PATCH] qemu-iothread: IOThread supports the GMainContext event loop

From: Wang Yong<[email protected]>

IOThread uses AioContext event loop and does not run a GMainContext. Therefore,chardev cannot work in IOThread,such as the chardev is used for colo-compare packets reception.

Quoted 5 lines

This patch makes the IOThread run the GMainContext event loop, chardev and IOThread can work together.

Nice. Just a note, I think an iothread should have its own (optional) GMainContext accessed with iothread_get_g_main_context(iothread). When you call it for the first time, the iothread:

1) creates a GMainContext

2) adds the AioContext as a GSource in the GMainContext

3) asks iothread_run to switch from the AioContext loop to the GMainContext loop

Quoted 5 lines

To simplify thread-safety:

1) the GMainContext can be wrapped with a GOnce

2) the GOnce callback can leave steps 2 and 3 to a bottom half.

Thanks. Another thing, please add a call to
g_main_context_push_thread_default() and
g_main_context_pop_thread_default().

Thanks,

Paolo

Quoted 61 lines
Quoted 60 lines
---
 include/sysemu/iothread.h |  1 +
 iothread.c                | 13 +++++++++++++
 2 files changed, 14 insertions(+)
include/sysemu/iothread.h +1 -0
index e6da1a4..ffe4e8a 100644
@@ -24,6 +24,7 @@ typedef struct {
 
     QemuThread thread
     AioContext *ctx
+    GMainLoop *loop
     QemuMutex init_done_lock
     QemuCond init_done_cond    /* is thread initialization done? */
     bool stopping
iothread.c +13 -1
index beeb870..b6f3c3c 100644
@@ -46,6 +46,7 @@ AioContext *qemu_get_current_aio_context(void)
 static void *iothread_run(void *opaque)
 {
     IOThread *iothread = opaque
+    GMainContext *context
 
     rcu_register_thread()
 
@@ -57,6 +58,15 @@ static void *iothread_run(void *opaque)
 
     while (!atomic_read(&iothread->stopping)) {
         aio_poll(iothread->ctx, true)
+
+        context = iothread->ctx->source.context
+        if (context) {
+            iothread->loop = g_main_loop_new(context, TRUE)
+            g_main_loop_run(iothread->loop)
+
+            g_main_loop_unref(iothread->loop)
+            g_main_context_unref(context)
+        }
     }
 
     rcu_unregister_thread()
@@ -72,6 +82,9 @@ static int iothread_stop(Object *object, void *opaque)
         return 0
     }
     iothread->stopping = true
+    if (iothread->loop) {
+        g_main_loop_quit(iothread->loop)
+    }
     aio_notify(iothread->ctx)
     qemu_thread_join(&iothread->thread)
     return 0
--

1.8.3.1

#4 lore raw
In reply to Wang yong
Quoted 41 lines · Wang yong

From: Wang Yong<[email protected]>

IOThread uses AioContext event loop and does not run a GMainContext. Therefore,chardev cannot work in IOThread,such as the chardev is used for colo-compare packets reception.

This patch makes the IOThread run the GMainContext event loop, chardev and IOThread can work together.

---
 include/sysemu/iothread.h |  1 +
 iothread.c                | 13 +++++++++++++
 2 files changed, 14 insertions(+)
include/sysemu/iothread.h +1 -0
index e6da1a4..ffe4e8a 100644
@@ -24,6 +24,7 @@ typedef struct {
 
     QemuThread thread;
     AioContext *ctx;
+    GMainLoop *loop;
     QemuMutex init_done_lock;
     QemuCond init_done_cond;    /* is thread initialization done? */
     bool stopping;
iothread.c +1 -0
index beeb870..b6f3c3c 100644
@@ -46,6 +46,7 @@ AioContext *qemu_get_current_aio_context(void)
 static void *iothread_run(void *opaque)
 {
     IOThread *iothread = opaque;
+    GMainContext *context;
 
     rcu_register_thread();
 

iothread.c static void *iothread_run(void *opaque) · line 67

Quoted 10 lines
iothread.c +7 -0
@@ -57,6 +58,15 @@ static void *iothread_run(void *opaque)
 
     while (!atomic_read(&iothread->stopping)) {
         aio_poll(iothread->ctx, true);
+
+        context = iothread->ctx->source.context;
+        if (context) {
+            iothread->loop = g_main_loop_new(context, TRUE);
+            g_main_loop_run(iothread->loop);
+
+            g_main_loop_unref(iothread->loop);

Is it better to set iothread->loop to NULL after unref? If in the future a second place adds g_main_loop_quit(), it can coordinate with iothread_stop by checking iothread->loop == NULL.

iothread.c static void *iothread_run(void *opaque) · line 68

Quoted 1 line
iothread.c +1 -0
+            g_main_context_unref(context);

Where is the matching g_main_context_ref? I think the one reference in iothread->loop goes away with g_main_loop_unref. Is this line unnecessary?

Quoted 19 lines
iothread.c +4 -0
+        }
     }
 
     rcu_unregister_thread();
@@ -72,6 +82,9 @@ static int iothread_stop(Object *object, void *opaque)
         return 0;
     }
     iothread->stopping = true;
+    if (iothread->loop) {
+        g_main_loop_quit(iothread->loop);
+    }
     aio_notify(iothread->ctx);
     qemu_thread_join(&iothread->thread);
     return 0;
Signature · 1 line
1.8.3.1

Though I haven't seen the chardev changes yet, the general idea looks good to me.

Fam