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