Home | History | Annotate | Download | only in pthread_getschedparam
      1 /*
      2 * Copyright (c) 2005, Bull S.A..  All rights reserved.
      3 * Created by: Sebastien Decugis
      4 
      5 * This program is free software; you can redistribute it and/or modify it
      6 * under the terms of version 2 of the GNU General Public License as
      7 * published by the Free Software Foundation.
      8 *
      9 * This program is distributed in the hope that it would be useful, but
     10 * WITHOUT ANY WARRANTY; without even the implied warranty of
     11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
     12 *
     13 * You should have received a copy of the GNU General Public License along
     14 * with this program; if not, write the Free Software Foundation, Inc.,
     15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
     16 *
     17 * This sample test aims to check the following assertion:
     18 *
     19 * The function gets the scheduling policy and parameter of
     20 * the specified thread. The priority value is the one last
     21 * set with pthread_setschedparam, pthread_setschedprio or
     22 * pthread_create
     23 *
     24 * The steps are:
     25 * -> create a new thread with a known scheduling policy & param.
     26 * -> check the created thread has the required policy & param.
     27 * -> change the policy with pthread_setschedparam & check the result.
     28 * -> change the param with pthread_setschedprio & check the result.
     29 *
     30 * The test fails if an inconsistency is detected.
     31 *
     32 */
     33 #include <pthread.h>
     34 #include <stdarg.h>
     35 #include <stdio.h>
     36 #include <stdlib.h>
     37 #include <string.h>
     38 #include <unistd.h>
     39 #include <sched.h>
     40 #include <errno.h>
     41 #include <posixtest.h>
     42 
     43 #define ERR_MSG(f, rc)  printf("Failed: line: %d func: %s rc: %s (%u)\n", \
     44 				__LINE__, f, strerror(rc), rc)
     45 
     46 static void check_param(pthread_t thread, int policy, int priority)
     47 {
     48 	int ret;
     49 	int t_pol;
     50 	struct sched_param t_parm;
     51 
     52 	ret = pthread_getschedparam(thread, &t_pol, &t_parm);
     53 	if (ret) {
     54 		ERR_MSG("pthread_getscheparam()", ret);
     55 		exit(PTS_UNRESOLVED);
     56 	}
     57 
     58 	if (t_pol != policy) {
     59 		printf("Failed: polices: %u != %u\n", t_pol, policy);
     60 		exit(PTS_FAIL);
     61 	}
     62 
     63 	if (t_parm.sched_priority != priority) {
     64 		printf("Failed: priorities: %u != %u\n", t_pol, policy);
     65 		exit(PTS_FAIL);
     66 	}
     67 }
     68 
     69 /* thread function */
     70 static void *threaded(void *arg)
     71 {
     72 	int ret;
     73 	pthread_t self = pthread_self();
     74 
     75 	check_param(self, SCHED_RR, sched_get_priority_min(SCHED_RR));
     76 
     77 	ret = pthread_barrier_wait(arg);
     78 	if (ret && (ret != PTHREAD_BARRIER_SERIAL_THREAD)) {
     79 		ERR_MSG("pthread_barrier_wait()", ret);
     80 		exit(PTS_UNRESOLVED);
     81 	}
     82 
     83 	ret = pthread_barrier_wait(arg);
     84 	if (ret && (ret != PTHREAD_BARRIER_SERIAL_THREAD)) {
     85 		ERR_MSG("pthread_barrier_wait()", ret);
     86 		exit(PTS_UNRESOLVED);
     87 	}
     88 
     89 	check_param(self, SCHED_FIFO, sched_get_priority_min(SCHED_FIFO));
     90 
     91 	ret = pthread_barrier_wait(arg);
     92 	if (ret && (ret != PTHREAD_BARRIER_SERIAL_THREAD)) {
     93 		ERR_MSG("pthread_barrier_wait()", ret);
     94 		exit(PTS_UNRESOLVED);
     95 	}
     96 
     97 	ret = pthread_barrier_wait(arg);
     98 	if (ret && (ret != PTHREAD_BARRIER_SERIAL_THREAD)) {
     99 		ERR_MSG("pthread_barrier_wait()", ret);
    100 		exit(PTS_UNRESOLVED);
    101 	}
    102 
    103 	check_param(self, SCHED_FIFO, sched_get_priority_max(SCHED_FIFO));
    104 
    105 	ret = pthread_barrier_wait(arg);
    106 	if (ret && (ret != PTHREAD_BARRIER_SERIAL_THREAD)) {
    107 		ERR_MSG("pthread_barrier_wait()", ret);
    108 		exit(PTS_UNRESOLVED);
    109 	}
    110 
    111 	return NULL;
    112 }
    113 
    114 /* The main test function. */
    115 int main(void)
    116 {
    117 	int ret;
    118 	pthread_t child;
    119 	pthread_attr_t ta;
    120 	pthread_barrier_t bar;
    121 	struct sched_param sp;
    122 
    123 	ret = pthread_barrier_init(&bar, NULL, 2);
    124 	if (ret) {
    125 		ERR_MSG("pthread_barrier_init()", ret);
    126 		exit(PTS_UNRESOLVED);
    127 	}
    128 
    129 	ret = pthread_attr_init(&ta);
    130 	if (ret) {
    131 		ERR_MSG("pthread_attr_init()", ret);
    132 		exit(PTS_UNRESOLVED);
    133 	}
    134 
    135 	ret = pthread_attr_setinheritsched(&ta, PTHREAD_EXPLICIT_SCHED);
    136 	if (ret) {
    137 		ERR_MSG("pthread_attr_setinheritsched()", ret);
    138 		exit(PTS_UNRESOLVED);
    139 	}
    140 
    141 	ret = pthread_attr_setschedpolicy(&ta, SCHED_RR);
    142 	if (ret) {
    143 		ERR_MSG("pthread_attr_setschedpolicy()", ret);
    144 		exit(PTS_UNRESOLVED);
    145 	}
    146 
    147 	sp.sched_priority = sched_get_priority_min(SCHED_RR);
    148 	if (sp.sched_priority == -1) {
    149 		ERR_MSG("sched_get_priority_min()", errno);
    150 		exit(PTS_UNRESOLVED);
    151 	}
    152 
    153 	ret = pthread_attr_setschedparam(&ta, &sp);
    154 	if (ret) {
    155 		ERR_MSG("pthread_attr_setschedparam()", ret);
    156 		exit(PTS_UNRESOLVED);
    157 	}
    158 
    159 	ret = pthread_create(&child, &ta, threaded, &bar);
    160 	if (ret) {
    161 		ERR_MSG("pthread_create()", ret);
    162 		exit(PTS_UNRESOLVED);
    163 	}
    164 
    165 	check_param(child, SCHED_RR, sp.sched_priority);
    166 
    167 	ret = pthread_barrier_wait(&bar);
    168 	if (ret && (ret != PTHREAD_BARRIER_SERIAL_THREAD)) {
    169 		ERR_MSG("pthread_barrier_wait()", ret);
    170 		exit(PTS_UNRESOLVED);
    171 	}
    172 
    173 	sp.sched_priority = sched_get_priority_min(SCHED_FIFO);
    174 	if (sp.sched_priority == -1) {
    175 		ERR_MSG("pthread_barrier_wait()", errno);
    176 		exit(PTS_UNRESOLVED);
    177 	}
    178 
    179 	ret = pthread_setschedparam(child, SCHED_FIFO, &sp);
    180 	if (ret) {
    181 		ERR_MSG("pthread_setschedparam()", ret);
    182 		exit(PTS_UNRESOLVED);
    183 	}
    184 
    185 	ret = pthread_barrier_wait(&bar);
    186 	if (ret && (ret != PTHREAD_BARRIER_SERIAL_THREAD)) {
    187 		ERR_MSG("pthread_barrier_wait()", ret);
    188 		exit(PTS_UNRESOLVED);
    189 	}
    190 
    191 	check_param(child, SCHED_FIFO, sp.sched_priority);
    192 
    193 	ret = pthread_barrier_wait(&bar);
    194 	if (ret && (ret != PTHREAD_BARRIER_SERIAL_THREAD)) {
    195 		ERR_MSG("pthread_barrier_wait()", ret);
    196 		exit(PTS_UNRESOLVED);
    197 	}
    198 
    199 	sp.sched_priority = sched_get_priority_max(SCHED_FIFO);
    200 	if (sp.sched_priority == -1) {
    201 		ERR_MSG("sched_get_priority_max()", errno);
    202 		exit(PTS_UNRESOLVED);
    203 	}
    204 
    205 	ret = pthread_setschedprio(child, sp.sched_priority);
    206 	if (ret != 0) {
    207 		ERR_MSG("pthread_setschedprio()", ret);
    208 		exit(PTS_UNRESOLVED);
    209 	}
    210 
    211 	ret = pthread_barrier_wait(&bar);
    212 	if (ret && (ret != PTHREAD_BARRIER_SERIAL_THREAD)) {
    213 		ERR_MSG("pthread_barrier_wait()", ret);
    214 		exit(PTS_UNRESOLVED);
    215 	}
    216 
    217 	check_param(child, SCHED_FIFO, sched_get_priority_max(SCHED_FIFO));
    218 
    219 	ret = pthread_barrier_wait(&bar);
    220 	if (ret && (ret != PTHREAD_BARRIER_SERIAL_THREAD)) {
    221 		ERR_MSG("pthread_barrier_wait()", ret);
    222 		exit(PTS_UNRESOLVED);
    223 	}
    224 
    225 	pthread_join(child, NULL);
    226 
    227 	printf("Test PASSED\n");
    228 
    229 	return PTS_PASS;
    230 }
    231