1/* Copyright (C) 2003-2024 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
17
18#include <errno.h>
19#include <pthread.h>
20#include <signal.h>
21#include <semaphore.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <unistd.h>
25
26
27#define N 10
28static pthread_t th[N];
29
30
31static int do_test (void);
32
33#define TEST_FUNCTION do_test ()
34#include "../test-skeleton.c"
35
36#define CB(n) \
37static void \
38cb##n (void) \
39{ \
40 if (th[n] != pthread_self ()) \
41 { \
42 write_message ("wrong callback\n"); \
43 _exit (1); \
44 } \
45}
46CB (0)
47CB (1)
48CB (2)
49CB (3)
50CB (4)
51CB (5)
52CB (6)
53CB (7)
54CB (8)
55CB (9)
56static void (*cbs[]) (void) =
57{
58 cb0, cb1, cb2, cb3, cb4, cb5, cb6, cb7, cb8, cb9
59};
60
61
62static __thread void (*fp) (void) __attribute__ ((tls_model ("local-exec")));
63
64
65static sem_t s;
66
67
68#define THE_SIG SIGUSR1
69static void
70handler (int sig)
71{
72 if (sig != THE_SIG)
73 {
74 write_message (message: "wrong signal\n");
75 _exit (1);
76 }
77
78 fp ();
79
80 if (sem_post (sem: &s) != 0)
81 {
82 write_message (message: "sem_post failed\n");
83 _exit (1);
84 }
85}
86
87
88static pthread_barrier_t b;
89
90#define TOTAL_SIGS 1000
91static int nsigs;
92
93
94static void *
95tf (void *arg)
96{
97 fp = arg;
98
99 pthread_barrier_wait (barrier: &b);
100
101 pthread_barrier_wait (barrier: &b);
102
103 if (nsigs != TOTAL_SIGS)
104 {
105 puts (s: "barrier_wait prematurely returns");
106 exit (1);
107 }
108
109 return NULL;
110}
111
112
113int
114do_test (void)
115{
116 if (pthread_barrier_init (barrier: &b, NULL, N + 1) != 0)
117 {
118 puts (s: "barrier_init failed");
119 exit (1);
120 }
121
122 if (sem_init (sem: &s, pshared: 0, value: 0) != 0)
123 {
124 puts (s: "sem_init failed");
125 exit (1);
126 }
127
128 struct sigaction sa;
129 sa.sa_handler = handler;
130 sigemptyset (&sa.sa_mask);
131 sa.sa_flags = 0;
132 if (sigaction (THE_SIG, act: &sa, NULL) != 0)
133 {
134 puts (s: "sigaction failed");
135 exit (1);
136 }
137
138 pthread_attr_t a;
139
140 if (pthread_attr_init (attr: &a) != 0)
141 {
142 puts (s: "attr_init failed");
143 exit (1);
144 }
145
146 if (pthread_attr_setstacksize (attr: &a, stacksize: 1 * 1024 * 1024) != 0)
147 {
148 puts (s: "attr_setstacksize failed");
149 return 1;
150 }
151
152 int i;
153 for (i = 0; i < N; ++i)
154 if (pthread_create (newthread: &th[i], attr: &a, start_routine: tf, arg: cbs[i]) != 0)
155 {
156 puts (s: "pthread_create failed");
157 exit (1);
158 }
159
160 if (pthread_attr_destroy (attr: &a) != 0)
161 {
162 puts (s: "attr_destroy failed");
163 exit (1);
164 }
165
166 pthread_barrier_wait (barrier: &b);
167
168 sigset_t ss;
169 sigemptyset (&ss);
170 sigaddset (&ss, THE_SIG);
171 if (pthread_sigmask (SIG_BLOCK, newmask: &ss, NULL) != 0)
172 {
173 puts (s: "pthread_sigmask failed");
174 exit (1);
175 }
176
177 /* Start sending signals. */
178 for (i = 0; i < TOTAL_SIGS; ++i)
179 {
180 if (kill (pid: getpid (), THE_SIG) != 0)
181 {
182 puts (s: "kill failed");
183 exit (1);
184 }
185
186 if (TEMP_FAILURE_RETRY (sem_wait (&s)) != 0)
187 {
188 puts (s: "sem_wait failed");
189 exit (1);
190 }
191
192 ++nsigs;
193 }
194
195 pthread_barrier_wait (barrier: &b);
196
197 for (i = 0; i < N; ++i)
198 if (pthread_join (th: th[i], NULL) != 0)
199 {
200 puts (s: "join failed");
201 exit (1);
202 }
203
204 return 0;
205}
206

source code of glibc/sysdeps/pthread/tst-pt-tls2.c