Blame nptl/tst-robust7.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 <stdbool.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
Packit 6c4009
Packit 6c4009
static pthread_barrier_t b;
Packit 6c4009
static pthread_cond_t c = PTHREAD_COND_INITIALIZER;
Packit 6c4009
static pthread_mutex_t m;
Packit 6c4009
static bool first = true;
Packit 6c4009
Packit 6c4009
Packit 6c4009
static void *
Packit 6c4009
tf (void *arg)
Packit 6c4009
{
Packit 6c4009
  long int n = (long int) arg;
Packit 6c4009
Packit 6c4009
  if (pthread_mutex_lock (&m) != 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("thread %ld: mutex_lock failed\n", n + 1);
Packit 6c4009
      exit (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 ("thread %ld: barrier_wait failed\n", n + 1);
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  e = pthread_cond_wait (&c, &m);
Packit 6c4009
  if (first)
Packit 6c4009
    {
Packit 6c4009
      if (e != 0)
Packit 6c4009
	{
Packit 6c4009
	  printf ("thread %ld: cond_wait failed\n", n + 1);
Packit 6c4009
	  exit (1);
Packit 6c4009
	}
Packit 6c4009
      first = false;
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      if (e != EOWNERDEAD)
Packit 6c4009
	{
Packit 6c4009
	  printf ("thread %ld: cond_wait did not return EOWNERDEAD\n", n + 1);
Packit 6c4009
	  exit (1);
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (pthread_cancel (pthread_self ()) != 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("thread %ld: cancel failed\n", n + 1);
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  pthread_testcancel ();
Packit 6c4009
Packit 6c4009
  printf ("thread %ld: testcancel returned\n", n + 1);
Packit 6c4009
  exit (1);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_test (void)
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
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
#endif
Packit 6c4009
Packit 6c4009
  int e;
Packit 6c4009
  e = pthread_mutex_init (&m, &a);
Packit 6c4009
  if (e != 0)
Packit 6c4009
    {
Packit 6c4009
#ifdef ENABLE_PI
Packit 6c4009
      if (e == ENOTSUP)
Packit 6c4009
	{
Packit 6c4009
	  puts ("PI robust mutexes not supported");
Packit 6c4009
	  return 0;
Packit 6c4009
	}
Packit 6c4009
#endif
Packit 6c4009
      puts ("mutex_init failed");
Packit 6c4009
      return 1;
Packit 6c4009
    }
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
  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
#define N 5
Packit 6c4009
  pthread_t th[N];
Packit 6c4009
  for (long int n = 0; n < N; ++n)
Packit 6c4009
    {
Packit 6c4009
      if (pthread_create (&th[n], NULL, tf, (void *) n) != 0)
Packit 6c4009
	{
Packit 6c4009
	  printf ("pthread_create loop %ld failed\n", n + 1);
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 ("parent: barrier_wait failed in round %ld\n", n + 1);
Packit 6c4009
	  return 1;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (pthread_mutex_lock (&m) != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("parent: mutex_lock failed");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (pthread_mutex_unlock (&m) != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("parent: mutex_unlock failed");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (pthread_cond_broadcast (&c) != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("cond_broadcast failed");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  for (int n = 0; n < N; ++n)
Packit 6c4009
    {
Packit 6c4009
      void *res;
Packit 6c4009
      if (pthread_join (th[n], &res) != 0)
Packit 6c4009
	{
Packit 6c4009
	  printf ("join round %d failed\n", n + 1);
Packit 6c4009
	  return 1;
Packit 6c4009
	}
Packit 6c4009
      if (res != PTHREAD_CANCELED)
Packit 6c4009
	{
Packit 6c4009
	  printf ("thread %d not canceled\n", n + 1);
Packit 6c4009
	  return 1;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  e = pthread_mutex_lock (&m);
Packit 6c4009
  if (e == 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("parent: 2nd mutex_lock succeeded");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
  if (e != EOWNERDEAD)
Packit 6c4009
    {
Packit 6c4009
      puts ("parent: mutex_lock did not return EOWNERDEAD");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (pthread_mutex_unlock (&m) != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("parent: 2nd mutex_unlock failed");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (pthread_mutex_destroy (&m) != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("mutex_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"