On Wed, Feb 20, 2019 at 05:06:25PM +0100, Marc-André Lureau wrote: > Another thread may acquire the glib context (temporarily) before > g_main_context_push_thread_default(). > > This can happen with the following qemu_chr_fe_set_handlers() > modifications. > > Unfortunately, g_main_context_wait() is deprecated in glib > 2.58 (apparently it was a broken interface). Use a polling loop. > > Signed-off-by: Marc-André Lureau <marcandre.lur...@redhat.com> > --- > iothread.c | 7 +++++++ > 1 file changed, 7 insertions(+) > > diff --git a/iothread.c b/iothread.c > index e615b7ae52..93cc3aa875 100644 > --- a/iothread.c > +++ b/iothread.c > @@ -70,6 +70,11 @@ static void *iothread_run(void *opaque) > if (iothread->running && atomic_read(&iothread->worker_context)) { > GMainLoop *loop; > > + /* we may race with another thread acquiring the context */ > + while (!g_main_context_acquire(iothread->worker_context)) { > + g_usleep(10000); > + }
Could you help explain why need this explicitly? Since AFAIU g_main_loop_run() below will do context acquire too so IIUC you're taking it twice (while g_main_context_acquire should allow it to happen, though)? > + > g_main_context_push_thread_default(iothread->worker_context); > iothread->main_loop = > g_main_loop_new(iothread->worker_context, TRUE); > @@ -80,6 +85,8 @@ static void *iothread_run(void *opaque) > g_main_loop_unref(loop); > > g_main_context_pop_thread_default(iothread->worker_context); > + > + g_main_context_release(iothread->worker_context); > } > } > > -- > 2.21.0.rc1 > > Regards, -- Peter Xu