thr_setschedparam.c revision 112918
1101099Srwatson/*
2126097Srwatson * Copyright (c) 1998 Daniel Eischen <eischen@vigrid.com>.
3140628Srwatson * All rights reserved.
4101099Srwatson *
5101099Srwatson * Redistribution and use in source and binary forms, with or without
6101099Srwatson * modification, are permitted provided that the following conditions
7101099Srwatson * are met:
8140628Srwatson * 1. Redistributions of source code must retain the above copyright
9140628Srwatson *    notice, this list of conditions and the following disclaimer.
10140628Srwatson * 2. Redistributions in binary form must reproduce the above copyright
11140628Srwatson *    notice, this list of conditions and the following disclaimer in the
12101099Srwatson *    documentation and/or other materials provided with the distribution.
13101099Srwatson * 3. All advertising materials mentioning features or use of this software
14101099Srwatson *    must display the following acknowledgement:
15101099Srwatson *	This product includes software developed by Daniel Eischen.
16101099Srwatson * 4. Neither the name of the author nor the names of any co-contributors
17101099Srwatson *    may be used to endorse or promote products derived from this software
18101099Srwatson *    without specific prior written permission.
19101099Srwatson *
20101099Srwatson * THIS SOFTWARE IS PROVIDED BY DANIEL EISCHEN AND CONTRIBUTORS ``AS IS'' AND
21101099Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22101099Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23101099Srwatson * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24101099Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25101099Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26101099Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27101099Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28101099Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29101099Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30101099Srwatson * SUCH DAMAGE.
31101099Srwatson *
32101099Srwatson * $FreeBSD: head/lib/libthr/thread/thr_setschedparam.c 112918 2003-04-01 03:46:29Z jeff $
33101099Srwatson */
34101099Srwatson#include <errno.h>
35101099Srwatson#include <sys/param.h>
36101099Srwatson#include <pthread.h>
37101099Srwatson#include "thr_private.h"
38101099Srwatson
39101099Srwatson__weak_reference(_pthread_setschedparam, pthread_setschedparam);
40101099Srwatson
41101099Srwatsonint
42101099Srwatson_pthread_setschedparam(pthread_t pthread, int policy,
43101099Srwatson	const struct sched_param *param)
44101099Srwatson{
45101099Srwatson#if 0	/* XXXTHR */
46105988Srwatson	int old_prio, in_readyq = 0, ret = 0;
47101099Srwatson#endif
48164184Strhodes
49101099Srwatson	if ((param == NULL) || (policy < SCHED_FIFO) || (policy > SCHED_RR))
50103183Sbde		return (EINVAL);
51145076Scsjp
52101099Srwatson	if ((param->sched_priority < PTHREAD_MIN_PRIORITY) ||
53101099Srwatson	    (param->sched_priority > PTHREAD_MAX_PRIORITY))
54115497Srwatson		return (ENOTSUP);
55101099Srwatson
56101099Srwatson	return (0);
57101099Srwatson#if 0	/* XXXTHR */
58105696Srwatson	/* Find the thread in the list of active threads: */
59101099Srwatson	if ((ret = _find_thread(pthread)) == 0) {
60101099Srwatson		GIANT_LOCK();
61101099Srwatson
62101099Srwatson		if (param->sched_priority !=
63101099Srwatson		    PTHREAD_BASE_PRIORITY(pthread->base_priority)) {
64150340Sphk			/*
65101099Srwatson			 * Remove the thread from its current priority
66140628Srwatson			 * queue before any adjustments are made to its
67140628Srwatson			 * active priority:
68140628Srwatson			 */
69101099Srwatson			old_prio = pthread->active_priority;
70101099Srwatson			if ((pthread->flags & PTHREAD_FLAGS_IN_PRIOQ) != 0) {
71101099Srwatson				in_readyq = 1;
72101099Srwatson				PTHREAD_PRIOQ_REMOVE(pthread);
73101099Srwatson			}
74101099Srwatson
75101099Srwatson			/* Set the thread base priority: */
76101099Srwatson			pthread->base_priority &=
77101099Srwatson			    (PTHREAD_SIGNAL_PRIORITY | PTHREAD_RT_PRIORITY);
78122875Srwatson			pthread->base_priority = param->sched_priority;
79101099Srwatson
80101099Srwatson			/* Recalculate the active priority: */
81122879Srwatson			pthread->active_priority = MAX(pthread->base_priority,
82101099Srwatson			    pthread->inherited_priority);
83101099Srwatson
84101099Srwatson			if (in_readyq) {
85101099Srwatson				if ((pthread->priority_mutex_count > 0) &&
86101099Srwatson				    (old_prio > pthread->active_priority)) {
87101099Srwatson					/*
88101099Srwatson					 * POSIX states that if the priority is
89101099Srwatson					 * being lowered, the thread must be
90101099Srwatson					 * inserted at the head of the queue for
91101099Srwatson					 * its priority if it owns any priority
92101099Srwatson					 * protection or inheritence mutexes.
93105988Srwatson					 */
94105988Srwatson					PTHREAD_PRIOQ_INSERT_HEAD(pthread);
95105988Srwatson				}
96105988Srwatson				else
97107731Srwatson					PTHREAD_PRIOQ_INSERT_TAIL(pthread);
98101099Srwatson			}
99101099Srwatson
100102980Srwatson			/*
101101099Srwatson			 * Check for any mutex priority adjustments.  This
102101099Srwatson			 * includes checking for a priority mutex on which
103101099Srwatson			 * this thread is waiting.
104101099Srwatson			 */
105101099Srwatson			_mutex_notify_priochange(pthread);
106101099Srwatson		}
107101099Srwatson
108101099Srwatson		/* Set the scheduling policy: */
109101099Srwatson		pthread->attr.sched_policy = policy;
110101099Srwatson
111101099Srwatson		GIANT_UNLOCK();
112101099Srwatson	}
113101099Srwatson	return(ret);
114101099Srwatson#endif
115101099Srwatson}
116101099Srwatson