1 /* pthread_getattr_np test.
2 Copyright (C) 2003-2020 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Jakub Jelinek <jakub@redhat.com>, 2003.
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.
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.
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 <https://www.gnu.org/licenses/>. */
28 #include <stackinfo.h>
29 #include <libc-diag.h>
34 pthread_attr_t a
, *ap
, a2
;
41 err
= pthread_attr_init (ap
);
44 error (0, err
, "pthread_attr_init failed");
49 ap
= (pthread_attr_t
*) arg
;
51 err
= pthread_getattr_np (pthread_self (), &a
);
54 error (0, err
, "pthread_getattr_np failed");
58 int detachstate1
, detachstate2
;
59 err
= pthread_attr_getdetachstate (&a
, &detachstate1
);
62 error (0, err
, "pthread_attr_getdetachstate failed");
67 err
= pthread_attr_getdetachstate (ap
, &detachstate2
);
70 error (0, err
, "pthread_attr_getdetachstate failed");
73 else if (detachstate1
!= detachstate2
)
75 error (0, 0, "detachstate differs %d != %d",
76 detachstate1
, detachstate2
);
83 err
= pthread_attr_getstack (&a
, &stackaddr
, &stacksize
);
86 error (0, err
, "pthread_attr_getstack failed");
89 else if ((void *) &a
< stackaddr
90 || (void *) &a
>= stackaddr
+ stacksize
)
92 error (0, 0, "pthread_attr_getstack returned range does not cover thread's stack");
96 printf ("thread stack %p-%p (0x%zx)\n", stackaddr
, stackaddr
+ stacksize
,
99 size_t guardsize1
, guardsize2
;
100 err
= pthread_attr_getguardsize (&a
, &guardsize1
);
103 error (0, err
, "pthread_attr_getguardsize failed");
108 err
= pthread_attr_getguardsize (ap
, &guardsize2
);
111 error (0, err
, "pthread_attr_getguardsize failed");
114 else if (guardsize1
!= guardsize2
)
116 error (0, 0, "guardsize differs %zd != %zd",
117 guardsize1
, guardsize2
);
121 printf ("thread guardsize %zd\n", guardsize1
);
125 err
= pthread_attr_getscope (&a
, &scope1
);
128 error (0, err
, "pthread_attr_getscope failed");
133 err
= pthread_attr_getscope (ap
, &scope2
);
136 error (0, err
, "pthread_attr_getscope failed");
139 else if (scope1
!= scope2
)
141 error (0, 0, "scope differs %d != %d",
147 int inheritsched1
, inheritsched2
;
148 err
= pthread_attr_getinheritsched (&a
, &inheritsched1
);
151 error (0, err
, "pthread_attr_getinheritsched failed");
156 err
= pthread_attr_getinheritsched (ap
, &inheritsched2
);
159 error (0, err
, "pthread_attr_getinheritsched failed");
162 else if (inheritsched1
!= inheritsched2
)
164 error (0, 0, "inheritsched differs %d != %d",
165 inheritsched1
, inheritsched2
);
171 err
= pthread_getaffinity_np (pthread_self (), sizeof (c1
), &c1
);
174 err
= pthread_attr_getaffinity_np (&a
, sizeof (c2
), &c2
);
177 error (0, err
, "pthread_attr_getaffinity_np failed");
180 else if (memcmp (&c1
, &c2
, sizeof (c1
)))
182 error (0, 0, "pthread_attr_getaffinity_np returned different CPU mask than pthread_getattr_np");
187 err
= pthread_attr_destroy (&a
);
190 error (0, err
, "pthread_attr_destroy failed");
196 err
= pthread_attr_destroy (ap
);
199 error (0, err
, "pthread_attr_destroy failed");
215 int err
= pthread_attr_init (&a
);
218 error (0, err
, "pthread_attr_init failed");
222 err
= pthread_attr_getaffinity_np (&a
, sizeof (c1
), &c1
);
223 if (err
&& err
!= ENOSYS
)
225 error (0, err
, "pthread_attr_getaffinity_np failed");
229 err
= pthread_attr_destroy (&a
);
232 error (0, err
, "pthread_attr_destroy failed");
236 err
= pthread_getattr_np (pthread_self (), &a
);
239 error (0, err
, "pthread_getattr_np failed");
244 err
= pthread_attr_getdetachstate (&a
, &detachstate
);
247 error (0, err
, "pthread_attr_getdetachstate failed");
250 else if (detachstate
!= PTHREAD_CREATE_JOINABLE
)
252 error (0, 0, "initial thread not joinable");
258 err
= pthread_attr_getstack (&a
, &stackaddr
, &stacksize
);
261 error (0, err
, "pthread_attr_getstack failed");
264 else if ((void *) &a
< stackaddr
265 || (void *) &a
>= stackaddr
+ stacksize
)
267 error (0, 0, "pthread_attr_getstack returned range does not cover main's stack");
271 printf ("initial thread stack %p-%p (0x%zx)\n", stackaddr
,
272 stackaddr
+ stacksize
, stacksize
);
275 err
= pthread_attr_getguardsize (&a
, &guardsize
);
278 error (0, err
, "pthread_attr_getguardsize failed");
281 else if (guardsize
!= 0)
283 error (0, 0, "pthread_attr_getguardsize returned %zd != 0",
289 err
= pthread_attr_getscope (&a
, &scope
);
292 error (0, err
, "pthread_attr_getscope failed");
295 else if (scope
!= PTHREAD_SCOPE_SYSTEM
)
297 error (0, 0, "pthread_attr_getscope returned %d != PTHREAD_SCOPE_SYSTEM",
303 err
= pthread_attr_getinheritsched (&a
, &inheritsched
);
306 error (0, err
, "pthread_attr_getinheritsched failed");
309 else if (inheritsched
!= PTHREAD_INHERIT_SCHED
)
311 error (0, 0, "pthread_attr_getinheritsched returned %d != PTHREAD_INHERIT_SCHED",
316 err
= pthread_getaffinity_np (pthread_self (), sizeof (c1
), &c1
);
319 err
= pthread_attr_getaffinity_np (&a
, sizeof (c2
), &c2
);
322 error (0, err
, "pthread_attr_getaffinity_np failed");
325 else if (memcmp (&c1
, &c2
, sizeof (c1
)))
327 error (0, 0, "pthread_attr_getaffinity_np returned different CPU mask than pthread_getattr_np");
332 err
= pthread_attr_destroy (&a
);
335 error (0, err
, "pthread_attr_destroy failed");
340 err
= pthread_create (&th
, NULL
, tf
, NULL
);
343 error (0, err
, "pthread_create #1 failed");
349 err
= pthread_join (th
, &ret
);
352 error (0, err
, "pthread_join #1 failed");
355 else if (ret
!= NULL
)
359 err
= pthread_attr_init (&a
);
362 error (0, err
, "pthread_attr_init failed");
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");
374 err
= pthread_create (&th
, &a
, tf
, &a
);
375 DIAG_POP_NEEDS_COMMENT
;
378 error (0, err
, "pthread_create #2 failed");
384 err
= pthread_join (th
, &ret
);
387 error (0, err
, "pthread_join #2 failed");
390 else if (ret
!= NULL
)
394 err
= pthread_attr_setguardsize (&a
, 16 * sysconf (_SC_PAGESIZE
));
397 error (0, err
, "pthread_attr_setguardsize failed");
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");
409 err
= pthread_create (&th
, &a
, tf
, &a
);
410 DIAG_POP_NEEDS_COMMENT
;
413 error (0, err
, "pthread_create #3 failed");
419 err
= pthread_join (th
, &ret
);
422 error (0, err
, "pthread_join #3 failed");
425 else if (ret
!= NULL
)
429 err
= pthread_attr_destroy (&a
);
432 error (0, err
, "pthread_attr_destroy failed");
439 #define TEST_FUNCTION do_test ()
440 #include "../test-skeleton.c"