Blame nptl/tst-barrier3.c

Packit 6c4009
/* Test of POSIX barriers.
Packit 6c4009
   Copyright (C) 2002-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>, 2002.
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 <pthread.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
Packit 6c4009
#define NTHREADS 20
Packit 6c4009
Packit 6c4009
#define ROUNDS 20
Packit 6c4009
Packit 6c4009
static pthread_barrier_t barriers[NTHREADS];
Packit 6c4009
Packit 6c4009
static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
Packit 6c4009
static int counters[NTHREADS];
Packit 6c4009
static int serial[NTHREADS];
Packit 6c4009
Packit 6c4009
static void *
Packit 6c4009
worker (void *arg)
Packit 6c4009
{
Packit 6c4009
  void *result = NULL;
Packit 6c4009
  int nr = (long int) arg;
Packit 6c4009
  int i;
Packit 6c4009
Packit 6c4009
  for (i = 0; i < ROUNDS; ++i)
Packit 6c4009
    {
Packit 6c4009
      int j;
Packit 6c4009
      int retval;
Packit 6c4009
Packit 6c4009
      if (nr == 0)
Packit 6c4009
	{
Packit 6c4009
	  memset (counters, '\0', sizeof (counters));
Packit 6c4009
	  memset (serial, '\0', sizeof (serial));
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      retval = pthread_barrier_wait (&barriers[NTHREADS - 1]);
Packit 6c4009
      if (retval != 0 && retval != PTHREAD_BARRIER_SERIAL_THREAD)
Packit 6c4009
	{
Packit 6c4009
	  printf ("thread %d failed to wait for all the others\n", nr);
Packit 6c4009
	  result = (void *) 1;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      for (j = nr; j < NTHREADS; ++j)
Packit 6c4009
	{
Packit 6c4009
	  /* Increment the counter for this round.  */
Packit 6c4009
	  pthread_mutex_lock (&lock);
Packit 6c4009
	  ++counters[j];
Packit 6c4009
	  pthread_mutex_unlock (&lock);
Packit 6c4009
Packit 6c4009
	  /* Wait for the rest.  */
Packit 6c4009
	  retval = pthread_barrier_wait (&barriers[j]);
Packit 6c4009
Packit 6c4009
	  /* Test the result.  */
Packit 6c4009
	  if (nr == 0 && counters[j] != j + 1)
Packit 6c4009
	    {
Packit 6c4009
	      printf ("barrier in round %d released but count is %d\n",
Packit 6c4009
		      j, counters[j]);
Packit 6c4009
	      result = (void *) 1;
Packit 6c4009
	    }
Packit 6c4009
Packit 6c4009
	  if (retval != 0)
Packit 6c4009
	    {
Packit 6c4009
	      if (retval != PTHREAD_BARRIER_SERIAL_THREAD)
Packit 6c4009
		{
Packit 6c4009
		  printf ("thread %d in round %d has nonzero return value != PTHREAD_BARRIER_SERIAL_THREAD\n",
Packit 6c4009
			  nr, j);
Packit 6c4009
		  result = (void *) 1;
Packit 6c4009
		}
Packit 6c4009
	      else
Packit 6c4009
		{
Packit 6c4009
		  pthread_mutex_lock (&lock);
Packit 6c4009
		  ++serial[j];
Packit 6c4009
		  pthread_mutex_unlock (&lock);
Packit 6c4009
		}
Packit 6c4009
	    }
Packit 6c4009
Packit 6c4009
	  /* Wait for the rest again.  */
Packit 6c4009
	  retval = pthread_barrier_wait (&barriers[j]);
Packit 6c4009
Packit 6c4009
	  /* Now we can check whether exactly one thread was serializing.  */
Packit 6c4009
	  if (nr == 0 && serial[j] != 1)
Packit 6c4009
	    {
Packit 6c4009
	      printf ("not exactly one serial thread in round %d\n", j);
Packit 6c4009
	      result = (void *) 1;
Packit 6c4009
	    }
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return result;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
#define TEST_FUNCTION do_test ()
Packit 6c4009
#define TIMEOUT 60
Packit 6c4009
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  pthread_t threads[NTHREADS];
Packit 6c4009
  int i;
Packit 6c4009
  void *res;
Packit 6c4009
  int result = 0;
Packit 6c4009
Packit 6c4009
  /* Initialized the barrier variables.  */
Packit 6c4009
  for (i = 0; i < NTHREADS; ++i)
Packit 6c4009
    if (pthread_barrier_init (&barriers[i], NULL, i + 1) != 0)
Packit 6c4009
      {
Packit 6c4009
	printf ("Failed to initialize barrier %d\n", i);
Packit 6c4009
	exit (1);
Packit 6c4009
      }
Packit 6c4009
Packit 6c4009
  /* Start the threads.  */
Packit 6c4009
  for (i = 0; i < NTHREADS; ++i)
Packit 6c4009
    if (pthread_create (&threads[i], NULL, worker, (void *) (long int) i) != 0)
Packit 6c4009
      {
Packit 6c4009
	printf ("Failed to start thread %d\n", i);
Packit 6c4009
	exit (1);
Packit 6c4009
      }
Packit 6c4009
Packit 6c4009
  /* And wait for them.  */
Packit 6c4009
  for (i = 0; i < NTHREADS; ++i)
Packit 6c4009
    if (pthread_join (threads[i], &res) != 0 || res != NULL)
Packit 6c4009
      {
Packit 6c4009
	printf ("thread %d returned a failure\n", i);
Packit 6c4009
	result = 1;
Packit 6c4009
      }
Packit 6c4009
    else
Packit 6c4009
      printf ("joined threads %d\n", i);
Packit 6c4009
Packit 6c4009
  if (result == 0)
Packit 6c4009
    puts ("all OK");
Packit 6c4009
Packit 6c4009
  return result;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#include "../test-skeleton.c"