Warning: That file was not part of the compilation database. It may have many parsing errors.

1/* pthread_getattr_np test.
2 Copyright (C) 2003-2019 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Jakub Jelinek <jakub@redhat.com>, 2003.
5
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
10
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
19
20#include <errno.h>
21#include <error.h>
22#include <pthread.h>
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <unistd.h>
27
28#include <stackinfo.h>
29#include <libc-diag.h>
30
31static void *
32tf (void *arg)
33{
34 pthread_attr_t a, *ap, a2;
35 int err;
36 void *result = NULL;
37
38 if (arg == NULL)
39 {
40 ap = &a2;
41 err = pthread_attr_init (ap);
42 if (err)
43 {
44 error (0, err, "pthread_attr_init failed");
45 return tf;
46 }
47 }
48 else
49 ap = (pthread_attr_t *) arg;
50
51 err = pthread_getattr_np (pthread_self (), &a);
52 if (err)
53 {
54 error (0, err, "pthread_getattr_np failed");
55 result = tf;
56 }
57
58 int detachstate1, detachstate2;
59 err = pthread_attr_getdetachstate (&a, &detachstate1);
60 if (err)
61 {
62 error (0, err, "pthread_attr_getdetachstate failed");
63 result = tf;
64 }
65 else
66 {
67 err = pthread_attr_getdetachstate (ap, &detachstate2);
68 if (err)
69 {
70 error (0, err, "pthread_attr_getdetachstate failed");
71 result = tf;
72 }
73 else if (detachstate1 != detachstate2)
74 {
75 error (0, 0, "detachstate differs %d != %d",
76 detachstate1, detachstate2);
77 result = tf;
78 }
79 }
80
81 void *stackaddr;
82 size_t stacksize;
83 err = pthread_attr_getstack (&a, &stackaddr, &stacksize);
84 if (err)
85 {
86 error (0, err, "pthread_attr_getstack failed");
87 result = tf;
88 }
89 else if ((void *) &a < stackaddr
90 || (void *) &a >= stackaddr + stacksize)
91 {
92 error (0, 0, "pthread_attr_getstack returned range does not cover thread's stack");
93 result = tf;
94 }
95 else
96 printf ("thread stack %p-%p (0x%zx)\n", stackaddr, stackaddr + stacksize,
97 stacksize);
98
99 size_t guardsize1, guardsize2;
100 err = pthread_attr_getguardsize (&a, &guardsize1);
101 if (err)
102 {
103 error (0, err, "pthread_attr_getguardsize failed");
104 result = tf;
105 }
106 else
107 {
108 err = pthread_attr_getguardsize (ap, &guardsize2);
109 if (err)
110 {
111 error (0, err, "pthread_attr_getguardsize failed");
112 result = tf;
113 }
114 else if (guardsize1 != guardsize2)
115 {
116 error (0, 0, "guardsize differs %zd != %zd",
117 guardsize1, guardsize2);
118 result = tf;
119 }
120 else
121 printf ("thread guardsize %zd\n", guardsize1);
122 }
123
124 int scope1, scope2;
125 err = pthread_attr_getscope (&a, &scope1);
126 if (err)
127 {
128 error (0, err, "pthread_attr_getscope failed");
129 result = tf;
130 }
131 else
132 {
133 err = pthread_attr_getscope (ap, &scope2);
134 if (err)
135 {
136 error (0, err, "pthread_attr_getscope failed");
137 result = tf;
138 }
139 else if (scope1 != scope2)
140 {
141 error (0, 0, "scope differs %d != %d",
142 scope1, scope2);
143 result = tf;
144 }
145 }
146
147 int inheritsched1, inheritsched2;
148 err = pthread_attr_getinheritsched (&a, &inheritsched1);
149 if (err)
150 {
151 error (0, err, "pthread_attr_getinheritsched failed");
152 result = tf;
153 }
154 else
155 {
156 err = pthread_attr_getinheritsched (ap, &inheritsched2);
157 if (err)
158 {
159 error (0, err, "pthread_attr_getinheritsched failed");
160 result = tf;
161 }
162 else if (inheritsched1 != inheritsched2)
163 {
164 error (0, 0, "inheritsched differs %d != %d",
165 inheritsched1, inheritsched2);
166 result = tf;
167 }
168 }
169
170 cpu_set_t c1, c2;
171 err = pthread_getaffinity_np (pthread_self (), sizeof (c1), &c1);
172 if (err == 0)
173 {
174 err = pthread_attr_getaffinity_np (&a, sizeof (c2), &c2);
175 if (err)
176 {
177 error (0, err, "pthread_attr_getaffinity_np failed");
178 result = tf;
179 }
180 else if (memcmp (&c1, &c2, sizeof (c1)))
181 {
182 error (0, 0, "pthread_attr_getaffinity_np returned different CPU mask than pthread_getattr_np");
183 result = tf;
184 }
185 }
186
187 err = pthread_attr_destroy (&a);
188 if (err)
189 {
190 error (0, err, "pthread_attr_destroy failed");
191 result = tf;
192 }
193
194 if (ap == &a2)
195 {
196 err = pthread_attr_destroy (ap);
197 if (err)
198 {
199 error (0, err, "pthread_attr_destroy failed");
200 result = tf;
201 }
202 }
203
204 return result;
205}
206
207
208static int
209do_test (void)
210{
211 int result = 0;
212 pthread_attr_t a;
213 cpu_set_t c1, c2;
214
215 int err = pthread_attr_init (&a);
216 if (err)
217 {
218 error (0, err, "pthread_attr_init failed");
219 result = 1;
220 }
221
222 err = pthread_attr_getaffinity_np (&a, sizeof (c1), &c1);
223 if (err && err != ENOSYS)
224 {
225 error (0, err, "pthread_attr_getaffinity_np failed");
226 result = 1;
227 }
228
229 err = pthread_attr_destroy (&a);
230 if (err)
231 {
232 error (0, err, "pthread_attr_destroy failed");
233 result = 1;
234 }
235
236 err = pthread_getattr_np (pthread_self (), &a);
237 if (err)
238 {
239 error (0, err, "pthread_getattr_np failed");
240 result = 1;
241 }
242
243 int detachstate;
244 err = pthread_attr_getdetachstate (&a, &detachstate);
245 if (err)
246 {
247 error (0, err, "pthread_attr_getdetachstate failed");
248 result = 1;
249 }
250 else if (detachstate != PTHREAD_CREATE_JOINABLE)
251 {
252 error (0, 0, "initial thread not joinable");
253 result = 1;
254 }
255
256 void *stackaddr;
257 size_t stacksize;
258 err = pthread_attr_getstack (&a, &stackaddr, &stacksize);
259 if (err)
260 {
261 error (0, err, "pthread_attr_getstack failed");
262 result = 1;
263 }
264 else if ((void *) &a < stackaddr
265 || (void *) &a >= stackaddr + stacksize)
266 {
267 error (0, 0, "pthread_attr_getstack returned range does not cover main's stack");
268 result = 1;
269 }
270 else
271 printf ("initial thread stack %p-%p (0x%zx)\n", stackaddr,
272 stackaddr + stacksize, stacksize);
273
274 size_t guardsize;
275 err = pthread_attr_getguardsize (&a, &guardsize);
276 if (err)
277 {
278 error (0, err, "pthread_attr_getguardsize failed");
279 result = 1;
280 }
281 else if (guardsize != 0)
282 {
283 error (0, 0, "pthread_attr_getguardsize returned %zd != 0",
284 guardsize);
285 result = 1;
286 }
287
288 int scope;
289 err = pthread_attr_getscope (&a, &scope);
290 if (err)
291 {
292 error (0, err, "pthread_attr_getscope failed");
293 result = 1;
294 }
295 else if (scope != PTHREAD_SCOPE_SYSTEM)
296 {
297 error (0, 0, "pthread_attr_getscope returned %d != PTHREAD_SCOPE_SYSTEM",
298 scope);
299 result = 1;
300 }
301
302 int inheritsched;
303 err = pthread_attr_getinheritsched (&a, &inheritsched);
304 if (err)
305 {
306 error (0, err, "pthread_attr_getinheritsched failed");
307 result = 1;
308 }
309 else if (inheritsched != PTHREAD_INHERIT_SCHED)
310 {
311 error (0, 0, "pthread_attr_getinheritsched returned %d != PTHREAD_INHERIT_SCHED",
312 inheritsched);
313 result = 1;
314 }
315
316 err = pthread_getaffinity_np (pthread_self (), sizeof (c1), &c1);
317 if (err == 0)
318 {
319 err = pthread_attr_getaffinity_np (&a, sizeof (c2), &c2);
320 if (err)
321 {
322 error (0, err, "pthread_attr_getaffinity_np failed");
323 result = 1;
324 }
325 else if (memcmp (&c1, &c2, sizeof (c1)))
326 {
327 error (0, 0, "pthread_attr_getaffinity_np returned different CPU mask than pthread_getattr_np");
328 result = 1;
329 }
330 }
331
332 err = pthread_attr_destroy (&a);
333 if (err)
334 {
335 error (0, err, "pthread_attr_destroy failed");
336 result = 1;
337 }
338
339 pthread_t th;
340 err = pthread_create (&th, NULL, tf, NULL);
341 if (err)
342 {
343 error (0, err, "pthread_create #1 failed");
344 result = 1;
345 }
346 else
347 {
348 void *ret;
349 err = pthread_join (th, &ret);
350 if (err)
351 {
352 error (0, err, "pthread_join #1 failed");
353 result = 1;
354 }
355 else if (ret != NULL)
356 result = 1;
357 }
358
359 err = pthread_attr_init (&a);
360 if (err)
361 {
362 error (0, err, "pthread_attr_init failed");
363 result = 1;
364 }
365
366 DIAG_PUSH_NEEDS_COMMENT;
367#if __GNUC_PREREQ (7, 0)
368 /* GCC 8 warns about aliasing of the restrict-qualified arguments
369 passed &a. Since pthread_create does not dereference its fourth
370 argument, this aliasing, which is deliberate in this test, cannot
371 in fact cause problems. */
372 DIAG_IGNORE_NEEDS_COMMENT (8, "-Wrestrict");
373#endif
374 err = pthread_create (&th, &a, tf, &a);
375 DIAG_POP_NEEDS_COMMENT;
376 if (err)
377 {
378 error (0, err, "pthread_create #2 failed");
379 result = 1;
380 }
381 else
382 {
383 void *ret;
384 err = pthread_join (th, &ret);
385 if (err)
386 {
387 error (0, err, "pthread_join #2 failed");
388 result = 1;
389 }
390 else if (ret != NULL)
391 result = 1;
392 }
393
394 err = pthread_attr_setguardsize (&a, 16 * sysconf (_SC_PAGESIZE));
395 if (err)
396 {
397 error (0, err, "pthread_attr_setguardsize failed");
398 result = 1;
399 }
400
401 DIAG_PUSH_NEEDS_COMMENT;
402#if __GNUC_PREREQ (7, 0)
403 /* GCC 8 warns about aliasing of the restrict-qualified arguments
404 passed &a. Since pthread_create does not dereference its fourth
405 argument, this aliasing, which is deliberate in this test, cannot
406 in fact cause problems. */
407 DIAG_IGNORE_NEEDS_COMMENT (8, "-Wrestrict");
408#endif
409 err = pthread_create (&th, &a, tf, &a);
410 DIAG_POP_NEEDS_COMMENT;
411 if (err)
412 {
413 error (0, err, "pthread_create #3 failed");
414 result = 1;
415 }
416 else
417 {
418 void *ret;
419 err = pthread_join (th, &ret);
420 if (err)
421 {
422 error (0, err, "pthread_join #3 failed");
423 result = 1;
424 }
425 else if (ret != NULL)
426 result = 1;
427 }
428
429 err = pthread_attr_destroy (&a);
430 if (err)
431 {
432 error (0, err, "pthread_attr_destroy failed");
433 result = 1;
434 }
435
436 return result;
437}
438
439#define TEST_FUNCTION do_test ()
440#include "../test-skeleton.c"
441

Warning: That file was not part of the compilation database. It may have many parsing errors.