Home | History | Annotate | Download | only in pthread_attr_getschedparam
      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 pthread_attr_getschedparam()
      9  *
     10  * Steps:
     11  * 1.  Initialize a pthread_attr_t object using pthread_attr_init()
     12  * 2.  Call pthread_attr_setschedpolicy with policy parameter
     13  * 3.  Call pthread_attr_setschedparam with a sched param parameter
     14  * 4.  Call pthread_attr_getschedparam to get the sched param
     15  *
     16  */
     17 
     18 #include <pthread.h>
     19 #include <stdio.h>
     20 #include <stdlib.h>
     21 #include "posixtest.h"
     22 
     23 #define TEST "1-1"
     24 #define FUNCTION "pthread_attr_getschedparam"
     25 #define ERROR_PREFIX "unexpected error: " FUNCTION " " TEST ": "
     26 
     27 #define FIFOPOLICY SCHED_FIFO
     28 #define RRPOLICY SCHED_RR
     29 #define OTHERPOLICY SCHED_OTHER
     30 
     31 int verify_param(pthread_attr_t * attr, int priority)
     32 {
     33 	int rc;
     34 	struct sched_param param;
     35 
     36 	rc = pthread_attr_getschedparam(attr, &param);
     37 	if (rc != 0) {
     38 		printf(ERROR_PREFIX "pthread_attr_getschedparam\n");
     39 		exit(PTS_UNRESOLVED);
     40 	}
     41 	if (priority != param.sched_priority) {
     42 		printf(ERROR_PREFIX "got wrong sched param\n");
     43 		exit(PTS_FAIL);
     44 	}
     45 	return 0;
     46 }
     47 
     48 int main(void)
     49 {
     50 	int rc = 0;
     51 	pthread_attr_t attr;
     52 	struct sched_param param;
     53 	int priority;
     54 
     55 	rc = pthread_attr_init(&attr);
     56 	if (rc != 0) {
     57 		printf(ERROR_PREFIX "pthread_attr_init\n");
     58 		exit(PTS_UNRESOLVED);
     59 	}
     60 
     61 	rc = pthread_attr_setschedpolicy(&attr, FIFOPOLICY);
     62 	if (rc != 0) {
     63 		printf(ERROR_PREFIX "pthread_attr_setschedpolicy\n");
     64 		exit(PTS_UNRESOLVED);
     65 	}
     66 	priority = sched_get_priority_max(FIFOPOLICY);
     67 	if (priority == -1) {
     68 		printf(ERROR_PREFIX "sched_get_priority_max\n");
     69 		exit(PTS_UNRESOLVED);
     70 	}
     71 	param.sched_priority = priority;
     72 	rc = pthread_attr_setschedparam(&attr, &param);
     73 	if (rc != 0) {
     74 		printf(ERROR_PREFIX "pthread_attr_setschedparam\n");
     75 		exit(PTS_UNRESOLVED);
     76 	}
     77 	verify_param(&attr, priority);
     78 
     79 	rc = pthread_attr_setschedpolicy(&attr, RRPOLICY);
     80 	if (rc != 0) {
     81 		printf(ERROR_PREFIX "pthread_attr_setschedpolicy\n");
     82 		exit(PTS_UNRESOLVED);
     83 	}
     84 	priority = sched_get_priority_max(RRPOLICY);
     85 	if (priority == -1) {
     86 		printf(ERROR_PREFIX "sched_get_priority_max\n");
     87 		exit(PTS_UNRESOLVED);
     88 	}
     89 	param.sched_priority = priority;
     90 	rc = pthread_attr_setschedparam(&attr, &param);
     91 	if (rc != 0) {
     92 		printf(ERROR_PREFIX "pthread_attr_setschedparam\n");
     93 		exit(PTS_UNRESOLVED);
     94 	}
     95 	verify_param(&attr, priority);
     96 
     97 	rc = pthread_attr_destroy(&attr);
     98 	if (rc != 0) {
     99 		printf(ERROR_PREFIX "pthread_attr_destroy\n");
    100 		exit(PTS_UNRESOLVED);
    101 	}
    102 	printf("Test PASSED\n");
    103 	return PTS_PASS;
    104 }
    105