thr_setschedparam.c revision 50476
1117395Skan/* 2117395Skan * Copyright (c) 1998 Daniel Eischen <eischen@vigrid.com>. 3117395Skan * All rights reserved. 4117395Skan * 5117395Skan * Redistribution and use in source and binary forms, with or without 6117395Skan * modification, are permitted provided that the following conditions 7117395Skan * are met: 8117395Skan * 1. Redistributions of source code must retain the above copyright 9117395Skan * notice, this list of conditions and the following disclaimer. 10117395Skan * 2. Redistributions in binary form must reproduce the above copyright 11117395Skan * notice, this list of conditions and the following disclaimer in the 12117395Skan * documentation and/or other materials provided with the distribution. 13117395Skan * 3. All advertising materials mentioning features or use of this software 14117395Skan * must display the following acknowledgement: 15117395Skan * This product includes software developed by Daniel Eischen. 16117395Skan * 4. Neither the name of the author nor the names of any co-contributors 17117395Skan * may be used to endorse or promote products derived from this software 18117395Skan * without specific prior written permission. 19117395Skan * 20117395Skan * THIS SOFTWARE IS PROVIDED BY DANIEL EISCHEN AND CONTRIBUTORS ``AS IS'' AND 21117395Skan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22117395Skan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23117395Skan * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 24117395Skan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25117395Skan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26117395Skan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27117395Skan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28117395Skan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29117395Skan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30117395Skan * SUCH DAMAGE. 31117395Skan * 32117395Skan * $FreeBSD: head/lib/libkse/thread/thr_setschedparam.c 50476 1999-08-28 00:22:10Z peter $ 33117395Skan */ 34117395Skan#include <errno.h> 35117395Skan#include <sys/param.h> 36117395Skan#ifdef _THREAD_SAFE 37117395Skan#include <pthread.h> 38117395Skan#include "pthread_private.h" 39117395Skan 40117395Skanint 41117395Skanpthread_setschedparam(pthread_t pthread, int policy, struct sched_param *param) 42117395Skan{ 43117395Skan int old_prio, in_readyq = 0, ret = 0; 44117395Skan 45117395Skan if ((param == NULL) || (param->sched_priority < PTHREAD_MIN_PRIORITY) || 46117395Skan (param->sched_priority > PTHREAD_MAX_PRIORITY) || 47117395Skan (policy < SCHED_FIFO) || (policy > SCHED_RR)) 48117395Skan /* Return an invalid argument error: */ 49117395Skan ret = EINVAL; 50117395Skan 51117395Skan /* Find the thread in the list of active threads: */ 52117395Skan else if ((ret = _find_thread(pthread)) == 0) { 53117395Skan /* 54117395Skan * Defer signals to protect the scheduling queues from 55117395Skan * access by the signal handler: 56117395Skan */ 57117395Skan _thread_kern_sig_defer(); 58117395Skan 59117395Skan if (param->sched_priority != pthread->base_priority) { 60117395Skan /* 61117395Skan * Remove the thread from its current priority 62117395Skan * queue before any adjustments are made to its 63117395Skan * active priority: 64117395Skan */ 65117395Skan if ((pthread->flags & PTHREAD_FLAGS_IN_PRIOQ) != 0) { 66117395Skan in_readyq = 1; 67117395Skan old_prio = pthread->active_priority; 68117395Skan PTHREAD_PRIOQ_REMOVE(pthread); 69117395Skan } 70117395Skan 71117395Skan /* Set the thread base priority: */ 72117395Skan pthread->base_priority = param->sched_priority; 73117395Skan 74117395Skan /* Recalculate the active priority: */ 75117395Skan pthread->active_priority = MAX(pthread->base_priority, 76117395Skan pthread->inherited_priority); 77117395Skan 78117395Skan if (in_readyq) { 79117395Skan if ((pthread->priority_mutex_count > 0) && 80117395Skan (old_prio > pthread->active_priority)) { 81117395Skan /* 82117395Skan * POSIX states that if the priority is 83117395Skan * being lowered, the thread must be 84117395Skan * inserted at the head of the queue for 85117395Skan * its priority if it owns any priority 86117395Skan * protection or inheritence mutexes. 87117395Skan */ 88117395Skan PTHREAD_PRIOQ_INSERT_HEAD(pthread); 89117395Skan } 90117395Skan else 91117395Skan PTHREAD_PRIOQ_INSERT_TAIL(pthread); 92117395Skan } 93117395Skan 94117395Skan /* 95117395Skan * Check for any mutex priority adjustments. This 96117395Skan * includes checking for a priority mutex on which 97117395Skan * this thread is waiting. 98117395Skan */ 99117395Skan _mutex_notify_priochange(pthread); 100117395Skan } 101117395Skan 102117395Skan /* Set the scheduling policy: */ 103117395Skan pthread->attr.sched_policy = policy; 104117395Skan 105117395Skan /* 106117395Skan * Undefer and handle pending signals, yielding if 107117395Skan * necessary: 108117395Skan */ 109117395Skan _thread_kern_sig_undefer(); 110117395Skan } 111117395Skan return(ret); 112117395Skan} 113117395Skan#endif 114117395Skan