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
27 * Use run_auto.sh script in current directory to build and run test.
35 *****************************************************************************/
42 #include <sys/types.h>
43 #include <sys/syscall.h>
45 #include <librttest.h>
47 pthread_barrier_t barrier
;
52 printf("testpi-2 specific options:\n");
55 int parse_args(int c
, char *v
)
72 return syscall(__NR_gettid
);
75 typedef void* (*entrypoint_t
)(void*);
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();
89 rc
= sched_setaffinity(0, sizeof(mask
), &mask
);
91 printf("Thread %d: Can't set affinity: %d %s\n", tid
, rc
, strerror(rc
));
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
++) {
103 printf("Thread %d loop %d pthread pol %d pri %d\n", tid
, i
, pthr
->policy
, pthr
->priority
);
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
);
116 void* func_rt(void* arg
)
118 struct thread
* pthr
= (struct thread
* )arg
;
119 int rc
, i
, j
, tid
= gettid();
124 rc
= sched_setaffinity(0, sizeof(mask
), &mask
);
126 printf("Thread %d: Can't set affinity: %d %s\n", tid
, rc
, strerror(rc
));
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
++) {
142 printf("Thread %d loop %d pthread pol %d pri %d\n", tid
, i
, pthr
->policy
, pthr
->priority
);
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
);
155 void* func_noise(void* arg
)
157 struct thread
* pthr
= (struct thread
* )arg
;
158 int rc
, i
, j
, tid
= gettid();
163 rc
= sched_setaffinity(0, sizeof(mask
), &mask
);
165 printf("Thread %d: Can't set affinity: %d %s\n", tid
, rc
, strerror(rc
));
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
++) {
174 printf("Noise Thread %d loop %d pthread pol %d pri %d\n", tid
, i
, pthr
->policy
, pthr
->priority
);
178 for (j
=0;j
<5000;j
++) {
179 pthread_mutex_lock(&(pthr
->mutex
));
180 pthread_mutex_unlock(&(pthr
->mutex
));
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;
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
));
203 retc
= sched_setaffinity(0, sizeof(mask
), &mask
);
205 printf("Main Thread: Can't set affinity: %d %s\n", retc
, strerror(retc
));
209 for (i
=0;i
<argc
;i
++) {
210 if (strcmp(argv
[i
],"nopi") == 0) nopi
= 1;
213 printf("Start %s\n",argv
[0]);
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");
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");
241 printf("Criteria: Low Priority Thread and High Priority Thread should prempt each other multiple times\n");