diff options
author | Philip Withnall <pwithnall@endlessos.org> | 2022-12-19 10:04:11 +0000 |
---|---|---|
committer | Philip Withnall <pwithnall@endlessos.org> | 2022-12-19 11:32:59 +0000 |
commit | b4ecb604aa7eb5609b272b4237e3b1c490991045 (patch) | |
tree | 5fb95afb8f01ad54938a30277ab8457a3c14af76 | |
parent | 94d53af427f69a1967245a3b57043b6acc32baf8 (diff) | |
download | glib-b4ecb604aa7eb5609b272b4237e3b1c490991045.tar.gz |
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>
-rw-r--r-- | glib/tests/slice-concurrent.c | 15 |
1 files changed, 9 insertions, 6 deletions
diff --git a/glib/tests/slice-concurrent.c b/glib/tests/slice-concurrent.c index 2b46a045b..241d36cd1 100644 --- a/glib/tests/slice-concurrent.c +++ b/glib/tests/slice-concurrent.c @@ -43,24 +43,25 @@ thread_func (void *arg) { int i; struct ThreadData *td = arg; + GRand *thread_rand = g_rand_new (); for (i = 0; i < N_ALLOCS; i++) { int bytes, f, t; 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); /* 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); 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 */ - 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); tdata[t].to_free[tdata[t].n_to_free] = mem; 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); /* 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 */ else g_usleep (1000); /* concurrent shuffling for multi core */ @@ -88,6 +89,8 @@ thread_func (void *arg) g_mutex_unlock (&td->to_free_mutex); } + g_rand_free (thread_rand); + return NULL; } |