]> 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:09:58 +0000 (08:09 +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 0ba0ee3258605ba853717d4553d2a8926d88fa0c..ec97c989b85bae45f5f4992f64f514dba06d4e85 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 4a021d22fa79bed0dc84a08b95c21250072ca7a7..063a56d67f7d76b069e09a522ccfb41236c0d284 100644 (file)
@@ -7,6 +7,7 @@ Depends-on:
 thrd
 lock
 stdint
+random
 
 configure.ac:
 AC_CHECK_HEADERS_ONCE([semaphore.h])
index 4575ef5c7965d85a514a5f57da1979686d50e741..9475a84a99b93b736282feb12d5b72d8a9b75e89 100644 (file)
@@ -6,6 +6,7 @@ tests/macros.h
 Depends-on:
 pthread-thread
 sched_yield
+random
 
 configure.ac:
 
index af29895580ed8f5af094c1eeb50909894c9e708f..129570b67d83b3e0d725c251db3cdb10c086455e 100644 (file)
@@ -7,6 +7,7 @@ Depends-on:
 pthread-thread
 pthread-mutex
 sched_yield
+random
 
 configure.ac:
 
index 9dfa698ca4d8d91a036032abfe3e9251f7f24702..4aec8e20be130197747d32b7e1fe44a920b2635f 100644 (file)
@@ -8,6 +8,7 @@ Depends-on:
 pthread-thread
 pthread-mutex
 sched_yield
+random
 
 configure.ac:
 AC_CHECK_HEADERS_ONCE([semaphore.h])
index 1d339345dd40d9527f0c5be708ea26ff6ab5efb1..c3060b5ee5f9045025526d6d387a1b7c6ecf5b98 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 b00a9a554a34f3ab3ddc68071d16252650646bc7..7e7d9a6a2c168fa047eba12d1c72a919531a6e12 100644 (file)
@@ -92,7 +92,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
@@ -131,7 +131,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", 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 0caf475e5400797fe91357d082d79719e7e32e8e..e68e890e53a611a8ab4c38b46b30872e69254320 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 6a59a65ab0d0486920ed338fb032c7615a2a1c6c..a29afbd90ae3ce83f017d8e8edef68230ca047c8 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 9d1db0249ad77c5074c647cbff1b3f95587e893c..ad9fd45b57bee7771fbc1df621fa33b833a79f15 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);