thr_setschedparam.c revision 67097
1254721Semaste/*
2254721Semaste * Copyright (c) 1998 Daniel Eischen <eischen@vigrid.com>.
3254721Semaste * All rights reserved.
4254721Semaste *
5254721Semaste * Redistribution and use in source and binary forms, with or without
6254721Semaste * modification, are permitted provided that the following conditions
7254721Semaste * are met:
8254721Semaste * 1. Redistributions of source code must retain the above copyright
9254721Semaste *    notice, this list of conditions and the following disclaimer.
10254721Semaste * 2. Redistributions in binary form must reproduce the above copyright
11254721Semaste *    notice, this list of conditions and the following disclaimer in the
12254721Semaste *    documentation and/or other materials provided with the distribution.
13254721Semaste * 3. All advertising materials mentioning features or use of this software
14254721Semaste *    must display the following acknowledgement:
15254721Semaste *	This product includes software developed by Daniel Eischen.
16254721Semaste * 4. Neither the name of the author nor the names of any co-contributors
17254721Semaste *    may be used to endorse or promote products derived from this software
18254721Semaste *    without specific prior written permission.
19254721Semaste *
20254721Semaste * THIS SOFTWARE IS PROVIDED BY DANIEL EISCHEN AND CONTRIBUTORS ``AS IS'' AND
21254721Semaste * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22254721Semaste * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23254721Semaste * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24254721Semaste * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25254721Semaste * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26254721Semaste * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27254721Semaste * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28254721Semaste * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29254721Semaste * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30254721Semaste * SUCH DAMAGE.
31254721Semaste *
32254721Semaste * $FreeBSD: head/lib/libkse/thread/thr_setschedparam.c 67097 2000-10-13 22:12:32Z deischen $
33254721Semaste */
34254721Semaste#include <errno.h>
35254721Semaste#include <sys/param.h>
36254721Semaste#ifdef _THREAD_SAFE
37254721Semaste#include <pthread.h>
38254721Semaste#include "pthread_private.h"
39254721Semaste
40254721Semasteint
41254721Semastepthread_setschedparam(pthread_t pthread, int policy,
42254721Semaste	const struct sched_param *param)
43254721Semaste{
44254721Semaste	int old_prio, in_readyq = 0, ret = 0;
45254721Semaste
46254721Semaste	if ((param == NULL) || (policy < SCHED_FIFO) || (policy > SCHED_RR)) {
47254721Semaste		/* Return an invalid argument error: */
48254721Semaste		ret = EINVAL;
49254721Semaste	} else if ((param->sched_priority < PTHREAD_MIN_PRIORITY) ||
50254721Semaste	    (param->sched_priority > PTHREAD_MAX_PRIORITY)) {
51254721Semaste		/* Return an unsupported value error. */
52254721Semaste		ret = ENOTSUP;
53254721Semaste
54254721Semaste	/* Find the thread in the list of active threads: */
55254721Semaste	} else if ((ret = _find_thread(pthread)) == 0) {
56254721Semaste		/*
57254721Semaste		 * Defer signals to protect the scheduling queues from
58254721Semaste		 * access by the signal handler:
59254721Semaste		 */
60254721Semaste		_thread_kern_sig_defer();
61254721Semaste
62254721Semaste		if (param->sched_priority !=
63254721Semaste		    PTHREAD_BASE_PRIORITY(pthread->base_priority)) {
64254721Semaste			/*
65254721Semaste			 * Remove the thread from its current priority
66254721Semaste			 * queue before any adjustments are made to its
67254721Semaste			 * active priority:
68254721Semaste			 */
69254721Semaste			old_prio = pthread->active_priority;
70254721Semaste			if ((pthread->flags & PTHREAD_FLAGS_IN_PRIOQ) != 0) {
71254721Semaste				in_readyq = 1;
72254721Semaste				PTHREAD_PRIOQ_REMOVE(pthread);
73254721Semaste			}
74254721Semaste
75254721Semaste			/* Set the thread base priority: */
76254721Semaste			pthread->base_priority &=
77254721Semaste			    (PTHREAD_SIGNAL_PRIORITY | PTHREAD_RT_PRIORITY);
78254721Semaste			pthread->base_priority = param->sched_priority;
79254721Semaste
80254721Semaste			/* Recalculate the active priority: */
81254721Semaste			pthread->active_priority = MAX(pthread->base_priority,
82254721Semaste			    pthread->inherited_priority);
83254721Semaste
84254721Semaste			if (in_readyq) {
85254721Semaste				if ((pthread->priority_mutex_count > 0) &&
86254721Semaste				    (old_prio > pthread->active_priority)) {
87254721Semaste					/*
88254721Semaste					 * POSIX states that if the priority is
89254721Semaste					 * being lowered, the thread must be
90254721Semaste					 * inserted at the head of the queue for
91254721Semaste					 * its priority if it owns any priority
92254721Semaste					 * protection or inheritence mutexes.
93254721Semaste					 */
94254721Semaste					PTHREAD_PRIOQ_INSERT_HEAD(pthread);
95254721Semaste				}
96254721Semaste				else
97254721Semaste					PTHREAD_PRIOQ_INSERT_TAIL(pthread);
98254721Semaste			}
99254721Semaste
100254721Semaste			/*
101263363Semaste			 * Check for any mutex priority adjustments.  This
102254721Semaste			 * includes checking for a priority mutex on which
103254721Semaste			 * this thread is waiting.
104254721Semaste			 */
105254721Semaste			_mutex_notify_priochange(pthread);
106254721Semaste		}
107254721Semaste
108254721Semaste		/* Set the scheduling policy: */
109254721Semaste		pthread->attr.sched_policy = policy;
110254721Semaste
111254721Semaste		/*
112254721Semaste		 * Undefer and handle pending signals, yielding if
113254721Semaste		 * necessary:
114254721Semaste		 */
115254721Semaste		_thread_kern_sig_undefer();
116254721Semaste	}
117254721Semaste	return(ret);
118254721Semaste}
119254721Semaste#endif
120254721Semaste