main-loop: Use thread-specific context instead of global context

This commit is contained in:
Matt Borgerson 2020-06-02 21:34:57 -07:00
parent f896ebc1e2
commit 4cb8301631
1 changed files with 47 additions and 14 deletions

View File

@ -34,15 +34,21 @@
#include "qemu/error-report.h"
#include "qemu/queue.h"
GMainContext *qemu_context = NULL;
GMainLoop *qemu_loop = NULL;
#define g_main_context_default g_main_context_default_intercepted
static GMainContext *g_main_context_default_intercepted(void)
{
assert(qemu_context != NULL);
return qemu_context;
}
#ifdef XBOX
/* FIXME: This is a slightly incomplete implementation of moving
* QEMU to a dedicated glib context.
*
* * Some glib functions which implicitly use the default main context
* (specified by NULL) are still in the code base; e.g.:
* - g_source_remove
* - g_main_loop_new (nbd)
* * Needs a nicer way to get the main context instead of referencing a
* global context pointer.
*
*/
GMainContext *qemu_main_context = NULL;
GMainLoop *qemu_main_loop_obj = NULL;
#endif
#ifndef _WIN32
#include <sys/wait.h>
@ -160,9 +166,12 @@ int qemu_init_main_loop(Error **errp)
GSource *src;
Error *local_error = NULL;
qemu_context = g_main_context_new();
assert(qemu_context != NULL);
qemu_loop = g_main_loop_new(qemu_context, FALSE);
#ifdef XBOX
qemu_main_context = g_main_context_new();
assert(qemu_main_context != NULL);
g_main_context_push_thread_default(qemu_main_context);
qemu_main_loop_obj = g_main_loop_new(qemu_main_context, FALSE);
#endif
init_clocks(qemu_timer_notify_cb);
@ -180,11 +189,19 @@ int qemu_init_main_loop(Error **errp)
gpollfds = g_array_new(FALSE, FALSE, sizeof(GPollFD));
src = aio_get_g_source(qemu_aio_context);
g_source_set_name(src, "aio-context");
g_source_attach(src, qemu_context);
#ifdef XBOX
g_source_attach(src, qemu_main_context);
#else
g_source_attach(src, NULL);
#endif
g_source_unref(src);
src = iohandler_get_g_source();
g_source_set_name(src, "io-handler");
g_source_attach(src, qemu_context);
#ifdef XBOX
g_source_attach(src, qemu_main_context);
#else
g_source_attach(src, NULL);
#endif
g_source_unref(src);
return 0;
}
@ -197,7 +214,11 @@ static int glib_n_poll_fds;
static void glib_pollfds_fill(int64_t *cur_timeout)
{
#ifdef XBOX
GMainContext *context = g_main_context_get_thread_default();
#else
GMainContext *context = g_main_context_default();
#endif
int timeout = 0;
int64_t timeout_ns;
int n;
@ -226,7 +247,11 @@ static void glib_pollfds_fill(int64_t *cur_timeout)
static void glib_pollfds_poll(void)
{
#ifdef XBOX
GMainContext *context = g_main_context_get_thread_default();
#else
GMainContext *context = g_main_context_default();
#endif
GPollFD *pfds = &g_array_index(gpollfds, GPollFD, glib_pollfds_idx);
if (g_main_context_check(context, max_priority, pfds, glib_n_poll_fds)) {
@ -238,7 +263,11 @@ static void glib_pollfds_poll(void)
static int os_host_main_loop_wait(int64_t timeout)
{
#ifdef XBOX
GMainContext *context = g_main_context_get_thread_default();
#else
GMainContext *context = g_main_context_default();
#endif
int ret;
g_main_context_acquire(context);
@ -401,7 +430,11 @@ static void pollfds_poll(GArray *pollfds, int nfds, fd_set *rfds,
static int os_host_main_loop_wait(int64_t timeout)
{
#ifdef XBOX
GMainContext *context = g_main_context_get_thread_default();
#else
GMainContext *context = g_main_context_default();
#endif
GPollFD poll_fds[1024 * 2]; /* this is probably overkill */
int select_ret = 0;
int g_poll_ret, ret, i, n_poll_fds;