blob: 3615d1b81506e8c67fc3b6811db083277a76b966 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/* Copyright (C) 2003, 2004 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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
19
20#include <errno.h>
21#include <pthread.h>
22#include <signal.h>
23#include <semaphore.h>
24#include <stdio.h>
25#include <stdlib.h>
26#include <unistd.h>
27#include <tls.h>
28
29#if HAVE___THREAD
30
31#define N 10
32static pthread_t th[N];
33
34
35#define CB(n) \
36static void \
37cb##n (void) \
38{ \
39 if (th[n] != pthread_self ()) \
40 { \
41 write (STDOUT_FILENO, "wrong callback\n", 15); \
42 _exit (1); \
43 } \
44}
45CB (0)
46CB (1)
47CB (2)
48CB (3)
49CB (4)
50CB (5)
51CB (6)
52CB (7)
53CB (8)
54CB (9)
55static void (*cbs[]) (void) =
56{
57 cb0, cb1, cb2, cb3, cb4, cb5, cb6, cb7, cb8, cb9
58};
59
60
61static __thread void (*fp) (void) __attribute__ ((tls_model ("local-exec")));
62
63
64static sem_t s;
65
66
67#define THE_SIG SIGUSR1
68static void
69handler (int sig)
70{
71 if (sig != THE_SIG)
72 {
73 write (STDOUT_FILENO, "wrong signal\n", 13);
74 _exit (1);
75 }
76
77 fp ();
78
79 if (sem_post (&s) != 0)
80 {
81 write (STDOUT_FILENO, "sem_post failed\n", 16);
82 _exit (1);
83 }
84}
85
86
87static pthread_barrier_t b;
88
89#define TOTAL_SIGS 1000
90static int nsigs;
91
92
93static void *
94tf (void *arg)
95{
96 fp = arg;
97
98 pthread_barrier_wait (&b);
99
100 pthread_barrier_wait (&b);
101
102 if (nsigs != TOTAL_SIGS)
103 {
104 puts ("barrier_wait prematurely returns");
105 exit (1);
106 }
107
108 return NULL;
109}
110#endif
111
112int
113do_test (void)
114{
115#if !HAVE___THREAD
116
117 puts ("No __thread support in compiler, test skipped.");
118
119 return 0;
120#else
121
122 if (pthread_barrier_init (&b, NULL, N + 1) != 0)
123 {
124 puts ("barrier_init failed");
125 exit (1);
126 }
127
128 if (sem_init (&s, 0, 0) != 0)
129 {
130 puts ("sem_init failed");
131 exit (1);
132 }
133
134 struct sigaction sa;
135 sa.sa_handler = handler;
136 sigemptyset (&sa.sa_mask);
137 sa.sa_flags = 0;
138 if (sigaction (THE_SIG, &sa, NULL) != 0)
139 {
140 puts ("sigaction failed");
141 exit (1);
142 }
143
144 pthread_attr_t a;
145
146 if (pthread_attr_init (&a) != 0)
147 {
148 puts ("attr_init failed");
149 exit (1);
150 }
151
152 if (pthread_attr_setstacksize (&a, 1 * 1024 * 1024) != 0)
153 {
154 puts ("attr_setstacksize failed");
155 return 1;
156 }
157
158 int i;
159 for (i = 0; i < N; ++i)
160 if (pthread_create (&th[i], &a, tf, cbs[i]) != 0)
161 {
162 puts ("pthread_create failed");
163 exit (1);
164 }
165
166 if (pthread_attr_destroy (&a) != 0)
167 {
168 puts ("attr_destroy failed");
169 exit (1);
170 }
171
172 pthread_barrier_wait (&b);
173
174 sigset_t ss;
175 sigemptyset (&ss);
176 sigaddset (&ss, THE_SIG);
177 if (pthread_sigmask (SIG_BLOCK, &ss, NULL) != 0)
178 {
179 puts ("pthread_sigmask failed");
180 exit (1);
181 }
182
183 /* Start sending signals. */
184 for (i = 0; i < TOTAL_SIGS; ++i)
185 {
186 if (kill (getpid (), THE_SIG) != 0)
187 {
188 puts ("kill failed");
189 exit (1);
190 }
191
192 if (TEMP_FAILURE_RETRY (sem_wait (&s)) != 0)
193 {
194 puts ("sem_wait failed");
195 exit (1);
196 }
197
198 ++nsigs;
199 }
200
201 pthread_barrier_wait (&b);
202
203 for (i = 0; i < N; ++i)
204 if (pthread_join (th[i], NULL) != 0)
205 {
206 puts ("join failed");
207 exit (1);
208 }
209
210 return 0;
211#endif
212}
213
214
215#define TEST_FUNCTION do_test ()
216#include "../test-skeleton.c"