glib-aux: add nm_g_main_context_can_acquire() helper

This commit is contained in:
Thomas Haller 2022-03-11 11:42:23 +01:00
parent 9c8a937280
commit 5dc9307a34
No known key found for this signature in database
GPG key ID: 29C2366E4DFC5728
2 changed files with 22 additions and 0 deletions

View file

@ -6682,3 +6682,23 @@ nm_g_main_context_iterate_for_msec(GMainContext *context, guint timeout_msec)
while (source)
g_main_context_iteration(context, TRUE);
}
/*****************************************************************************/
gboolean
nm_g_main_context_can_acquire(GMainContext *context)
{
/* Fast path. Usually we don't pass contexts between threads
* and operate while iterating the context. Hence, usually we
* already acquired the context. Check that first. */
if (g_main_context_is_owner(context))
return TRUE;
/* Either the context is not owned, or owned by somebody else. Only
* one way to find out. */
if (!g_main_context_acquire(context))
return FALSE;
g_main_context_release(context);
return TRUE;
}

View file

@ -1931,6 +1931,8 @@ nm_g_main_context_iterate_ready(GMainContext *context)
void nm_g_main_context_iterate_for_msec(GMainContext *context, guint timeout_msec);
gboolean nm_g_main_context_can_acquire(GMainContext *context);
/*****************************************************************************/
static inline int