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 sets the scheduling policy and parameter of the specified thread.
20
21* The steps are:
22* -> Create a new thread
23* -> create another thread which changes the scheduling policy of the 1st thread
24
25* The test fails if the 1st thread policy is not changed.
26*/
27
28/* We are testing conformance to IEEE Std 1003.1, 2003 Edition */
29#define _POSIX_C_SOURCE 200112L
30
31/******************************************************************************/
32/*********************** standard includes ************************************/
33/******************************************************************************/
34#include <pthread.h>
35#include <stdarg.h>
36#include <stdio.h>
37#include <stdlib.h>
38#include <string.h>
39#include <unistd.h>
40
41#include <sched.h>
42#include <errno.h>
43
44/******************************************************************************/
45/***********************   Test framework   ***********************************/
46/******************************************************************************/
47#include "../testfrmw/testfrmw.h"
48#include "../testfrmw/testfrmw.c"
49/* This header is responsible for defining the following macros:
50 * UNRESOLVED(ret, descr);
51 *    where descr is a description of the error and ret is an int
52 *   (error code for example)
53 * FAILED(descr);
54 *    where descr is a short text saying why the test has failed.
55 * PASSED();
56 *    No parameter.
57 *
58 * Both three macros shall terminate the calling process.
59 * The testcase shall not terminate in any other maneer.
60 *
61 * The other file defines the functions
62 * void output_init()
63 * void output(char * string, ...)
64 *
65 * Those may be used to output information.
66 */
67
68/******************************************************************************/
69/***************************** Configuration **********************************/
70/******************************************************************************/
71#ifndef VERBOSE
72#define VERBOSE 1
73#endif
74
75/******************************************************************************/
76/*****************************    Test case   *********************************/
77/******************************************************************************/
78
79/* This function checks the thread policy & priority */
80void check_param(pthread_t thread, int policy, int priority)
81{
82	int ret = 0;
83
84	int t_pol;
85
86	struct sched_param t_parm;
87
88	/* Check the priority is valid */
89
90	if (priority == -1) {
91		UNRESOLVED(errno, "Wrong priority value");
92	}
93
94	/* Get the thread's parameters */
95	ret = pthread_getschedparam(thread, &t_pol, &t_parm);
96
97	if (ret != 0) {
98		UNRESOLVED(ret, "Failed to get thread's parameters");
99	}
100
101	if (t_pol != policy) {
102		FAILED("The thread's policy is not as expected");
103	}
104
105	if (t_parm.sched_priority != priority) {
106		FAILED("The thread's priority is not as expected");
107	}
108}
109
110/* thread function 1 */
111void *controler(void *arg)
112{
113	int ret = 0;
114
115	/* Wait until the policy has been changed. */
116	ret = pthread_barrier_wait(arg);
117
118	if (ret != 0 && ret != PTHREAD_BARRIER_SERIAL_THREAD) {
119		UNRESOLVED(ret, "barrier wait failed");
120	}
121
122	/* check the thread attributes have been applied
123	   (we only check what is reported, not the real behavior)
124	 */
125	check_param(pthread_self(), SCHED_RR, sched_get_priority_min(SCHED_RR));
126
127	return NULL;
128}
129
130/* thread function 2 */
131void *changer(void *arg)
132{
133	int ret = 0;
134
135	struct sched_param sp;
136	sp.sched_priority = sched_get_priority_min(SCHED_RR);
137
138	if (sp.sched_priority < 0) {
139		UNTESTED("Failed to get min SCHED_RR range");
140	}
141
142	/* set the other thread's policy */
143	ret = pthread_setschedparam(*(pthread_t *) arg, SCHED_RR, &sp);
144
145	if (ret != 0) {
146		UNRESOLVED(ret, "Failed to set other's thread policy");
147	}
148
149	return NULL;
150}
151
152/* The main test function. */
153int main(void)
154{
155	int ret = 0;
156	pthread_t tcontrol, tchange;
157	pthread_barrier_t bar;
158
159	/* Initialize output routine */
160	output_init();
161
162	ret = pthread_barrier_init(&bar, NULL, 2);
163
164	if (ret != 0) {
165		UNRESOLVED(ret, "Failed to init barrier");
166	}
167
168	/* Create the controler thread */
169	ret = pthread_create(&tcontrol, NULL, controler, &bar);
170
171	if (ret != 0) {
172		UNRESOLVED(ret, "thread creation failed");
173	}
174
175	/* Now create the changer thread */
176	ret = pthread_create(&tchange, NULL, changer, &tcontrol);
177
178	if (ret != 0) {
179		UNRESOLVED(ret, "thread creation failed");
180	}
181
182	/* wait until the changer finishes */
183	ret = pthread_join(tchange, NULL);
184
185	if (ret != 0) {
186		UNRESOLVED(ret, "Failed to join the thread");
187	}
188
189	/* let the controler control */
190	ret = pthread_barrier_wait(&bar);
191
192	if (ret != 0 && ret != PTHREAD_BARRIER_SERIAL_THREAD) {
193		UNRESOLVED(ret, "barrier wait failed");
194	}
195
196	ret = pthread_join(tcontrol, NULL);
197
198	if (ret != 0) {
199		UNRESOLVED(ret, "Failed to join the thread");
200	}
201
202	ret = pthread_barrier_destroy(&bar);
203
204	if (ret != 0) {
205		UNRESOLVED(ret, "barrier destroy failed");
206	}
207
208	PASSED;
209}
210