1
0
mirror of https://git.savannah.gnu.org/git/gnulib.git synced 2025-08-08 17:22:05 +03:00

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.
This commit is contained in:
Bruno Haible
2023-11-10 19:01:40 +01:00
parent 6c7ea007d0
commit 8c5f631464
21 changed files with 81 additions and 38 deletions

View File

@@ -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>
doc: Mention an srandom limitation on OpenBSD.

View File

@@ -8,6 +8,7 @@ Depends-on:
thread
lock
yield
random
configure.ac:
AC_CHECK_HEADERS_ONCE([semaphore.h])

View File

@@ -11,6 +11,7 @@ thread
stdint
usleep
yield
random
configure.ac:
AC_CHECK_HEADERS_ONCE([semaphore.h])

View File

@@ -7,6 +7,7 @@ Depends-on:
thrd
lock
stdint
random
configure.ac:
AC_CHECK_HEADERS_ONCE([semaphore.h])

View File

@@ -6,6 +6,7 @@ tests/macros.h
Depends-on:
pthread-thread
sched_yield
random
configure.ac:

View File

@@ -7,6 +7,7 @@ Depends-on:
pthread-thread
pthread-mutex
sched_yield
random
configure.ac:

View File

@@ -8,6 +8,7 @@ Depends-on:
pthread-thread
pthread-mutex
sched_yield
random
configure.ac:
AC_CHECK_HEADERS_ONCE([semaphore.h])

View File

@@ -6,6 +6,7 @@ Depends-on:
pthread-thread
pthread-mutex
sched_yield
random
configure.ac:

View File

@@ -7,6 +7,7 @@ Depends-on:
threads-h-c++-tests
thrd
stdint
random
configure.ac:
AC_CHECK_DECLS_ONCE([alarm])

View File

@@ -6,6 +6,7 @@ thread
lock
stdint
yield
random
configure.ac:
AC_CHECK_DECLS_ONCE([alarm])

View File

@@ -6,6 +6,7 @@ Depends-on:
thrd
mtx
stdint
random
configure.ac:
AC_CHECK_DECLS_ONCE([alarm])

View 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;

View 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 (_GL_UNUSED 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 (_GL_UNUSED 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 ());

View 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 ());

View 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 ());

View 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;

View 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;

View 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);

View 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];

View 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]);

View 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);