Blame nptl/tst-robust1.c

Packit 6c4009
/* Copyright (C) 2005-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Contributed by Ulrich Drepper <drepper@redhat.com>, 2005.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <pthread.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
Packit 6c4009
Packit 6c4009
static pthread_mutex_t m1;
Packit 6c4009
static pthread_mutex_t m2;
Packit 6c4009
static pthread_barrier_t b;
Packit 6c4009
Packit 6c4009
Packit 6c4009
#ifndef LOCK
Packit 6c4009
# define LOCK(m) pthread_mutex_lock (m)
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
Packit 6c4009
static void *
Packit 6c4009
tf (void *arg)
Packit 6c4009
{
Packit 6c4009
  long int round = (long int) arg;
Packit 6c4009
Packit 6c4009
  if (pthread_setcancelstate (PTHREAD_CANCEL_ENABLE, NULL) != 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("%ld: setcancelstate failed\n", round);
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  int e = LOCK (&m1;;
Packit 6c4009
  if (e != 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("%ld: child: mutex_lock m1 failed with error %d\n", round, e);
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  e = LOCK (&m2;;
Packit 6c4009
  if (e != 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("%ld: child: mutex_lock m2 failed with error %d\n", round, e);
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  e = pthread_barrier_wait (&b);
Packit 6c4009
  if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
Packit 6c4009
    {
Packit 6c4009
      printf ("%ld: child: 1st barrier_wait failed\n", round);
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  e = pthread_barrier_wait (&b);
Packit 6c4009
  if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
Packit 6c4009
    {
Packit 6c4009
      printf ("%ld: child: 2nd barrier_wait failed\n", round);
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  pthread_testcancel ();
Packit 6c4009
Packit 6c4009
  printf ("%ld: testcancel returned\n", round);
Packit 6c4009
  exit (1);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
#ifdef PREPARE_TMO
Packit 6c4009
  PREPARE_TMO;
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
  pthread_mutexattr_t a;
Packit 6c4009
  if (pthread_mutexattr_init (&a) != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("mutexattr_init failed");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
  if (pthread_mutexattr_setrobust_np (&a, PTHREAD_MUTEX_ROBUST_NP) != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("mutexattr_setrobust failed");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
#ifdef ENABLE_PI
Packit 6c4009
  if (pthread_mutexattr_setprotocol (&a, PTHREAD_PRIO_INHERIT) != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("pthread_mutexattr_setprotocol failed");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      int e = pthread_mutex_init (&m1, &a);
Packit 6c4009
      if (e == ENOTSUP)
Packit 6c4009
	{
Packit 6c4009
	  puts ("PI robust mutexes not supported");
Packit 6c4009
	  return 0;
Packit 6c4009
	}
Packit 6c4009
      else if (e != 0)
Packit 6c4009
	{
Packit 6c4009
	  puts ("mutex_init m1 failed");
Packit 6c4009
	  return 1;
Packit 6c4009
	}
Packit 6c4009
      pthread_mutex_destroy (&m1;;
Packit 6c4009
    }
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
#ifndef NOT_CONSISTENT
Packit 6c4009
  if (pthread_mutex_init (&m1, &a) != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("mutex_init m1 failed");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (pthread_mutex_init (&m2, &a) != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("mutex_init m2 failed");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
  if (pthread_barrier_init (&b, NULL, 2) != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("barrier_init failed");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  for (long int round = 1; round < 5; ++round)
Packit 6c4009
    {
Packit 6c4009
#ifdef NOT_CONSISTENT
Packit 6c4009
      if (pthread_mutex_init (&m1 , &a) != 0)
Packit 6c4009
	{
Packit 6c4009
	  puts ("mutex_init m1 failed");
Packit 6c4009
	  return 1;
Packit 6c4009
	}
Packit 6c4009
      if (pthread_mutex_init (&m2 , &a) != 0)
Packit 6c4009
	{
Packit 6c4009
	  puts ("mutex_init m2 failed");
Packit 6c4009
	  return 1;
Packit 6c4009
	}
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
      pthread_t th;
Packit 6c4009
      if (pthread_create (&th, NULL, tf, (void *) round) != 0)
Packit 6c4009
	{
Packit 6c4009
	  printf ("%ld: create failed\n", round);
Packit 6c4009
	  return 1;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      int e = pthread_barrier_wait (&b);
Packit 6c4009
      if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
Packit 6c4009
	{
Packit 6c4009
	  printf ("%ld: parent: 1st barrier_wait failed\n", round);
Packit 6c4009
	  return 1;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      if (pthread_cancel (th) != 0)
Packit 6c4009
	{
Packit 6c4009
	  printf ("%ld: cancel failed\n", round);
Packit 6c4009
	  return 1;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      e = pthread_barrier_wait (&b);
Packit 6c4009
      if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
Packit 6c4009
	{
Packit 6c4009
	  printf ("%ld: parent: 2nd barrier_wait failed\n", round);
Packit 6c4009
	  return 1;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
#ifndef AFTER_JOIN
Packit 6c4009
      if (round & 1)
Packit 6c4009
#endif
Packit 6c4009
	{
Packit 6c4009
	  void *res;
Packit 6c4009
	  if (pthread_join (th, &res) != 0)
Packit 6c4009
	    {
Packit 6c4009
	      printf ("%ld: join failed\n", round);
Packit 6c4009
	      return 1;
Packit 6c4009
	    }
Packit 6c4009
	  if (res != PTHREAD_CANCELED)
Packit 6c4009
	    {
Packit 6c4009
	      printf ("%ld: thread not canceled\n", round);
Packit 6c4009
	      return 1;
Packit 6c4009
	    }
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      e = LOCK (&m1;;
Packit 6c4009
      if (e == 0)
Packit 6c4009
	{
Packit 6c4009
	  printf ("%ld: parent: mutex_lock m1 succeeded\n", round);
Packit 6c4009
	  return 1;
Packit 6c4009
	}
Packit 6c4009
      if (e != EOWNERDEAD)
Packit 6c4009
	{
Packit 6c4009
	  printf ("%ld: parent: mutex_lock m1 returned wrong code\n", round);
Packit 6c4009
	  return 1;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      e = LOCK (&m2;;
Packit 6c4009
      if (e == 0)
Packit 6c4009
	{
Packit 6c4009
	  printf ("%ld: parent: mutex_lock m2 succeeded\n", round);
Packit 6c4009
	  return 1;
Packit 6c4009
	}
Packit 6c4009
      if (e != EOWNERDEAD)
Packit 6c4009
	{
Packit 6c4009
	  printf ("%ld: parent: mutex_lock m2 returned wrong code\n", round);
Packit 6c4009
	  return 1;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
#ifndef AFTER_JOIN
Packit 6c4009
      if ((round & 1) == 0)
Packit 6c4009
	{
Packit 6c4009
	  void *res;
Packit 6c4009
	  if (pthread_join (th, &res) != 0)
Packit 6c4009
	    {
Packit 6c4009
	      printf ("%ld: join failed\n", round);
Packit 6c4009
	      return 1;
Packit 6c4009
	    }
Packit 6c4009
	  if (res != PTHREAD_CANCELED)
Packit 6c4009
	    {
Packit 6c4009
	      printf ("%ld: thread not canceled\n", round);
Packit 6c4009
	      return 1;
Packit 6c4009
	    }
Packit 6c4009
	}
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
#ifndef NOT_CONSISTENT
Packit 6c4009
      e = pthread_mutex_consistent_np (&m1;;
Packit 6c4009
      if (e != 0)
Packit 6c4009
	{
Packit 6c4009
	  printf ("%ld: mutex_consistent m1 failed with error %d\n", round, e);
Packit 6c4009
	  return 1;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      e = pthread_mutex_consistent_np (&m2;;
Packit 6c4009
      if (e != 0)
Packit 6c4009
	{
Packit 6c4009
	  printf ("%ld: mutex_consistent m2 failed with error %d\n", round, e);
Packit 6c4009
	  return 1;
Packit 6c4009
	}
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
      e = pthread_mutex_unlock (&m1;;
Packit 6c4009
      if (e != 0)
Packit 6c4009
	{
Packit 6c4009
	  printf ("%ld: mutex_unlock m1 failed with %d\n", round, e);
Packit 6c4009
	  return 1;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      e = pthread_mutex_unlock (&m2;;
Packit 6c4009
      if (e != 0)
Packit 6c4009
	{
Packit 6c4009
	  printf ("%ld: mutex_unlock m2 failed with %d\n", round, e);
Packit 6c4009
	  return 1;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
#ifdef NOT_CONSISTENT
Packit 6c4009
      e = LOCK (&m1;;
Packit 6c4009
      if (e == 0)
Packit 6c4009
	{
Packit 6c4009
	  printf ("%ld: locking inconsistent mutex m1 succeeded\n", round);
Packit 6c4009
	  return 1;
Packit 6c4009
	}
Packit 6c4009
      if (e != ENOTRECOVERABLE)
Packit 6c4009
	{
Packit 6c4009
	  printf ("%ld: locking inconsistent mutex m1 failed with error %d\n",
Packit 6c4009
		  round, e);
Packit 6c4009
	  return 1;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      if (pthread_mutex_destroy (&m1) != 0)
Packit 6c4009
	{
Packit 6c4009
	  puts ("mutex_destroy m1 failed");
Packit 6c4009
	  return 1;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      e = LOCK (&m2;;
Packit 6c4009
      if (e == 0)
Packit 6c4009
	{
Packit 6c4009
	  printf ("%ld: locking inconsistent mutex m2 succeeded\n", round);
Packit 6c4009
	  return 1;
Packit 6c4009
	}
Packit 6c4009
      if (e != ENOTRECOVERABLE)
Packit 6c4009
	{
Packit 6c4009
	  printf ("%ld: locking inconsistent mutex m2 failed with error %d\n",
Packit 6c4009
		  round, e);
Packit 6c4009
	  return 1;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      if (pthread_mutex_destroy (&m2) != 0)
Packit 6c4009
	{
Packit 6c4009
	  puts ("mutex_destroy m2 failed");
Packit 6c4009
	  return 1;
Packit 6c4009
	}
Packit 6c4009
#endif
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
#ifndef NOT_CONSISTENT
Packit 6c4009
  if (pthread_mutex_destroy (&m1) != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("mutex_destroy m1 failed");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (pthread_mutex_destroy (&m2) != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("mutex_destroy m2 failed");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
  if (pthread_mutexattr_destroy (&a) != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("mutexattr_destroy failed");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#define TEST_FUNCTION do_test ()
Packit 6c4009
#include "../test-skeleton.c"