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/error-report.h"
#include "qemu/queue.h" #include "qemu/queue.h"
GMainContext *qemu_context = NULL; #ifdef XBOX
GMainLoop *qemu_loop = NULL; /* FIXME: This is a slightly incomplete implementation of moving
* QEMU to a dedicated glib context.
#define g_main_context_default g_main_context_default_intercepted *
static GMainContext *g_main_context_default_intercepted(void) * * Some glib functions which implicitly use the default main context
{ * (specified by NULL) are still in the code base; e.g.:
assert(qemu_context != NULL); * - g_source_remove
return qemu_context; * - 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 #ifndef _WIN32
#include <sys/wait.h> #include <sys/wait.h>
@ -160,9 +166,12 @@ int qemu_init_main_loop(Error **errp)
GSource *src; GSource *src;
Error *local_error = NULL; Error *local_error = NULL;
qemu_context = g_main_context_new(); #ifdef XBOX
assert(qemu_context != NULL); qemu_main_context = g_main_context_new();
qemu_loop = g_main_loop_new(qemu_context, FALSE); 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); 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)); gpollfds = g_array_new(FALSE, FALSE, sizeof(GPollFD));
src = aio_get_g_source(qemu_aio_context); src = aio_get_g_source(qemu_aio_context);
g_source_set_name(src, "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); g_source_unref(src);
src = iohandler_get_g_source(); src = iohandler_get_g_source();
g_source_set_name(src, "io-handler"); 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); g_source_unref(src);
return 0; return 0;
} }
@ -197,7 +214,11 @@ static int glib_n_poll_fds;
static void glib_pollfds_fill(int64_t *cur_timeout) 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(); GMainContext *context = g_main_context_default();
#endif
int timeout = 0; int timeout = 0;
int64_t timeout_ns; int64_t timeout_ns;
int n; int n;
@ -226,7 +247,11 @@ static void glib_pollfds_fill(int64_t *cur_timeout)
static void glib_pollfds_poll(void) static void glib_pollfds_poll(void)
{ {
#ifdef XBOX
GMainContext *context = g_main_context_get_thread_default();
#else
GMainContext *context = g_main_context_default(); GMainContext *context = g_main_context_default();
#endif
GPollFD *pfds = &g_array_index(gpollfds, GPollFD, glib_pollfds_idx); GPollFD *pfds = &g_array_index(gpollfds, GPollFD, glib_pollfds_idx);
if (g_main_context_check(context, max_priority, pfds, glib_n_poll_fds)) { 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) 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(); GMainContext *context = g_main_context_default();
#endif
int ret; int ret;
g_main_context_acquire(context); 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) 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(); GMainContext *context = g_main_context_default();
#endif
GPollFD poll_fds[1024 * 2]; /* this is probably overkill */ GPollFD poll_fds[1024 * 2]; /* this is probably overkill */
int select_ret = 0; int select_ret = 0;
int g_poll_ret, ret, i, n_poll_fds; int g_poll_ret, ret, i, n_poll_fds;