Blame nptl/tst-tls4.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 <dlfcn.h>
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <pthread.h>
Packit 6c4009
#include <stdint.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
Packit 6c4009
#define N 3
Packit 6c4009
Packit 6c4009
void (*test1) (void), (*test2) (void);
Packit 6c4009
Packit 6c4009
pthread_barrier_t b2, b3;
Packit 6c4009
Packit 6c4009
static void *
Packit 6c4009
tf (void *arg)
Packit 6c4009
{
Packit 6c4009
  int i;
Packit 6c4009
Packit 6c4009
  for (i = 0; i <= (uintptr_t) arg; ++i)
Packit 6c4009
    {
Packit 6c4009
      int r = pthread_barrier_wait (&b3;;
Packit 6c4009
      if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
Packit 6c4009
	{
Packit 6c4009
	  puts ("tf: barrier_wait failed");
Packit 6c4009
	  exit (1);
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  test1 ();
Packit 6c4009
Packit 6c4009
  for (i = 0; i < 3; ++i)
Packit 6c4009
    {
Packit 6c4009
      int r = pthread_barrier_wait (&b3;;
Packit 6c4009
      if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
Packit 6c4009
	{
Packit 6c4009
	  puts ("tf: barrier_wait failed");
Packit 6c4009
	  exit (1);
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  test2 ();
Packit 6c4009
Packit 6c4009
  for (i = 0; i < 3 - (uintptr_t) arg; ++i)
Packit 6c4009
    {
Packit 6c4009
      int r = pthread_barrier_wait (&b3;;
Packit 6c4009
      if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
Packit 6c4009
	{
Packit 6c4009
	  puts ("tf: barrier_wait failed");
Packit 6c4009
	  exit (1);
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return NULL;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static void *
Packit 6c4009
tf2 (void *arg)
Packit 6c4009
{
Packit 6c4009
  int r = pthread_barrier_wait (&b2;;
Packit 6c4009
  if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
Packit 6c4009
    {
Packit 6c4009
      puts ("tf2: barrier_wait failed");
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  int i;
Packit 6c4009
  for (i = 0; i < N; ++i)
Packit 6c4009
    tf (arg);
Packit 6c4009
  return NULL;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  pthread_t th[2];
Packit 6c4009
  const char *modules[N]
Packit 6c4009
    = { "tst-tls4moda.so", "tst-tls4moda.so", "tst-tls4modb.so" };
Packit 6c4009
Packit 6c4009
  if (pthread_barrier_init (&b2, NULL, 2) != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("barrier_init failed");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (pthread_barrier_init (&b3, NULL, 3) != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("barrier_init failed");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (pthread_create (&th[0], NULL, tf2, (void *) (uintptr_t) 1))
Packit 6c4009
    {
Packit 6c4009
      puts ("pthread_create failed");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  int r = pthread_barrier_wait (&b2;;
Packit 6c4009
  if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
Packit 6c4009
    {
Packit 6c4009
      puts ("barrier_wait failed");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  int i;
Packit 6c4009
  for (i = 0; i < N; ++i)
Packit 6c4009
    {
Packit 6c4009
      void *h = dlopen (modules[i], RTLD_LAZY);
Packit 6c4009
      if (h == NULL)
Packit 6c4009
	{
Packit 6c4009
	  printf ("dlopen failed %s\n", dlerror ());
Packit 6c4009
	  return 1;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      test1 = dlsym (h, "test1");
Packit 6c4009
      if (test1 == NULL)
Packit 6c4009
	{
Packit 6c4009
	  printf ("dlsym for test1 failed %s\n", dlerror ());
Packit 6c4009
	  return 1;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      test2 = dlsym (h, "test2");
Packit 6c4009
      if (test2 == NULL)
Packit 6c4009
	{
Packit 6c4009
	  printf ("dlsym for test2 failed %s\n", dlerror ());
Packit 6c4009
	  return 1;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      if (pthread_create (&th[1], NULL, tf, (void *) (uintptr_t) 2))
Packit 6c4009
	{
Packit 6c4009
	  puts ("pthread_create failed");
Packit 6c4009
	  return 1;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      tf ((void *) (uintptr_t) 0);
Packit 6c4009
Packit 6c4009
      if (pthread_join (th[1], NULL) != 0)
Packit 6c4009
	{
Packit 6c4009
	  puts ("join failed");
Packit 6c4009
	  return 1;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      if (dlclose (h))
Packit 6c4009
	{
Packit 6c4009
	  puts ("dlclose failed");
Packit 6c4009
	  return 1;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      printf ("test %d with %s succeeded\n", i, modules[i]);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (pthread_join (th[0], NULL) != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("join failed");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#define TIMEOUT 5
Packit 6c4009
#define TEST_FUNCTION do_test ()
Packit 6c4009
#include "../test-skeleton.c"