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 a21791
#include <stdint.h>
Packit Service a21791
#include <inttypes.h>
Packit Service a21791
#include <support/support.h>
Packit Service a21791
#include <support/check.h>
Packit Service a21791
#include <support/xthread.h>
Packit 6c4009
Packit 6c4009
struct test_s
Packit 6c4009
{
Packit Service a21791
  __attribute__ ((aligned(0x20))) int a;
Packit Service a21791
  __attribute__ ((aligned(0x200))) 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 Service a21791
/* Use noinline in combination with not static to ensure that the
Packit Service a21791
   alignment check is really done.  Otherwise it was optimized out!  */
Packit Service a21791
__attribute__ ((noinline)) void
Packit Service a21791
check_alignment (const char *thr_name, const char *ptr_name,
Packit Service a21791
		 int *ptr, int alignment)
Packit Service a21791
{
Packit Service a21791
  uintptr_t offset_aligment = ((uintptr_t) ptr) & (alignment - 1);
Packit Service a21791
  if (offset_aligment)
Packit Service a21791
    {
Packit Service a21791
      FAIL_EXIT1 ("%s (%p) is not 0x%x-byte aligned in %s thread\n",
Packit Service a21791
		  ptr_name, ptr, alignment, thr_name);
Packit Service a21791
    }
Packit Service a21791
}
Packit Service a21791
Packit Service a21791
static void
Packit Service a21791
check_s (const char *thr_name)
Packit Service a21791
{
Packit Service a21791
  if (s.a != INIT_A || s.b != INIT_B)
Packit Service a21791
    FAIL_EXIT1 ("initial value of s in %s thread wrong\n", thr_name);
Packit Service a21791
Packit Service a21791
  check_alignment (thr_name, "s.a", &s.a, 0x20);
Packit Service a21791
  check_alignment (thr_name, "s.b", &s.b, 0x200);
Packit Service a21791
}
Packit 6c4009
Packit 6c4009
static void *
Packit 6c4009
tf (void *arg)
Packit 6c4009
{
Packit Service a21791
  check_s ("child");
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 a21791
  check_s ("main");
Packit 6c4009
Packit 6c4009
  pthread_attr_t a;
Packit 6c4009
Packit Service a21791
  xpthread_attr_init (&a);
Packit 6c4009
Packit Service a21791
#define STACK_SIZE (1 * 1024 * 1024)
Packit Service a21791
  xpthread_attr_setstacksize (&a, STACK_SIZE);
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 a21791
	th[j] = xpthread_create (&a, tf, NULL);
Packit 6c4009
Packit 6c4009
      for (j = 0; j < M; ++j)
Packit Service a21791
	xpthread_join (th[j]);
Packit 6c4009
    }
Packit 6c4009
Packit Service a21791
  /* Also check the alignment of the tls variables if a misaligned stack is
Packit Service a21791
     specified.  */
Packit Service a21791
  pthread_t th;
Packit Service a21791
  void *thr_stack = NULL;
Packit Service a21791
  thr_stack = xposix_memalign (0x200, STACK_SIZE + 1);
Packit Service a21791
  xpthread_attr_setstack (&a, thr_stack + 1, STACK_SIZE);
Packit Service a21791
  th = xpthread_create (&a, tf, NULL);
Packit Service a21791
  xpthread_join (th);
Packit Service a21791
  free (thr_stack);
Packit Service a21791
Packit Service a21791
  xpthread_attr_destroy (&a);
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit Service a21791
#include <support/test-driver.c>