gtask: Don't overwrite source names

Only set a name in g_task_attach_source
if the source does not already have one.

Including a new test by Philip Withnall.
This commit is contained in:
Matthias Clasen 2022-08-27 21:32:46 -04:00 committed by Philip Withnall
parent debf3e4a87
commit 01f2c5aec9
2 changed files with 37 additions and 2 deletions

View File

@ -1645,7 +1645,8 @@ g_task_run_in_thread_sync (GTask *task,
* callback to @callback, with @task as the callback's `user_data`.
*
* It will set the @sources name to the tasks name (as set with
* g_task_set_name()), if one has been set.
* g_task_set_name()), if one has been set on the task and the source doesnt
* yet have a name.
*
* This takes a reference on @task until @source is destroyed.
*
@ -1661,7 +1662,7 @@ g_task_attach_source (GTask *task,
g_source_set_callback (source, callback,
g_object_ref (task), g_object_unref);
g_source_set_priority (source, task->priority);
if (task->name != NULL)
if (task->name != NULL && g_source_get_name (source) == NULL)
g_source_set_name (source, task->name);
g_source_attach (source, task->context);

View File

@ -2358,6 +2358,39 @@ test_return_value_first (void)
g_test_trap_assert_stderr ("*CRITICAL*assertion '!task->ever_returned' failed*");
}
static gboolean
source_cb (gpointer user_data)
{
return G_SOURCE_REMOVE;
}
static void
test_attach_source_set_name (void)
{
guint calls = 0;
GTask *task = NULL;
GSource *source = NULL;
GSourceFuncs source_funcs = { NULL, NULL, NULL, NULL, NULL, NULL };
g_test_summary ("Test that attaching a source to a task will set the sources name if unset");
task = g_task_new (NULL, NULL, task_complete_cb, &calls);
g_task_set_name (task, "test name");
source = g_source_new (&source_funcs, sizeof (GSource));
g_task_attach_source (task, source, source_cb);
g_assert_cmpstr (g_source_get_name (source), ==, "test name");
g_source_unref (source);
source = g_source_new (&source_funcs, sizeof (GSource));
g_source_set_name (source, "not the task name");
g_task_attach_source (task, source, source_cb);
g_assert_cmpstr (g_source_get_name (source), ==, "not the task name");
g_source_unref (source);
g_object_unref (task);
}
int
main (int argc, char **argv)
{
@ -2398,6 +2431,7 @@ main (int argc, char **argv)
g_test_add_func ("/gtask/return/in-idle/value-first", test_return_in_idle_value_first);
g_test_add_func ("/gtask/return/error-first", test_return_error_first);
g_test_add_func ("/gtask/return/value-first", test_return_value_first);
g_test_add_func ("/gtask/attach-source/set-name", test_attach_source_set_name);
ret = g_test_run();