[glib: 2/3] tests: Fix data race in task test
- From: Philip Withnall <pwithnall src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [glib: 2/3] tests: Fix data race in task test
- Date: Wed, 6 Mar 2019 13:53:47 +0000 (UTC)
commit 330f8999a8eb7bd175a6a6c5206d7bdd5b7ee3d0
Author: Tomasz Miąsko <>
Date: Wed Feb 27 00:00:00 2019 +0000
tests: Fix data race in task test
Ensure that all tasks have already completed before accessing buf array
from main thread to avoid conflicting data access from multiple threads.
gio/tests/task.c | 20 +++++++++++++-------
1 file changed, 13 insertions(+), 7 deletions(-)
---
diff --git a/gio/tests/task.c b/gio/tests/task.c
index 9f7ae2563..9b2c6912c 100644
--- a/gio/tests/task.c
+++ b/gio/tests/task.c
@@ -1317,6 +1317,7 @@ test_run_in_thread_nested (void)
* tasks, they won't all run at once.
*/
static GMutex overflow_mutex;
+static guint overflow_completed;
static void
run_overflow_task_thread (GTask *task,
@@ -1329,16 +1330,19 @@ run_overflow_task_thread (GTask *task,
if (g_task_return_error_if_cancelled (task))
{
*result = 'X';
- return;
}
+ else
+ {
+ /* Block until the main thread is ready. */
+ g_mutex_lock (&overflow_mutex);
+ g_mutex_unlock (&overflow_mutex);
- /* Block until the main thread is ready. */
- g_mutex_lock (&overflow_mutex);
- g_mutex_unlock (&overflow_mutex);
+ *result = '.';
- *result = '.';
+ g_task_return_boolean (task, TRUE);
+ }
- g_task_return_boolean (task, TRUE);
+ g_atomic_int_inc (&overflow_completed);
}
#define NUM_OVERFLOW_TASKS 1024
@@ -1382,9 +1386,11 @@ test_run_in_thread_overflow (void)
g_mutex_unlock (&overflow_mutex);
/* Wait for all tasks to complete. */
- while (strlen (buf) != NUM_OVERFLOW_TASKS)
+ while (g_atomic_int_get (&overflow_completed) != NUM_OVERFLOW_TASKS)
g_usleep (1000);
+ g_assert_cmpint (strlen (buf), ==, NUM_OVERFLOW_TASKS);
+
i = strspn (buf, ".");
/* Given the sleep times above, i should be 14 for normal, 40 for
* slow. But if the machine is too slow/busy then the scheduling
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]