mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-08-30 12:54:11 +02:00
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:
34
glib/gmain.c
34
glib/gmain.c
@@ -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 */
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user