Blame nptl/tst-rwlock20.c

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