1 /* pthread_getattr_np test.
2 Copyright (C) 2003-2022 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
27 #include <stackinfo.h>
28 #include <libc-diag.h>
33 pthread_attr_t a
, *ap
, a2
;
40 err
= pthread_attr_init (ap
);
43 error (0, err
, "pthread_attr_init failed");
48 ap
= (pthread_attr_t
*) arg
;
50 err
= pthread_getattr_np (pthread_self (), &a
);
53 error (0, err
, "pthread_getattr_np failed");
57 int detachstate1
, detachstate2
;
58 err
= pthread_attr_getdetachstate (&a
, &detachstate1
);
61 error (0, err
, "pthread_attr_getdetachstate failed");
66 err
= pthread_attr_getdetachstate (ap
, &detachstate2
);
69 error (0, err
, "pthread_attr_getdetachstate failed");
72 else if (detachstate1
!= detachstate2
)
74 error (0, 0, "detachstate differs %d != %d",
75 detachstate1
, detachstate2
);
82 err
= pthread_attr_getstack (&a
, &stackaddr
, &stacksize
);
85 error (0, err
, "pthread_attr_getstack failed");
88 else if ((void *) &a
< stackaddr
89 || (void *) &a
>= stackaddr
+ stacksize
)
91 error (0, 0, "pthread_attr_getstack returned range does not cover thread's stack");
95 printf ("thread stack %p-%p (0x%zx)\n", stackaddr
, stackaddr
+ stacksize
,
98 size_t guardsize1
, guardsize2
;
99 err
= pthread_attr_getguardsize (&a
, &guardsize1
);
102 error (0, err
, "pthread_attr_getguardsize failed");
107 err
= pthread_attr_getguardsize (ap
, &guardsize2
);
110 error (0, err
, "pthread_attr_getguardsize failed");
113 else if (guardsize1
!= guardsize2
)
115 error (0, 0, "guardsize differs %zd != %zd",
116 guardsize1
, guardsize2
);
120 printf ("thread guardsize %zd\n", guardsize1
);
124 err
= pthread_attr_getscope (&a
, &scope1
);
127 error (0, err
, "pthread_attr_getscope failed");
132 err
= pthread_attr_getscope (ap
, &scope2
);
135 error (0, err
, "pthread_attr_getscope failed");
138 else if (scope1
!= scope2
)
140 error (0, 0, "scope differs %d != %d",
146 int inheritsched1
, inheritsched2
;
147 err
= pthread_attr_getinheritsched (&a
, &inheritsched1
);
150 error (0, err
, "pthread_attr_getinheritsched failed");
155 err
= pthread_attr_getinheritsched (ap
, &inheritsched2
);
158 error (0, err
, "pthread_attr_getinheritsched failed");
161 else if (inheritsched1
!= inheritsched2
)
163 error (0, 0, "inheritsched differs %d != %d",
164 inheritsched1
, inheritsched2
);
170 err
= pthread_getaffinity_np (pthread_self (), sizeof (c1
), &c1
);
173 err
= pthread_attr_getaffinity_np (&a
, sizeof (c2
), &c2
);
176 error (0, err
, "pthread_attr_getaffinity_np failed");
179 else if (memcmp (&c1
, &c2
, sizeof (c1
)))
181 error (0, 0, "pthread_attr_getaffinity_np returned different CPU mask than pthread_getattr_np");
186 err
= pthread_attr_destroy (&a
);
189 error (0, err
, "pthread_attr_destroy failed");
195 err
= pthread_attr_destroy (ap
);
198 error (0, err
, "pthread_attr_destroy failed");
214 int err
= pthread_attr_init (&a
);
217 error (0, err
, "pthread_attr_init failed");
221 err
= pthread_attr_getaffinity_np (&a
, sizeof (c1
), &c1
);
222 if (err
&& err
!= ENOSYS
)
224 error (0, err
, "pthread_attr_getaffinity_np failed");
228 err
= pthread_attr_destroy (&a
);
231 error (0, err
, "pthread_attr_destroy failed");
235 err
= pthread_getattr_np (pthread_self (), &a
);
238 error (0, err
, "pthread_getattr_np failed");
243 err
= pthread_attr_getdetachstate (&a
, &detachstate
);
246 error (0, err
, "pthread_attr_getdetachstate failed");
249 else if (detachstate
!= PTHREAD_CREATE_JOINABLE
)
251 error (0, 0, "initial thread not joinable");
257 err
= pthread_attr_getstack (&a
, &stackaddr
, &stacksize
);
260 error (0, err
, "pthread_attr_getstack failed");
263 else if ((void *) &a
< stackaddr
264 || (void *) &a
>= stackaddr
+ stacksize
)
266 error (0, 0, "pthread_attr_getstack returned range does not cover main's stack");
270 printf ("initial thread stack %p-%p (0x%zx)\n", stackaddr
,
271 stackaddr
+ stacksize
, stacksize
);
274 err
= pthread_attr_getguardsize (&a
, &guardsize
);
277 error (0, err
, "pthread_attr_getguardsize failed");
280 else if (guardsize
!= 0)
282 error (0, 0, "pthread_attr_getguardsize returned %zd != 0",
288 err
= pthread_attr_getscope (&a
, &scope
);
291 error (0, err
, "pthread_attr_getscope failed");
294 else if (scope
!= PTHREAD_SCOPE_SYSTEM
)
296 error (0, 0, "pthread_attr_getscope returned %d != PTHREAD_SCOPE_SYSTEM",
302 err
= pthread_attr_getinheritsched (&a
, &inheritsched
);
305 error (0, err
, "pthread_attr_getinheritsched failed");
308 else if (inheritsched
!= PTHREAD_INHERIT_SCHED
)
310 error (0, 0, "pthread_attr_getinheritsched returned %d != PTHREAD_INHERIT_SCHED",
315 err
= pthread_getaffinity_np (pthread_self (), sizeof (c1
), &c1
);
318 err
= pthread_attr_getaffinity_np (&a
, sizeof (c2
), &c2
);
321 error (0, err
, "pthread_attr_getaffinity_np failed");
324 else if (memcmp (&c1
, &c2
, sizeof (c1
)))
326 error (0, 0, "pthread_attr_getaffinity_np returned different CPU mask than pthread_getattr_np");
331 err
= pthread_attr_destroy (&a
);
334 error (0, err
, "pthread_attr_destroy failed");
339 err
= pthread_create (&th
, NULL
, tf
, NULL
);
342 error (0, err
, "pthread_create #1 failed");
348 err
= pthread_join (th
, &ret
);
351 error (0, err
, "pthread_join #1 failed");
354 else if (ret
!= NULL
)
358 err
= pthread_attr_init (&a
);
361 error (0, err
, "pthread_attr_init failed");
365 DIAG_PUSH_NEEDS_COMMENT
;
366 #if __GNUC_PREREQ (7, 0)
367 /* GCC 8 warns about aliasing of the restrict-qualified arguments
368 passed &a. Since pthread_create does not dereference its fourth
369 argument, this aliasing, which is deliberate in this test, cannot
370 in fact cause problems. */
371 DIAG_IGNORE_NEEDS_COMMENT (8, "-Wrestrict");
373 err
= pthread_create (&th
, &a
, tf
, &a
);
374 DIAG_POP_NEEDS_COMMENT
;
377 error (0, err
, "pthread_create #2 failed");
383 err
= pthread_join (th
, &ret
);
386 error (0, err
, "pthread_join #2 failed");
389 else if (ret
!= NULL
)
393 err
= pthread_attr_setguardsize (&a
, 16 * sysconf (_SC_PAGESIZE
));
396 error (0, err
, "pthread_attr_setguardsize failed");
400 DIAG_PUSH_NEEDS_COMMENT
;
401 #if __GNUC_PREREQ (7, 0)
402 /* GCC 8 warns about aliasing of the restrict-qualified arguments
403 passed &a. Since pthread_create does not dereference its fourth
404 argument, this aliasing, which is deliberate in this test, cannot
405 in fact cause problems. */
406 DIAG_IGNORE_NEEDS_COMMENT (8, "-Wrestrict");
408 err
= pthread_create (&th
, &a
, tf
, &a
);
409 DIAG_POP_NEEDS_COMMENT
;
412 error (0, err
, "pthread_create #3 failed");
418 err
= pthread_join (th
, &ret
);
421 error (0, err
, "pthread_join #3 failed");
424 else if (ret
!= NULL
)
428 err
= pthread_attr_destroy (&a
);
431 error (0, err
, "pthread_attr_destroy failed");
438 #define TEST_FUNCTION do_test ()
439 #include "../test-skeleton.c"