thr_setschedparam.c revision 103388
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 103388 2002-09-16 08:45:36Z mini $
3344963Sjb */
3444963Sjb#include <errno.h>
3544963Sjb#include <sys/param.h>
3644963Sjb#include <pthread.h>
37103388Smini#include "thr_private.h"
3844963Sjb
3975369Sdeischen__weak_reference(_pthread_setschedparam, pthread_setschedparam);
4071581Sdeischen
4144963Sjbint
4271581Sdeischen_pthread_setschedparam(pthread_t pthread, int policy,
4353812Salfred	const struct sched_param *param)
4444963Sjb{
4544963Sjb	int old_prio, in_readyq = 0, ret = 0;
4644963Sjb
4759892Sjasone	if ((param == NULL) || (policy < SCHED_FIFO) || (policy > SCHED_RR)) {
4844963Sjb		/* Return an invalid argument error: */
4944963Sjb		ret = EINVAL;
5059892Sjasone	} else if ((param->sched_priority < PTHREAD_MIN_PRIORITY) ||
5159892Sjasone	    (param->sched_priority > PTHREAD_MAX_PRIORITY)) {
5259892Sjasone		/* Return an unsupported value error. */
5359892Sjasone		ret = ENOTSUP;
5444963Sjb
5544963Sjb	/* Find the thread in the list of active threads: */
5659892Sjasone	} else if ((ret = _find_thread(pthread)) == 0) {
5744963Sjb		/*
5848046Sjb		 * Defer signals to protect the scheduling queues from
5948046Sjb		 * access by the signal handler:
6044963Sjb		 */
6148046Sjb		_thread_kern_sig_defer();
6244963Sjb
6367097Sdeischen		if (param->sched_priority !=
6467097Sdeischen		    PTHREAD_BASE_PRIORITY(pthread->base_priority)) {
6544963Sjb			/*
6644963Sjb			 * Remove the thread from its current priority
6744963Sjb			 * queue before any adjustments are made to its
6844963Sjb			 * active priority:
6944963Sjb			 */
7055194Sdeischen			old_prio = pthread->active_priority;
7148046Sjb			if ((pthread->flags & PTHREAD_FLAGS_IN_PRIOQ) != 0) {
7244963Sjb				in_readyq = 1;
7344963Sjb				PTHREAD_PRIOQ_REMOVE(pthread);
7444963Sjb			}
7544963Sjb
7644963Sjb			/* Set the thread base priority: */
7767097Sdeischen			pthread->base_priority &=
7867097Sdeischen			    (PTHREAD_SIGNAL_PRIORITY | PTHREAD_RT_PRIORITY);
7944963Sjb			pthread->base_priority = param->sched_priority;
8044963Sjb
8144963Sjb			/* Recalculate the active priority: */
8244963Sjb			pthread->active_priority = MAX(pthread->base_priority,
8344963Sjb			    pthread->inherited_priority);
8444963Sjb
8544963Sjb			if (in_readyq) {
8644963Sjb				if ((pthread->priority_mutex_count > 0) &&
8744963Sjb				    (old_prio > pthread->active_priority)) {
8844963Sjb					/*
8944963Sjb					 * POSIX states that if the priority is
9044963Sjb					 * being lowered, the thread must be
9144963Sjb					 * inserted at the head of the queue for
9244963Sjb					 * its priority if it owns any priority
9344963Sjb					 * protection or inheritence mutexes.
9444963Sjb					 */
9544963Sjb					PTHREAD_PRIOQ_INSERT_HEAD(pthread);
9644963Sjb				}
9744963Sjb				else
9844963Sjb					PTHREAD_PRIOQ_INSERT_TAIL(pthread);
9944963Sjb			}
10044963Sjb
10144963Sjb			/*
10244963Sjb			 * Check for any mutex priority adjustments.  This
10344963Sjb			 * includes checking for a priority mutex on which
10444963Sjb			 * this thread is waiting.
10544963Sjb			 */
10644963Sjb			_mutex_notify_priochange(pthread);
10744963Sjb		}
10844963Sjb
10944963Sjb		/* Set the scheduling policy: */
11044963Sjb		pthread->attr.sched_policy = policy;
11144963Sjb
11244963Sjb		/*
11348046Sjb		 * Undefer and handle pending signals, yielding if
11448046Sjb		 * necessary:
11544963Sjb		 */
11648046Sjb		_thread_kern_sig_undefer();
11744963Sjb	}
11844963Sjb	return(ret);
11944963Sjb}
120