Warning: That file was not part of the compilation database. It may have many parsing errors.

1/* Copyright (C) 2003-2019 Free Software Foundation, Inc.
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
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
18
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>
27
28struct test_s
29{
30 __attribute__ ((aligned(0x20))) int a;
31 __attribute__ ((aligned(0x200))) int b;
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
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}
66
67static void *
68tf (void *arg)
69{
70 check_s ("child");
71
72 ++s.a;
73
74 return NULL;
75}
76
77
78int
79do_test (void)
80{
81 check_s ("main");
82
83 pthread_attr_t a;
84
85 xpthread_attr_init (&a);
86
87#define STACK_SIZE (1 * 1024 * 1024)
88 xpthread_attr_setstacksize (&a, STACK_SIZE);
89
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)
98 th[j] = xpthread_create (&a, tf, NULL);
99
100 for (j = 0; j < M; ++j)
101 xpthread_join (th[j]);
102 }
103
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);
115
116 return 0;
117}
118
119#include <support/test-driver.c>
120

Warning: That file was not part of the compilation database. It may have many parsing errors.