g_source_iter_init (&iter, context, TRUE);
while (g_source_iter_next (&iter, &source))
- g_source_destroy_internal (source, context, FALSE);
+ {
+ source->context = NULL;
+ g_source_destroy_internal (source, context, FALSE);
+ }
g_mutex_clear (&context->mutex);
* @source: a #GSource
*
* Gets the #GMainContext with which the source is associated.
- * Calling this function on a destroyed source is an error.
+ *
+ * You can call this on a source that has been destroyed, provided
+ * that the #GMainContext it was attached to still exists (in which
+ * case it will return that #GMainContext). In particular, you can
+ * always call this function on the source returned from
+ * g_main_current_source(). But calling this function on a source
+ * whose #GMainContext has been destroyed is an error.
*
* Return value: (transfer none) (allow-none): the #GMainContext with which the
* source is associated, or %NULL if the context has not
GMainContext *
g_source_get_context (GSource *source)
{
- g_return_val_if_fail (!SOURCE_DESTROYED (source), NULL);
+ g_return_val_if_fail (source->context != NULL || !SOURCE_DESTROYED (source), NULL);
return source->context;
}
g_assert (g_main_context_find_source_by_funcs_user_data (ctx, &funcs, NULL) == NULL);
id = g_source_attach (source, ctx);
- g_source_unref (source);
g_assert_cmpint (g_source_get_id (source), ==, id);
g_assert (g_main_context_find_source_by_id (ctx, id) == source);
g_assert_cmpint (g_source_get_priority (source), ==, G_PRIORITY_HIGH);
g_source_destroy (source);
+ g_assert (g_source_get_context (source) == ctx);
+ g_assert (g_main_context_find_source_by_id (ctx, id) == NULL);
+
+ g_source_unref (source);
g_main_context_unref (ctx);
ctx = g_main_context_default ();
{
TimeTestData *data = user_data;
GSource *source;
- gint64 time2;
+ gint64 time2, time3;
source = g_main_current_source ();
g_assert (source == data->timeout2);
time2 = g_source_get_time (source);
g_assert_cmpint (data->time1, ==, time2);
+ /* The source should still have a valid time even after being
+ * destroyed, since it's currently running.
+ */
+ g_source_destroy (source);
+ time3 = g_source_get_time (source);
+ g_assert_cmpint (time2, ==, time3);
+
return FALSE;
}