tests: Use a thread-local GRand in each slice-concurrent test thread

This prevents stalls/deadlocks/timeouts on macOS. I don’t know why, as I
don’t have access to a macOS machine to test — this MR was put together
via testing on CI.

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
This commit is contained in:
Philip Withnall 2022-12-19 10:04:11 +00:00
parent 94d53af427
commit b4ecb604aa

View File

@ -43,24 +43,25 @@ thread_func (void *arg)
{ {
int i; int i;
struct ThreadData *td = arg; struct ThreadData *td = arg;
GRand *thread_rand = g_rand_new ();
for (i = 0; i < N_ALLOCS; i++) for (i = 0; i < N_ALLOCS; i++)
{ {
int bytes, f, t; int bytes, f, t;
char *mem; char *mem;
if (g_random_int_range (0, N_ALLOCS / 20) == 0) if (g_rand_int_range (thread_rand, 0, N_ALLOCS / 20) == 0)
g_test_message ("%c", 'a' - 1 + td->thread_id); g_test_message ("%c", 'a' - 1 + td->thread_id);
/* allocate block of random size and randomly fill */ /* allocate block of random size and randomly fill */
bytes = g_random_int_range (0, MAX_BLOCK_SIZE) + 1; bytes = g_rand_int_range (thread_rand, 0, MAX_BLOCK_SIZE) + 1;
mem = g_slice_alloc (bytes); mem = g_slice_alloc (bytes);
for (f = 0; f < bytes; f++) for (f = 0; f < bytes; f++)
mem[f] = (char) g_random_int (); mem[f] = (char) g_rand_int (thread_rand);
/* associate block with random thread */ /* associate block with random thread */
t = g_random_int_range (0, N_THREADS); t = g_rand_int_range (thread_rand, 0, N_THREADS);
g_mutex_lock (&tdata[t].to_free_mutex); g_mutex_lock (&tdata[t].to_free_mutex);
tdata[t].to_free[tdata[t].n_to_free] = mem; tdata[t].to_free[tdata[t].n_to_free] = mem;
tdata[t].bytes_to_free[tdata[t].n_to_free] = bytes; tdata[t].bytes_to_free[tdata[t].n_to_free] = bytes;
@ -68,9 +69,9 @@ thread_func (void *arg)
g_mutex_unlock (&tdata[t].to_free_mutex); g_mutex_unlock (&tdata[t].to_free_mutex);
/* shuffle thread execution order every once in a while */ /* shuffle thread execution order every once in a while */
if (g_random_int_range (0, 97) == 0) if (g_rand_int_range (thread_rand, 0, 97) == 0)
{ {
if (g_random_boolean ()) if (g_rand_boolean (thread_rand))
g_thread_yield(); /* concurrent shuffling for single core */ g_thread_yield(); /* concurrent shuffling for single core */
else else
g_usleep (1000); /* concurrent shuffling for multi core */ g_usleep (1000); /* concurrent shuffling for multi core */
@ -88,6 +89,8 @@ thread_func (void *arg)
g_mutex_unlock (&td->to_free_mutex); g_mutex_unlock (&td->to_free_mutex);
} }
g_rand_free (thread_rand);
return NULL; return NULL;
} }