xref: /freebsd/lib/libthr/thread/thr_setschedparam.c (revision bb535300dd871731b2542594a917bc2892479ca0)
1bb535300SJeff Roberson /*
2bb535300SJeff Roberson  * Copyright (c) 1998 Daniel Eischen <eischen@vigrid.com>.
3bb535300SJeff Roberson  * All rights reserved.
4bb535300SJeff Roberson  *
5bb535300SJeff Roberson  * Redistribution and use in source and binary forms, with or without
6bb535300SJeff Roberson  * modification, are permitted provided that the following conditions
7bb535300SJeff Roberson  * are met:
8bb535300SJeff Roberson  * 1. Redistributions of source code must retain the above copyright
9bb535300SJeff Roberson  *    notice, this list of conditions and the following disclaimer.
10bb535300SJeff Roberson  * 2. Redistributions in binary form must reproduce the above copyright
11bb535300SJeff Roberson  *    notice, this list of conditions and the following disclaimer in the
12bb535300SJeff Roberson  *    documentation and/or other materials provided with the distribution.
13bb535300SJeff Roberson  * 3. All advertising materials mentioning features or use of this software
14bb535300SJeff Roberson  *    must display the following acknowledgement:
15bb535300SJeff Roberson  *	This product includes software developed by Daniel Eischen.
16bb535300SJeff Roberson  * 4. Neither the name of the author nor the names of any co-contributors
17bb535300SJeff Roberson  *    may be used to endorse or promote products derived from this software
18bb535300SJeff Roberson  *    without specific prior written permission.
19bb535300SJeff Roberson  *
20bb535300SJeff Roberson  * THIS SOFTWARE IS PROVIDED BY DANIEL EISCHEN AND CONTRIBUTORS ``AS IS'' AND
21bb535300SJeff Roberson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22bb535300SJeff Roberson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23bb535300SJeff Roberson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24bb535300SJeff Roberson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25bb535300SJeff Roberson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26bb535300SJeff Roberson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27bb535300SJeff Roberson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28bb535300SJeff Roberson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29bb535300SJeff Roberson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30bb535300SJeff Roberson  * SUCH DAMAGE.
31bb535300SJeff Roberson  *
32bb535300SJeff Roberson  * $FreeBSD$
33bb535300SJeff Roberson  */
34bb535300SJeff Roberson #include <errno.h>
35bb535300SJeff Roberson #include <sys/param.h>
36bb535300SJeff Roberson #include <pthread.h>
37bb535300SJeff Roberson #include "thr_private.h"
38bb535300SJeff Roberson 
39bb535300SJeff Roberson __weak_reference(_pthread_setschedparam, pthread_setschedparam);
40bb535300SJeff Roberson 
41bb535300SJeff Roberson int
42bb535300SJeff Roberson _pthread_setschedparam(pthread_t pthread, int policy,
43bb535300SJeff Roberson 	const struct sched_param *param)
44bb535300SJeff Roberson {
45bb535300SJeff Roberson #if 0	/* XXXTHR */
46bb535300SJeff Roberson 	int old_prio, in_readyq = 0, ret = 0;
47bb535300SJeff Roberson #endif
48bb535300SJeff Roberson 
49bb535300SJeff Roberson 	if ((param == NULL) || (policy < SCHED_FIFO) || (policy > SCHED_RR))
50bb535300SJeff Roberson 		return (EINVAL);
51bb535300SJeff Roberson 
52bb535300SJeff Roberson 	if ((param->sched_priority < PTHREAD_MIN_PRIORITY) ||
53bb535300SJeff Roberson 	    (param->sched_priority > PTHREAD_MAX_PRIORITY))
54bb535300SJeff Roberson 		return (ENOTSUP);
55bb535300SJeff Roberson 
56bb535300SJeff Roberson 	return (0);
57bb535300SJeff Roberson #if 0	/* XXXTHR */
58bb535300SJeff Roberson 	/* Find the thread in the list of active threads: */
59bb535300SJeff Roberson 	if ((ret = _find_thread(pthread)) == 0) {
60bb535300SJeff Roberson 		GIANT_LOCK();
61bb535300SJeff Roberson 
62bb535300SJeff Roberson 		if (param->sched_priority !=
63bb535300SJeff Roberson 		    PTHREAD_BASE_PRIORITY(pthread->base_priority)) {
64bb535300SJeff Roberson 			/*
65bb535300SJeff Roberson 			 * Remove the thread from its current priority
66bb535300SJeff Roberson 			 * queue before any adjustments are made to its
67bb535300SJeff Roberson 			 * active priority:
68bb535300SJeff Roberson 			 */
69bb535300SJeff Roberson 			old_prio = pthread->active_priority;
70bb535300SJeff Roberson 			if ((pthread->flags & PTHREAD_FLAGS_IN_PRIOQ) != 0) {
71bb535300SJeff Roberson 				in_readyq = 1;
72bb535300SJeff Roberson 				PTHREAD_PRIOQ_REMOVE(pthread);
73bb535300SJeff Roberson 			}
74bb535300SJeff Roberson 
75bb535300SJeff Roberson 			/* Set the thread base priority: */
76bb535300SJeff Roberson 			pthread->base_priority &=
77bb535300SJeff Roberson 			    (PTHREAD_SIGNAL_PRIORITY | PTHREAD_RT_PRIORITY);
78bb535300SJeff Roberson 			pthread->base_priority = param->sched_priority;
79bb535300SJeff Roberson 
80bb535300SJeff Roberson 			/* Recalculate the active priority: */
81bb535300SJeff Roberson 			pthread->active_priority = MAX(pthread->base_priority,
82bb535300SJeff Roberson 			    pthread->inherited_priority);
83bb535300SJeff Roberson 
84bb535300SJeff Roberson 			if (in_readyq) {
85bb535300SJeff Roberson 				if ((pthread->priority_mutex_count > 0) &&
86bb535300SJeff Roberson 				    (old_prio > pthread->active_priority)) {
87bb535300SJeff Roberson 					/*
88bb535300SJeff Roberson 					 * POSIX states that if the priority is
89bb535300SJeff Roberson 					 * being lowered, the thread must be
90bb535300SJeff Roberson 					 * inserted at the head of the queue for
91bb535300SJeff Roberson 					 * its priority if it owns any priority
92bb535300SJeff Roberson 					 * protection or inheritence mutexes.
93bb535300SJeff Roberson 					 */
94bb535300SJeff Roberson 					PTHREAD_PRIOQ_INSERT_HEAD(pthread);
95bb535300SJeff Roberson 				}
96bb535300SJeff Roberson 				else
97bb535300SJeff Roberson 					PTHREAD_PRIOQ_INSERT_TAIL(pthread);
98bb535300SJeff Roberson 			}
99bb535300SJeff Roberson 
100bb535300SJeff Roberson 			/*
101bb535300SJeff Roberson 			 * Check for any mutex priority adjustments.  This
102bb535300SJeff Roberson 			 * includes checking for a priority mutex on which
103bb535300SJeff Roberson 			 * this thread is waiting.
104bb535300SJeff Roberson 			 */
105bb535300SJeff Roberson 			_mutex_notify_priochange(pthread);
106bb535300SJeff Roberson 		}
107bb535300SJeff Roberson 
108bb535300SJeff Roberson 		/* Set the scheduling policy: */
109bb535300SJeff Roberson 		pthread->attr.sched_policy = policy;
110bb535300SJeff Roberson 
111bb535300SJeff Roberson 		GIANT_UNLOCK();
112bb535300SJeff Roberson 	}
113bb535300SJeff Roberson 	return(ret);
114bb535300SJeff Roberson #endif
115bb535300SJeff Roberson }
116