Blame nptl/tst-cond7.c

Packit 6c4009
/* Copyright (C) 2003-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Contributed by Jakub Jelinek <jakub@redhat.com>, 2003.
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
#include <string.h>
Packit 6c4009
#include <time.h>
Packit 6c4009
#include <sys/time.h>
Packit 6c4009
Packit 6c4009
Packit 6c4009
typedef struct
Packit 6c4009
  {
Packit 6c4009
    pthread_cond_t cond;
Packit 6c4009
    pthread_mutex_t lock;
Packit 6c4009
    pthread_t h;
Packit 6c4009
  } T;
Packit 6c4009
Packit 6c4009
Packit 6c4009
static volatile bool done;
Packit 6c4009
Packit 6c4009
Packit 6c4009
static void *
Packit 6c4009
tf (void *arg)
Packit 6c4009
{
Packit 6c4009
  puts ("child created");
Packit 6c4009
Packit 6c4009
  if (pthread_setcancelstate (PTHREAD_CANCEL_ENABLE, NULL) != 0
Packit 6c4009
      || pthread_setcanceltype (PTHREAD_CANCEL_DEFERRED, NULL) != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("cannot set cancellation options");
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  T *t = (T *) arg;
Packit 6c4009
Packit 6c4009
  if (pthread_mutex_lock (&t->lock) != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("child: lock failed");
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  done = true;
Packit 6c4009
Packit 6c4009
  if (pthread_cond_signal (&t->cond) != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("child: cond_signal failed");
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (pthread_cond_wait (&t->cond, &t->lock) != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("child: cond_wait failed");
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (pthread_mutex_unlock (&t->lock) != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("child: unlock failed");
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return NULL;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  int i;
Packit 6c4009
#define N 100
Packit 6c4009
  T *t[N];
Packit 6c4009
  for (i = 0; i < N; ++i)
Packit 6c4009
    {
Packit 6c4009
      printf ("round %d\n", i);
Packit 6c4009
Packit 6c4009
      t[i] = (T *) malloc (sizeof (T));
Packit 6c4009
      if (t[i] == NULL)
Packit 6c4009
	{
Packit 6c4009
	  puts ("out of memory");
Packit 6c4009
	  exit (1);
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      if (pthread_mutex_init (&t[i]->lock, NULL) != 0
Packit 6c4009
	  || pthread_cond_init (&t[i]->cond, NULL) != 0)
Packit 6c4009
	{
Packit 6c4009
	  puts ("an _init function failed");
Packit 6c4009
	  exit (1);
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      if (pthread_mutex_lock (&t[i]->lock) != 0)
Packit 6c4009
	{
Packit 6c4009
	  puts ("initial mutex_lock failed");
Packit 6c4009
	  exit (1);
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      done = false;
Packit 6c4009
Packit 6c4009
      if (pthread_create (&t[i]->h, NULL, tf, t[i]) != 0)
Packit 6c4009
	{
Packit 6c4009
	  puts ("pthread_create failed");
Packit 6c4009
	  exit (1);
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      do
Packit 6c4009
	if (pthread_cond_wait (&t[i]->cond, &t[i]->lock) != 0)
Packit 6c4009
	  {
Packit 6c4009
	    puts ("cond_wait failed");
Packit 6c4009
	    exit (1);
Packit 6c4009
	  }
Packit 6c4009
      while (! done);
Packit 6c4009
Packit 6c4009
      /* Release the lock since the cancel handler will get it.  */
Packit 6c4009
      if (pthread_mutex_unlock (&t[i]->lock) != 0)
Packit 6c4009
	{
Packit 6c4009
	  puts ("mutex_unlock failed");
Packit 6c4009
	  exit (1);
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      if (pthread_cancel (t[i]->h) != 0)
Packit 6c4009
	{
Packit 6c4009
	  puts ("cancel failed");
Packit 6c4009
	  exit (1);
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      puts ("parent: joining now");
Packit 6c4009
Packit 6c4009
      void *result;
Packit 6c4009
      if (pthread_join (t[i]->h, &result) != 0)
Packit 6c4009
	{
Packit 6c4009
	  puts ("join failed");
Packit 6c4009
	  exit (1);
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      if (result != PTHREAD_CANCELED)
Packit 6c4009
	{
Packit 6c4009
	  puts ("result != PTHREAD_CANCELED");
Packit 6c4009
	  exit (1);
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  for (i = 0; i < N; ++i)
Packit 6c4009
    free (t[i]);
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
#define TEST_FUNCTION do_test ()
Packit 6c4009
#include "../test-skeleton.c"