1 /*
2  * Copyright (c) 2004, QUALCOMM Inc. All rights reserved.
3  * Created by:  abisain REMOVE-THIS AT qualcomm DOT com
4  * This file is licensed under the GPL license.  For the full content
5  * of this license, see the COPYING file at the top level of this
6  * source tree.
7 
8  * Test that pthread_create(pthread_attr)
9  *   shall create a thread with the pthread_attr settings passed to it
10  *
11  * Steps:
12  * 1. Create a pthread_attr structure and set policy and priority in it
13  * 2. Create a thread using this attr
14  * 3. In the thread, check these settings.
15  *
16  */
17 
18 #include <pthread.h>
19 #include <stdio.h>
20 #include <sys/time.h>
21 #include <stdlib.h>
22 #include "posixtest.h"
23 
24 #define TEST "3-2"
25 #define AREA "scheduler"
26 #define ERROR_PREFIX "unexpected error: " AREA " " TEST ": "
27 
28 #define PRIORITY 20
29 #define POLICY SCHED_RR
30 
31 /* the thread uses this to indicate to main or success */
32 int policy_correct = -1;
33 /* the thread uses this to indicate to main or success */
34 int priority_correct = -1;
35 
36 
37 /* Thread function which checks the scheduler settings for itself */
thread(void * tmp)38 void * thread(void *tmp)
39 {
40 	struct sched_param     param;
41 	int                    policy;
42 	int                    rc = 0;
43 
44 	rc = pthread_getschedparam(pthread_self(), &policy, &param);
45 	if(rc != 0) {
46 		printf(ERROR_PREFIX "pthread_getschedparam\n");
47 		exit(PTS_UNRESOLVED);
48 	}
49 	if(policy == POLICY) {
50 		policy_correct = 1;
51 	}
52 	if(param.sched_priority == PRIORITY) {
53 		priority_correct = 1;
54 	}
55 	return NULL;
56 }
57 
main()58 int main()
59 {
60 	pthread_t               thread_id;
61 	pthread_attr_t          attr;
62 	struct sched_param      param;
63 	int                     rc = 0;
64 
65 	/* Initialze the attribute struct and set policy and priority in it*/
66 	rc = pthread_attr_init(&attr);
67 	if(rc != 0) {
68 		printf(ERROR_PREFIX "pthread_attr_init\n");
69 		exit(PTS_UNRESOLVED);
70 	}
71 	rc = pthread_attr_setschedpolicy( &attr, POLICY);
72 	if(rc != 0) {
73 		printf(ERROR_PREFIX "pthread_attr_setschedpolicy\n");
74 		exit(PTS_UNRESOLVED);
75 	}
76 	param.sched_priority = PRIORITY;
77 	rc = pthread_attr_setschedparam(&attr, &param);
78 	if(rc != 0) {
79 		printf(ERROR_PREFIX "pthread_attr_setschedparam\n");
80 		exit(PTS_UNRESOLVED);
81 	}
82 
83 	rc = pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
84 	if(rc != 0) {
85 		printf(ERROR_PREFIX "pthread_attr_setinheritsched\n");
86 		exit(PTS_UNRESOLVED);
87 	}
88 
89 
90 	/* Create the thread with the attr */
91 	rc = pthread_create(&thread_id, &attr, thread, NULL);
92 	if(rc != 0) {
93 		printf(ERROR_PREFIX "pthread_create\n");
94 		exit(PTS_UNRESOLVED);
95 	}
96 
97 	/* Wait for that thread to finish */
98 	rc = pthread_join(thread_id, NULL);
99 	if(rc != 0) {
100 		printf(ERROR_PREFIX "pthread_join\nn");
101 		exit(PTS_PASS);
102 	}
103 
104 	/* test the result */
105 	if(priority_correct != 1) {
106 		printf("Test FAILED. Priority set incorrectly\n");
107 		exit(PTS_FAIL);
108 	}
109 	if(policy_correct != 1) {
110 		printf("Test FAILED. Policy set incorrectly\n");
111 		exit(PTS_FAIL);
112 	}
113 
114 	printf("Test PASS\n");
115 	exit(PTS_PASS);
116 }
117