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_attr_setinheritsched() sets the inheritance of 9 * policy and priority. 10 11 * Steps: 12 * 1. Create a pthread_attr struct using pthread_attr_init 13 * 2. Change main's priority and policy 14 * 3. Set the inherit sched in it by calling pthread_attr_setinheritsched 15 * 4. Create a new thread. 16 * 5. Check that it has correct priority and policy 17 */ 18 19 #include <pthread.h> 20 #include <stdio.h> 21 #include <stdlib.h> 22 #include "posixtest.h" 23 24 #define TEST "4-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 /* Flags that the threads use to indicate events */ 32 int policy_correct = -1; 33 int priority_correct = -1; 34 35 void *thread(void *tmp) 36 { 37 struct sched_param param; 38 int policy; 39 int rc = 0; 40 41 rc = pthread_getschedparam(pthread_self(), &policy, ¶m); 42 if (rc != 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 53 return NULL; 54 } 55 56 int main(void) 57 { 58 pthread_attr_t attr; 59 pthread_t thread_id; 60 struct sched_param param; 61 int rc = 0; 62 63 param.sched_priority = PRIORITY; 64 65 rc = pthread_setschedparam(pthread_self(), POLICY, ¶m); 66 if (rc != 0) { 67 printf(ERROR_PREFIX "pthread_setschedparam\n"); 68 exit(PTS_UNRESOLVED); 69 } 70 71 rc = pthread_attr_init(&attr); 72 if (rc != 0) { 73 printf(ERROR_PREFIX "pthread_attr_init\n"); 74 exit(PTS_UNRESOLVED); 75 } 76 77 rc = pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED); 78 if (rc != 0) { 79 printf(ERROR_PREFIX "pthread_attr_setinheritsched\n"); 80 printf("Test FAILED\n"); 81 exit(PTS_FAIL); 82 } 83 84 rc = pthread_create(&thread_id, &attr, thread, NULL); 85 if (rc != 0) { 86 printf(ERROR_PREFIX "pthread_create\n"); 87 exit(PTS_UNRESOLVED); 88 } 89 90 rc = pthread_join(thread_id, NULL); 91 if (rc != 0) { 92 printf(ERROR_PREFIX "pthread_join\n"); 93 exit(PTS_UNRESOLVED); 94 } 95 96 if ((priority_correct != 1) || (policy_correct != 1)) { 97 printf("Test FAIL\n"); 98 exit(PTS_FAIL); 99 } 100 101 printf("Test PASSED\n"); 102 exit(PTS_PASS); 103 } 104