| xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 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. | 
|  | 4 |  | 
|  | 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. | 
|  | 9 |  | 
|  | 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. | 
|  | 14 |  | 
|  | 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/>.  */ | 
|  | 18 |  | 
|  | 19 | #include <assert.h> | 
|  | 20 | #include <errno.h> | 
|  | 21 | #include <pthread.h> | 
|  | 22 | #include <stdio.h> | 
|  | 23 | #include <stdlib.h> | 
|  | 24 | #include <string.h> | 
|  | 25 |  | 
|  | 26 |  | 
|  | 27 | static void * | 
|  | 28 | thread_function (void *arg) | 
|  | 29 | { | 
|  | 30 | abort (); | 
|  | 31 | } | 
|  | 32 |  | 
|  | 33 |  | 
|  | 34 | static int | 
|  | 35 | do_test (void) | 
|  | 36 | { | 
|  | 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 | 
|  | 41 |  | 
|  | 42 | int err; | 
|  | 43 |  | 
|  | 44 | #define TRY(func, arglist)                              \ | 
|  | 45 | if ((err = func arglist) != 0)                        \ | 
|  | 46 | {                                                   \ | 
|  | 47 | printf ("%s: %s\n", #func, strerror (err));       \ | 
|  | 48 | return 2;                                         \ | 
|  | 49 | } | 
|  | 50 |  | 
|  | 51 | int fifo_max = sched_get_priority_max (SCHED_FIFO); | 
|  | 52 | if (fifo_max == -1) | 
|  | 53 | { | 
|  | 54 | assert (errno == ENOTSUP || errno == ENOSYS); | 
|  | 55 | puts ("SCHED_FIFO not supported, cannot test"); | 
|  | 56 | return 0; /* 77 */ | 
|  | 57 | } | 
|  | 58 |  | 
|  | 59 | int other_max = sched_get_priority_max (SCHED_OTHER); | 
|  | 60 | if (other_max == -1) | 
|  | 61 | { | 
|  | 62 | assert (errno == ENOTSUP || errno == ENOSYS); | 
|  | 63 | puts ("SCHED_OTHER not supported, cannot test"); | 
|  | 64 | return 0; /* 77 */ | 
|  | 65 | } | 
|  | 66 |  | 
|  | 67 | assert (fifo_max > other_max); | 
|  | 68 |  | 
|  | 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)); | 
|  | 73 |  | 
|  | 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, ¶m)); | 
|  | 78 |  | 
|  | 79 | TRY (pthread_attr_setschedpolicy, (&attr, SCHED_OTHER)); | 
|  | 80 |  | 
|  | 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) | 
|  | 85 | { | 
|  | 86 | printf ("pthread_create returned %d (%s), expected %d (EINVAL: %s)\n", | 
|  | 87 | err, strerror (err), EINVAL, strerror (EINVAL)); | 
|  | 88 | return 1; | 
|  | 89 | } | 
|  | 90 |  | 
|  | 91 | return 0; | 
|  | 92 | #endif | 
|  | 93 | } | 
|  | 94 |  | 
|  | 95 |  | 
|  | 96 | #define TEST_FUNCTION do_test () | 
|  | 97 | #include "../test-skeleton.c" |