From 44616ebafdca578e0c9ad7a345b2c286c89040c5 Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Mon, 6 Mar 2023 15:43:23 +0000 Subject: gmain: More explicitly document g_main_context_release() prereqs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The documentation was fairly clear before, but we can make it clearer: it’s a programming error to call `g_main_context_release()` if you have not received a true return value from `g_main_context_acquire()` before. Inspired by https://gitlab.gnome.org/GNOME/glib/-/merge_requests/3302. Signed-off-by: Philip Withnall --- glib/gmain.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/glib/gmain.c b/glib/gmain.c index 7442d3d05..d34648f51 100644 --- a/glib/gmain.c +++ b/glib/gmain.c @@ -3525,7 +3525,7 @@ g_main_dispatch (GMainContext *context) * * You must be the owner of a context before you * can call g_main_context_prepare(), g_main_context_query(), - * g_main_context_check(), g_main_context_dispatch(). + * g_main_context_check(), g_main_context_dispatch(), g_main_context_release(). * * Since 2.76 @context can be %NULL to use the global-default * main context. @@ -3583,6 +3583,9 @@ g_main_context_acquire_unlocked (GMainContext *context) * with g_main_context_acquire(). If the context was acquired multiple * times, the ownership will be released only when g_main_context_release() * is called as many times as it was acquired. + * + * You must have successfully acquired the context with + * g_main_context_acquire() before you may call this function. **/ void g_main_context_release (GMainContext *context) -- cgit v1.2.1 From 3926af723a7469a2ea492307f421820361d617b3 Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Mon, 6 Mar 2023 15:44:26 +0000 Subject: gmain: Add precondition assertions to g_main_context_release() As with the previous commit. The logic has to be a little contorted here to avoid leaving the context locked after emitting the critical warning. Execution does (and should) continue after a critical warning by default, so we should do our best to recover. Inspired by https://gitlab.gnome.org/GNOME/glib/-/merge_requests/3302. Signed-off-by: Philip Withnall --- glib/gmain.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/glib/gmain.c b/glib/gmain.c index d34648f51..191e77345 100644 --- a/glib/gmain.c +++ b/glib/gmain.c @@ -3592,9 +3592,23 @@ g_main_context_release (GMainContext *context) { if (context == NULL) context = g_main_context_default (); - + LOCK_CONTEXT (context); +#ifndef G_DISABLE_CHECKS + if (G_UNLIKELY (context->owner != G_THREAD_SELF || context->owner_count == 0)) + { + GThread *context_owner = context->owner; + guint context_owner_count = context->owner_count; + + UNLOCK_CONTEXT (context); + + g_critical ("g_main_context_release() called on a context (%p, owner %p, " + "owner count %u) which is not acquired by the current thread", + context, context_owner, context_owner_count); + } +#endif /* !G_DISABLE_CHECKS */ + g_main_context_release_unlocked (context); UNLOCK_CONTEXT (context); -- cgit v1.2.1