Fix warning from latest GCC in tst-printf.c
[glibc.git] / nptl / tst-bad-schedattr.c
blob422697d8d3a5825a2a9de5ca0d957942dd37b06c
1 /* Test that pthread_create diagnoses invalid scheduling parameters.
2 Copyright (C) 2014-2016 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 <http://www.gnu.org/licenses/>. */
19 #include <assert.h>
20 #include <errno.h>
21 #include <pthread.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
27 static void *
28 thread_function (void *arg)
30 abort ();
34 static int
35 do_test (void)
37 #if !defined SCHED_FIFO || !defined SCHED_OTHER
38 puts ("SCHED_FIFO or SCHED_OTHER not available at compile time");
39 return 0; /* 77 */
40 #else
42 int err;
44 #define TRY(func, arglist) \
45 if ((err = func arglist) != 0) \
46 { \
47 printf ("%s: %s\n", #func, strerror (err)); \
48 return 2; \
51 int fifo_max = sched_get_priority_max (SCHED_FIFO);
52 if (fifo_max == -1)
54 assert (errno == ENOTSUP || errno == ENOSYS);
55 puts ("SCHED_FIFO not supported, cannot test");
56 return 0; /* 77 */
59 int other_max = sched_get_priority_max (SCHED_OTHER);
60 if (other_max == -1)
62 assert (errno == ENOTSUP || errno == ENOSYS);
63 puts ("SCHED_OTHER not supported, cannot test");
64 return 0; /* 77 */
67 assert (fifo_max > other_max);
69 pthread_attr_t attr;
70 TRY (pthread_attr_init, (&attr));
71 TRY (pthread_attr_setinheritsched, (&attr, PTHREAD_EXPLICIT_SCHED));
72 TRY (pthread_attr_setschedpolicy, (&attr, SCHED_FIFO));
74 /* This value is chosen so as to be valid for SCHED_FIFO but invalid for
75 SCHED_OTHER. */
76 struct sched_param param = { .sched_priority = other_max + 1 };
77 TRY (pthread_attr_setschedparam, (&attr, &param));
79 TRY (pthread_attr_setschedpolicy, (&attr, SCHED_OTHER));
81 /* Now ATTR has a sched_param that is invalid for its policy. */
82 pthread_t th;
83 err = pthread_create (&th, &attr, &thread_function, NULL);
84 if (err != EINVAL)
86 printf ("pthread_create returned %d (%s), expected %d (EINVAL: %s)\n",
87 err, strerror (err), EINVAL, strerror (EINVAL));
88 return 1;
91 return 0;
92 #endif
96 #define TEST_FUNCTION do_test ()
97 #include "../test-skeleton.c"