Blame nptl/tst-rwlock-pwn.c

Packit Bot dc47af
/* Test rwlock with PREFER_WRITER_NONRECURSIVE_NP (bug 23861).
Packit Bot dc47af
   Copyright (C) 2018 Free Software Foundation, Inc.
Packit Bot dc47af
   This file is part of the GNU C Library.
Packit Bot dc47af
Packit Bot dc47af
   The GNU C Library is free software; you can redistribute it and/or
Packit Bot dc47af
   modify it under the terms of the GNU Lesser General Public
Packit Bot dc47af
   License as published by the Free Software Foundation; either
Packit Bot dc47af
   version 2.1 of the License, or (at your option) any later version.
Packit Bot dc47af
Packit Bot dc47af
   The GNU C Library is distributed in the hope that it will be useful,
Packit Bot dc47af
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Bot dc47af
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Bot dc47af
   Lesser General Public License for more details.
Packit Bot dc47af
Packit Bot dc47af
   You should have received a copy of the GNU Lesser General Public
Packit Bot dc47af
   License along with the GNU C Library; if not, see
Packit Bot dc47af
   <http://www.gnu.org/licenses/>.  */
Packit Bot dc47af
Packit Bot dc47af
#include <stdio.h>
Packit Bot dc47af
#include <stdlib.h>
Packit Bot dc47af
#include <unistd.h>
Packit Bot dc47af
#include <pthread.h>
Packit Bot dc47af
#include <support/xthread.h>
Packit Bot dc47af
Packit Bot dc47af
/* We choose 10 iterations because this happens to be able to trigger the
Packit Bot dc47af
   stall on contemporary hardware.  */
Packit Bot dc47af
#define LOOPS 10
Packit Bot dc47af
/* We need 3 threads to trigger bug 23861.  One thread as a writer, and
Packit Bot dc47af
   two reader threads.  The test verifies that the second-to-last reader
Packit Bot dc47af
   is able to notify the *last* reader that it should be done waiting.
Packit Bot dc47af
   If the second-to-last reader fails to notify the last reader or does
Packit Bot dc47af
   so incorrectly then the last reader may stall indefinitely.  */
Packit Bot dc47af
#define NTHREADS 3
Packit Bot dc47af
Packit Bot dc47af
_Atomic int do_exit;
Packit Bot dc47af
pthread_rwlockattr_t mylock_attr;
Packit Bot dc47af
pthread_rwlock_t mylock;
Packit Bot dc47af
Packit Bot dc47af
void *
Packit Bot dc47af
run_loop (void *a)
Packit Bot dc47af
{
Packit Bot dc47af
  while (!do_exit)
Packit Bot dc47af
    {
Packit Bot dc47af
      if (random () & 1)
Packit Bot dc47af
	{
Packit Bot dc47af
	  xpthread_rwlock_wrlock (&mylock);
Packit Bot dc47af
	  xpthread_rwlock_unlock (&mylock);
Packit Bot dc47af
	}
Packit Bot dc47af
      else
Packit Bot dc47af
	{
Packit Bot dc47af
	  xpthread_rwlock_rdlock (&mylock);
Packit Bot dc47af
	  xpthread_rwlock_unlock (&mylock);
Packit Bot dc47af
	}
Packit Bot dc47af
    }
Packit Bot dc47af
  return NULL;
Packit Bot dc47af
}
Packit Bot dc47af
Packit Bot dc47af
int
Packit Bot dc47af
do_test (void)
Packit Bot dc47af
{
Packit Bot dc47af
  xpthread_rwlockattr_init (&mylock_attr);
Packit Bot dc47af
  xpthread_rwlockattr_setkind_np (&mylock_attr,
Packit Bot dc47af
				  PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP);
Packit Bot dc47af
  xpthread_rwlock_init (&mylock, &mylock_attr);
Packit Bot dc47af
Packit Bot dc47af
  for (int n = 0; n < LOOPS; n++)
Packit Bot dc47af
    {
Packit Bot dc47af
      pthread_t tids[NTHREADS];
Packit Bot dc47af
      do_exit = 0;
Packit Bot dc47af
      for (int i = 0; i < NTHREADS; i++)
Packit Bot dc47af
	tids[i] = xpthread_create (NULL, run_loop, NULL);
Packit Bot dc47af
      /* Let the threads run for some time.  */
Packit Bot dc47af
      sleep (1);
Packit Bot dc47af
      printf ("Exiting...");
Packit Bot dc47af
      fflush (stdout);
Packit Bot dc47af
      do_exit = 1;
Packit Bot dc47af
      for (int i = 0; i < NTHREADS; i++)
Packit Bot dc47af
	xpthread_join (tids[i]);
Packit Bot dc47af
      printf ("done.\n");
Packit Bot dc47af
    }
Packit Bot dc47af
  pthread_rwlock_destroy (&mylock);
Packit Bot dc47af
  pthread_rwlockattr_destroy (&mylock_attr);
Packit Bot dc47af
  return 0;
Packit Bot dc47af
}
Packit Bot dc47af
Packit Bot dc47af
#define TIMEOUT (DEFAULT_TIMEOUT + 3 * LOOPS)
Packit Bot dc47af
#include <support/test-driver.c>