Merge branch 'async-queue-tests-threading' into 'main'

tests: Add atomics to asyncqueue test global variables

See merge request GNOME/glib!4650
This commit is contained in:
Michael Catanzaro
2025-05-27 14:14:55 -05:00

View File

@@ -40,8 +40,8 @@ compare_func (gconstpointer d1, gconstpointer d2, gpointer data)
return i1 - i2; return i1 - i2;
} }
static static void
void test_async_queue_sort (void) test_async_queue_sort (void)
{ {
GAsyncQueue *q; GAsyncQueue *q;
@@ -136,9 +136,8 @@ test_async_queue_destroy (void)
static GAsyncQueue *global_queue; static GAsyncQueue *global_queue;
static GThread *threads[10]; static GThread *threads[10];
static gint counts[10]; static gint counts[10]; /* (atomic) */
static gint sums[10]; static gint sums[10]; /* (atomic) */
static gint total;
static gpointer static gpointer
thread_func (gpointer data) thread_func (gpointer data)
@@ -153,8 +152,8 @@ thread_func (gpointer data)
if (value == -1) if (value == -1)
break; break;
counts[pos]++; g_atomic_int_inc (&counts[pos]);
sums[pos] += value; g_atomic_int_add (&sums[pos], value);
g_usleep (1000); g_usleep (1000);
} }
@@ -168,6 +167,7 @@ test_async_queue_threads (void)
gint i, j; gint i, j;
gint s, c; gint s, c;
gint value; gint value;
int total = 0;
global_queue = g_async_queue_new (); global_queue = g_async_queue_new ();
@@ -200,10 +200,13 @@ test_async_queue_threads (void)
for (i = 0; i < 10; i++) for (i = 0; i < 10; i++)
{ {
g_assert_cmpint (sums[i], >, 0); int sums_i = g_atomic_int_get (&sums[i]);
g_assert_cmpint (counts[i], >, 0); int counts_i = g_atomic_int_get (&counts[i]);
s += sums[i];
c += 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); g_assert_cmpint (s, ==, total);