Blame nptl/tst-mutex10.c

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