hjl / source-git / glibc

Forked from source-git/glibc 3 years ago
Clone

Blame nptl/tst-mutex10.c

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