Home | History | Annotate | Download | only in pthread_attr_setschedparam
      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  * pthread_attr_setschedparam()
      9 
     10  * 1. Create a pthread_attr object and initialize it
     11  * 2. Set the policy and priority in that object
     12  * 3. Create a thread with this object
     13 
     14  * Test SCHED_RR
     15  */
     16 
     17 #include <pthread.h>
     18 #include <stdio.h>
     19 #include <stdlib.h>
     20 #include <errno.h>
     21 #include "posixtest.h"
     22 
     23 #define TEST "1-2"
     24 #define FUNCTION "pthread_attr_setschedparam"
     25 #define ERROR_PREFIX "unexpected error: " FUNCTION " " TEST ": "
     26 
     27 #define RRPOLICY SCHED_RR
     28 
     29 volatile int thread_created = 0;
     30 
     31 void *thread_func()
     32 {
     33 	thread_created = 1;
     34 	pthread_exit(0);
     35 	return (void *)(0);
     36 }
     37 
     38 int main(void)
     39 {
     40 	pthread_t thread;
     41 	pthread_attr_t attr;
     42 	void *status;
     43 	int rc = 0;
     44 	int policy = RRPOLICY;
     45 	struct sched_param param;
     46 	int priority;
     47 
     48 	rc = pthread_attr_init(&attr);
     49 	if (rc != 0) {
     50 		printf(ERROR_PREFIX "pthread_attr_init\n");
     51 		exit(PTS_UNRESOLVED);
     52 	}
     53 
     54 	rc = pthread_attr_setschedpolicy(&attr, policy);
     55 	if (rc != 0) {
     56 		printf(ERROR_PREFIX "pthread_attr_setschedpolicy\n");
     57 		exit(PTS_FAIL);
     58 	}
     59 
     60 	priority = sched_get_priority_max(policy);
     61 	if (priority == -1) {
     62 		printf(ERROR_PREFIX "sched_priority_get_max\n");
     63 		exit(PTS_FAIL);
     64 	}
     65 
     66 	param.sched_priority = priority;
     67 	rc = pthread_attr_setschedparam(&attr, &param);
     68 	if (rc != 0) {
     69 		printf(ERROR_PREFIX "pthread_attr_setschedparam\n");
     70 		exit(PTS_FAIL);
     71 	}
     72 
     73 	rc = pthread_create(&thread, &attr, thread_func, NULL);
     74 	if (rc != 0) {
     75 		if (rc == EPERM) {
     76 			printf(ERROR_PREFIX
     77 			       "Permission Denied when creating thread with policy %d\n",
     78 			       policy);
     79 			exit(PTS_UNRESOLVED);
     80 		} else {
     81 			printf(ERROR_PREFIX "pthread_create()\n");
     82 			exit(PTS_FAIL);
     83 		}
     84 	}
     85 
     86 	pthread_join(thread, &status);
     87 	pthread_attr_destroy(&attr);
     88 
     89 	if (thread_created == 0) {
     90 		printf(ERROR_PREFIX "Thread was not created\n");
     91 		exit(PTS_FAIL);
     92 	}
     93 
     94 	printf("Test PASSED\n");
     95 	exit(PTS_PASS);
     96 }
     97