]> git.ipfire.org Git - thirdparty/glibc.git/blame - nptl/tst-tls2.c
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / nptl / tst-tls2.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
PE
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
5e47b76b 18
ccf1d573 19#include <errno.h>
5e47b76b
UD
20#include <pthread.h>
21#include <signal.h>
22#include <semaphore.h>
23#include <stdio.h>
24#include <stdlib.h>
25#include <unistd.h>
26
27
28#define N 10
29static pthread_t th[N];
30
31
d5b38790
GG
32static int do_test (void);
33
34#define TEST_FUNCTION do_test ()
35#include "../test-skeleton.c"
36
5e47b76b
UD
37#define CB(n) \
38static void \
39cb##n (void) \
40{ \
41 if (th[n] != pthread_self ()) \
42 { \
d5b38790 43 write_message ("wrong callback\n"); \
5e47b76b
UD
44 _exit (1); \
45 } \
46}
47CB (0)
48CB (1)
49CB (2)
50CB (3)
51CB (4)
52CB (5)
53CB (6)
54CB (7)
55CB (8)
56CB (9)
57static void (*cbs[]) (void) =
58{
59 cb0, cb1, cb2, cb3, cb4, cb5, cb6, cb7, cb8, cb9
60};
61
62
63static __thread void (*fp) (void) __attribute__ ((tls_model ("local-exec")));
64
65
66static sem_t s;
67
68
69#define THE_SIG SIGUSR1
70static void
71handler (int sig)
72{
73 if (sig != THE_SIG)
74 {
d5b38790 75 write_message ("wrong signal\n");
5e47b76b
UD
76 _exit (1);
77 }
78
79 fp ();
80
81 if (sem_post (&s) != 0)
82 {
d5b38790 83 write_message ("sem_post failed\n");
5e47b76b
UD
84 _exit (1);
85 }
86}
87
88
89static pthread_barrier_t b;
90
91#define TOTAL_SIGS 1000
92static int nsigs;
93
94
95static void *
96tf (void *arg)
97{
98 fp = arg;
99
100 pthread_barrier_wait (&b);
101
102 pthread_barrier_wait (&b);
103
104 if (nsigs != TOTAL_SIGS)
105 {
106 puts ("barrier_wait prematurely returns");
107 exit (1);
108 }
109
110 return NULL;
111}
3ce1f295 112
5e47b76b
UD
113
114int
115do_test (void)
116{
117 if (pthread_barrier_init (&b, NULL, N + 1) != 0)
118 {
119 puts ("barrier_init failed");
120 exit (1);
121 }
122
123 if (sem_init (&s, 0, 0) != 0)
124 {
125 puts ("sem_init failed");
126 exit (1);
127 }
128
129 struct sigaction sa;
130 sa.sa_handler = handler;
131 sigemptyset (&sa.sa_mask);
132 sa.sa_flags = 0;
133 if (sigaction (THE_SIG, &sa, NULL) != 0)
134 {
135 puts ("sigaction failed");
136 exit (1);
137 }
138
4d1a02ef
UD
139 pthread_attr_t a;
140
141 if (pthread_attr_init (&a) != 0)
142 {
143 puts ("attr_init failed");
144 exit (1);
145 }
146
147 if (pthread_attr_setstacksize (&a, 1 * 1024 * 1024) != 0)
148 {
149 puts ("attr_setstacksize failed");
150 return 1;
151 }
152
5e47b76b
UD
153 int i;
154 for (i = 0; i < N; ++i)
4d1a02ef 155 if (pthread_create (&th[i], &a, tf, cbs[i]) != 0)
5e47b76b
UD
156 {
157 puts ("pthread_create failed");
158 exit (1);
159 }
160
4d1a02ef
UD
161 if (pthread_attr_destroy (&a) != 0)
162 {
163 puts ("attr_destroy failed");
164 exit (1);
165 }
166
5e47b76b
UD
167 pthread_barrier_wait (&b);
168
169 sigset_t ss;
170 sigemptyset (&ss);
171 sigaddset (&ss, THE_SIG);
172 if (pthread_sigmask (SIG_BLOCK, &ss, NULL) != 0)
173 {
174 puts ("pthread_sigmask failed");
175 exit (1);
176 }
177
178 /* Start sending signals. */
179 for (i = 0; i < TOTAL_SIGS; ++i)
180 {
181 if (kill (getpid (), THE_SIG) != 0)
182 {
183 puts ("kill failed");
184 exit (1);
185 }
186
ccf1d573 187 if (TEMP_FAILURE_RETRY (sem_wait (&s)) != 0)
5e47b76b
UD
188 {
189 puts ("sem_wait failed");
190 exit (1);
191 }
192
193 ++nsigs;
194 }
195
196 pthread_barrier_wait (&b);
197
198 for (i = 0; i < N; ++i)
199 if (pthread_join (th[i], NULL) != 0)
200 {
201 puts ("join failed");
202 exit (1);
203 }
204
205 return 0;
206}