10dc076565f772bb1953209fb69ea150b494aaa40robbiew/*
20dc076565f772bb1953209fb69ea150b494aaa40robbiew* Copyright (c) 2005, Bull S.A..  All rights reserved.
30dc076565f772bb1953209fb69ea150b494aaa40robbiew* Created by: Sebastien Decugis
40dc076565f772bb1953209fb69ea150b494aaa40robbiew
50dc076565f772bb1953209fb69ea150b494aaa40robbiew* This program is free software; you can redistribute it and/or modify it
60dc076565f772bb1953209fb69ea150b494aaa40robbiew* under the terms of version 2 of the GNU General Public License as
70dc076565f772bb1953209fb69ea150b494aaa40robbiew* published by the Free Software Foundation.
80dc076565f772bb1953209fb69ea150b494aaa40robbiew*
90dc076565f772bb1953209fb69ea150b494aaa40robbiew* This program is distributed in the hope that it would be useful, but
100dc076565f772bb1953209fb69ea150b494aaa40robbiew* WITHOUT ANY WARRANTY; without even the implied warranty of
110dc076565f772bb1953209fb69ea150b494aaa40robbiew* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
120dc076565f772bb1953209fb69ea150b494aaa40robbiew*
130dc076565f772bb1953209fb69ea150b494aaa40robbiew* You should have received a copy of the GNU General Public License along
14fed9641096e27f79a0f2d9adfe9839dd8d11dc0fWanlong Gao* with this program; if not, write the Free Software Foundation, Inc.,
15fed9641096e27f79a0f2d9adfe9839dd8d11dc0fWanlong Gao* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
160dc076565f772bb1953209fb69ea150b494aaa40robbiew
170dc076565f772bb1953209fb69ea150b494aaa40robbiew* This stress test aims to test the following assertion:
180dc076565f772bb1953209fb69ea150b494aaa40robbiew
190dc076565f772bb1953209fb69ea150b494aaa40robbiew*  pthread_getschedparam() always returns the scheduling parameters of
200dc076565f772bb1953209fb69ea150b494aaa40robbiew* the queried thread.
210dc076565f772bb1953209fb69ea150b494aaa40robbiew
220dc076565f772bb1953209fb69ea150b494aaa40robbiew* The steps are:
230dc076565f772bb1953209fb69ea150b494aaa40robbiew* -> Create several threads with different scheduling parameters.
240dc076565f772bb1953209fb69ea150b494aaa40robbiew* -> create more threads which call continuously the routine, and check
250dc076565f772bb1953209fb69ea150b494aaa40robbiew* -> that the correct parameters are always returned.
260dc076565f772bb1953209fb69ea150b494aaa40robbiew
270dc076565f772bb1953209fb69ea150b494aaa40robbiew*/
280dc076565f772bb1953209fb69ea150b494aaa40robbiew
290dc076565f772bb1953209fb69ea150b494aaa40robbiew/* We are testing conformance to IEEE Std 1003.1, 2003 Edition */
300dc076565f772bb1953209fb69ea150b494aaa40robbiew#define _POSIX_C_SOURCE 200112L
310dc076565f772bb1953209fb69ea150b494aaa40robbiew
320dc076565f772bb1953209fb69ea150b494aaa40robbiew/********************************************************************************************/
330dc076565f772bb1953209fb69ea150b494aaa40robbiew/****************************** standard includes *****************************************/
340dc076565f772bb1953209fb69ea150b494aaa40robbiew/********************************************************************************************/
350dc076565f772bb1953209fb69ea150b494aaa40robbiew#include <pthread.h>
360dc076565f772bb1953209fb69ea150b494aaa40robbiew#include <stdarg.h>
370dc076565f772bb1953209fb69ea150b494aaa40robbiew#include <stdio.h>
380dc076565f772bb1953209fb69ea150b494aaa40robbiew#include <stdlib.h>
390dc076565f772bb1953209fb69ea150b494aaa40robbiew#include <string.h>
400dc076565f772bb1953209fb69ea150b494aaa40robbiew#include <unistd.h>
410dc076565f772bb1953209fb69ea150b494aaa40robbiew
420dc076565f772bb1953209fb69ea150b494aaa40robbiew#include <errno.h>
430dc076565f772bb1953209fb69ea150b494aaa40robbiew#include <signal.h>
440dc076565f772bb1953209fb69ea150b494aaa40robbiew#include <sched.h>
450dc076565f772bb1953209fb69ea150b494aaa40robbiew
460dc076565f772bb1953209fb69ea150b494aaa40robbiew/********************************************************************************************/
470dc076565f772bb1953209fb69ea150b494aaa40robbiew/******************************   Test framework   *****************************************/
480dc076565f772bb1953209fb69ea150b494aaa40robbiew/********************************************************************************************/
490dc076565f772bb1953209fb69ea150b494aaa40robbiew#include "testfrmw.h"
50354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao#include "testfrmw.c"
510dc076565f772bb1953209fb69ea150b494aaa40robbiew/* This header is responsible for defining the following macros:
522c28215423293e443469a07ae7011135d058b671Garrett Cooper * UNRESOLVED(ret, descr);
530dc076565f772bb1953209fb69ea150b494aaa40robbiew *    where descr is a description of the error and ret is an int (error code for example)
540dc076565f772bb1953209fb69ea150b494aaa40robbiew * FAILED(descr);
550dc076565f772bb1953209fb69ea150b494aaa40robbiew *    where descr is a short text saying why the test has failed.
560dc076565f772bb1953209fb69ea150b494aaa40robbiew * PASSED();
570dc076565f772bb1953209fb69ea150b494aaa40robbiew *    No parameter.
582c28215423293e443469a07ae7011135d058b671Garrett Cooper *
590dc076565f772bb1953209fb69ea150b494aaa40robbiew * Both three macros shall terminate the calling process.
600dc076565f772bb1953209fb69ea150b494aaa40robbiew * The testcase shall not terminate in any other maneer.
612c28215423293e443469a07ae7011135d058b671Garrett Cooper *
620dc076565f772bb1953209fb69ea150b494aaa40robbiew * The other file defines the functions
630dc076565f772bb1953209fb69ea150b494aaa40robbiew * void output_init()
640dc076565f772bb1953209fb69ea150b494aaa40robbiew * void output(char * string, ...)
652c28215423293e443469a07ae7011135d058b671Garrett Cooper *
660dc076565f772bb1953209fb69ea150b494aaa40robbiew * Those may be used to output information.
670dc076565f772bb1953209fb69ea150b494aaa40robbiew */
680dc076565f772bb1953209fb69ea150b494aaa40robbiew
690dc076565f772bb1953209fb69ea150b494aaa40robbiew/********************************************************************************************/
700dc076565f772bb1953209fb69ea150b494aaa40robbiew/********************************** Configuration ******************************************/
710dc076565f772bb1953209fb69ea150b494aaa40robbiew/********************************************************************************************/
720dc076565f772bb1953209fb69ea150b494aaa40robbiew#ifndef VERBOSE
730dc076565f772bb1953209fb69ea150b494aaa40robbiew#define VERBOSE 1
740dc076565f772bb1953209fb69ea150b494aaa40robbiew#endif
750dc076565f772bb1953209fb69ea150b494aaa40robbiew
760dc076565f772bb1953209fb69ea150b494aaa40robbiew#define NTHREADS 30
770dc076565f772bb1953209fb69ea150b494aaa40robbiew
780dc076565f772bb1953209fb69ea150b494aaa40robbiew/********************************************************************************************/
790dc076565f772bb1953209fb69ea150b494aaa40robbiew/***********************************    Test cases  *****************************************/
800dc076565f772bb1953209fb69ea150b494aaa40robbiew/********************************************************************************************/
810dc076565f772bb1953209fb69ea150b494aaa40robbiew
820dc076565f772bb1953209fb69ea150b494aaa40robbiewchar do_it = 1;
830dc076565f772bb1953209fb69ea150b494aaa40robbiewlong long iterations = 0;
840dc076565f772bb1953209fb69ea150b494aaa40robbiew
850dc076565f772bb1953209fb69ea150b494aaa40robbiew/* Handler for user request to terminate */
86e9410dfd93b8e415ecbe3f7e09a085462b27836eGarrett Coopervoid sighdl(int sig)
870dc076565f772bb1953209fb69ea150b494aaa40robbiew{
88354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	do {
890dc076565f772bb1953209fb69ea150b494aaa40robbiew		do_it = 0;
900dc076565f772bb1953209fb69ea150b494aaa40robbiew	}
91e9410dfd93b8e415ecbe3f7e09a085462b27836eGarrett Cooper	while (do_it);
920dc076565f772bb1953209fb69ea150b494aaa40robbiew}
930dc076565f772bb1953209fb69ea150b494aaa40robbiew
94354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gaotypedef struct _tdata {
950dc076565f772bb1953209fb69ea150b494aaa40robbiew	int policy;
960dc076565f772bb1953209fb69ea150b494aaa40robbiew	int prio;
970dc076565f772bb1953209fb69ea150b494aaa40robbiew	pthread_t thread;
98354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao} testdata_t;
990dc076565f772bb1953209fb69ea150b494aaa40robbiew
100354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gaotestdata_t td[4];
1010dc076565f772bb1953209fb69ea150b494aaa40robbiew
1020dc076565f772bb1953209fb69ea150b494aaa40robbiew/* Thread function */
103354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gaovoid *threaded(void *arg)
1040dc076565f772bb1953209fb69ea150b494aaa40robbiew{
1050dc076565f772bb1953209fb69ea150b494aaa40robbiew	int ret = 0;
1060dc076565f772bb1953209fb69ea150b494aaa40robbiew	int i = 0;
1070dc076565f772bb1953209fb69ea150b494aaa40robbiew	int pol;
1080dc076565f772bb1953209fb69ea150b494aaa40robbiew
1090dc076565f772bb1953209fb69ea150b494aaa40robbiew	struct sched_param sp;
1100dc076565f772bb1953209fb69ea150b494aaa40robbiew
111354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	while (do_it) {
112354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		for (i = 0; i < 4; i++) {
113354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			ret = pthread_getschedparam(td[i].thread, &pol, &sp);
1140dc076565f772bb1953209fb69ea150b494aaa40robbiew
115354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			if (ret != 0) {
116e9410dfd93b8e415ecbe3f7e09a085462b27836eGarrett Cooper				UNRESOLVED(ret, "Failed to get sched param");
1170dc076565f772bb1953209fb69ea150b494aaa40robbiew			}
1180dc076565f772bb1953209fb69ea150b494aaa40robbiew
119354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			if (pol != td[i].policy) {
120e9410dfd93b8e415ecbe3f7e09a085462b27836eGarrett Cooper				FAILED("Wrong scheduling policy read");
1210dc076565f772bb1953209fb69ea150b494aaa40robbiew			}
1220dc076565f772bb1953209fb69ea150b494aaa40robbiew
123354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			if (sp.sched_priority != td[i].prio) {
124e9410dfd93b8e415ecbe3f7e09a085462b27836eGarrett Cooper				FAILED("Wrong scheduling priority read");
1250dc076565f772bb1953209fb69ea150b494aaa40robbiew			}
1260dc076565f772bb1953209fb69ea150b494aaa40robbiew
1270dc076565f772bb1953209fb69ea150b494aaa40robbiew		}
1280dc076565f772bb1953209fb69ea150b494aaa40robbiew
1290dc076565f772bb1953209fb69ea150b494aaa40robbiew		/* We don't really care about concurrent access for this data */
1300dc076565f772bb1953209fb69ea150b494aaa40robbiew		iterations++;
1310dc076565f772bb1953209fb69ea150b494aaa40robbiew	}
1320dc076565f772bb1953209fb69ea150b494aaa40robbiew
1330dc076565f772bb1953209fb69ea150b494aaa40robbiew	return NULL;
1340dc076565f772bb1953209fb69ea150b494aaa40robbiew}
1350dc076565f772bb1953209fb69ea150b494aaa40robbiew
1360dc076565f772bb1953209fb69ea150b494aaa40robbiew/* alternative policy threads */
137354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gaovoid *rt_thread(void *arg)
1380dc076565f772bb1953209fb69ea150b494aaa40robbiew{
1390dc076565f772bb1953209fb69ea150b494aaa40robbiew	int ret = 0;
1400dc076565f772bb1953209fb69ea150b494aaa40robbiew
1410dc076565f772bb1953209fb69ea150b494aaa40robbiew	/* This thread does almost nothing but wait... */
142e9410dfd93b8e415ecbe3f7e09a085462b27836eGarrett Cooper	ret = pthread_barrier_wait(arg);
1430dc076565f772bb1953209fb69ea150b494aaa40robbiew
144354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	if ((ret != 0) && (ret != PTHREAD_BARRIER_SERIAL_THREAD)) {
145e9410dfd93b8e415ecbe3f7e09a085462b27836eGarrett Cooper		UNRESOLVED(ret, "Failed to wait for barrier");
1460dc076565f772bb1953209fb69ea150b494aaa40robbiew	}
1470dc076565f772bb1953209fb69ea150b494aaa40robbiew
1480dc076565f772bb1953209fb69ea150b494aaa40robbiew	return NULL;
1490dc076565f772bb1953209fb69ea150b494aaa40robbiew}
1500dc076565f772bb1953209fb69ea150b494aaa40robbiew
1510dc076565f772bb1953209fb69ea150b494aaa40robbiew/* Main function */
152354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gaoint main(int argc, char *argv[])
1530dc076565f772bb1953209fb69ea150b494aaa40robbiew{
1540dc076565f772bb1953209fb69ea150b494aaa40robbiew	int ret = 0, i;
1550dc076565f772bb1953209fb69ea150b494aaa40robbiew
1560dc076565f772bb1953209fb69ea150b494aaa40robbiew	struct sigaction sa;
1570dc076565f772bb1953209fb69ea150b494aaa40robbiew
1580dc076565f772bb1953209fb69ea150b494aaa40robbiew	pthread_barrier_t bar;
1590dc076565f772bb1953209fb69ea150b494aaa40robbiew
160354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	pthread_attr_t ta[4];
1610dc076565f772bb1953209fb69ea150b494aaa40robbiew
162354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	pthread_t th[NTHREADS];
1630dc076565f772bb1953209fb69ea150b494aaa40robbiew
1640dc076565f772bb1953209fb69ea150b494aaa40robbiew	struct sched_param sp;
1650dc076565f772bb1953209fb69ea150b494aaa40robbiew
1660dc076565f772bb1953209fb69ea150b494aaa40robbiew	/* Initialize output routine */
1670dc076565f772bb1953209fb69ea150b494aaa40robbiew	output_init();
1680dc076565f772bb1953209fb69ea150b494aaa40robbiew
1690dc076565f772bb1953209fb69ea150b494aaa40robbiew	/* Initialize barrier */
170e9410dfd93b8e415ecbe3f7e09a085462b27836eGarrett Cooper	ret = pthread_barrier_init(&bar, NULL, 5);
1710dc076565f772bb1953209fb69ea150b494aaa40robbiew
172354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	if (ret != 0) {
173e9410dfd93b8e415ecbe3f7e09a085462b27836eGarrett Cooper		UNRESOLVED(ret, "Failed to init barrier");
1740dc076565f772bb1953209fb69ea150b494aaa40robbiew	}
1750dc076565f772bb1953209fb69ea150b494aaa40robbiew
1760dc076565f772bb1953209fb69ea150b494aaa40robbiew	/* Register the signal handler for SIGUSR1 */
177354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	sigemptyset(&sa.sa_mask);
1780dc076565f772bb1953209fb69ea150b494aaa40robbiew
1790dc076565f772bb1953209fb69ea150b494aaa40robbiew	sa.sa_flags = 0;
1800dc076565f772bb1953209fb69ea150b494aaa40robbiew
1810dc076565f772bb1953209fb69ea150b494aaa40robbiew	sa.sa_handler = sighdl;
1820dc076565f772bb1953209fb69ea150b494aaa40robbiew
183354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	if ((ret = sigaction(SIGUSR1, &sa, NULL))) {
184e9410dfd93b8e415ecbe3f7e09a085462b27836eGarrett Cooper		UNRESOLVED(ret, "Unable to register signal handler");
1850dc076565f772bb1953209fb69ea150b494aaa40robbiew	}
1860dc076565f772bb1953209fb69ea150b494aaa40robbiew
187354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	if ((ret = sigaction(SIGALRM, &sa, NULL))) {
188e9410dfd93b8e415ecbe3f7e09a085462b27836eGarrett Cooper		UNRESOLVED(ret, "Unable to register signal handler");
1890dc076565f772bb1953209fb69ea150b494aaa40robbiew	}
1900dc076565f772bb1953209fb69ea150b494aaa40robbiew#if VERBOSE > 1
191e9410dfd93b8e415ecbe3f7e09a085462b27836eGarrett Cooper	output("[parent] Signal handler registered\n");
1920dc076565f772bb1953209fb69ea150b494aaa40robbiew
1930dc076565f772bb1953209fb69ea150b494aaa40robbiew#endif
1940dc076565f772bb1953209fb69ea150b494aaa40robbiew
195354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	td[0].policy = td[1].policy = SCHED_FIFO;
1960dc076565f772bb1953209fb69ea150b494aaa40robbiew
197354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	td[2].policy = td[3].policy = SCHED_RR;
1980dc076565f772bb1953209fb69ea150b494aaa40robbiew
199354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	td[0].prio = sched_get_priority_min(SCHED_FIFO);
2000dc076565f772bb1953209fb69ea150b494aaa40robbiew
201354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	if (td[0].prio == -1) {
202e9410dfd93b8e415ecbe3f7e09a085462b27836eGarrett Cooper		UNRESOLVED(errno, "Failed to get scheduler range value");
2030dc076565f772bb1953209fb69ea150b494aaa40robbiew	}
2040dc076565f772bb1953209fb69ea150b494aaa40robbiew
205354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	td[1].prio = sched_get_priority_max(SCHED_FIFO);
2060dc076565f772bb1953209fb69ea150b494aaa40robbiew
207354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	if (td[1].prio == -1) {
208e9410dfd93b8e415ecbe3f7e09a085462b27836eGarrett Cooper		UNRESOLVED(errno, "Failed to get scheduler range value");
2090dc076565f772bb1953209fb69ea150b494aaa40robbiew	}
2100dc076565f772bb1953209fb69ea150b494aaa40robbiew
211354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	td[2].prio = sched_get_priority_min(SCHED_RR);
2120dc076565f772bb1953209fb69ea150b494aaa40robbiew
213354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	if (td[2].prio == -1) {
214e9410dfd93b8e415ecbe3f7e09a085462b27836eGarrett Cooper		UNRESOLVED(errno, "Failed to get scheduler range value");
2150dc076565f772bb1953209fb69ea150b494aaa40robbiew	}
2160dc076565f772bb1953209fb69ea150b494aaa40robbiew
217354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	td[3].prio = sched_get_priority_max(SCHED_RR);
2180dc076565f772bb1953209fb69ea150b494aaa40robbiew
219354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	if (td[3].prio == -1) {
220e9410dfd93b8e415ecbe3f7e09a085462b27836eGarrett Cooper		UNRESOLVED(errno, "Failed to get scheduler range value");
2210dc076565f772bb1953209fb69ea150b494aaa40robbiew	}
2220dc076565f772bb1953209fb69ea150b494aaa40robbiew
2230dc076565f772bb1953209fb69ea150b494aaa40robbiew	/* Initialize the threads attributes and create the RT threads */
224354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	for (i = 0; i < 4; i++) {
225354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		ret = pthread_attr_init(&ta[i]);
2260dc076565f772bb1953209fb69ea150b494aaa40robbiew
227354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		if (ret != 0) {
228354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			UNRESOLVED(ret,
229354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				   "Failed to initialize thread attribute");
2300dc076565f772bb1953209fb69ea150b494aaa40robbiew		}
2310dc076565f772bb1953209fb69ea150b494aaa40robbiew
232354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		ret =
233354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		    pthread_attr_setinheritsched(&ta[i],
234354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao						 PTHREAD_EXPLICIT_SCHED);
2350dc076565f772bb1953209fb69ea150b494aaa40robbiew
236354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		if (ret != 0) {
237354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			UNRESOLVED(ret,
238354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				   "Failed to set explicit scheduling attribute");
2390dc076565f772bb1953209fb69ea150b494aaa40robbiew		}
2400dc076565f772bb1953209fb69ea150b494aaa40robbiew
241354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		sp.sched_priority = td[i].prio;
2420dc076565f772bb1953209fb69ea150b494aaa40robbiew
243354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		ret = pthread_attr_setschedparam(&ta[i], &sp);
2440dc076565f772bb1953209fb69ea150b494aaa40robbiew
245354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		if (ret != 0) {
246354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			UNRESOLVED(ret,
247354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				   "failed to set thread attribute sched param");
2480dc076565f772bb1953209fb69ea150b494aaa40robbiew		}
2490dc076565f772bb1953209fb69ea150b494aaa40robbiew
250354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		ret = pthread_attr_setschedpolicy(&ta[i], td[i].policy);
2510dc076565f772bb1953209fb69ea150b494aaa40robbiew
252354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		if (ret != 0) {
253354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			UNRESOLVED(ret,
254354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				   "failed to set thread attribute sched prio");
2550dc076565f772bb1953209fb69ea150b494aaa40robbiew		}
2560dc076565f772bb1953209fb69ea150b494aaa40robbiew
257354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		ret = pthread_create(&td[i].thread, &ta[i], rt_thread, &bar);
2580dc076565f772bb1953209fb69ea150b494aaa40robbiew
259354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		if (ret != 0) {
260354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			UNRESOLVED(ret,
261354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				   "Failed to create a RT thread -- need more privilege?");
2620dc076565f772bb1953209fb69ea150b494aaa40robbiew		}
2630dc076565f772bb1953209fb69ea150b494aaa40robbiew
2640dc076565f772bb1953209fb69ea150b494aaa40robbiew	}
2650dc076565f772bb1953209fb69ea150b494aaa40robbiew
2660dc076565f772bb1953209fb69ea150b494aaa40robbiew	/* Create the worker threads */
267354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	for (i = 0; i < NTHREADS; i++) {
268354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		ret = pthread_create(&th[i], NULL, threaded, NULL);
2690dc076565f772bb1953209fb69ea150b494aaa40robbiew
270354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		if (ret != 0) {
271e9410dfd93b8e415ecbe3f7e09a085462b27836eGarrett Cooper			UNRESOLVED(ret, "failed to create a worker thread");
2720dc076565f772bb1953209fb69ea150b494aaa40robbiew		}
2730dc076565f772bb1953209fb69ea150b494aaa40robbiew	}
2740dc076565f772bb1953209fb69ea150b494aaa40robbiew
2750dc076565f772bb1953209fb69ea150b494aaa40robbiew	/* Wait for the worker threads to finish */
276354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	for (i = 0; i < NTHREADS; i++) {
277354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		ret = pthread_join(th[i], NULL);
2780dc076565f772bb1953209fb69ea150b494aaa40robbiew
279354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		if (ret != 0) {
280e9410dfd93b8e415ecbe3f7e09a085462b27836eGarrett Cooper			UNRESOLVED(ret, "failed to join a worker thread");
2810dc076565f772bb1953209fb69ea150b494aaa40robbiew		}
2820dc076565f772bb1953209fb69ea150b494aaa40robbiew	}
2830dc076565f772bb1953209fb69ea150b494aaa40robbiew
2840dc076565f772bb1953209fb69ea150b494aaa40robbiew	/* Join the barrier to terminate the RT threads */
285e9410dfd93b8e415ecbe3f7e09a085462b27836eGarrett Cooper	ret = pthread_barrier_wait(&bar);
2860dc076565f772bb1953209fb69ea150b494aaa40robbiew
287354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	if ((ret != 0) && (ret != PTHREAD_BARRIER_SERIAL_THREAD)) {
288e9410dfd93b8e415ecbe3f7e09a085462b27836eGarrett Cooper		UNRESOLVED(ret, "Failed to wait for the barrier");
2890dc076565f772bb1953209fb69ea150b494aaa40robbiew	}
2900dc076565f772bb1953209fb69ea150b494aaa40robbiew
2910dc076565f772bb1953209fb69ea150b494aaa40robbiew	/* Join the RT threads */
292354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	for (i = 0; i < 4; i++) {
293354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		ret = pthread_join(td[i].thread, NULL);
2940dc076565f772bb1953209fb69ea150b494aaa40robbiew
295354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		if (ret != 0) {
296e9410dfd93b8e415ecbe3f7e09a085462b27836eGarrett Cooper			UNRESOLVED(ret, "Failed to join a thread");
2970dc076565f772bb1953209fb69ea150b494aaa40robbiew		}
2980dc076565f772bb1953209fb69ea150b494aaa40robbiew	}
2990dc076565f772bb1953209fb69ea150b494aaa40robbiew
3000dc076565f772bb1953209fb69ea150b494aaa40robbiew	/* Done! */
301354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	output("pthread_getschedparam stress test PASSED -- %llu iterations\n",
302354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	       iterations);
3030dc076565f772bb1953209fb69ea150b494aaa40robbiew
304e9410dfd93b8e415ecbe3f7e09a085462b27836eGarrett Cooper	ret = pthread_barrier_destroy(&bar);
3050dc076565f772bb1953209fb69ea150b494aaa40robbiew
306354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	if (ret != 0) {
307e9410dfd93b8e415ecbe3f7e09a085462b27836eGarrett Cooper		UNRESOLVED(ret, "Failed to destroy the barrier");
3080dc076565f772bb1953209fb69ea150b494aaa40robbiew	}
3090dc076565f772bb1953209fb69ea150b494aaa40robbiew
3100dc076565f772bb1953209fb69ea150b494aaa40robbiew	PASSED;
311ec6edca7aa42b6affd989ef91b5897f96795e40fChris Dearman}
312