thr_setschedparam.c revision 55194
144963Sjb/*
244963Sjb * Copyright (c) 1998 Daniel Eischen <eischen@vigrid.com>.
344963Sjb * All rights reserved.
444963Sjb *
544963Sjb * Redistribution and use in source and binary forms, with or without
644963Sjb * modification, are permitted provided that the following conditions
744963Sjb * are met:
844963Sjb * 1. Redistributions of source code must retain the above copyright
944963Sjb *    notice, this list of conditions and the following disclaimer.
1044963Sjb * 2. Redistributions in binary form must reproduce the above copyright
1144963Sjb *    notice, this list of conditions and the following disclaimer in the
1244963Sjb *    documentation and/or other materials provided with the distribution.
1344963Sjb * 3. All advertising materials mentioning features or use of this software
1444963Sjb *    must display the following acknowledgement:
1544963Sjb *	This product includes software developed by Daniel Eischen.
1644963Sjb * 4. Neither the name of the author nor the names of any co-contributors
1744963Sjb *    may be used to endorse or promote products derived from this software
1844963Sjb *    without specific prior written permission.
1944963Sjb *
2044963Sjb * THIS SOFTWARE IS PROVIDED BY DANIEL EISCHEN AND CONTRIBUTORS ``AS IS'' AND
2144963Sjb * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2244963Sjb * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2344963Sjb * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2444963Sjb * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2544963Sjb * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2644963Sjb * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2744963Sjb * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2844963Sjb * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2944963Sjb * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3044963Sjb * SUCH DAMAGE.
3144963Sjb *
3250476Speter * $FreeBSD: head/lib/libkse/thread/thr_setschedparam.c 55194 1999-12-28 18:13:04Z deischen $
3344963Sjb */
3444963Sjb#include <errno.h>
3544963Sjb#include <sys/param.h>
3644963Sjb#ifdef _THREAD_SAFE
3744963Sjb#include <pthread.h>
3844963Sjb#include "pthread_private.h"
3944963Sjb
4044963Sjbint
4153812Salfredpthread_setschedparam(pthread_t pthread, int policy,
4253812Salfred	const struct sched_param *param)
4344963Sjb{
4444963Sjb	int old_prio, in_readyq = 0, ret = 0;
4544963Sjb
4644963Sjb	if ((param == NULL) || (param->sched_priority < PTHREAD_MIN_PRIORITY) ||
4744963Sjb	    (param->sched_priority > PTHREAD_MAX_PRIORITY) ||
4844963Sjb	    (policy < SCHED_FIFO) || (policy > SCHED_RR))
4944963Sjb		/* Return an invalid argument error: */
5044963Sjb		ret = EINVAL;
5144963Sjb
5244963Sjb	/* Find the thread in the list of active threads: */
5344963Sjb	else if ((ret = _find_thread(pthread)) == 0) {
5444963Sjb		/*
5548046Sjb		 * Defer signals to protect the scheduling queues from
5648046Sjb		 * access by the signal handler:
5744963Sjb		 */
5848046Sjb		_thread_kern_sig_defer();
5944963Sjb
6044963Sjb		if (param->sched_priority != pthread->base_priority) {
6144963Sjb			/*
6244963Sjb			 * Remove the thread from its current priority
6344963Sjb			 * queue before any adjustments are made to its
6444963Sjb			 * active priority:
6544963Sjb			 */
6655194Sdeischen			old_prio = pthread->active_priority;
6748046Sjb			if ((pthread->flags & PTHREAD_FLAGS_IN_PRIOQ) != 0) {
6844963Sjb				in_readyq = 1;
6944963Sjb				PTHREAD_PRIOQ_REMOVE(pthread);
7044963Sjb			}
7144963Sjb
7244963Sjb			/* Set the thread base priority: */
7344963Sjb			pthread->base_priority = param->sched_priority;
7444963Sjb
7544963Sjb			/* Recalculate the active priority: */
7644963Sjb			pthread->active_priority = MAX(pthread->base_priority,
7744963Sjb			    pthread->inherited_priority);
7844963Sjb
7944963Sjb			if (in_readyq) {
8044963Sjb				if ((pthread->priority_mutex_count > 0) &&
8144963Sjb				    (old_prio > pthread->active_priority)) {
8244963Sjb					/*
8344963Sjb					 * POSIX states that if the priority is
8444963Sjb					 * being lowered, the thread must be
8544963Sjb					 * inserted at the head of the queue for
8644963Sjb					 * its priority if it owns any priority
8744963Sjb					 * protection or inheritence mutexes.
8844963Sjb					 */
8944963Sjb					PTHREAD_PRIOQ_INSERT_HEAD(pthread);
9044963Sjb				}
9144963Sjb				else
9244963Sjb					PTHREAD_PRIOQ_INSERT_TAIL(pthread);
9344963Sjb			}
9444963Sjb
9544963Sjb			/*
9644963Sjb			 * Check for any mutex priority adjustments.  This
9744963Sjb			 * includes checking for a priority mutex on which
9844963Sjb			 * this thread is waiting.
9944963Sjb			 */
10044963Sjb			_mutex_notify_priochange(pthread);
10144963Sjb		}
10244963Sjb
10344963Sjb		/* Set the scheduling policy: */
10444963Sjb		pthread->attr.sched_policy = policy;
10544963Sjb
10644963Sjb		/*
10748046Sjb		 * Undefer and handle pending signals, yielding if
10848046Sjb		 * necessary:
10944963Sjb		 */
11048046Sjb		_thread_kern_sig_undefer();
11144963Sjb	}
11244963Sjb	return(ret);
11344963Sjb}
11444963Sjb#endif
115