From 6545a040af554f325c0cd63779a46f3f37222541 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Fri, 10 Nov 2023 19:01:40 +0100 Subject: [PATCH] tests: In multithreaded tests, use random() instead of rand(). * tests/test-asyncsafe-spin2.c (random_account, lock_mutator_thread): Use random() instead of rand(). * tests/test-lock.c (random_account, lock_mutator_thread, rwlock_mutator_thread, recshuffle): Likewise. * tests/test-mtx.c (random_account, lock_mutator_thread, recshuffle): Likewise. * tests/test-pthread-mutex.c (random_account, lock_mutator_thread, recshuffle): Likewise. * tests/test-pthread-rwlock.c (random_account, rwlock_mutator_thread): Likewise. * tests/test-pthread-spin.c (random_account, lock_mutator_thread): Likewise. * tests/test-pthread-tss.c (perhaps_yield, worker_thread, racecheck_thread): Likewise. * tests/test-thread_local.c (perhaps_yield, worker_thread): Likewise. * tests/test-tls.c (perhaps_yield, worker_thread, racecheck_thread): Likewise. * tests/test-tss.c (perhaps_yield, worker_thread, racecheck_thread): Likewise. * asyncsafe-spin-tests (Depends-on): Add random. * lock-tests (Depends-on): Likewise. * mtx-tests (Depends-on): Likewise. * pthread-mutex-tests (Depends-on): Likewise. * pthread-rwlock-tests (Depends-on): Likewise. * pthread-spin-tests (Depends-on): Likewise. * pthread-tss-tests (Depends-on): Likewise. * threads-h-tests (Depends-on): Likewise. * tls-tests (Depends-on): Likewise. * tss-tests (Depends-on): Likewise. --- ChangeLog | 33 +++++++++++++++++++++++++++++++++ modules/asyncsafe-spin-tests | 1 + modules/lock-tests | 1 + modules/mtx-tests | 1 + modules/pthread-mutex-tests | 1 + modules/pthread-rwlock-tests | 1 + modules/pthread-spin-tests | 1 + modules/pthread-tss-tests | 1 + modules/threads-h-tests | 1 + modules/tls-tests | 1 + modules/tss-tests | 1 + tests/test-asyncsafe-spin2.c | 4 ++-- tests/test-lock.c | 10 +++++----- tests/test-mtx.c | 8 ++++---- tests/test-pthread-mutex.c | 8 ++++---- tests/test-pthread-rwlock.c | 4 ++-- tests/test-pthread-spin.c | 4 ++-- tests/test-pthread-tss.c | 10 +++++----- tests/test-thread_local.c | 8 ++++---- tests/test-tls.c | 10 +++++----- tests/test-tss.c | 10 +++++----- 21 files changed, 81 insertions(+), 38 deletions(-) diff --git a/ChangeLog b/ChangeLog index 396668b66c..e7b85edc80 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,36 @@ +2023-11-10 Bruno Haible + + tests: In multithreaded tests, use random() instead of rand(). + * tests/test-asyncsafe-spin2.c (random_account, lock_mutator_thread): + Use random() instead of rand(). + * tests/test-lock.c (random_account, lock_mutator_thread, + rwlock_mutator_thread, recshuffle): Likewise. + * tests/test-mtx.c (random_account, lock_mutator_thread, recshuffle): + Likewise. + * tests/test-pthread-mutex.c (random_account, lock_mutator_thread, + recshuffle): Likewise. + * tests/test-pthread-rwlock.c (random_account, rwlock_mutator_thread): + Likewise. + * tests/test-pthread-spin.c (random_account, lock_mutator_thread): + Likewise. + * tests/test-pthread-tss.c (perhaps_yield, worker_thread, + racecheck_thread): Likewise. + * tests/test-thread_local.c (perhaps_yield, worker_thread): Likewise. + * tests/test-tls.c (perhaps_yield, worker_thread, racecheck_thread): + Likewise. + * tests/test-tss.c (perhaps_yield, worker_thread, racecheck_thread): + Likewise. + * asyncsafe-spin-tests (Depends-on): Add random. + * lock-tests (Depends-on): Likewise. + * mtx-tests (Depends-on): Likewise. + * pthread-mutex-tests (Depends-on): Likewise. + * pthread-rwlock-tests (Depends-on): Likewise. + * pthread-spin-tests (Depends-on): Likewise. + * pthread-tss-tests (Depends-on): Likewise. + * threads-h-tests (Depends-on): Likewise. + * tls-tests (Depends-on): Likewise. + * tss-tests (Depends-on): Likewise. + 2023-11-10 Bruno Haible random: Fix multithread-safety bug. diff --git a/modules/asyncsafe-spin-tests b/modules/asyncsafe-spin-tests index 301c2afc1a..dacb26aaac 100644 --- a/modules/asyncsafe-spin-tests +++ b/modules/asyncsafe-spin-tests @@ -8,6 +8,7 @@ Depends-on: thread lock yield +random configure.ac: AC_CHECK_HEADERS_ONCE([semaphore.h]) diff --git a/modules/lock-tests b/modules/lock-tests index 502c36882f..7b398e2327 100644 --- a/modules/lock-tests +++ b/modules/lock-tests @@ -11,6 +11,7 @@ thread stdint usleep yield +random configure.ac: AC_CHECK_HEADERS_ONCE([semaphore.h]) diff --git a/modules/mtx-tests b/modules/mtx-tests index 52310625a5..6de1f3d840 100644 --- a/modules/mtx-tests +++ b/modules/mtx-tests @@ -8,6 +8,7 @@ Depends-on: thrd lock stdint +random configure.ac: AC_CHECK_HEADERS_ONCE([semaphore.h]) diff --git a/modules/pthread-mutex-tests b/modules/pthread-mutex-tests index c419dcc00d..2424b0e498 100644 --- a/modules/pthread-mutex-tests +++ b/modules/pthread-mutex-tests @@ -6,6 +6,7 @@ tests/macros.h Depends-on: pthread-thread sched_yield +random configure.ac: diff --git a/modules/pthread-rwlock-tests b/modules/pthread-rwlock-tests index 594f6b6601..85d79e9fe0 100644 --- a/modules/pthread-rwlock-tests +++ b/modules/pthread-rwlock-tests @@ -7,6 +7,7 @@ Depends-on: pthread-thread pthread-mutex sched_yield +random configure.ac: diff --git a/modules/pthread-spin-tests b/modules/pthread-spin-tests index aafe8c7bcd..4378f1bdaf 100644 --- a/modules/pthread-spin-tests +++ b/modules/pthread-spin-tests @@ -8,6 +8,7 @@ Depends-on: pthread-thread pthread-mutex sched_yield +random configure.ac: AC_CHECK_HEADERS_ONCE([semaphore.h]) diff --git a/modules/pthread-tss-tests b/modules/pthread-tss-tests index caa4520e52..80064708ad 100644 --- a/modules/pthread-tss-tests +++ b/modules/pthread-tss-tests @@ -6,6 +6,7 @@ Depends-on: pthread-thread pthread-mutex sched_yield +random configure.ac: diff --git a/modules/threads-h-tests b/modules/threads-h-tests index abbd43aca5..c9195e5f56 100644 --- a/modules/threads-h-tests +++ b/modules/threads-h-tests @@ -7,6 +7,7 @@ Depends-on: threads-h-c++-tests thrd stdint +random configure.ac: AC_CHECK_DECLS_ONCE([alarm]) diff --git a/modules/tls-tests b/modules/tls-tests index aec1935afc..2eed659425 100644 --- a/modules/tls-tests +++ b/modules/tls-tests @@ -6,6 +6,7 @@ thread lock stdint yield +random configure.ac: AC_CHECK_DECLS_ONCE([alarm]) diff --git a/modules/tss-tests b/modules/tss-tests index c96c10d002..3526d22c28 100644 --- a/modules/tss-tests +++ b/modules/tss-tests @@ -6,6 +6,7 @@ Depends-on: thrd mtx stdint +random configure.ac: AC_CHECK_DECLS_ONCE([alarm]) diff --git a/tests/test-asyncsafe-spin2.c b/tests/test-asyncsafe-spin2.c index ac4eab52c3..9824aef66c 100644 --- a/tests/test-asyncsafe-spin2.c +++ b/tests/test-asyncsafe-spin2.c @@ -95,7 +95,7 @@ static int account[ACCOUNT_COUNT]; static int random_account (void) { - return ((unsigned int) rand () >> 3) % ACCOUNT_COUNT; + return ((unsigned long) random () >> 3) % ACCOUNT_COUNT; } static void @@ -135,7 +135,7 @@ lock_mutator_thread (void *arg) i1 = random_account (); i2 = random_account (); - value = ((unsigned int) rand () >> 3) % 10; + value = ((unsigned long) random () >> 3) % 10; account[i1] += value; account[i2] -= value; diff --git a/tests/test-lock.c b/tests/test-lock.c index b52f7e0cd8..a8d92fa073 100644 --- a/tests/test-lock.c +++ b/tests/test-lock.c @@ -118,7 +118,7 @@ static int account[ACCOUNT_COUNT]; static int random_account (void) { - return ((unsigned int) rand () >> 3) % ACCOUNT_COUNT; + return ((unsigned long) random () >> 3) % ACCOUNT_COUNT; } static void @@ -157,7 +157,7 @@ lock_mutator_thread (void *arg) i1 = random_account (); i2 = random_account (); - value = ((unsigned int) rand () >> 3) % 10; + value = ((unsigned long) random () >> 3) % 10; account[i1] += value; account[i2] -= value; @@ -248,7 +248,7 @@ rwlock_mutator_thread (void *arg) i1 = random_account (); i2 = random_account (); - value = ((unsigned int) rand () >> 3) % 10; + value = ((unsigned long) random () >> 3) % 10; account[i1] += value; account[i2] -= value; @@ -331,12 +331,12 @@ recshuffle (void) i1 = random_account (); i2 = random_account (); - value = ((unsigned int) rand () >> 3) % 10; + value = ((unsigned long) random () >> 3) % 10; account[i1] += value; account[i2] -= value; /* Recursive with probability 0.5. */ - if (((unsigned int) rand () >> 3) % 2) + if (((unsigned long) random () >> 3) % 2) recshuffle (); dbgprintf ("Mutator %p before unlock\n", gl_thread_self_pointer ()); diff --git a/tests/test-mtx.c b/tests/test-mtx.c index 128c403720..a7712f6e5e 100644 --- a/tests/test-mtx.c +++ b/tests/test-mtx.c @@ -93,7 +93,7 @@ static int account[ACCOUNT_COUNT]; static int random_account (void) { - return ((unsigned int) rand () >> 3) % ACCOUNT_COUNT; + return ((unsigned long) random () >> 3) % ACCOUNT_COUNT; } static void @@ -132,7 +132,7 @@ lock_mutator_thread (void *arg) i1 = random_account (); i2 = random_account (); - value = ((unsigned int) rand () >> 3) % 10; + value = ((unsigned long) random () >> 3) % 10; account[i1] += value; account[i2] -= value; @@ -221,12 +221,12 @@ recshuffle (void) i1 = random_account (); i2 = random_account (); - value = ((unsigned int) rand () >> 3) % 10; + value = ((unsigned long) random () >> 3) % 10; account[i1] += value; account[i2] -= value; /* Recursive with probability 0.5. */ - if (((unsigned int) rand () >> 3) % 2) + if (((unsigned long) random () >> 3) % 2) recshuffle (); dbgprintf ("Mutator %p before unlock\n", thrd_current_pointer ()); diff --git a/tests/test-pthread-mutex.c b/tests/test-pthread-mutex.c index 4f093eb25f..ca698093a4 100644 --- a/tests/test-pthread-mutex.c +++ b/tests/test-pthread-mutex.c @@ -93,7 +93,7 @@ static int account[ACCOUNT_COUNT]; static int random_account (void) { - return ((unsigned int) rand () >> 3) % ACCOUNT_COUNT; + return ((unsigned long) random () >> 3) % ACCOUNT_COUNT; } static void @@ -132,7 +132,7 @@ lock_mutator_thread (void *arg) i1 = random_account (); i2 = random_account (); - value = ((unsigned int) rand () >> 3) % 10; + value = ((unsigned long) random () >> 3) % 10; account[i1] += value; account[i2] -= value; @@ -220,12 +220,12 @@ recshuffle (void) i1 = random_account (); i2 = random_account (); - value = ((unsigned int) rand () >> 3) % 10; + value = ((unsigned long) random () >> 3) % 10; account[i1] += value; account[i2] -= value; /* Recursive with probability 0.5. */ - if (((unsigned int) rand () >> 3) % 2) + if (((unsigned long) random () >> 3) % 2) recshuffle (); dbgprintf ("Mutator %p before unlock\n", pthread_self_pointer ()); diff --git a/tests/test-pthread-rwlock.c b/tests/test-pthread-rwlock.c index d4a59812ff..467357abd6 100644 --- a/tests/test-pthread-rwlock.c +++ b/tests/test-pthread-rwlock.c @@ -87,7 +87,7 @@ static int account[ACCOUNT_COUNT]; static int random_account (void) { - return ((unsigned int) rand () >> 3) % ACCOUNT_COUNT; + return ((unsigned long) random () >> 3) % ACCOUNT_COUNT; } static void @@ -126,7 +126,7 @@ rwlock_mutator_thread (void *arg) i1 = random_account (); i2 = random_account (); - value = ((unsigned int) rand () >> 3) % 10; + value = ((unsigned long) random () >> 3) % 10; account[i1] += value; account[i2] -= value; diff --git a/tests/test-pthread-spin.c b/tests/test-pthread-spin.c index ede62e33dd..8abe9cfb72 100644 --- a/tests/test-pthread-spin.c +++ b/tests/test-pthread-spin.c @@ -85,7 +85,7 @@ static int account[ACCOUNT_COUNT]; static int random_account (void) { - return ((unsigned int) rand () >> 3) % ACCOUNT_COUNT; + return ((unsigned long) random () >> 3) % ACCOUNT_COUNT; } static void @@ -124,7 +124,7 @@ lock_mutator_thread (void *arg) i1 = random_account (); i2 = random_account (); - value = ((unsigned int) rand () >> 3) % 10; + value = ((unsigned long) random () >> 3) % 10; account[i1] += value; account[i2] -= value; diff --git a/tests/test-pthread-tss.c b/tests/test-pthread-tss.c index 513e142d51..23fec9caf2 100644 --- a/tests/test-pthread-tss.c +++ b/tests/test-pthread-tss.c @@ -71,7 +71,7 @@ perhaps_yield (void) { /* Call yield () only with a certain probability, otherwise the sequence of thread activations may be too predictable. */ - if ((((unsigned int) rand () >> 3) % 4) == 0) + if ((((unsigned long) random () >> 3) % 4) == 0) yield (); } @@ -100,7 +100,7 @@ worker_thread (void *arg) /* Initialize the per-thread storage. */ for (i = 0; i < KEYS_COUNT; i++) { - values[i] = (((unsigned int) rand () >> 3) % 1000000) * THREAD_COUNT + id; + values[i] = (((unsigned long) random () >> 3) % 1000000) * THREAD_COUNT + id; /* Hopefully no arithmetic overflow. */ if ((values[i] % THREAD_COUNT) != id) abort (); @@ -132,8 +132,8 @@ worker_thread (void *arg) for (repeat = REPEAT_COUNT; repeat > 0; repeat--) { dbgprintf ("Worker %p doing value swapping\n", pthread_self_pointer ()); - i = ((unsigned int) rand () >> 3) % KEYS_COUNT; - j = ((unsigned int) rand () >> 3) % KEYS_COUNT; + i = ((unsigned long) random () >> 3) % KEYS_COUNT; + j = ((unsigned long) random () >> 3) % KEYS_COUNT; if (i != j) { void *vi = pthread_getspecific (mykeys[i]); @@ -462,7 +462,7 @@ racecheck_thread (void *arg) for (repeat = REPEAT_COUNT; repeat > 0; repeat--) { - i = ((unsigned int) rand () >> 3) % KEYS_COUNT; + i = ((unsigned long) random () >> 3) % KEYS_COUNT; dbgprintf ("Worker %p reallocating key %d\n", pthread_self_pointer (), i); ASSERT (pthread_key_delete (keys[i]) == 0); ASSERT (pthread_key_create (&keys[i], destructor_table[i]) == 0); diff --git a/tests/test-thread_local.c b/tests/test-thread_local.c index 28536535ff..4ea788f902 100644 --- a/tests/test-thread_local.c +++ b/tests/test-thread_local.c @@ -76,7 +76,7 @@ perhaps_yield (void) { /* Call yield () only with a certain probability, otherwise the sequence of thread activations may be too predictable. */ - if ((((unsigned int) rand () >> 3) % 4) == 0) + if ((((unsigned long) random () >> 3) % 4) == 0) yield (); } @@ -102,7 +102,7 @@ worker_thread (void *arg) dbgprintf ("Worker %p before first assignment\n", thrd_current_pointer ()); for (i = 0; i < KEYS_COUNT; i++) { - *values[i] = (((unsigned int) rand () >> 3) % 1000000) * THREAD_COUNT + id; + *values[i] = (((unsigned long) random () >> 3) % 1000000) * THREAD_COUNT + id; /* Hopefully no arithmetic overflow. */ if ((*values[i] % THREAD_COUNT) != id) abort (); @@ -114,8 +114,8 @@ worker_thread (void *arg) for (repeat = REPEAT_COUNT; repeat > 0; repeat--) { dbgprintf ("Worker %p doing value swapping\n", thrd_current_pointer ()); - i = ((unsigned int) rand () >> 3) % KEYS_COUNT; - j = ((unsigned int) rand () >> 3) % KEYS_COUNT; + i = ((unsigned long) random () >> 3) % KEYS_COUNT; + j = ((unsigned long) random () >> 3) % KEYS_COUNT; if (i != j) { unsigned int vi = *values[i]; diff --git a/tests/test-tls.c b/tests/test-tls.c index 2b0a33ec5c..42e8591a77 100644 --- a/tests/test-tls.c +++ b/tests/test-tls.c @@ -68,7 +68,7 @@ static void perhaps_yield (void) { /* This helps making the sequence of thread activations less predictable. */ - if ((((unsigned int) rand () >> 3) % 4) == 0) + if ((((unsigned long) random () >> 3) % 4) == 0) yield (); } @@ -97,7 +97,7 @@ worker_thread (void *arg) /* Initialize the per-thread storage. */ for (i = 0; i < KEYS_COUNT; i++) { - values[i] = (((unsigned int) rand () >> 3) % 1000000) * THREAD_COUNT + id; + values[i] = (((unsigned long) random () >> 3) % 1000000) * THREAD_COUNT + id; /* Hopefully no arithmetic overflow. */ if ((values[i] % THREAD_COUNT) != id) abort (); @@ -127,8 +127,8 @@ worker_thread (void *arg) for (repeat = REPEAT_COUNT; repeat > 0; repeat--) { dbgprintf ("Worker %p doing value swapping\n", gl_thread_self_pointer ()); - i = ((unsigned int) rand () >> 3) % KEYS_COUNT; - j = ((unsigned int) rand () >> 3) % KEYS_COUNT; + i = ((unsigned long) random () >> 3) % KEYS_COUNT; + j = ((unsigned long) random () >> 3) % KEYS_COUNT; if (i != j) { void *vi = gl_tls_get (mykeys[i]); @@ -436,7 +436,7 @@ racecheck_thread (void *arg) for (repeat = REPEAT_COUNT; repeat > 0; repeat--) { - i = ((unsigned int) rand () >> 3) % KEYS_COUNT; + i = ((unsigned long) random () >> 3) % KEYS_COUNT; dbgprintf ("Worker %p reallocating key %d\n", gl_thread_self_pointer (), i); gl_tls_key_destroy (keys[i]); gl_tls_key_init (keys[i], destructor_table[i]); diff --git a/tests/test-tss.c b/tests/test-tss.c index ae17844ee5..ea7524e155 100644 --- a/tests/test-tss.c +++ b/tests/test-tss.c @@ -68,7 +68,7 @@ perhaps_yield (void) { /* Call yield () only with a certain probability, otherwise the sequence of thread activations may be too predictable. */ - if ((((unsigned int) rand () >> 3) % 4) == 0) + if ((((unsigned long) random () >> 3) % 4) == 0) yield (); } @@ -97,7 +97,7 @@ worker_thread (void *arg) /* Initialize the per-thread storage. */ for (i = 0; i < KEYS_COUNT; i++) { - values[i] = (((unsigned int) rand () >> 3) % 1000000) * THREAD_COUNT + id; + values[i] = (((unsigned long) random () >> 3) % 1000000) * THREAD_COUNT + id; /* Hopefully no arithmetic overflow. */ if ((values[i] % THREAD_COUNT) != id) abort (); @@ -127,8 +127,8 @@ worker_thread (void *arg) for (repeat = REPEAT_COUNT; repeat > 0; repeat--) { dbgprintf ("Worker %p doing value swapping\n", thrd_current_pointer ()); - i = ((unsigned int) rand () >> 3) % KEYS_COUNT; - j = ((unsigned int) rand () >> 3) % KEYS_COUNT; + i = ((unsigned long) random () >> 3) % KEYS_COUNT; + j = ((unsigned long) random () >> 3) % KEYS_COUNT; if (i != j) { void *vi = tss_get (mykeys[i]); @@ -442,7 +442,7 @@ racecheck_thread (void *arg) for (repeat = REPEAT_COUNT; repeat > 0; repeat--) { - i = ((unsigned int) rand () >> 3) % KEYS_COUNT; + i = ((unsigned long) random () >> 3) % KEYS_COUNT; dbgprintf ("Worker %p reallocating key %d\n", thrd_current_pointer (), i); tss_delete (keys[i]); ASSERT (tss_create (&keys[i], destructor_table[i]) == thrd_success); -- 2.39.5