Blame nptl/tst-tls1.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 Ulrich Drepper <drepper@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 <pthread.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit Service 1f32a9
Packit 6c4009
Packit 6c4009
struct test_s
Packit 6c4009
{
Packit Service 1f32a9
  int a;
Packit Service 1f32a9
  int b;
Packit 6c4009
};
Packit 6c4009
Packit 6c4009
#define INIT_A 1
Packit 6c4009
#define INIT_B 42
Packit 6c4009
/* Deliberately not static.  */
Packit 6c4009
__thread struct test_s s __attribute__ ((tls_model ("initial-exec"))) =
Packit 6c4009
{
Packit 6c4009
  .a = INIT_A,
Packit 6c4009
  .b = INIT_B
Packit 6c4009
};
Packit 6c4009
Packit 6c4009
Packit 6c4009
static void *
Packit 6c4009
tf (void *arg)
Packit 6c4009
{
Packit Service 1f32a9
  if (s.a != INIT_A || s.b != INIT_B)
Packit Service 1f32a9
    {
Packit Service 1f32a9
      puts ("initial value of s in child thread wrong");
Packit Service 1f32a9
      exit (1);
Packit Service 1f32a9
    }
Packit 6c4009
Packit 6c4009
  ++s.a;
Packit 6c4009
Packit 6c4009
  return NULL;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit Service 1f32a9
  if (s.a != INIT_A || s.b != INIT_B)
Packit Service 1f32a9
    {
Packit Service 1f32a9
      puts ("initial value of s in main thread wrong");
Packit Service 1f32a9
      exit (1);
Packit Service 1f32a9
    }
Packit 6c4009
Packit 6c4009
  pthread_attr_t a;
Packit 6c4009
Packit Service 1f32a9
  if (pthread_attr_init (&a) != 0)
Packit Service 1f32a9
    {
Packit Service 1f32a9
      puts ("attr_init failed");
Packit Service 1f32a9
      exit (1);
Packit Service 1f32a9
    }
Packit 6c4009
Packit Service 1f32a9
  if (pthread_attr_setstacksize (&a, 1 * 1024 * 1024) != 0)
Packit Service 1f32a9
    {
Packit Service 1f32a9
      puts ("attr_setstacksize failed");
Packit Service 1f32a9
      return 1;
Packit Service 1f32a9
    }
Packit 6c4009
Packit 6c4009
#define N 10
Packit 6c4009
  int i;
Packit 6c4009
  for (i = 0; i < N; ++i)
Packit 6c4009
    {
Packit 6c4009
#define M 10
Packit 6c4009
      pthread_t th[M];
Packit 6c4009
      int j;
Packit 6c4009
      for (j = 0; j < M; ++j, ++s.a)
Packit Service 1f32a9
	if (pthread_create (&th[j], &a, tf, NULL) != 0)
Packit Service 1f32a9
	  {
Packit Service 1f32a9
	    puts ("pthread_create failed");
Packit Service 1f32a9
	    exit (1);
Packit Service 1f32a9
	  }
Packit 6c4009
Packit 6c4009
      for (j = 0; j < M; ++j)
Packit Service 1f32a9
	if (pthread_join (th[j], NULL) != 0)
Packit Service 1f32a9
	  {
Packit Service 1f32a9
	    puts ("pthread_join failed");
Packit Service 1f32a9
	    exit (1);
Packit Service 1f32a9
	  }
Packit 6c4009
    }
Packit 6c4009
Packit Service 1f32a9
  if (pthread_attr_destroy (&a) != 0)
Packit Service 1f32a9
    {
Packit Service 1f32a9
      puts ("attr_destroy failed");
Packit Service 1f32a9
      exit (1);
Packit Service 1f32a9
    }
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit Service 1f32a9
Packit Service 1f32a9
#define TEST_FUNCTION do_test ()
Packit Service 1f32a9
#include "../test-skeleton.c"