1*49cdfc7eSAndroid Build Coastguard Worker /******************************************************************************
2*49cdfc7eSAndroid Build Coastguard Worker *
3*49cdfc7eSAndroid Build Coastguard Worker * Copyright © International Business Machines Corp., 2005, 2008
4*49cdfc7eSAndroid Build Coastguard Worker *
5*49cdfc7eSAndroid Build Coastguard Worker * This program is free software; you can redistribute it and/or modify
6*49cdfc7eSAndroid Build Coastguard Worker * it under the terms of the GNU General Public License as published by
7*49cdfc7eSAndroid Build Coastguard Worker * the Free Software Foundation; either version 2 of the License, or
8*49cdfc7eSAndroid Build Coastguard Worker * (at your option) any later version.
9*49cdfc7eSAndroid Build Coastguard Worker *
10*49cdfc7eSAndroid Build Coastguard Worker * This program is distributed in the hope that it will be useful,
11*49cdfc7eSAndroid Build Coastguard Worker * but WITHOUT ANY WARRANTY; without even the implied warranty of
12*49cdfc7eSAndroid Build Coastguard Worker * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13*49cdfc7eSAndroid Build Coastguard Worker * the GNU General Public License for more details.
14*49cdfc7eSAndroid Build Coastguard Worker *
15*49cdfc7eSAndroid Build Coastguard Worker * You should have received a copy of the GNU General Public License
16*49cdfc7eSAndroid Build Coastguard Worker * along with this program; if not, write to the Free Software
17*49cdfc7eSAndroid Build Coastguard Worker * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18*49cdfc7eSAndroid Build Coastguard Worker *
19*49cdfc7eSAndroid Build Coastguard Worker * NAME
20*49cdfc7eSAndroid Build Coastguard Worker * testpi-3.c
21*49cdfc7eSAndroid Build Coastguard Worker *
22*49cdfc7eSAndroid Build Coastguard Worker * DESCRIPTION
23*49cdfc7eSAndroid Build Coastguard Worker *
24*49cdfc7eSAndroid Build Coastguard Worker *
25*49cdfc7eSAndroid Build Coastguard Worker * USAGE:
26*49cdfc7eSAndroid Build Coastguard Worker * Use run_auto.sh script in current directory to build and run test.
27*49cdfc7eSAndroid Build Coastguard Worker *
28*49cdfc7eSAndroid Build Coastguard Worker * AUTHOR
29*49cdfc7eSAndroid Build Coastguard Worker *
30*49cdfc7eSAndroid Build Coastguard Worker *
31*49cdfc7eSAndroid Build Coastguard Worker * HISTORY
32*49cdfc7eSAndroid Build Coastguard Worker *
33*49cdfc7eSAndroid Build Coastguard Worker *
34*49cdfc7eSAndroid Build Coastguard Worker *****************************************************************************/
35*49cdfc7eSAndroid Build Coastguard Worker
36*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
37*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
38*49cdfc7eSAndroid Build Coastguard Worker #include <string.h>
39*49cdfc7eSAndroid Build Coastguard Worker #include <sched.h>
40*49cdfc7eSAndroid Build Coastguard Worker #include <pthread.h>
41*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
42*49cdfc7eSAndroid Build Coastguard Worker #include <sys/syscall.h>
43*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
44*49cdfc7eSAndroid Build Coastguard Worker #include <librttest.h>
45*49cdfc7eSAndroid Build Coastguard Worker
usage(void)46*49cdfc7eSAndroid Build Coastguard Worker void usage(void)
47*49cdfc7eSAndroid Build Coastguard Worker {
48*49cdfc7eSAndroid Build Coastguard Worker rt_help();
49*49cdfc7eSAndroid Build Coastguard Worker printf("testpi-3 specific options:\n");
50*49cdfc7eSAndroid Build Coastguard Worker }
51*49cdfc7eSAndroid Build Coastguard Worker
parse_args(int c,char * v)52*49cdfc7eSAndroid Build Coastguard Worker int parse_args(int c, char *v)
53*49cdfc7eSAndroid Build Coastguard Worker {
54*49cdfc7eSAndroid Build Coastguard Worker
55*49cdfc7eSAndroid Build Coastguard Worker int handled = 1;
56*49cdfc7eSAndroid Build Coastguard Worker switch (c) {
57*49cdfc7eSAndroid Build Coastguard Worker case 'h':
58*49cdfc7eSAndroid Build Coastguard Worker usage();
59*49cdfc7eSAndroid Build Coastguard Worker exit(0);
60*49cdfc7eSAndroid Build Coastguard Worker default:
61*49cdfc7eSAndroid Build Coastguard Worker handled = 0;
62*49cdfc7eSAndroid Build Coastguard Worker break;
63*49cdfc7eSAndroid Build Coastguard Worker }
64*49cdfc7eSAndroid Build Coastguard Worker return handled;
65*49cdfc7eSAndroid Build Coastguard Worker }
66*49cdfc7eSAndroid Build Coastguard Worker
gettid(void)67*49cdfc7eSAndroid Build Coastguard Worker int gettid(void)
68*49cdfc7eSAndroid Build Coastguard Worker {
69*49cdfc7eSAndroid Build Coastguard Worker return syscall(__NR_gettid);
70*49cdfc7eSAndroid Build Coastguard Worker }
71*49cdfc7eSAndroid Build Coastguard Worker
72*49cdfc7eSAndroid Build Coastguard Worker typedef void *(*entrypoint_t) (void *);
73*49cdfc7eSAndroid Build Coastguard Worker
74*49cdfc7eSAndroid Build Coastguard Worker #define THREAD_STOP 1
75*49cdfc7eSAndroid Build Coastguard Worker
76*49cdfc7eSAndroid Build Coastguard Worker pthread_mutex_t glob_mutex;
77*49cdfc7eSAndroid Build Coastguard Worker
78*49cdfc7eSAndroid Build Coastguard Worker /*typedef struct thread {
79*49cdfc7eSAndroid Build Coastguard Worker int priority;
80*49cdfc7eSAndroid Build Coastguard Worker int policy;
81*49cdfc7eSAndroid Build Coastguard Worker entrypoint_t func;
82*49cdfc7eSAndroid Build Coastguard Worker pthread_attr_t attr;
83*49cdfc7eSAndroid Build Coastguard Worker pthread_t handle;
84*49cdfc7eSAndroid Build Coastguard Worker pthread_mutex_t mutex;
85*49cdfc7eSAndroid Build Coastguard Worker pthread_cond_t cond;
86*49cdfc7eSAndroid Build Coastguard Worker int flags;
87*49cdfc7eSAndroid Build Coastguard Worker int count;
88*49cdfc7eSAndroid Build Coastguard Worker } Thread;*/
89*49cdfc7eSAndroid Build Coastguard Worker
90*49cdfc7eSAndroid Build Coastguard Worker typedef struct thread Thread;
91*49cdfc7eSAndroid Build Coastguard Worker
92*49cdfc7eSAndroid Build Coastguard Worker Thread arg1, arg2, arg3, arg4, arg5;
93*49cdfc7eSAndroid Build Coastguard Worker
94*49cdfc7eSAndroid Build Coastguard Worker int strartThread(Thread * thr);
95*49cdfc7eSAndroid Build Coastguard Worker void stopThread(Thread * thr);
96*49cdfc7eSAndroid Build Coastguard Worker void joinThread(Thread * thr);
97*49cdfc7eSAndroid Build Coastguard Worker
func_nonrt(void * arg)98*49cdfc7eSAndroid Build Coastguard Worker void *func_nonrt(void *arg)
99*49cdfc7eSAndroid Build Coastguard Worker {
100*49cdfc7eSAndroid Build Coastguard Worker Thread *pthr = (Thread *) arg;
101*49cdfc7eSAndroid Build Coastguard Worker int rc, i, j, policy, tid = gettid();
102*49cdfc7eSAndroid Build Coastguard Worker struct sched_param schedp;
103*49cdfc7eSAndroid Build Coastguard Worker cpu_set_t mask;
104*49cdfc7eSAndroid Build Coastguard Worker CPU_ZERO(&mask);
105*49cdfc7eSAndroid Build Coastguard Worker CPU_SET(0, &mask);
106*49cdfc7eSAndroid Build Coastguard Worker
107*49cdfc7eSAndroid Build Coastguard Worker rc = sched_setaffinity(0, sizeof(mask), &mask);
108*49cdfc7eSAndroid Build Coastguard Worker if (rc < 0) {
109*49cdfc7eSAndroid Build Coastguard Worker printf("Thread %d: Can't set affinity: %d %s\n", tid, rc,
110*49cdfc7eSAndroid Build Coastguard Worker strerror(rc));
111*49cdfc7eSAndroid Build Coastguard Worker exit(-1);
112*49cdfc7eSAndroid Build Coastguard Worker }
113*49cdfc7eSAndroid Build Coastguard Worker rc = sched_getaffinity(0, sizeof(mask), &mask);
114*49cdfc7eSAndroid Build Coastguard Worker
115*49cdfc7eSAndroid Build Coastguard Worker printf("Thread started %d on CPU %ld\n", pthr->priority,
116*49cdfc7eSAndroid Build Coastguard Worker (long)mask.__bits[0]);
117*49cdfc7eSAndroid Build Coastguard Worker pthread_getschedparam(pthr->pthread, &policy, &schedp);
118*49cdfc7eSAndroid Build Coastguard Worker printf("Thread running %d\n", pthr->priority);
119*49cdfc7eSAndroid Build Coastguard Worker
120*49cdfc7eSAndroid Build Coastguard Worker while (1) {
121*49cdfc7eSAndroid Build Coastguard Worker pthread_mutex_lock(&glob_mutex);
122*49cdfc7eSAndroid Build Coastguard Worker printf
123*49cdfc7eSAndroid Build Coastguard Worker ("Thread %d at start pthread pol %d pri %d - Got global lock\n",
124*49cdfc7eSAndroid Build Coastguard Worker pthr->priority, policy, schedp.sched_priority);
125*49cdfc7eSAndroid Build Coastguard Worker sleep(2);
126*49cdfc7eSAndroid Build Coastguard Worker for (i = 0; i < 10000; i++) {
127*49cdfc7eSAndroid Build Coastguard Worker if ((i % 100) == 0) {
128*49cdfc7eSAndroid Build Coastguard Worker sched_getparam(tid, &schedp);
129*49cdfc7eSAndroid Build Coastguard Worker policy = sched_getscheduler(tid);
130*49cdfc7eSAndroid Build Coastguard Worker printf("Thread %d(%d) loop %d pthread pol %d "
131*49cdfc7eSAndroid Build Coastguard Worker "pri %d\n", tid, pthr->priority, i,
132*49cdfc7eSAndroid Build Coastguard Worker policy, schedp.sched_priority);
133*49cdfc7eSAndroid Build Coastguard Worker fflush(NULL);
134*49cdfc7eSAndroid Build Coastguard Worker }
135*49cdfc7eSAndroid Build Coastguard Worker pthr->id++;
136*49cdfc7eSAndroid Build Coastguard Worker for (j = 0; j < 5000; j++) {
137*49cdfc7eSAndroid Build Coastguard Worker pthread_mutex_lock(&(pthr->mutex));
138*49cdfc7eSAndroid Build Coastguard Worker pthread_mutex_unlock(&(pthr->mutex));
139*49cdfc7eSAndroid Build Coastguard Worker }
140*49cdfc7eSAndroid Build Coastguard Worker }
141*49cdfc7eSAndroid Build Coastguard Worker pthread_mutex_unlock(&glob_mutex);
142*49cdfc7eSAndroid Build Coastguard Worker sched_yield();
143*49cdfc7eSAndroid Build Coastguard Worker }
144*49cdfc7eSAndroid Build Coastguard Worker return NULL;
145*49cdfc7eSAndroid Build Coastguard Worker }
146*49cdfc7eSAndroid Build Coastguard Worker
func_rt(void * arg)147*49cdfc7eSAndroid Build Coastguard Worker void *func_rt(void *arg)
148*49cdfc7eSAndroid Build Coastguard Worker {
149*49cdfc7eSAndroid Build Coastguard Worker Thread *pthr = (Thread *) arg;
150*49cdfc7eSAndroid Build Coastguard Worker int rc, i, j, policy, tid = gettid();
151*49cdfc7eSAndroid Build Coastguard Worker struct sched_param schedp;
152*49cdfc7eSAndroid Build Coastguard Worker cpu_set_t mask;
153*49cdfc7eSAndroid Build Coastguard Worker CPU_ZERO(&mask);
154*49cdfc7eSAndroid Build Coastguard Worker CPU_SET(0, &mask);
155*49cdfc7eSAndroid Build Coastguard Worker
156*49cdfc7eSAndroid Build Coastguard Worker rc = sched_setaffinity(0, sizeof(mask), &mask);
157*49cdfc7eSAndroid Build Coastguard Worker if (rc < 0) {
158*49cdfc7eSAndroid Build Coastguard Worker printf("Thread %d: Can't set affinity: %d %s\n", tid, rc,
159*49cdfc7eSAndroid Build Coastguard Worker strerror(rc));
160*49cdfc7eSAndroid Build Coastguard Worker exit(-1);
161*49cdfc7eSAndroid Build Coastguard Worker }
162*49cdfc7eSAndroid Build Coastguard Worker rc = sched_getaffinity(0, sizeof(mask), &mask);
163*49cdfc7eSAndroid Build Coastguard Worker
164*49cdfc7eSAndroid Build Coastguard Worker printf("Thread started %d on CPU %ld\n", pthr->priority,
165*49cdfc7eSAndroid Build Coastguard Worker (long)mask.__bits[0]);
166*49cdfc7eSAndroid Build Coastguard Worker pthread_getschedparam(pthr->pthread, &policy, &schedp);
167*49cdfc7eSAndroid Build Coastguard Worker
168*49cdfc7eSAndroid Build Coastguard Worker while (1) {
169*49cdfc7eSAndroid Build Coastguard Worker sleep(2);
170*49cdfc7eSAndroid Build Coastguard Worker printf("Thread running %d\n", pthr->priority);
171*49cdfc7eSAndroid Build Coastguard Worker pthread_mutex_lock(&glob_mutex);
172*49cdfc7eSAndroid Build Coastguard Worker printf
173*49cdfc7eSAndroid Build Coastguard Worker ("Thread %d at start pthread pol %d pri %d - Got global lock\n",
174*49cdfc7eSAndroid Build Coastguard Worker pthr->priority, policy, schedp.sched_priority);
175*49cdfc7eSAndroid Build Coastguard Worker
176*49cdfc7eSAndroid Build Coastguard Worker /* we just use the mutex as something to slow things down */
177*49cdfc7eSAndroid Build Coastguard Worker /* say who we are and then do nothing for a while. The aim
178*49cdfc7eSAndroid Build Coastguard Worker * of this is to show that high priority threads make more
179*49cdfc7eSAndroid Build Coastguard Worker * progress than lower priority threads..
180*49cdfc7eSAndroid Build Coastguard Worker */
181*49cdfc7eSAndroid Build Coastguard Worker for (i = 0; i < 1000; i++) {
182*49cdfc7eSAndroid Build Coastguard Worker if (i % 100 == 0) {
183*49cdfc7eSAndroid Build Coastguard Worker sched_getparam(tid, &schedp);
184*49cdfc7eSAndroid Build Coastguard Worker policy = sched_getscheduler(tid);
185*49cdfc7eSAndroid Build Coastguard Worker printf
186*49cdfc7eSAndroid Build Coastguard Worker ("Thread %d(%d) loop %d pthread pol %d pri %d\n",
187*49cdfc7eSAndroid Build Coastguard Worker tid, pthr->priority, i, policy,
188*49cdfc7eSAndroid Build Coastguard Worker schedp.sched_priority);
189*49cdfc7eSAndroid Build Coastguard Worker fflush(NULL);
190*49cdfc7eSAndroid Build Coastguard Worker }
191*49cdfc7eSAndroid Build Coastguard Worker pthr->id++;
192*49cdfc7eSAndroid Build Coastguard Worker for (j = 0; j < 5000; j++) {
193*49cdfc7eSAndroid Build Coastguard Worker pthread_mutex_lock(&(pthr->mutex));
194*49cdfc7eSAndroid Build Coastguard Worker pthread_mutex_unlock(&(pthr->mutex));
195*49cdfc7eSAndroid Build Coastguard Worker }
196*49cdfc7eSAndroid Build Coastguard Worker }
197*49cdfc7eSAndroid Build Coastguard Worker pthread_mutex_unlock(&glob_mutex);
198*49cdfc7eSAndroid Build Coastguard Worker sleep(2);
199*49cdfc7eSAndroid Build Coastguard Worker }
200*49cdfc7eSAndroid Build Coastguard Worker return NULL;
201*49cdfc7eSAndroid Build Coastguard Worker }
202*49cdfc7eSAndroid Build Coastguard Worker
func_noise(void * arg)203*49cdfc7eSAndroid Build Coastguard Worker void *func_noise(void *arg)
204*49cdfc7eSAndroid Build Coastguard Worker {
205*49cdfc7eSAndroid Build Coastguard Worker Thread *pthr = (Thread *) arg;
206*49cdfc7eSAndroid Build Coastguard Worker int rc, i, j, policy, tid = gettid();
207*49cdfc7eSAndroid Build Coastguard Worker struct sched_param schedp;
208*49cdfc7eSAndroid Build Coastguard Worker cpu_set_t mask;
209*49cdfc7eSAndroid Build Coastguard Worker CPU_ZERO(&mask);
210*49cdfc7eSAndroid Build Coastguard Worker CPU_SET(0, &mask);
211*49cdfc7eSAndroid Build Coastguard Worker
212*49cdfc7eSAndroid Build Coastguard Worker rc = sched_setaffinity(0, sizeof(mask), &mask);
213*49cdfc7eSAndroid Build Coastguard Worker if (rc < 0) {
214*49cdfc7eSAndroid Build Coastguard Worker printf("Thread %d: Can't set affinity: %d %s\n", tid, rc,
215*49cdfc7eSAndroid Build Coastguard Worker strerror(rc));
216*49cdfc7eSAndroid Build Coastguard Worker exit(-1);
217*49cdfc7eSAndroid Build Coastguard Worker }
218*49cdfc7eSAndroid Build Coastguard Worker rc = sched_getaffinity(0, sizeof(mask), &mask);
219*49cdfc7eSAndroid Build Coastguard Worker
220*49cdfc7eSAndroid Build Coastguard Worker printf("Noise Thread started %d on CPU %ld\n", pthr->priority,
221*49cdfc7eSAndroid Build Coastguard Worker (long)mask.__bits[0]);
222*49cdfc7eSAndroid Build Coastguard Worker pthread_getschedparam(pthr->pthread, &policy, &schedp);
223*49cdfc7eSAndroid Build Coastguard Worker
224*49cdfc7eSAndroid Build Coastguard Worker while (1) {
225*49cdfc7eSAndroid Build Coastguard Worker sleep(1);
226*49cdfc7eSAndroid Build Coastguard Worker printf("Noise Thread running %d\n", pthr->priority);
227*49cdfc7eSAndroid Build Coastguard Worker
228*49cdfc7eSAndroid Build Coastguard Worker for (i = 0; i < 10000; i++) {
229*49cdfc7eSAndroid Build Coastguard Worker if ((i % 100) == 0) {
230*49cdfc7eSAndroid Build Coastguard Worker sched_getparam(tid, &schedp);
231*49cdfc7eSAndroid Build Coastguard Worker policy = sched_getscheduler(tid);
232*49cdfc7eSAndroid Build Coastguard Worker printf
233*49cdfc7eSAndroid Build Coastguard Worker ("Noise Thread %d(%d) loop %d pthread pol %d pri %d\n",
234*49cdfc7eSAndroid Build Coastguard Worker tid, pthr->priority, i, policy,
235*49cdfc7eSAndroid Build Coastguard Worker schedp.sched_priority);
236*49cdfc7eSAndroid Build Coastguard Worker fflush(NULL);
237*49cdfc7eSAndroid Build Coastguard Worker }
238*49cdfc7eSAndroid Build Coastguard Worker pthr->id++;
239*49cdfc7eSAndroid Build Coastguard Worker for (j = 0; j < 5000; j++) {
240*49cdfc7eSAndroid Build Coastguard Worker pthread_mutex_lock(&(pthr->mutex));
241*49cdfc7eSAndroid Build Coastguard Worker pthread_mutex_unlock(&(pthr->mutex));
242*49cdfc7eSAndroid Build Coastguard Worker }
243*49cdfc7eSAndroid Build Coastguard Worker }
244*49cdfc7eSAndroid Build Coastguard Worker sched_yield();
245*49cdfc7eSAndroid Build Coastguard Worker }
246*49cdfc7eSAndroid Build Coastguard Worker return NULL;
247*49cdfc7eSAndroid Build Coastguard Worker }
248*49cdfc7eSAndroid Build Coastguard Worker
startThread(Thread * thrd)249*49cdfc7eSAndroid Build Coastguard Worker int startThread(Thread * thrd)
250*49cdfc7eSAndroid Build Coastguard Worker {
251*49cdfc7eSAndroid Build Coastguard Worker struct sched_param schedp;
252*49cdfc7eSAndroid Build Coastguard Worker pthread_condattr_t condattr;
253*49cdfc7eSAndroid Build Coastguard Worker int retc, policy, inherit;
254*49cdfc7eSAndroid Build Coastguard Worker
255*49cdfc7eSAndroid Build Coastguard Worker printf("Start thread priority %d\n", thrd->priority);
256*49cdfc7eSAndroid Build Coastguard Worker if (pthread_attr_init(&(thrd->attr)) != 0) {
257*49cdfc7eSAndroid Build Coastguard Worker printf("Attr init failed");
258*49cdfc7eSAndroid Build Coastguard Worker exit(2);
259*49cdfc7eSAndroid Build Coastguard Worker }
260*49cdfc7eSAndroid Build Coastguard Worker thrd->flags = 0;
261*49cdfc7eSAndroid Build Coastguard Worker memset(&schedp, 0, sizeof(schedp));
262*49cdfc7eSAndroid Build Coastguard Worker schedp.sched_priority = thrd->priority;
263*49cdfc7eSAndroid Build Coastguard Worker policy = thrd->policy;
264*49cdfc7eSAndroid Build Coastguard Worker
265*49cdfc7eSAndroid Build Coastguard Worker if (pthread_attr_setschedpolicy(&(thrd->attr), policy) != 0) {
266*49cdfc7eSAndroid Build Coastguard Worker printf("Can't set policy %d\n", policy);
267*49cdfc7eSAndroid Build Coastguard Worker }
268*49cdfc7eSAndroid Build Coastguard Worker if (pthread_attr_getschedpolicy(&(thrd->attr), &policy) != 0) {
269*49cdfc7eSAndroid Build Coastguard Worker printf("Can't get policy\n");
270*49cdfc7eSAndroid Build Coastguard Worker } else {
271*49cdfc7eSAndroid Build Coastguard Worker printf("Policy in attribs is %d\n", policy);
272*49cdfc7eSAndroid Build Coastguard Worker }
273*49cdfc7eSAndroid Build Coastguard Worker if (pthread_attr_setschedparam(&(thrd->attr), &schedp) != 0) {
274*49cdfc7eSAndroid Build Coastguard Worker printf("Can't set params");
275*49cdfc7eSAndroid Build Coastguard Worker }
276*49cdfc7eSAndroid Build Coastguard Worker if (pthread_attr_getschedparam(&(thrd->attr), &schedp) != 0) {
277*49cdfc7eSAndroid Build Coastguard Worker printf("Can't get params");
278*49cdfc7eSAndroid Build Coastguard Worker } else {
279*49cdfc7eSAndroid Build Coastguard Worker printf("Priority in attribs is %d\n", schedp.sched_priority);
280*49cdfc7eSAndroid Build Coastguard Worker }
281*49cdfc7eSAndroid Build Coastguard Worker if (pthread_attr_setinheritsched(&(thrd->attr), PTHREAD_EXPLICIT_SCHED)
282*49cdfc7eSAndroid Build Coastguard Worker != 0) {
283*49cdfc7eSAndroid Build Coastguard Worker printf("Can't set inheritsched\n");
284*49cdfc7eSAndroid Build Coastguard Worker }
285*49cdfc7eSAndroid Build Coastguard Worker if (pthread_attr_getinheritsched(&(thrd->attr), &inherit) != 0) {
286*49cdfc7eSAndroid Build Coastguard Worker printf("Can't get inheritsched\n");
287*49cdfc7eSAndroid Build Coastguard Worker } else {
288*49cdfc7eSAndroid Build Coastguard Worker printf("inherit sched in attribs is %d\n", inherit);
289*49cdfc7eSAndroid Build Coastguard Worker }
290*49cdfc7eSAndroid Build Coastguard Worker if ((retc = pthread_mutex_init(&(thrd->mutex), NULL)) != 0) {
291*49cdfc7eSAndroid Build Coastguard Worker printf("Failed to init mutex: %d\n", retc);
292*49cdfc7eSAndroid Build Coastguard Worker }
293*49cdfc7eSAndroid Build Coastguard Worker if (pthread_condattr_init(&condattr) != 0) {
294*49cdfc7eSAndroid Build Coastguard Worker printf("Failed to init condattr\n");
295*49cdfc7eSAndroid Build Coastguard Worker }
296*49cdfc7eSAndroid Build Coastguard Worker if (pthread_cond_init(&(thrd->cond), &condattr) != 0) {
297*49cdfc7eSAndroid Build Coastguard Worker printf("Failed to init cond\n");
298*49cdfc7eSAndroid Build Coastguard Worker }
299*49cdfc7eSAndroid Build Coastguard Worker retc =
300*49cdfc7eSAndroid Build Coastguard Worker pthread_create(&(thrd->pthread), &(thrd->attr), thrd->func, thrd);
301*49cdfc7eSAndroid Build Coastguard Worker printf("Create returns %d\n\n", retc);
302*49cdfc7eSAndroid Build Coastguard Worker return retc;
303*49cdfc7eSAndroid Build Coastguard Worker }
304*49cdfc7eSAndroid Build Coastguard Worker
stopThread(Thread * thr)305*49cdfc7eSAndroid Build Coastguard Worker void stopThread(Thread * thr)
306*49cdfc7eSAndroid Build Coastguard Worker {
307*49cdfc7eSAndroid Build Coastguard Worker thr->flags += THREAD_STOP;
308*49cdfc7eSAndroid Build Coastguard Worker joinThread(thr);
309*49cdfc7eSAndroid Build Coastguard Worker }
310*49cdfc7eSAndroid Build Coastguard Worker
joinThread(Thread * thr)311*49cdfc7eSAndroid Build Coastguard Worker void joinThread(Thread * thr)
312*49cdfc7eSAndroid Build Coastguard Worker {
313*49cdfc7eSAndroid Build Coastguard Worker void *ret = NULL;
314*49cdfc7eSAndroid Build Coastguard Worker if (pthread_join(thr->pthread, &ret) != 0) {
315*49cdfc7eSAndroid Build Coastguard Worker printf("Join failed\n");
316*49cdfc7eSAndroid Build Coastguard Worker }
317*49cdfc7eSAndroid Build Coastguard Worker printf("Join gave %p\n", ret);
318*49cdfc7eSAndroid Build Coastguard Worker }
319*49cdfc7eSAndroid Build Coastguard Worker
320*49cdfc7eSAndroid Build Coastguard Worker /*
321*49cdfc7eSAndroid Build Coastguard Worker * Test pthread creation at different thread priorities.
322*49cdfc7eSAndroid Build Coastguard Worker */
main(int argc,char * argv[])323*49cdfc7eSAndroid Build Coastguard Worker int main(int argc, char *argv[])
324*49cdfc7eSAndroid Build Coastguard Worker {
325*49cdfc7eSAndroid Build Coastguard Worker int i, retc, nopi = 0;
326*49cdfc7eSAndroid Build Coastguard Worker cpu_set_t mask;
327*49cdfc7eSAndroid Build Coastguard Worker CPU_ZERO(&mask);
328*49cdfc7eSAndroid Build Coastguard Worker CPU_SET(0, &mask);
329*49cdfc7eSAndroid Build Coastguard Worker setup();
330*49cdfc7eSAndroid Build Coastguard Worker
331*49cdfc7eSAndroid Build Coastguard Worker rt_init("h", parse_args, argc, argv);
332*49cdfc7eSAndroid Build Coastguard Worker
333*49cdfc7eSAndroid Build Coastguard Worker retc = sched_setaffinity(0, sizeof(mask), &mask);
334*49cdfc7eSAndroid Build Coastguard Worker if (retc < 0) {
335*49cdfc7eSAndroid Build Coastguard Worker printf("Main Thread: Can't set affinity: %d %s\n", retc,
336*49cdfc7eSAndroid Build Coastguard Worker strerror(retc));
337*49cdfc7eSAndroid Build Coastguard Worker exit(1);
338*49cdfc7eSAndroid Build Coastguard Worker }
339*49cdfc7eSAndroid Build Coastguard Worker retc = sched_getaffinity(0, sizeof(mask), &mask);
340*49cdfc7eSAndroid Build Coastguard Worker
341*49cdfc7eSAndroid Build Coastguard Worker /*
342*49cdfc7eSAndroid Build Coastguard Worker * XXX: Have you ever heard of structures with c89/c99?
343*49cdfc7eSAndroid Build Coastguard Worker * Inline assignment is a beautiful thing.
344*49cdfc7eSAndroid Build Coastguard Worker */
345*49cdfc7eSAndroid Build Coastguard Worker arg1.policy = SCHED_OTHER;
346*49cdfc7eSAndroid Build Coastguard Worker arg1.priority = 0;
347*49cdfc7eSAndroid Build Coastguard Worker arg1.func = func_nonrt;
348*49cdfc7eSAndroid Build Coastguard Worker arg2.policy = SCHED_RR;
349*49cdfc7eSAndroid Build Coastguard Worker arg2.priority = 20;
350*49cdfc7eSAndroid Build Coastguard Worker arg2.func = func_rt;
351*49cdfc7eSAndroid Build Coastguard Worker arg3.policy = SCHED_RR;
352*49cdfc7eSAndroid Build Coastguard Worker arg3.priority = 30;
353*49cdfc7eSAndroid Build Coastguard Worker arg3.func = func_rt;
354*49cdfc7eSAndroid Build Coastguard Worker arg4.policy = SCHED_RR;
355*49cdfc7eSAndroid Build Coastguard Worker arg4.priority = 40;
356*49cdfc7eSAndroid Build Coastguard Worker arg4.func = func_rt;
357*49cdfc7eSAndroid Build Coastguard Worker arg5.policy = SCHED_RR;
358*49cdfc7eSAndroid Build Coastguard Worker arg5.priority = 40;
359*49cdfc7eSAndroid Build Coastguard Worker arg5.func = func_noise;
360*49cdfc7eSAndroid Build Coastguard Worker
361*49cdfc7eSAndroid Build Coastguard Worker for (i = 0; i < argc; i++) {
362*49cdfc7eSAndroid Build Coastguard Worker if (strcmp(argv[i], "nopi") == 0)
363*49cdfc7eSAndroid Build Coastguard Worker nopi = 1;
364*49cdfc7eSAndroid Build Coastguard Worker }
365*49cdfc7eSAndroid Build Coastguard Worker
366*49cdfc7eSAndroid Build Coastguard Worker printf("Start %s\n", argv[0]);
367*49cdfc7eSAndroid Build Coastguard Worker
368*49cdfc7eSAndroid Build Coastguard Worker #if HAS_PRIORITY_INHERIT
369*49cdfc7eSAndroid Build Coastguard Worker if (!nopi) {
370*49cdfc7eSAndroid Build Coastguard Worker pthread_mutexattr_t mutexattr;
371*49cdfc7eSAndroid Build Coastguard Worker int protocol;
372*49cdfc7eSAndroid Build Coastguard Worker
373*49cdfc7eSAndroid Build Coastguard Worker if (pthread_mutexattr_init(&mutexattr) != 0) {
374*49cdfc7eSAndroid Build Coastguard Worker printf("Failed to init mutexattr\n");
375*49cdfc7eSAndroid Build Coastguard Worker };
376*49cdfc7eSAndroid Build Coastguard Worker if (pthread_mutexattr_setprotocol
377*49cdfc7eSAndroid Build Coastguard Worker (&mutexattr, PTHREAD_PRIO_INHERIT) != 0) {
378*49cdfc7eSAndroid Build Coastguard Worker printf("Can't set protocol prio inherit\n");
379*49cdfc7eSAndroid Build Coastguard Worker }
380*49cdfc7eSAndroid Build Coastguard Worker if (pthread_mutexattr_getprotocol(&mutexattr, &protocol) != 0) {
381*49cdfc7eSAndroid Build Coastguard Worker printf("Can't get mutexattr protocol\n");
382*49cdfc7eSAndroid Build Coastguard Worker } else {
383*49cdfc7eSAndroid Build Coastguard Worker printf("protocol in mutexattr is %d\n", protocol);
384*49cdfc7eSAndroid Build Coastguard Worker }
385*49cdfc7eSAndroid Build Coastguard Worker if ((retc = pthread_mutex_init(&glob_mutex, &mutexattr)) != 0) {
386*49cdfc7eSAndroid Build Coastguard Worker printf("Failed to init mutex: %d\n", retc);
387*49cdfc7eSAndroid Build Coastguard Worker }
388*49cdfc7eSAndroid Build Coastguard Worker }
389*49cdfc7eSAndroid Build Coastguard Worker #endif
390*49cdfc7eSAndroid Build Coastguard Worker
391*49cdfc7eSAndroid Build Coastguard Worker startThread(&arg1);
392*49cdfc7eSAndroid Build Coastguard Worker startThread(&arg2);
393*49cdfc7eSAndroid Build Coastguard Worker startThread(&arg3);
394*49cdfc7eSAndroid Build Coastguard Worker startThread(&arg4);
395*49cdfc7eSAndroid Build Coastguard Worker startThread(&arg5);
396*49cdfc7eSAndroid Build Coastguard Worker
397*49cdfc7eSAndroid Build Coastguard Worker sleep(10);
398*49cdfc7eSAndroid Build Coastguard Worker
399*49cdfc7eSAndroid Build Coastguard Worker printf("Stopping threads\n");
400*49cdfc7eSAndroid Build Coastguard Worker stopThread(&arg1);
401*49cdfc7eSAndroid Build Coastguard Worker stopThread(&arg2);
402*49cdfc7eSAndroid Build Coastguard Worker stopThread(&arg3);
403*49cdfc7eSAndroid Build Coastguard Worker stopThread(&arg4);
404*49cdfc7eSAndroid Build Coastguard Worker stopThread(&arg5);
405*49cdfc7eSAndroid Build Coastguard Worker
406*49cdfc7eSAndroid Build Coastguard Worker printf("Thread counts %d %d %d %d %d\n", arg1.id, arg2.id, arg3.id,
407*49cdfc7eSAndroid Build Coastguard Worker arg4.id, arg5.id);
408*49cdfc7eSAndroid Build Coastguard Worker printf("Done\n");
409*49cdfc7eSAndroid Build Coastguard Worker
410*49cdfc7eSAndroid Build Coastguard Worker return 0;
411*49cdfc7eSAndroid Build Coastguard Worker }
412