mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-02-04 02:06:18 +01:00
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.
This commit is contained in:
parent
0679bd2732
commit
330f8999a8
@ -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,9 +1330,9 @@ 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);
|
||||
@ -1339,6 +1340,9 @@ run_overflow_task_thread (GTask *task,
|
||||
*result = '.';
|
||||
|
||||
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
|
||||
|
Loading…
Reference in New Issue
Block a user