mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-08-20 07:38:54 +02:00
tests: Use atomics to access counter shared between threads
This should fix some sporadic test failures in this test, although I can’t be sure as I was unable to reproduce the original failure. Signed-off-by: Philip Withnall <withnall@endlessm.com> Closes: #1764
This commit is contained in:
@@ -29,8 +29,8 @@ static gint num_async_operations = 0;
|
|||||||
|
|
||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
guint iterations_requested;
|
guint iterations_requested; /* construct-only */
|
||||||
guint iterations_done;
|
guint iterations_done; /* (atomic) */
|
||||||
} MockOperationData;
|
} MockOperationData;
|
||||||
|
|
||||||
static void
|
static void
|
||||||
@@ -60,7 +60,7 @@ mock_operation_thread (GTask *task,
|
|||||||
|
|
||||||
if (g_test_verbose ())
|
if (g_test_verbose ())
|
||||||
g_test_message ("THRD: %u stopped at %u", data->iterations_requested, i);
|
g_test_message ("THRD: %u stopped at %u", data->iterations_requested, i);
|
||||||
data->iterations_done = i;
|
g_atomic_int_add (&data->iterations_done, i);
|
||||||
|
|
||||||
g_task_return_boolean (task, TRUE);
|
g_task_return_boolean (task, TRUE);
|
||||||
}
|
}
|
||||||
@@ -71,11 +71,13 @@ mock_operation_timeout (gpointer user_data)
|
|||||||
GTask *task;
|
GTask *task;
|
||||||
MockOperationData *data;
|
MockOperationData *data;
|
||||||
gboolean done = FALSE;
|
gboolean done = FALSE;
|
||||||
|
guint iterations_done;
|
||||||
|
|
||||||
task = G_TASK (user_data);
|
task = G_TASK (user_data);
|
||||||
data = g_task_get_task_data (task);
|
data = g_task_get_task_data (task);
|
||||||
|
iterations_done = g_atomic_int_get (&data->iterations_done);
|
||||||
|
|
||||||
if (data->iterations_done >= data->iterations_requested)
|
if (iterations_done >= data->iterations_requested)
|
||||||
done = TRUE;
|
done = TRUE;
|
||||||
|
|
||||||
if (g_cancellable_is_cancelled (g_task_get_cancellable (task)))
|
if (g_cancellable_is_cancelled (g_task_get_cancellable (task)))
|
||||||
@@ -85,16 +87,16 @@ mock_operation_timeout (gpointer user_data)
|
|||||||
{
|
{
|
||||||
if (g_test_verbose ())
|
if (g_test_verbose ())
|
||||||
g_test_message ("LOOP: %u stopped at %u",
|
g_test_message ("LOOP: %u stopped at %u",
|
||||||
data->iterations_requested, data->iterations_done);
|
data->iterations_requested, iterations_done);
|
||||||
g_task_return_boolean (task, TRUE);
|
g_task_return_boolean (task, TRUE);
|
||||||
return FALSE; /* don't call timeout again */
|
return FALSE; /* don't call timeout again */
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
data->iterations_done++;
|
g_atomic_int_inc (&data->iterations_done);
|
||||||
if (g_test_verbose ())
|
if (g_test_verbose ())
|
||||||
g_test_message ("LOOP: %u iteration %u",
|
g_test_message ("LOOP: %u iteration %u",
|
||||||
data->iterations_requested, data->iterations_done);
|
data->iterations_requested, iterations_done + 1);
|
||||||
return TRUE; /* call timeout */
|
return TRUE; /* call timeout */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -147,7 +149,7 @@ mock_operation_finish (GAsyncResult *result,
|
|||||||
data = g_task_get_task_data (task);
|
data = g_task_get_task_data (task);
|
||||||
|
|
||||||
g_task_propagate_boolean (task, error);
|
g_task_propagate_boolean (task, error);
|
||||||
return data->iterations_done;
|
return g_atomic_int_get (&data->iterations_done);
|
||||||
}
|
}
|
||||||
|
|
||||||
GMainLoop *loop;
|
GMainLoop *loop;
|
||||||
|
Reference in New Issue
Block a user