Blame nptl/tst-mutex10.c

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