Add g_main_context_ref_thread_default()

Add g_main_context_ref_thread_default(), which always returns a
reffed GMainContext, rather than sometimes returning a (non-reffed)
GMainContext, and sometimes returning NULL. This simplifies the
bookkeeping in any code that needs to keep a reference to the
thread-default context for a while.

https://bugzilla.gnome.org/show_bug.cgi?id=660994
This commit is contained in:
Dan Winship
2011-10-05 10:46:57 -04:00
parent d91769b641
commit 59f1f54655
15 changed files with 84 additions and 91 deletions

View File

@@ -719,12 +719,16 @@ g_main_context_pop_thread_default (GMainContext *context)
*
* Gets the thread-default #GMainContext for this thread. Asynchronous
* operations that want to be able to be run in contexts other than
* the default one should call this method to get a #GMainContext to
* add their #GSource<!-- -->s to. (Note that even in single-threaded
* the default one should call this method or
* g_main_context_ref_thread_default() to get a #GMainContext to add
* their #GSource<!-- -->s to. (Note that even in single-threaded
* programs applications may sometimes want to temporarily push a
* non-default context, so it is not safe to assume that this will
* always return %NULL if you are running in the default thread.)
*
* If you need to hold a reference on the context, use
* g_main_context_ref_thread_default() instead.
*
* Returns: (transfer none): the thread-default #GMainContext, or
* %NULL if the thread-default context is the global default context.
*
@@ -742,6 +746,32 @@ g_main_context_get_thread_default (void)
return NULL;
}
/**
* g_main_context_ref_thread_default:
*
* Gets the thread-default #GMainContext for this thread, as with
* g_main_context_get_thread_default(), but also adds a reference to
* it with g_main_context_ref(). In addition, unlike
* g_main_context_get_thread_default(), if the thread-default context
* is the global default context, this will return that #GMainContext
* (with a ref added to it) rather than returning %NULL.
*
* Returns: (transfer full): the thread-default #GMainContext. Unref
* with g_main_context_unref() when you are done with it.
*
* Since: 2.32
*/
GMainContext *
g_main_context_ref_thread_default (void)
{
GMainContext *context;
context = g_main_context_get_thread_default ();
if (!context)
context = g_main_context_default ();
return g_main_context_ref (context);
}
/* Hooks for adding to the main loop */
/**