powerpc64le: bump binutils version requirement to >= 2.26
[glibc.git] / nptl / tst-tls1.c
blob727610bc0ad3ad73aa6862611827289a8fa7fd28
1 /* Copyright (C) 2003-2020 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2003.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
19 #include <pthread.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <stdint.h>
23 #include <inttypes.h>
24 #include <support/support.h>
25 #include <support/check.h>
26 #include <support/xthread.h>
28 struct test_s
30 __attribute__ ((aligned(0x20))) int a;
31 __attribute__ ((aligned(0x200))) int b;
34 #define INIT_A 1
35 #define INIT_B 42
36 /* Deliberately not static. */
37 __thread struct test_s s __attribute__ ((tls_model ("initial-exec"))) =
39 .a = INIT_A,
40 .b = INIT_B
43 /* Use noinline in combination with not static to ensure that the
44 alignment check is really done. Otherwise it was optimized out! */
45 __attribute__ ((noinline)) void
46 check_alignment (const char *thr_name, const char *ptr_name,
47 int *ptr, int alignment)
49 uintptr_t offset_aligment = ((uintptr_t) ptr) & (alignment - 1);
50 if (offset_aligment)
52 FAIL_EXIT1 ("%s (%p) is not 0x%x-byte aligned in %s thread\n",
53 ptr_name, ptr, alignment, thr_name);
57 static void
58 check_s (const char *thr_name)
60 if (s.a != INIT_A || s.b != INIT_B)
61 FAIL_EXIT1 ("initial value of s in %s thread wrong\n", thr_name);
63 check_alignment (thr_name, "s.a", &s.a, 0x20);
64 check_alignment (thr_name, "s.b", &s.b, 0x200);
67 static void *
68 tf (void *arg)
70 check_s ("child");
72 ++s.a;
74 return NULL;
78 int
79 do_test (void)
81 check_s ("main");
83 pthread_attr_t a;
85 xpthread_attr_init (&a);
87 #define STACK_SIZE (1 * 1024 * 1024)
88 xpthread_attr_setstacksize (&a, STACK_SIZE);
90 #define N 10
91 int i;
92 for (i = 0; i < N; ++i)
94 #define M 10
95 pthread_t th[M];
96 int j;
97 for (j = 0; j < M; ++j, ++s.a)
98 th[j] = xpthread_create (&a, tf, NULL);
100 for (j = 0; j < M; ++j)
101 xpthread_join (th[j]);
104 /* Also check the alignment of the tls variables if a misaligned stack is
105 specified. */
106 pthread_t th;
107 void *thr_stack = NULL;
108 thr_stack = xposix_memalign (0x200, STACK_SIZE + 1);
109 xpthread_attr_setstack (&a, thr_stack + 1, STACK_SIZE);
110 th = xpthread_create (&a, tf, NULL);
111 xpthread_join (th);
112 free (thr_stack);
114 xpthread_attr_destroy (&a);
116 return 0;
119 #include <support/test-driver.c>