|
Packit Service |
0da75f |
/* Bug 23844: Test for pthread_rwlock_trywrlock stalls.
|
|
Packit Service |
0da75f |
Copyright (C) 2019 Free Software Foundation, Inc.
|
|
Packit Service |
0da75f |
This file is part of the GNU C Library.
|
|
Packit Service |
0da75f |
|
|
Packit Service |
0da75f |
The GNU C Library is free software; you can redistribute it and/or
|
|
Packit Service |
0da75f |
modify it under the terms of the GNU Lesser General Public
|
|
Packit Service |
0da75f |
License as published by the Free Software Foundation; either
|
|
Packit Service |
0da75f |
version 2.1 of the License, or (at your option) any later version.
|
|
Packit Service |
0da75f |
|
|
Packit Service |
0da75f |
The GNU C Library is distributed in the hope that it will be useful,
|
|
Packit Service |
0da75f |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit Service |
0da75f |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit Service |
0da75f |
Lesser General Public License for more details.
|
|
Packit Service |
0da75f |
|
|
Packit Service |
0da75f |
You should have received a copy of the GNU Lesser General Public
|
|
Packit Service |
0da75f |
License along with the GNU C Library; if not, see
|
|
Packit Service |
0da75f |
<http://www.gnu.org/licenses/>. */
|
|
Packit Service |
0da75f |
|
|
Packit Service |
0da75f |
/* For a full analysis see comments in tst-rwlock-tryrdlock-stall.c.
|
|
Packit Service |
0da75f |
|
|
Packit Service |
0da75f |
Summary for the pthread_rwlock_trywrlock() stall:
|
|
Packit Service |
0da75f |
|
|
Packit Service |
0da75f |
The stall is caused by pthread_rwlock_trywrlock setting
|
|
Packit Service |
0da75f |
__wrphase_futex futex to 1 and loosing the
|
|
Packit Service |
0da75f |
PTHREAD_RWLOCK_FUTEX_USED bit.
|
|
Packit Service |
0da75f |
|
|
Packit Service |
0da75f |
The fix for bug 23844 ensures that waiters on __wrphase_futex are
|
|
Packit Service |
0da75f |
correctly woken. Before the fix the test stalls as readers can
|
|
Packit Service |
0da75f |
wait forever on __wrphase_futex. */
|
|
Packit Service |
0da75f |
|
|
Packit Service |
0da75f |
#include <stdio.h>
|
|
Packit Service |
0da75f |
#include <stdlib.h>
|
|
Packit Service |
0da75f |
#include <unistd.h>
|
|
Packit Service |
0da75f |
#include <pthread.h>
|
|
Packit Service |
0da75f |
#include <support/xthread.h>
|
|
Packit Service |
0da75f |
#include <errno.h>
|
|
Packit Service |
0da75f |
|
|
Packit Service |
0da75f |
/* We need only one lock to reproduce the issue. We will need multiple
|
|
Packit Service |
0da75f |
threads to get the exact case where we have a read, try, and unlock
|
|
Packit Service |
0da75f |
all interleaving to produce the case where the readers are waiting
|
|
Packit Service |
0da75f |
and the try clears the PTHREAD_RWLOCK_FUTEX_USED bit and a
|
|
Packit Service |
0da75f |
subsequent unlock fails to wake them. */
|
|
Packit Service |
0da75f |
pthread_rwlock_t onelock;
|
|
Packit Service |
0da75f |
|
|
Packit Service |
0da75f |
/* The number of threads is arbitrary but empirically chosen to have
|
|
Packit Service |
0da75f |
enough threads that we see the condition where waiting readers are
|
|
Packit Service |
0da75f |
not woken by a successful unlock. */
|
|
Packit Service |
0da75f |
#define NTHREADS 32
|
|
Packit Service |
0da75f |
|
|
Packit Service |
0da75f |
_Atomic int do_exit;
|
|
Packit Service |
0da75f |
|
|
Packit Service |
0da75f |
void *
|
|
Packit Service |
0da75f |
run_loop (void *arg)
|
|
Packit Service |
0da75f |
{
|
|
Packit Service |
0da75f |
int i = 0, ret;
|
|
Packit Service |
0da75f |
while (!do_exit)
|
|
Packit Service |
0da75f |
{
|
|
Packit Service |
0da75f |
/* Arbitrarily choose if we are the writer or reader. Choose a
|
|
Packit Service |
0da75f |
high enough ratio of readers to writers to make it likely
|
|
Packit Service |
0da75f |
that readers block (and eventually are susceptable to
|
|
Packit Service |
0da75f |
stalling).
|
|
Packit Service |
0da75f |
|
|
Packit Service |
0da75f |
If we are a writer, take the write lock, and then unlock.
|
|
Packit Service |
0da75f |
If we are a reader, try the lock, then lock, then unlock. */
|
|
Packit Service |
0da75f |
if ((i % 8) != 0)
|
|
Packit Service |
0da75f |
{
|
|
Packit Service |
0da75f |
if ((ret = pthread_rwlock_trywrlock (&onelock)) != 0)
|
|
Packit Service |
0da75f |
{
|
|
Packit Service |
0da75f |
if (ret == EBUSY)
|
|
Packit Service |
0da75f |
xpthread_rwlock_wrlock (&onelock);
|
|
Packit Service |
0da75f |
else
|
|
Packit Service |
0da75f |
exit (EXIT_FAILURE);
|
|
Packit Service |
0da75f |
}
|
|
Packit Service |
0da75f |
}
|
|
Packit Service |
0da75f |
else
|
|
Packit Service |
0da75f |
xpthread_rwlock_rdlock (&onelock);
|
|
Packit Service |
0da75f |
/* Thread does some work and then unlocks. */
|
|
Packit Service |
0da75f |
xpthread_rwlock_unlock (&onelock);
|
|
Packit Service |
0da75f |
i++;
|
|
Packit Service |
0da75f |
}
|
|
Packit Service |
0da75f |
return NULL;
|
|
Packit Service |
0da75f |
}
|
|
Packit Service |
0da75f |
|
|
Packit Service |
0da75f |
int
|
|
Packit Service |
0da75f |
do_test (void)
|
|
Packit Service |
0da75f |
{
|
|
Packit Service |
0da75f |
int i;
|
|
Packit Service |
0da75f |
pthread_t tids[NTHREADS];
|
|
Packit Service |
0da75f |
xpthread_rwlock_init (&onelock, NULL);
|
|
Packit Service |
0da75f |
for (i = 0; i < NTHREADS; i++)
|
|
Packit Service |
0da75f |
tids[i] = xpthread_create (NULL, run_loop, NULL);
|
|
Packit Service |
0da75f |
/* Run for some amount of time. The pthread_rwlock_tryrwlock stall
|
|
Packit Service |
0da75f |
is very easy to trigger and happens in seconds under the test
|
|
Packit Service |
0da75f |
conditions. */
|
|
Packit Service |
0da75f |
sleep (10);
|
|
Packit Service |
0da75f |
/* Then exit. */
|
|
Packit Service |
0da75f |
printf ("INFO: Exiting...\n");
|
|
Packit Service |
0da75f |
do_exit = 1;
|
|
Packit Service |
0da75f |
/* If any readers stalled then we will timeout waiting for them. */
|
|
Packit Service |
0da75f |
for (i = 0; i < NTHREADS; i++)
|
|
Packit Service |
0da75f |
xpthread_join (tids[i]);
|
|
Packit Service |
0da75f |
printf ("INFO: Done.\n");
|
|
Packit Service |
0da75f |
xpthread_rwlock_destroy (&onelock);
|
|
Packit Service |
0da75f |
printf ("PASS: No pthread_rwlock_tryrwlock stalls detected.\n");
|
|
Packit Service |
0da75f |
return 0;
|
|
Packit Service |
0da75f |
}
|
|
Packit Service |
0da75f |
|
|
Packit Service |
0da75f |
#include <support/test-driver.c>
|