mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-08-02 07:23:41 +02:00
tests: Add atomics to asyncqueue test global variables
While the test is structured so that each member of these arrays is only ever accessed by one worker thread, there’s currently no barriers to synchronise the final read (in the main thread) with the final write (in a worker thread), which could lead to very occasional failures like this one (https://gitlab.gnome.org/GNOME/glib/-/jobs/5119324): ``` GLib:ERROR:../glib/tests/asyncqueue.c:203:test_async_queue_threads: assertion failed (sums[i] > 0): (0 > 0) not ok /asyncqueue/threads - GLib:ERROR:../glib/tests/asyncqueue.c:203:test_async_queue_threads: assertion failed (sums[i] > 0): (0 > 0) ``` Signed-off-by: Philip Withnall <pwithnall@gnome.org>
This commit is contained in:
@@ -136,9 +136,8 @@ test_async_queue_destroy (void)
|
||||
static GAsyncQueue *global_queue;
|
||||
|
||||
static GThread *threads[10];
|
||||
static gint counts[10];
|
||||
static gint sums[10];
|
||||
static gint total;
|
||||
static gint counts[10]; /* (atomic) */
|
||||
static gint sums[10]; /* (atomic) */
|
||||
|
||||
static gpointer
|
||||
thread_func (gpointer data)
|
||||
@@ -153,8 +152,8 @@ thread_func (gpointer data)
|
||||
if (value == -1)
|
||||
break;
|
||||
|
||||
counts[pos]++;
|
||||
sums[pos] += value;
|
||||
g_atomic_int_inc (&counts[pos]);
|
||||
g_atomic_int_add (&sums[pos], value);
|
||||
|
||||
g_usleep (1000);
|
||||
}
|
||||
@@ -168,6 +167,7 @@ test_async_queue_threads (void)
|
||||
gint i, j;
|
||||
gint s, c;
|
||||
gint value;
|
||||
int total = 0;
|
||||
|
||||
global_queue = g_async_queue_new ();
|
||||
|
||||
@@ -200,10 +200,13 @@ test_async_queue_threads (void)
|
||||
|
||||
for (i = 0; i < 10; i++)
|
||||
{
|
||||
g_assert_cmpint (sums[i], >, 0);
|
||||
g_assert_cmpint (counts[i], >, 0);
|
||||
s += sums[i];
|
||||
c += counts[i];
|
||||
int sums_i = g_atomic_int_get (&sums[i]);
|
||||
int counts_i = g_atomic_int_get (&counts[i]);
|
||||
|
||||
g_assert_cmpint (sums_i, >, 0);
|
||||
g_assert_cmpint (counts_i, >, 0);
|
||||
s += sums_i;
|
||||
c += counts_i;
|
||||
}
|
||||
|
||||
g_assert_cmpint (s, ==, total);
|
||||
|
Reference in New Issue
Block a user