|
Packit Service |
82fcde |
/* Test program for a read-phase / write-phase explicit hand-over.
|
|
Packit Service |
82fcde |
Copyright (C) 2017-2018 Free Software Foundation, Inc.
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
The GNU C Library is free software; you can redistribute it and/or
|
|
Packit Service |
82fcde |
modify it under the terms of the GNU Lesser General Public License as
|
|
Packit Service |
82fcde |
published by the Free Software Foundation; either version 2.1 of the
|
|
Packit Service |
82fcde |
License, or (at your option) any later version.
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
The GNU C Library is distributed in the hope that it will be useful,
|
|
Packit Service |
82fcde |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit Service |
82fcde |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit Service |
82fcde |
Lesser General Public License for more details.
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
You should have received a copy of the GNU Lesser General Public
|
|
Packit Service |
82fcde |
License along with the GNU C Library; see the file COPYING.LIB. If
|
|
Packit Service |
82fcde |
not, see <http://www.gnu.org/licenses/>. */
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
#include <errno.h>
|
|
Packit Service |
82fcde |
#include <error.h>
|
|
Packit Service |
82fcde |
#include <pthread.h>
|
|
Packit Service |
82fcde |
#include <stdio.h>
|
|
Packit Service |
82fcde |
#include <stdlib.h>
|
|
Packit Service |
82fcde |
#include <unistd.h>
|
|
Packit Service |
82fcde |
#include <stdint.h>
|
|
Packit Service |
82fcde |
#include <time.h>
|
|
Packit Service |
82fcde |
#include <atomic.h>
|
|
Packit Service |
82fcde |
#include <support/xthread.h>
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
/* We realy want to set threads to 2 to reproduce this issue. The goal
|
|
Packit Service |
82fcde |
is to have one primary writer and a single reader, and to hit the
|
|
Packit Service |
82fcde |
bug that happens in the interleaving of those two phase transitions.
|
|
Packit Service |
82fcde |
However, on most hardware, adding a second writer seems to help the
|
|
Packit Service |
82fcde |
interleaving happen slightly more often, say 20% of the time. On a
|
|
Packit Service |
82fcde |
16 core ppc64 machine this fails 100% of the time with an unpatched
|
|
Packit Service |
82fcde |
glibc. On a 8 core x86_64 machine this fails ~93% of the time, but
|
|
Packit Service |
82fcde |
it doesn't fail at all on a 4 core system, so having available
|
|
Packit Service |
82fcde |
unloaded cores makes a big difference in reproducibility. On an 8
|
|
Packit Service |
82fcde |
core qemu/kvm guest the reproducer reliability drops to ~10%. */
|
|
Packit Service |
82fcde |
#define THREADS 3
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
#define KIND PTHREAD_RWLOCK_PREFER_READER_NP
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
static pthread_rwlock_t lock;
|
|
Packit Service |
82fcde |
static int done = 0;
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
static void*
|
|
Packit Service |
82fcde |
tf (void* arg)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
while (atomic_load_relaxed (&done) == 0)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
int rcnt = 0;
|
|
Packit Service |
82fcde |
int wcnt = 100;
|
|
Packit Service |
82fcde |
if ((uintptr_t) arg == 0)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
rcnt = 1;
|
|
Packit Service |
82fcde |
wcnt = 1;
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
do
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
if (wcnt)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
xpthread_rwlock_wrlock (&lock);
|
|
Packit Service |
82fcde |
xpthread_rwlock_unlock (&lock);
|
|
Packit Service |
82fcde |
wcnt--;
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
if (rcnt)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
xpthread_rwlock_rdlock (&lock);
|
|
Packit Service |
82fcde |
xpthread_rwlock_unlock (&lock);
|
|
Packit Service |
82fcde |
rcnt--;
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
while ((atomic_load_relaxed (&done) == 0) && (rcnt + wcnt > 0));
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
return NULL;
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
static int
|
|
Packit Service |
82fcde |
do_test (void)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
pthread_t thr[THREADS];
|
|
Packit Service |
82fcde |
int n;
|
|
Packit Service |
82fcde |
pthread_rwlockattr_t attr;
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
xpthread_rwlockattr_init (&attr);
|
|
Packit Service |
82fcde |
xpthread_rwlockattr_setkind_np (&attr, KIND);
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
xpthread_rwlock_init (&lock, &attr);
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
/* Make standard error the same as standard output. */
|
|
Packit Service |
82fcde |
dup2 (1, 2);
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
/* Make sure we see all message, even those on stdout. */
|
|
Packit Service |
82fcde |
setvbuf (stdout, NULL, _IONBF, 0);
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
for (n = 0; n < THREADS; ++n)
|
|
Packit Service |
82fcde |
thr[n] = xpthread_create (NULL, tf, (void *) (uintptr_t) n);
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
struct timespec delay;
|
|
Packit Service |
82fcde |
delay.tv_sec = 10;
|
|
Packit Service |
82fcde |
delay.tv_nsec = 0;
|
|
Packit Service |
82fcde |
nanosleep (&delay, NULL);
|
|
Packit Service |
82fcde |
atomic_store_relaxed (&done, 1);
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
/* Wait for all the threads. */
|
|
Packit Service |
82fcde |
for (n = 0; n < THREADS; ++n)
|
|
Packit Service |
82fcde |
xpthread_join (thr[n]);
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
return 0;
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
#include <support/test-driver.c>
|