Add private glib_get_worker_context() API

The first time this is called, this creates a GMainContext * and a
thread to run it.  Future calls return the same.  There are a lot of
places that we could use this in GLib.
This commit is contained in:
Ryan Lortie 2011-08-31 18:20:24 -04:00
parent b6a2f502f2
commit 1facd36d00
4 changed files with 44 additions and 0 deletions

View File

@ -113,6 +113,7 @@ uninstall-ms-lib:
libglib_2_0_la_SOURCES = \
glib_probes.d \
glibprivate.h \
garray.c \
gasyncqueue.c \
gatomic.c \

View File

@ -1273,6 +1273,7 @@ g_utf16_to_ucs4
g_utf16_to_utf8
g_unichar_to_utf8
g_unichar_validate
glib_get_worker_context
glib_pgettext
glib_gettext
#ifdef G_OS_WIN32

6
glib/glibprivate.h Normal file
View File

@ -0,0 +1,6 @@
#ifndef __GLIBPRIVATE_H__
#define __GLIBPRIVATE_H__
GMainContext *glib_get_worker_context (void);
#endif /* __GLIBPRIVATE_H__ */

View File

@ -98,6 +98,8 @@
#include "gwakeup.h"
#include "glibprivate.h"
/**
* SECTION:main
* @title: The Main Event Loop
@ -378,6 +380,8 @@ static gboolean g_idle_dispatch (GSource *source,
GSourceFunc callback,
gpointer user_data);
static GMainContext *glib_worker_context;
G_LOCK_DEFINE_STATIC (main_loop);
static GMainContext *default_main_context;
static GSList *main_contexts_without_pipe = NULL;
@ -4954,3 +4958,35 @@ g_main_context_invoke_full (GMainContext *context,
}
}
}
static gpointer
glib_worker_main (gpointer data)
{
LOCK_CONTEXT (glib_worker_context);
while (TRUE)
g_main_context_iterate (glib_worker_context, TRUE, TRUE, G_THREAD_SELF);
return NULL; /* worst GCC warning message ever... */
}
GMainContext *
glib_get_worker_context (void)
{
gsize initialised;
g_thread_init_glib ();
if (g_once_init_enter (&initialised))
{
GError *error = NULL;
glib_worker_context = g_main_context_new ();
if (g_thread_create (glib_worker_main, NULL, FALSE, &error) == NULL)
g_error ("Creating GLib worker thread failed: %s\n", error->message);
g_once_init_leave (&initialised, TRUE);
}
return glib_worker_context;
}