1 /* Verify that pthread_[gs]etattr_default_np work correctly.
3 Copyright (C) 2013-2024 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
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 #define RETURN_IF_FAIL(f, ...) \
30 int ret = f (__VA_ARGS__); \
33 printf ("%s:%d: %s returned %d (errno = %d)\n", __FILE__, __LINE__, \
39 static int (*verify_result
) (pthread_attr_t
*);
40 static size_t stacksize
= 1024 * 1024;
41 static size_t guardsize
;
42 static bool do_join
= true;
43 static int running
= 0;
44 static int detach_failed
= 0;
45 static pthread_mutex_t m
= PTHREAD_MUTEX_INITIALIZER
;
46 static pthread_cond_t c
= PTHREAD_COND_INITIALIZER
;
49 thr (void *unused
__attribute__ ((unused
)))
54 memset (&attr
, 0xab, sizeof attr
);
55 /* To verify that the pthread_setattr_default_np worked. */
56 if ((ret
= pthread_getattr_default_np (&attr
)) != 0)
58 printf ("pthread_getattr_default_np failed: %s\n", strerror (ret
));
62 if ((ret
= (*verify_result
) (&attr
)) != 0)
65 memset (&attr
, 0xab, sizeof attr
);
66 /* To verify that the attributes actually got applied. */
67 if ((ret
= pthread_getattr_np (pthread_self (), &attr
)) != 0)
69 printf ("pthread_getattr_default_np failed: %s\n", strerror (ret
));
73 ret
= (*verify_result
) (&attr
);
78 pthread_mutex_lock (&m
);
80 pthread_cond_signal (&c
);
81 pthread_mutex_unlock (&m
);
86 return (void *) (uintptr_t) ret
;
90 run_threads (const pthread_attr_t
*attr
)
95 RETURN_IF_FAIL (pthread_setattr_default_np
, attr
);
97 /* Run twice to ensure that the attributes do not get overwritten in the
99 for (int i
= 0; i
< 2; i
++)
101 RETURN_IF_FAIL (pthread_create
, &t
, NULL
, thr
, NULL
);
103 RETURN_IF_FAIL (pthread_join
, t
, &tret
);
106 pthread_mutex_lock (&m
);
108 pthread_mutex_unlock (&m
);
113 puts ("Thread failed");
118 /* Stay in sync for detached threads and get their status. */
121 pthread_mutex_lock (&m
);
124 pthread_mutex_unlock (&m
);
127 pthread_cond_wait (&c
, &m
);
128 pthread_mutex_unlock (&m
);
135 verify_detach_result (pthread_attr_t
*attr
)
139 RETURN_IF_FAIL (pthread_attr_getdetachstate
, attr
, &state
);
141 if (state
!= PTHREAD_CREATE_DETACHED
)
143 puts ("failed to set detach state");
151 do_detach_test (void)
156 RETURN_IF_FAIL (pthread_attr_init
, &attr
);
157 RETURN_IF_FAIL (pthread_attr_setdetachstate
, &attr
, PTHREAD_CREATE_DETACHED
);
159 RETURN_IF_FAIL (run_threads
, &attr
);
160 return detach_failed
;
164 verify_affinity_result (pthread_attr_t
*attr
)
168 RETURN_IF_FAIL (pthread_attr_getaffinity_np
, attr
, sizeof (cpuset
), &cpuset
);
169 if (!CPU_ISSET (0, &cpuset
))
171 puts ("failed to set cpu affinity");
179 do_affinity_test (void)
183 RETURN_IF_FAIL (pthread_attr_init
, &attr
);
185 /* Processor affinity. Like scheduling policy, this could fail if the user
186 does not have the necessary privileges. So we only spew a warning if
187 pthread_create fails with EPERM. A computer has at least one CPU. */
190 CPU_SET (0, &cpuset
);
191 RETURN_IF_FAIL (pthread_attr_setaffinity_np
, &attr
, sizeof (cpuset
), &cpuset
);
193 int ret
= run_threads (&attr
);
197 printf ("Skipping CPU Affinity test: %s\n", strerror (ret
));
207 verify_sched_result (pthread_attr_t
*attr
)
209 int inherited
, policy
;
210 struct sched_param param
;
212 RETURN_IF_FAIL (pthread_attr_getinheritsched
, attr
, &inherited
);
213 if (inherited
!= PTHREAD_EXPLICIT_SCHED
)
215 puts ("failed to set EXPLICIT_SCHED (%d != %d)");
219 RETURN_IF_FAIL (pthread_attr_getschedpolicy
, attr
, &policy
);
220 if (policy
!= SCHED_RR
)
222 printf ("failed to set SCHED_RR (%d != %d)\n", policy
, SCHED_RR
);
226 RETURN_IF_FAIL (pthread_attr_getschedparam
, attr
, ¶m
);
227 if (param
.sched_priority
!= 42)
229 printf ("failed to set sched_priority (%d != %d)\n",
230 param
.sched_priority
, 42);
242 RETURN_IF_FAIL (pthread_attr_init
, &attr
);
244 /* Scheduling policy. Note that we don't always test these since it's
245 possible that the user the tests run as don't have the appropriate
247 RETURN_IF_FAIL (pthread_attr_setinheritsched
, &attr
, PTHREAD_EXPLICIT_SCHED
);
248 RETURN_IF_FAIL (pthread_attr_setschedpolicy
, &attr
, SCHED_RR
);
250 struct sched_param param
;
251 param
.sched_priority
= 42;
252 RETURN_IF_FAIL (pthread_attr_setschedparam
, &attr
, ¶m
);
254 int ret
= run_threads (&attr
);
258 printf ("Skipping Scheduler Attributes test: %s\n", strerror (ret
));
268 verify_guardsize_result (pthread_attr_t
*attr
)
272 RETURN_IF_FAIL (pthread_attr_getguardsize
, attr
, &guard
);
274 if (guardsize
!= guard
)
276 printf ("failed to set guardsize (%zu, %zu)\n", guardsize
, guard
);
284 do_guardsize_test (void)
286 long int pagesize
= sysconf (_SC_PAGESIZE
);
291 printf ("sysconf failed: %s\n", strerror (errno
));
295 RETURN_IF_FAIL (pthread_getattr_default_np
, &attr
);
297 /* Increase default guardsize by a page. */
298 RETURN_IF_FAIL (pthread_attr_getguardsize
, &attr
, &guardsize
);
299 guardsize
+= pagesize
;
300 RETURN_IF_FAIL (pthread_attr_setguardsize
, &attr
, guardsize
);
301 RETURN_IF_FAIL (run_threads
, &attr
);
307 verify_stacksize_result (pthread_attr_t
*attr
)
311 RETURN_IF_FAIL (pthread_attr_getstacksize
, attr
, &stack
);
313 if (stacksize
!= stack
)
315 printf ("failed to set default stacksize (%zu, %zu)\n", stacksize
, stack
);
323 do_stacksize_test (void)
325 long int pagesize
= sysconf (_SC_PAGESIZE
);
330 printf ("sysconf failed: %s\n", strerror (errno
));
334 /* Perturb the size by a page so that we're not aligned on the 64K boundary.
335 pthread_create does this perturbation on x86 to avoid causing the 64k
336 aliasing conflict. We want to prevent pthread_create from doing that
337 since it is not consistent for all architectures. */
338 stacksize
+= pagesize
;
340 RETURN_IF_FAIL (pthread_attr_init
, &attr
);
342 /* Run twice to ensure that we don't give a false positive. */
343 RETURN_IF_FAIL (pthread_attr_setstacksize
, &attr
, stacksize
);
344 RETURN_IF_FAIL (run_threads
, &attr
);
346 RETURN_IF_FAIL (pthread_attr_setstacksize
, &attr
, stacksize
);
347 RETURN_IF_FAIL (run_threads
, &attr
);
351 /* We test each attribute separately because sched and affinity tests may need
352 additional user privileges that may not be available during the test run.
353 Each attribute test is a set of two functions, viz. a function to set the
354 default attribute (do_foo_test) and another to verify its result
355 (verify_foo_result). Each test spawns a thread and checks (1) if the
356 attribute values were applied correctly and (2) if the change in the default
361 puts ("stacksize test");
362 verify_result
= verify_stacksize_result
;
363 RETURN_IF_FAIL (do_stacksize_test
);
365 puts ("guardsize test");
366 verify_result
= verify_guardsize_result
;
367 RETURN_IF_FAIL (do_guardsize_test
);
370 verify_result
= verify_sched_result
;
371 RETURN_IF_FAIL (do_sched_test
);
373 puts ("affinity test");
374 verify_result
= verify_affinity_result
;
375 RETURN_IF_FAIL (do_affinity_test
);
377 puts ("detach test");
378 verify_result
= verify_detach_result
;
379 RETURN_IF_FAIL (do_detach_test
);
384 #define TEST_FUNCTION do_test ()
385 #include "../test-skeleton.c"