]> Savannah Git Hosting - gnulib.git/commitdiff
tests: In multithreaded tests, use random() instead of rand().
authorBruno Haible <bruno@clisp.org>
Fri, 10 Nov 2023 18:01:40 +0000 (19:01 +0100)
committerBruno Haible <bruno@clisp.org>
Fri, 24 Nov 2023 07:27:42 +0000 (08:27 +0100)
* 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.

21 files changed:
ChangeLog
modules/asyncsafe-spin-tests
modules/lock-tests
modules/mtx-tests
modules/pthread-mutex-tests
modules/pthread-rwlock-tests
modules/pthread-spin-tests
modules/pthread-tss-tests
modules/threads-h-tests
modules/tls-tests
modules/tss-tests
tests/test-asyncsafe-spin2.c
tests/test-lock.c
tests/test-mtx.c
tests/test-pthread-mutex.c
tests/test-pthread-rwlock.c
tests/test-pthread-spin.c
tests/test-pthread-tss.c
tests/test-thread_local.c
tests/test-tls.c
tests/test-tss.c

index 396668b66c7345dd55dad6bf852b3644295812e5..e7b85edc8069ecc25031f0c5772f0e2e97e5eac5 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,36 @@
+2023-11-10  Bruno Haible  <bruno@clisp.org>
+
+       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  <bruno@clisp.org>
 
        random: Fix multithread-safety bug.
index 301c2afc1ac9a8a2b3c358cd187fe0d643f069cf..dacb26aaace52bd316b24d6af1c93c0116396594 100644 (file)
@@ -8,6 +8,7 @@ Depends-on:
 thread
 lock
 yield
+random
 
 configure.ac:
 AC_CHECK_HEADERS_ONCE([semaphore.h])
index 502c36882fb1c82447d5e3df0537e90c99d1ee6b..7b398e2327a1abfb41ed63e479426607414a4010 100644 (file)
@@ -11,6 +11,7 @@ thread
 stdint
 usleep
 yield
+random
 
 configure.ac:
 AC_CHECK_HEADERS_ONCE([semaphore.h])
index 52310625a558da82f95f5c67ed0341522586fe54..6de1f3d84062831440b2194769f39d369d2abc1c 100644 (file)
@@ -8,6 +8,7 @@ Depends-on:
 thrd
 lock
 stdint
+random
 
 configure.ac:
 AC_CHECK_HEADERS_ONCE([semaphore.h])
index c419dcc00d84608c5161be2f34a35fb769639207..2424b0e49852a3de48d04d27be6ac0e5c6173481 100644 (file)
@@ -6,6 +6,7 @@ tests/macros.h
 Depends-on:
 pthread-thread
 sched_yield
+random
 
 configure.ac:
 
index 594f6b6601d979b4e019b3ab2ae8f11a916aca06..85d79e9fe060613002ef3966da9f4133fa35777f 100644 (file)
@@ -7,6 +7,7 @@ Depends-on:
 pthread-thread
 pthread-mutex
 sched_yield
+random
 
 configure.ac:
 
index aafe8c7bcdcf00bbf7ec97480cf4700e87c46149..4378f1bdaf2a186c9c2596338b931809907ed341 100644 (file)
@@ -8,6 +8,7 @@ Depends-on:
 pthread-thread
 pthread-mutex
 sched_yield
+random
 
 configure.ac:
 AC_CHECK_HEADERS_ONCE([semaphore.h])
index caa4520e52133d8e0c894513dfbfee51761807d2..80064708adcf16f23d0f171e4505cbb1cf154b4e 100644 (file)
@@ -6,6 +6,7 @@ Depends-on:
 pthread-thread
 pthread-mutex
 sched_yield
+random
 
 configure.ac:
 
index abbd43aca594eec1e2e85a6fd555ba7de27a5d41..c9195e5f56c6313c16aa2ab44da639c5bda2f363 100644 (file)
@@ -7,6 +7,7 @@ Depends-on:
 threads-h-c++-tests
 thrd
 stdint
+random
 
 configure.ac:
 AC_CHECK_DECLS_ONCE([alarm])
index aec1935afc6e4a772e965bbfbdf3e220743a4f3f..2eed6594253f022528626f665fadc765a0fde3bb 100644 (file)
@@ -6,6 +6,7 @@ thread
 lock
 stdint
 yield
+random
 
 configure.ac:
 AC_CHECK_DECLS_ONCE([alarm])
index c96c10d002902fafb575fc789a7c40603415cc90..3526d22c28c9527b02781e8b768adceb3fe55bf9 100644 (file)
@@ -6,6 +6,7 @@ Depends-on:
 thrd
 mtx
 stdint
+random
 
 configure.ac:
 AC_CHECK_DECLS_ONCE([alarm])
index ac4eab52c3de017d8d753652fcdb9e1fc25edbc7..9824aef66ce94678bbe4cbf2ab6763f5e24971bc 100644 (file)
@@ -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;
 
index b52f7e0cd85562f3ac38a84f3ffccb9cb983366a..a8d92fa073ed0e1cebd353c8d74f3fa079163cb1 100644 (file)
@@ -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 ());
index 128c403720fd53b6d986375a174ad1a554d7ba3b..a7712f6e5ed5f042fe525feaae1b1375c1dcd6e7 100644 (file)
@@ -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 ());
index 4f093eb25f0e4e785233439c4c87e32d98bfc579..ca698093a489339280c62d9355f2a199906a6f8c 100644 (file)
@@ -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 ());
index d4a59812ff6d07d6cd34a41b4b5f681619eab5cc..467357abd67abb4a3a8f4da00abd0e2c48fef7e1 100644 (file)
@@ -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;
 
index ede62e33dd572c9d2789695bbd1c65eef45eaff3..8abe9cfb729483ae0e83c9518ea4f1b5eb703331 100644 (file)
@@ -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;
 
index 513e142d5162613b4f29a3e6cd4294719108d89d..23fec9caf259ab623f2ad96fb9ce4994e4009464 100644 (file)
@@ -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);
index 28536535fff1f96f42b9aefbcb1043d8c92988dd..4ea788f902a7791da22f37dfac216532d3144076 100644 (file)
@@ -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];
index 2b0a33ec5c9059090618f71869cd085b307879ed..42e8591a77b795d7dd4ab66a5fe2b6d85a4af750 100644 (file)
@@ -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]);
index ae17844ee56ea2e0ad5026b1cfb3ed7902d55d1f..ea7524e155ff2cbdc499f3a05cefec7236a1b246 100644 (file)
@@ -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);