thr_setschedparam.c revision 50476
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 50476 1999-08-28 00:22:10Z peter $ 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 4144963Sjbpthread_setschedparam(pthread_t pthread, int policy, struct sched_param *param) 4244963Sjb{ 4344963Sjb int old_prio, in_readyq = 0, ret = 0; 4444963Sjb 4544963Sjb if ((param == NULL) || (param->sched_priority < PTHREAD_MIN_PRIORITY) || 4644963Sjb (param->sched_priority > PTHREAD_MAX_PRIORITY) || 4744963Sjb (policy < SCHED_FIFO) || (policy > SCHED_RR)) 4844963Sjb /* Return an invalid argument error: */ 4944963Sjb ret = EINVAL; 5044963Sjb 5144963Sjb /* Find the thread in the list of active threads: */ 5244963Sjb else if ((ret = _find_thread(pthread)) == 0) { 5344963Sjb /* 5448046Sjb * Defer signals to protect the scheduling queues from 5548046Sjb * access by the signal handler: 5644963Sjb */ 5748046Sjb _thread_kern_sig_defer(); 5844963Sjb 5944963Sjb if (param->sched_priority != pthread->base_priority) { 6044963Sjb /* 6144963Sjb * Remove the thread from its current priority 6244963Sjb * queue before any adjustments are made to its 6344963Sjb * active priority: 6444963Sjb */ 6548046Sjb if ((pthread->flags & PTHREAD_FLAGS_IN_PRIOQ) != 0) { 6644963Sjb in_readyq = 1; 6744963Sjb old_prio = pthread->active_priority; 6844963Sjb PTHREAD_PRIOQ_REMOVE(pthread); 6944963Sjb } 7044963Sjb 7144963Sjb /* Set the thread base priority: */ 7244963Sjb pthread->base_priority = param->sched_priority; 7344963Sjb 7444963Sjb /* Recalculate the active priority: */ 7544963Sjb pthread->active_priority = MAX(pthread->base_priority, 7644963Sjb pthread->inherited_priority); 7744963Sjb 7844963Sjb if (in_readyq) { 7944963Sjb if ((pthread->priority_mutex_count > 0) && 8044963Sjb (old_prio > pthread->active_priority)) { 8144963Sjb /* 8244963Sjb * POSIX states that if the priority is 8344963Sjb * being lowered, the thread must be 8444963Sjb * inserted at the head of the queue for 8544963Sjb * its priority if it owns any priority 8644963Sjb * protection or inheritence mutexes. 8744963Sjb */ 8844963Sjb PTHREAD_PRIOQ_INSERT_HEAD(pthread); 8944963Sjb } 9044963Sjb else 9144963Sjb PTHREAD_PRIOQ_INSERT_TAIL(pthread); 9244963Sjb } 9344963Sjb 9444963Sjb /* 9544963Sjb * Check for any mutex priority adjustments. This 9644963Sjb * includes checking for a priority mutex on which 9744963Sjb * this thread is waiting. 9844963Sjb */ 9944963Sjb _mutex_notify_priochange(pthread); 10044963Sjb } 10144963Sjb 10244963Sjb /* Set the scheduling policy: */ 10344963Sjb pthread->attr.sched_policy = policy; 10444963Sjb 10544963Sjb /* 10648046Sjb * Undefer and handle pending signals, yielding if 10748046Sjb * necessary: 10844963Sjb */ 10948046Sjb _thread_kern_sig_undefer(); 11044963Sjb } 11144963Sjb return(ret); 11244963Sjb} 11344963Sjb#endif 114