]> git.ipfire.org Git - thirdparty/glibc.git/blame - nptl/tst-tls1.c
Prefer https to http for gnu.org and fsf.org URLs
[thirdparty/glibc.git] / nptl / tst-tls1.c
CommitLineData
04277e02 1/* Copyright (C) 2003-2019 Free Software Foundation, Inc.
5e47b76b
UD
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2003.
4
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.
9
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.
14
15 You should have received a copy of the GNU Lesser General Public
59ba27a6 16 License along with the GNU C Library; if not, see
5a82c748 17 <https://www.gnu.org/licenses/>. */
5e47b76b
UD
18
19#include <pthread.h>
20#include <stdio.h>
21#include <stdlib.h>
bc79db3f
SL
22#include <stdint.h>
23#include <inttypes.h>
24#include <support/support.h>
25#include <support/check.h>
26#include <support/xthread.h>
5e47b76b
UD
27
28struct test_s
29{
bc79db3f
SL
30 __attribute__ ((aligned(0x20))) int a;
31 __attribute__ ((aligned(0x200))) int b;
5e47b76b
UD
32};
33
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"))) =
38{
39 .a = INIT_A,
40 .b = INIT_B
41};
42
bc79db3f
SL
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
46check_alignment (const char *thr_name, const char *ptr_name,
47 int *ptr, int alignment)
48{
49 uintptr_t offset_aligment = ((uintptr_t) ptr) & (alignment - 1);
50 if (offset_aligment)
51 {
52 FAIL_EXIT1 ("%s (%p) is not 0x%x-byte aligned in %s thread\n",
53 ptr_name, ptr, alignment, thr_name);
54 }
55}
56
57static void
58check_s (const char *thr_name)
59{
60 if (s.a != INIT_A || s.b != INIT_B)
61 FAIL_EXIT1 ("initial value of s in %s thread wrong\n", thr_name);
62
63 check_alignment (thr_name, "s.a", &s.a, 0x20);
64 check_alignment (thr_name, "s.b", &s.b, 0x200);
65}
5e47b76b
UD
66
67static void *
68tf (void *arg)
69{
bc79db3f 70 check_s ("child");
5e47b76b
UD
71
72 ++s.a;
73
74 return NULL;
75}
76
77
78int
79do_test (void)
80{
bc79db3f 81 check_s ("main");
5e47b76b 82
4d1a02ef
UD
83 pthread_attr_t a;
84
bc79db3f 85 xpthread_attr_init (&a);
4d1a02ef 86
bc79db3f
SL
87#define STACK_SIZE (1 * 1024 * 1024)
88 xpthread_attr_setstacksize (&a, STACK_SIZE);
4d1a02ef 89
5e47b76b
UD
90#define N 10
91 int i;
92 for (i = 0; i < N; ++i)
93 {
94#define M 10
95 pthread_t th[M];
96 int j;
97 for (j = 0; j < M; ++j, ++s.a)
bc79db3f 98 th[j] = xpthread_create (&a, tf, NULL);
5e47b76b
UD
99
100 for (j = 0; j < M; ++j)
bc79db3f 101 xpthread_join (th[j]);
5e47b76b
UD
102 }
103
bc79db3f
SL
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);
113
114 xpthread_attr_destroy (&a);
4d1a02ef 115
5e47b76b
UD
116 return 0;
117}
118
bc79db3f 119#include <support/test-driver.c>