Home | History | Annotate | Download | only in pthread_attr_setinheritsched
      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.  Set the inherit sched in it by calling pthread_attr_setinheritsched
     14  * 3.  Change main's priority and policy
     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-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 /* 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, &param);
     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_attr_init(&attr);
     66 	if (rc != 0) {
     67 		printf(ERROR_PREFIX "pthread_attr_init\n");
     68 		exit(PTS_UNRESOLVED);
     69 	}
     70 
     71 	rc = pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED);
     72 	if (rc != 0) {
     73 		printf(ERROR_PREFIX "pthread_attr_setinheritsched\n");
     74 		exit(PTS_UNRESOLVED);
     75 	}
     76 
     77 	rc = pthread_setschedparam(pthread_self(), POLICY, &param);
     78 	if (rc != 0) {
     79 		printf(ERROR_PREFIX "pthread_setschedparam\n");
     80 		exit(PTS_UNRESOLVED);
     81 	}
     82 
     83 	rc = pthread_create(&thread_id, &attr, thread, NULL);
     84 	if (rc != 0) {
     85 		printf(ERROR_PREFIX "pthread_create\n");
     86 		exit(PTS_UNRESOLVED);
     87 	}
     88 
     89 	rc = pthread_join(thread_id, NULL);
     90 	if (rc != 0) {
     91 		printf(ERROR_PREFIX "pthread_join\n");
     92 		exit(PTS_UNRESOLVED);
     93 	}
     94 
     95 	if ((priority_correct != 1) || (policy_correct != 1)) {
     96 		printf("Test FAILED\n");
     97 		exit(PTS_FAIL);
     98 	}
     99 
    100 	printf("Test PASSED\n");
    101 	exit(PTS_PASS);
    102 }
    103