]> git.ipfire.org Git - thirdparty/glibc.git/blame - nptl/tst-tls3.c
Prefer https to http for gnu.org and fsf.org URLs
[thirdparty/glibc.git] / nptl / tst-tls3.c
CommitLineData
04277e02 1/* Copyright (C) 2003-2019 Free Software Foundation, Inc.
ffeb4481
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/>. */
ffeb4481
UD
18
19#include <dlfcn.h>
ccf1d573 20#include <errno.h>
ffeb4481
UD
21#include <pthread.h>
22#include <signal.h>
23#include <semaphore.h>
c874a32e 24#include <stdint.h>
ffeb4481
UD
25#include <stdio.h>
26#include <stdlib.h>
27#include <unistd.h>
c874a32e 28#include <pthreaddef.h>
ffeb4481
UD
29
30#define THE_SIG SIGUSR1
31
fc86a87d
FW
32/* The stack size can be overriden. With a sufficiently large stack
33 size, thread stacks for terminated threads are freed, but this does
34 not happen with the default size of 1 MiB. */
35enum { default_stack_size_in_mb = 1 };
36static long stack_size_in_mb;
ffeb4481
UD
37
38#define N 10
39static pthread_t th[N];
40
41
d5b38790
GG
42static int do_test (void);
43
d5b38790
GG
44#define TEST_FUNCTION do_test ()
45#include "../test-skeleton.c"
46
ffeb4481
UD
47#define CB(n) \
48static void \
49cb##n (void) \
50{ \
51 if (th[n] != pthread_self ()) \
52 { \
d5b38790 53 write_message ("wrong callback\n"); \
ffeb4481
UD
54 _exit (1); \
55 } \
56}
57CB (0)
58CB (1)
59CB (2)
60CB (3)
61CB (4)
62CB (5)
63CB (6)
64CB (7)
65CB (8)
66CB (9)
67static void (*cbs[]) (void) =
68{
69 cb0, cb1, cb2, cb3, cb4, cb5, cb6, cb7, cb8, cb9
70};
71
72
73sem_t s;
74
75
76pthread_barrier_t b;
77
78#define TOTAL_SIGS 1000
79int nsigs;
80
81
82int
83do_test (void)
84{
fc86a87d
FW
85 if (stack_size_in_mb == 0)
86 stack_size_in_mb = default_stack_size_in_mb;
87
c874a32e
UD
88 if ((uintptr_t) pthread_self () & (TCB_ALIGNMENT - 1))
89 {
90 puts ("initial thread's struct pthread not aligned enough");
91 exit (1);
92 }
93
ffeb4481
UD
94 if (pthread_barrier_init (&b, NULL, N + 1) != 0)
95 {
96 puts ("barrier_init failed");
97 exit (1);
98 }
99
100 if (sem_init (&s, 0, 0) != 0)
101 {
102 puts ("sem_init failed");
103 exit (1);
104 }
105
106 void *h = dlopen ("tst-tls3mod.so", RTLD_LAZY);
107 if (h == NULL)
108 {
109 puts ("dlopen failed");
110 exit (1);
111 }
112
113 void *(*tf) (void *) = dlsym (h, "tf");
114 if (tf == NULL)
115 {
116 puts ("dlsym for tf failed");
117 exit (1);
118 }
119
120 struct sigaction sa;
121 sa.sa_handler = dlsym (h, "handler");
122 if (sa.sa_handler == NULL)
123 {
124 puts ("dlsym for handler failed");
125 exit (1);
126 }
127 sigemptyset (&sa.sa_mask);
128 sa.sa_flags = 0;
129 if (sigaction (THE_SIG, &sa, NULL) != 0)
130 {
131 puts ("sigaction failed");
132 exit (1);
133 }
134
4d1a02ef
UD
135 pthread_attr_t a;
136
137 if (pthread_attr_init (&a) != 0)
138 {
139 puts ("attr_init failed");
140 exit (1);
141 }
142
fc86a87d 143 if (pthread_attr_setstacksize (&a, stack_size_in_mb * 1024 * 1024) != 0)
4d1a02ef
UD
144 {
145 puts ("attr_setstacksize failed");
146 return 1;
147 }
148
ffeb4481
UD
149 int r;
150 for (r = 0; r < 10; ++r)
151 {
152 int i;
153 for (i = 0; i < N; ++i)
4d1a02ef 154 if (pthread_create (&th[i], &a, tf, cbs[i]) != 0)
ffeb4481
UD
155 {
156 puts ("pthread_create failed");
157 exit (1);
158 }
159
160 nsigs = 0;
161
162 pthread_barrier_wait (&b);
163
164 sigset_t ss;
165 sigemptyset (&ss);
166 sigaddset (&ss, THE_SIG);
167 if (pthread_sigmask (SIG_BLOCK, &ss, NULL) != 0)
168 {
169 puts ("pthread_sigmask failed");
170 exit (1);
171 }
172
173 /* Start sending signals. */
174 for (i = 0; i < TOTAL_SIGS; ++i)
175 {
176 if (kill (getpid (), THE_SIG) != 0)
177 {
178 puts ("kill failed");
179 exit (1);
180 }
181
ccf1d573 182 if (TEMP_FAILURE_RETRY (sem_wait (&s)) != 0)
ffeb4481
UD
183 {
184 puts ("sem_wait failed");
185 exit (1);
186 }
187
188 ++nsigs;
189 }
190
191 pthread_barrier_wait (&b);
192
193 if (pthread_sigmask (SIG_UNBLOCK, &ss, NULL) != 0)
194 {
195 puts ("pthread_sigmask failed");
196 exit (1);
197 }
198
199 for (i = 0; i < N; ++i)
200 if (pthread_join (th[i], NULL) != 0)
201 {
202 puts ("join failed");
203 exit (1);
204 }
205 }
206
4d1a02ef
UD
207 if (pthread_attr_destroy (&a) != 0)
208 {
209 puts ("attr_destroy failed");
210 exit (1);
211 }
212
ffeb4481
UD
213 return 0;
214}