Modernize g_source_is_destroyed example

gtk_threads_{leave,enter} API is deprecated and a narrower critical section,
guarding just idle_id manipulation, is better anyway.

Fixes: https://gitlab.gnome.org/GNOME/glib/-/issues/2279
This commit is contained in:
Jan Tojnar 2021-01-04 14:19:22 +01:00 committed by Philip Withnall
parent 87e8eb67c3
commit 720dfa8cd3

View File

@ -3125,10 +3125,10 @@ g_main_current_source (void)
* {
* SomeWidget *self = data;
*
* GDK_THREADS_ENTER ();
* g_mutex_lock (&self->idle_id_mutex);
* // do stuff with self
* self->idle_id = 0;
* GDK_THREADS_LEAVE ();
* g_mutex_unlock (&self->idle_id_mutex);
*
* return G_SOURCE_REMOVE;
* }
@ -3136,9 +3136,19 @@ g_main_current_source (void)
* static void
* some_widget_do_stuff_later (SomeWidget *self)
* {
* g_mutex_lock (&self->idle_id_mutex);
* self->idle_id = g_idle_add (idle_callback, self);
* g_mutex_unlock (&self->idle_id_mutex);
* }
*
* static void
* some_widget_init (SomeWidget *self)
* {
* g_mutex_init (&self->idle_id_mutex);
*
* // ...
* }
*
* static void
* some_widget_finalize (GObject *object)
* {
@ -3147,6 +3157,8 @@ g_main_current_source (void)
* if (self->idle_id)
* g_source_remove (self->idle_id);
*
* g_mutex_clear (&self->idle_id_mutex);
*
* G_OBJECT_CLASS (parent_class)->finalize (object);
* }
* ]|
@ -3163,12 +3175,12 @@ g_main_current_source (void)
* {
* SomeWidget *self = data;
*
* GDK_THREADS_ENTER ();
* g_mutex_lock (&self->idle_id_mutex);
* if (!g_source_is_destroyed (g_main_current_source ()))
* {
* // do stuff with self
* }
* GDK_THREADS_LEAVE ();
* g_mutex_unlock (&self->idle_id_mutex);
*
* return FALSE;
* }