Blame nptl/tst-mutex10.c

Packit Service 2b441b
/* Testing race while enabling lock elision.
Packit Service 2b441b
   Copyright (C) 2018 Free Software Foundation, Inc.
Packit Service 2b441b
   This file is part of the GNU C Library.
Packit Service 2b441b
Packit Service 2b441b
   The GNU C Library is free software; you can redistribute it and/or
Packit Service 2b441b
   modify it under the terms of the GNU Lesser General Public
Packit Service 2b441b
   License as published by the Free Software Foundation; either
Packit Service 2b441b
   version 2.1 of the License, or (at your option) any later version.
Packit Service 2b441b
Packit Service 2b441b
   The GNU C Library is distributed in the hope that it will be useful,
Packit Service 2b441b
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 2b441b
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 2b441b
   Lesser General Public License for more details.
Packit Service 2b441b
Packit Service 2b441b
   You should have received a copy of the GNU Lesser General Public
Packit Service 2b441b
   License along with the GNU C Library; if not, see
Packit Service 2b441b
   <http://www.gnu.org/licenses/>.  */
Packit Service 2b441b
#include <stdio.h>
Packit Service 2b441b
#include <stdlib.h>
Packit Service 2b441b
#include <stdint.h>
Packit Service 2b441b
#include <pthread.h>
Packit Service 2b441b
#include <unistd.h>
Packit Service 2b441b
#include <getopt.h>
Packit Service 2b441b
#include <support/support.h>
Packit Service 2b441b
#include <support/xthread.h>
Packit Service 2b441b
Packit Service 2b441b
static pthread_barrier_t barrier;
Packit Service 2b441b
static pthread_mutex_t mutex;
Packit Service 2b441b
static long long int iteration_count = 1000000;
Packit Service 2b441b
static unsigned int thread_count = 3;
Packit Service 2b441b
Packit Service 2b441b
static void *
Packit Service 2b441b
thr_func (void *arg)
Packit Service 2b441b
{
Packit Service 2b441b
  long long int i;
Packit Service 2b441b
  for (i = 0; i < iteration_count; i++)
Packit Service 2b441b
    {
Packit Service 2b441b
      if ((uintptr_t) arg == 0)
Packit Service 2b441b
	{
Packit Service 2b441b
	  xpthread_mutex_destroy (&mutex);
Packit Service 2b441b
	  xpthread_mutex_init (&mutex, NULL);
Packit Service 2b441b
	}
Packit Service 2b441b
Packit Service 2b441b
      xpthread_barrier_wait (&barrier);
Packit Service 2b441b
Packit Service 2b441b
      /* Test if enabling lock elision works if it is enabled concurrently.
Packit Service 2b441b
	 There was a race in FORCE_ELISION macro which leads to either
Packit Service 2b441b
	 pthread_mutex_destroy returning EBUSY as the owner was recorded
Packit Service 2b441b
	 by pthread_mutex_lock - in "normal mutex" code path - but was not
Packit Service 2b441b
	 resetted in pthread_mutex_unlock - in "elision" code path.
Packit Service 2b441b
	 Or it leads to the assertion in nptl/pthread_mutex_lock.c:
Packit Service 2b441b
	 assert (mutex->__data.__owner == 0);
Packit Service 2b441b
	 Please ensure that the test is run with lock elision:
Packit Service 2b441b
	 export GLIBC_TUNABLES=glibc.elision.enable=1  */
Packit Service 2b441b
      xpthread_mutex_lock (&mutex);
Packit Service 2b441b
      xpthread_mutex_unlock (&mutex);
Packit Service 2b441b
Packit Service 2b441b
      xpthread_barrier_wait (&barrier);
Packit Service 2b441b
    }
Packit Service 2b441b
  return NULL;
Packit Service 2b441b
}
Packit Service 2b441b
Packit Service 2b441b
static int
Packit Service 2b441b
do_test (void)
Packit Service 2b441b
{
Packit Service 2b441b
  unsigned int i;
Packit Service 2b441b
  printf ("Starting %d threads to run %lld iterations.\n",
Packit Service 2b441b
	  thread_count, iteration_count);
Packit Service 2b441b
Packit Service 2b441b
  pthread_t *threads = xmalloc (thread_count * sizeof (pthread_t));
Packit Service 2b441b
  xpthread_barrier_init (&barrier, NULL, thread_count);
Packit Service 2b441b
  xpthread_mutex_init (&mutex, NULL);
Packit Service 2b441b
Packit Service 2b441b
  for (i = 0; i < thread_count; i++)
Packit Service 2b441b
    threads[i] = xpthread_create (NULL, thr_func, (void *) (uintptr_t) i);
Packit Service 2b441b
Packit Service 2b441b
  for (i = 0; i < thread_count; i++)
Packit Service 2b441b
    xpthread_join (threads[i]);
Packit Service 2b441b
Packit Service 2b441b
  xpthread_barrier_destroy (&barrier);
Packit Service 2b441b
  free (threads);
Packit Service 2b441b
Packit Service 2b441b
  return EXIT_SUCCESS;
Packit Service 2b441b
}
Packit Service 2b441b
Packit Service 2b441b
#define OPT_ITERATIONS	10000
Packit Service 2b441b
#define OPT_THREADS	10001
Packit Service 2b441b
#define CMDLINE_OPTIONS						\
Packit Service 2b441b
  { "iterations", required_argument, NULL, OPT_ITERATIONS },	\
Packit Service 2b441b
  { "threads", required_argument, NULL, OPT_THREADS },
Packit Service 2b441b
static void
Packit Service 2b441b
cmdline_process (int c)
Packit Service 2b441b
{
Packit Service 2b441b
  long long int arg = strtoll (optarg, NULL, 0);
Packit Service 2b441b
  switch (c)
Packit Service 2b441b
    {
Packit Service 2b441b
    case OPT_ITERATIONS:
Packit Service 2b441b
      if (arg > 0)
Packit Service 2b441b
	iteration_count = arg;
Packit Service 2b441b
      break;
Packit Service 2b441b
    case OPT_THREADS:
Packit Service 2b441b
      if (arg > 0 && arg < 100)
Packit Service 2b441b
	thread_count = arg;
Packit Service 2b441b
      break;
Packit Service 2b441b
    }
Packit Service 2b441b
}
Packit Service 2b441b
#define CMDLINE_PROCESS cmdline_process
Packit Service 2b441b
#define TIMEOUT 50
Packit Service 2b441b
#include <support/test-driver.c>