Blame nptl/tst-rwlock-trywrlock-stall.c

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