Home | History | Annotate | Download | only in pthread_attr_setinheritsched
      1 /*
      2  * Copyright (c) 2004, Intel Corporation. All rights reserved.
      3  * Created by:  crystal.xiong REMOVE-THIS AT intel 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_setinheritsched()
      9  *
     10  * Steps:
     11  * 1.  Initialize pthread_attr_t object (attr)
     12  * 2.  Set schedule policy (policy) in attr to SCHED_FIFO
     13  * 3.  Set inheritsched to PTHREAD_EXPLICIT_SCHED in attr
     14  * 4.  Call pthread_create with attr
     15  * 5.  Call pthread_getschedparam in the created thread and get the
     16  *     policy value(new_policy)
     17  * 6.  Compare new_policy with SCHED_OTHER. SCHED_OTHER is the
     18  *     default policy value in the creating thread. if new_policy is
     19  *     equal to SCHED_OTHER, the case fails.
     20  *
     21  * - adam.li (at) intel.com: 2004-04-30
     22  *  add code pthread_attr_setschedparam(). Otherwise pthread_create()
     23  *  will fail with EINVAL. And, the perror() thing is not correct.
     24  */
     25 
     26 #include <errno.h>
     27 #include <pthread.h>
     28 #include <stdio.h>
     29 #include <string.h>
     30 #include <stdlib.h>
     31 #include "posixtest.h"
     32 
     33 #define TEST "2-2"
     34 #define FUNCTION "pthread_attr_setinheritsched"
     35 #define ERROR_PREFIX "unexpected error: " FUNCTION " " TEST ": "
     36 
     37 const long int policy = SCHED_FIFO;
     38 const long int old_policy = SCHED_OTHER;
     39 void *thread_func(void *arg)
     40 {
     41 	int rc;
     42 	int new_policy;
     43 	pthread_t self = pthread_self();
     44 
     45 	struct sched_param param;
     46 	memset(&param, 0, sizeof(param));
     47 
     48 	rc = pthread_getschedparam(self, &new_policy, &param);
     49 	if (rc != 0) {
     50 		perror(ERROR_PREFIX "pthread_getschedparam");
     51 		exit(PTS_UNRESOLVED);
     52 	}
     53 	if (new_policy == old_policy) {
     54 		fprintf(stderr, ERROR_PREFIX "The scheduling attribute should "
     55 			"not be inherited from creating thread \n");
     56 		exit(PTS_FAIL);
     57 	}
     58 	pthread_exit(0);
     59 	return NULL;
     60 }
     61 
     62 int main(void)
     63 {
     64 	pthread_t new_th;
     65 	pthread_attr_t attr;
     66 	int rc;
     67 	struct sched_param sp;
     68 
     69 	/* Initialize attr */
     70 	rc = pthread_attr_init(&attr);
     71 	if (rc != 0) {
     72 		printf(ERROR_PREFIX "pthread_attr_init");
     73 		exit(PTS_UNRESOLVED);
     74 	}
     75 
     76 	rc = pthread_attr_setschedpolicy(&attr, policy);
     77 	if (rc != 0) {
     78 		printf(ERROR_PREFIX "pthread_attr_setschedpolicy");
     79 		exit(PTS_UNRESOLVED);
     80 	}
     81 
     82 	sp.sched_priority = 1;
     83 	rc = pthread_attr_setschedparam(&attr, &sp);
     84 	if (rc != 0) {
     85 		printf(ERROR_PREFIX "pthread_attr_setschedparam");
     86 		exit(PTS_UNRESOLVED);
     87 	}
     88 
     89 	int insched = PTHREAD_EXPLICIT_SCHED;
     90 	rc = pthread_attr_setinheritsched(&attr, insched);
     91 	if (rc != 0) {
     92 		printf(ERROR_PREFIX "pthread_attr_setinheritsched");
     93 		exit(PTS_UNRESOLVED);
     94 	}
     95 
     96 	rc = pthread_create(&new_th, &attr, thread_func, NULL);
     97 	if (rc != 0) {
     98 		printf("Error at pthread_create(): %s\n", strerror(rc));
     99 		exit(PTS_UNRESOLVED);
    100 	}
    101 
    102 	rc = pthread_join(new_th, NULL);
    103 	if (rc != 0) {
    104 		printf(ERROR_PREFIX "pthread_join");
    105 		exit(PTS_UNRESOLVED);
    106 	}
    107 	rc = pthread_attr_destroy(&attr);
    108 	if (rc != 0) {
    109 		printf(ERROR_PREFIX "pthread_attr_destroy");
    110 		exit(PTS_UNRESOLVED);
    111 	}
    112 	printf("Test PASSED\n");
    113 	return PTS_PASS;
    114 }
    115