Imported Upstream version 20091031
[ltp-debian.git] / testcases / realtime / func / pi-tests / testpi-2.c
blobf824c9466525ffa5b83a7affce9c0854025f3c6d
1 /******************************************************************************
3 * Copyright © International Business Machines Corp., 2005, 2008
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program 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
13 * the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 * NAME
20 * testpi-2.c
22 * DESCRIPTION
26 * USAGE:
27 * Use run_auto.sh script in current directory to build and run test.
29 * AUTHOR
32 * HISTORY
35 *****************************************************************************/
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <sched.h>
41 #include <pthread.h>
42 #include <sys/types.h>
43 #include <sys/syscall.h>
44 #include <unistd.h>
45 #include <librttest.h>
47 pthread_barrier_t barrier;
49 void usage(void)
51 rt_help();
52 printf("testpi-2 specific options:\n");
55 int parse_args(int c, char *v)
58 int handled = 1;
59 switch (c) {
60 case 'h':
61 usage();
62 exit(0);
63 default:
64 handled = 0;
65 break;
67 return handled;
70 int gettid(void)
72 return syscall(__NR_gettid);
75 typedef void* (*entrypoint_t)(void*);
77 #define THREAD_STOP 1
79 pthread_mutex_t glob_mutex;
81 void* func_lowrt(void* arg)
83 struct thread* pthr = (struct thread* )arg ;
84 int rc, i, j, tid = gettid();
85 cpu_set_t mask;
86 CPU_ZERO(&mask);
87 CPU_SET(0, &mask);
89 rc = sched_setaffinity(0, sizeof(mask), &mask);
90 if (rc < 0) {
91 printf("Thread %d: Can't set affinity: %d %s\n", tid, rc, strerror(rc));
92 exit(-1);
95 printf("Thread %d started running with priority %d\n", tid, pthr->priority);
96 pthread_mutex_lock(&glob_mutex);
97 printf("Thread %d at start pthread pol %d pri %d - Got global lock\n", tid, pthr->policy, pthr->priority);
98 /* Wait for other RT threads to start up */
99 pthread_barrier_wait(&barrier);
101 for (i=0;i<10000;i++) {
102 if (i%100 == 0) {
103 printf("Thread %d loop %d pthread pol %d pri %d\n", tid, i, pthr->policy, pthr->priority);
104 fflush(NULL);
106 pthr->id++;
107 for (j=0;j<5000;j++) {
108 pthread_mutex_lock(&(pthr->mutex));
109 pthread_mutex_unlock(&(pthr->mutex));
112 pthread_mutex_unlock(&glob_mutex);
113 return NULL;
116 void* func_rt(void* arg)
118 struct thread* pthr = (struct thread* )arg;
119 int rc, i, j, tid = gettid();
120 cpu_set_t mask;
121 CPU_ZERO(&mask);
122 CPU_SET(0, &mask);
124 rc = sched_setaffinity(0, sizeof(mask), &mask);
125 if (rc < 0) {
126 printf("Thread %d: Can't set affinity: %d %s\n", tid, rc, strerror(rc));
127 exit(-1);
130 printf("Thread %d started running with prio %d\n", tid, pthr->priority);
131 pthread_barrier_wait(&barrier);
132 pthread_mutex_lock(&glob_mutex);
133 printf("Thread %d at start pthread pol %d pri %d - Got global lock\n", tid, pthr->policy, pthr->priority);
135 /* we just use the mutex as something to slow things down */
136 /* say who we are and then do nothing for a while. The aim
137 * of this is to show that high priority threads make more
138 * progress than lower priority threads..
140 for (i=0;i<1000;i++) {
141 if (i%100 == 0) {
142 printf("Thread %d loop %d pthread pol %d pri %d\n", tid, i, pthr->policy, pthr->priority);
143 fflush(NULL);
145 pthr->id++;
146 for (j=0;j<5000;j++) {
147 pthread_mutex_lock(&(pthr->mutex));
148 pthread_mutex_unlock(&(pthr->mutex));
151 pthread_mutex_unlock(&glob_mutex);
152 return NULL;
155 void* func_noise(void* arg)
157 struct thread* pthr = (struct thread* )arg;
158 int rc, i, j, tid = gettid();
159 cpu_set_t mask;
160 CPU_ZERO(&mask);
161 CPU_SET(0, &mask);
163 rc = sched_setaffinity(0, sizeof(mask), &mask);
164 if (rc < 0) {
165 printf("Thread %d: Can't set affinity: %d %s\n", tid, rc, strerror(rc));
166 exit(-1);
169 printf("Noise Thread %d started running with prio %d\n", tid, pthr->priority);
170 pthread_barrier_wait(&barrier);
172 for (i=0;i<10000;i++) {
173 if (i%100 == 0) {
174 printf("Noise Thread %d loop %d pthread pol %d pri %d\n", tid, i, pthr->policy, pthr->priority);
175 fflush(NULL);
177 pthr->id++;
178 for (j=0;j<5000;j++) {
179 pthread_mutex_lock(&(pthr->mutex));
180 pthread_mutex_unlock(&(pthr->mutex));
183 return NULL;
187 * Test pthread creation at different thread priorities.
189 int main(int argc, char* argv[]) {
190 pthread_mutexattr_t mutexattr;
191 int i, retc, protocol, nopi = 0;
192 cpu_set_t mask;
193 CPU_ZERO(&mask);
194 CPU_SET(0, &mask);
195 setup();
196 rt_init("h",parse_args,argc,argv);
198 if ((retc = pthread_barrier_init(&barrier, NULL, 5))) {
199 printf("pthread_barrier_init failed: %s\n", strerror(retc));
200 exit(retc);
203 retc = sched_setaffinity(0, sizeof(mask), &mask);
204 if (retc < 0) {
205 printf("Main Thread: Can't set affinity: %d %s\n", retc, strerror(retc));
206 exit(-1);
209 for (i=0;i<argc;i++) {
210 if (strcmp(argv[i],"nopi") == 0) nopi = 1;
213 printf("Start %s\n",argv[0]);
215 if (!nopi) {
216 if (pthread_mutexattr_init(&mutexattr) != 0) {
217 printf("Failed to init mutexattr\n");
219 if (pthread_mutexattr_setprotocol(&mutexattr, PTHREAD_PRIO_INHERIT) != 0) {
220 printf("Can't set protocol prio inherit\n");
222 if (pthread_mutexattr_getprotocol(&mutexattr, &protocol) != 0) {
223 printf("Can't get mutexattr protocol\n");
224 } else {
225 printf("protocol in mutexattr is %d\n", protocol);
227 if ((retc = pthread_mutex_init(&glob_mutex, &mutexattr)) != 0) {
228 printf("Failed to init mutex: %d\n", retc);
232 create_rr_thread(func_lowrt, NULL, 10);
233 create_rr_thread(func_rt, NULL, 20);
234 create_fifo_thread(func_rt, NULL, 30);
235 create_fifo_thread(func_rt, NULL, 40);
236 create_rr_thread(func_noise, NULL, 40);
238 printf("Joining threads\n");
239 join_threads();
240 printf("Done\n");
241 printf("Criteria: Low Priority Thread and High Priority Thread should prempt each other multiple times\n");
242 return 0;