Home | History | Annotate | Download | only in pthread_attr_setschedpolicy
      1 /*
      2  *
      3  *   Copyright (c) Novell Inc. 2011
      4  *
      5  *   This program is free software;  you can redistribute it and/or modify
      6  *   it under the terms of the GNU General Public License as published by
      7  *   the Free Software Foundation; either version 2 of the License, or
      8  *   (at your option) any later version.
      9  *
     10  *   This program is distributed in the hope that it will be useful,
     11  *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
     12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
     13  *   the GNU General Public License for more details.
     14  *
     15  *   You should have received a copy of the GNU General Public License
     16  *   along with this program;  if not, write to the Free Software
     17  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
     18  *
     19  *   Author:  Peter W. Morreale <pmorreale AT novell DOT com>
     20  *
     21  *   Date:  20/05/2011
     22  */
     23 
     24 #include <pthread.h>
     25 #include <stdio.h>
     26 #include <errno.h>
     27 #include <string.h>
     28 #include "posixtest.h"
     29 
     30 #define PRIORITY_OTHER -1
     31 #define PRIORITY_FIFO 20
     32 #define PRIORITY_RR 20
     33 
     34 #define ERR_MSG(p, f, rc)					\
     35 	printf("Failed: policy: %s, func: %s, rc: %s (%u)\n",	\
     36 				p, f, strerror(rc), rc)
     37 
     38 struct params {
     39 	int	policy;
     40 	int	priority;
     41 	char	*policy_label;
     42 	int	status;
     43 };
     44 
     45 static void *thread_func(void *data)
     46 {
     47 	pthread_t self;
     48 	struct sched_param sp;
     49 	struct params *p = data;
     50 	int policy;
     51 	int rc;
     52 
     53 	self = pthread_self();
     54 	rc = pthread_getschedparam(self, &policy, &sp);
     55 	if (rc != 0) {
     56 		ERR_MSG(p->policy_label, "pthread_getschedparam()", rc);
     57 		goto done;
     58 	}
     59 
     60 	p->status = PTS_FAIL;
     61 
     62 	if (policy != p->policy) {
     63 		printf("Failed: policy: %s thread policy: %u != %u\n",
     64 			p->policy_label, policy, p->policy);
     65 		goto done;
     66 	}
     67 
     68 	if (p->priority != PRIORITY_OTHER && sp.sched_priority != p->priority) {
     69 		printf("Failed: policy: %s thread priority %u != %u\n",
     70 			p->policy_label, sp.sched_priority, p->priority);
     71 		goto done;
     72 	}
     73 
     74 	p->status = PTS_PASS;
     75 
     76 done:
     77 	return NULL;
     78 }
     79 
     80 static int init_attr(pthread_attr_t *attr, struct params *p)
     81 {
     82 	int rc;
     83 	char *func;
     84 	struct sched_param sp;
     85 
     86 	func = "pthread_attr_init()";
     87 	rc = pthread_attr_init(attr);
     88 	if (rc != 0)
     89 		goto done;
     90 
     91 	func = "pthread_attr_setschedpolicy()";
     92 	rc = pthread_attr_setschedpolicy(attr, p->policy);
     93 	if (rc != 0)
     94 		goto error;
     95 
     96 	func = "pthread_attr_setinheritsched()";
     97 	rc = pthread_attr_setinheritsched(attr, PTHREAD_EXPLICIT_SCHED);
     98 	if (rc != 0)
     99 		goto error;
    100 
    101 	func = "pthread_attr_setschedparam()";
    102 	sp.sched_priority = p->priority;
    103 	if (p->priority != PRIORITY_OTHER) {
    104 		rc = pthread_attr_setschedparam(attr, &sp);
    105 		if (rc != 0)
    106 			goto error;
    107 	}
    108 
    109 	return PTS_PASS;
    110 
    111 error:
    112 	pthread_attr_destroy(attr);
    113 done:
    114 	ERR_MSG(p->policy_label, func, rc);
    115 	return PTS_UNRESOLVED;
    116 }
    117 
    118 static int create_test_thread(struct params *p)
    119 {
    120 	pthread_t thread;
    121 	pthread_attr_t attr;
    122 	void *status;
    123 	int rc;
    124 
    125 	rc = init_attr(&attr, p);
    126 	if (rc != PTS_PASS)
    127 		return rc;
    128 
    129 	rc = pthread_create(&thread, &attr, thread_func, p);
    130 	if (rc != 0) {
    131 		ERR_MSG(p->policy_label, "pthread_create()", rc);
    132 		if (rc == EPERM)
    133 			return PTS_UNRESOLVED;
    134 		else
    135 			return PTS_FAIL;
    136 	}
    137 
    138 	pthread_join(thread, &status);
    139 
    140 	pthread_attr_destroy(&attr);
    141 
    142 	return p->status;
    143 }
    144