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