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-1"
25 #define AREA "scheduler"
26 #define ERROR_PREFIX "unexpected error: " AREA " " TEST ": "
27 
28 #define PRIORITY 20
29 #define POLICY SCHED_FIFO
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 	if(pthread_getschedparam(pthread_self(), &policy, &param) != 0) {
43 		printf(ERROR_PREFIX "pthread_getschedparam\n");
44 		exit(PTS_UNRESOLVED);
45 	}
46 	if(policy == POLICY) {
47 		policy_correct = 1;
48 	}
49 	if(param.sched_priority == PRIORITY) {
50 		priority_correct = 1;
51 	}
52 	return NULL;
53 }
54 
main()55 int main()
56 {
57 	pthread_t            thread_id;
58 	pthread_attr_t       attr;
59 	struct sched_param   param;
60 	int                  rc = 0;
61 
62 	/* initialze the attribute and set policy and priority in it*/
63 	rc = pthread_attr_init(&attr);
64 	if(rc != 0) {
65 		printf(ERROR_PREFIX "pthread_attr_init\n");
66 		exit(PTS_UNRESOLVED);
67 	}
68 	rc = pthread_attr_setschedpolicy(&attr, POLICY);
69 	if(rc != 0) {
70 		printf(ERROR_PREFIX "pthread_attr_setschedpolicy\n");
71 		exit(PTS_UNRESOLVED);
72 	}
73 	param.sched_priority = PRIORITY;
74 	rc = pthread_attr_setschedparam(&attr, &param);
75 	if(rc != 0) {
76 		printf(ERROR_PREFIX "pthread_attr_setschedparam\n");
77 		exit(PTS_UNRESOLVED);
78 	}
79 
80 	rc = pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
81 	if(rc != 0) {
82 		printf(ERROR_PREFIX "pthread_attr_setinheritsched\n");
83 		exit(PTS_UNRESOLVED);
84 	}
85 
86 	/* Create the thread with the attr */
87 	rc = pthread_create(&thread_id, &attr, thread, NULL);
88 	if(rc != 0) {
89 		printf(ERROR_PREFIX "pthread_create\n");
90 		exit(PTS_UNRESOLVED);
91 	}
92 
93 	rc = pthread_join(thread_id, NULL);
94 	if(rc != 0) {
95 		printf(ERROR_PREFIX "pthread_join\n");
96 		exit(PTS_UNRESOLVED);
97 	}
98 
99 	/* test the result */
100 	if(priority_correct != 1) {
101 		printf("Test FAILED. Priority set incorrectly\n");
102 		exit(PTS_FAIL);
103 	}
104 	if(policy_correct != 1) {
105 		printf("Test FAILED. Policy set incorrectly\n");
106 		exit(PTS_FAIL);
107 	}
108 
109 	printf("Test PASS\n");
110 	exit(PTS_PASS);
111 }
112