thr_setschedparam.c revision 71581
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 71581 2001-01-24 13:03:38Z deischen $ 33254721Semaste */ 34254721Semaste#include <errno.h> 35254721Semaste#include <sys/param.h> 36254721Semaste#include <pthread.h> 37254721Semaste#include "pthread_private.h" 38254721Semaste 39254721Semaste#pragma weak pthread_setschedparam=_pthread_setschedparam 40254721Semaste 41254721Semasteint 42254721Semaste_pthread_setschedparam(pthread_t pthread, int policy, 43254721Semaste const struct sched_param *param) 44254721Semaste{ 45254721Semaste int old_prio, in_readyq = 0, ret = 0; 46254721Semaste 47254721Semaste if ((param == NULL) || (policy < SCHED_FIFO) || (policy > SCHED_RR)) { 48254721Semaste /* Return an invalid argument error: */ 49254721Semaste ret = EINVAL; 50254721Semaste } else if ((param->sched_priority < PTHREAD_MIN_PRIORITY) || 51254721Semaste (param->sched_priority > PTHREAD_MAX_PRIORITY)) { 52254721Semaste /* Return an unsupported value error. */ 53254721Semaste ret = ENOTSUP; 54254721Semaste 55254721Semaste /* Find the thread in the list of active threads: */ 56254721Semaste } else if ((ret = _find_thread(pthread)) == 0) { 57254721Semaste /* 58254721Semaste * Defer signals to protect the scheduling queues from 59254721Semaste * access by the signal handler: 60254721Semaste */ 61254721Semaste _thread_kern_sig_defer(); 62254721Semaste 63254721Semaste if (param->sched_priority != 64254721Semaste PTHREAD_BASE_PRIORITY(pthread->base_priority)) { 65254721Semaste /* 66254721Semaste * Remove the thread from its current priority 67254721Semaste * queue before any adjustments are made to its 68254721Semaste * active priority: 69254721Semaste */ 70254721Semaste old_prio = pthread->active_priority; 71254721Semaste if ((pthread->flags & PTHREAD_FLAGS_IN_PRIOQ) != 0) { 72254721Semaste in_readyq = 1; 73254721Semaste PTHREAD_PRIOQ_REMOVE(pthread); 74254721Semaste } 75254721Semaste 76254721Semaste /* Set the thread base priority: */ 77254721Semaste pthread->base_priority &= 78254721Semaste (PTHREAD_SIGNAL_PRIORITY | PTHREAD_RT_PRIORITY); 79254721Semaste pthread->base_priority = param->sched_priority; 80254721Semaste 81254721Semaste /* Recalculate the active priority: */ 82254721Semaste pthread->active_priority = MAX(pthread->base_priority, 83254721Semaste pthread->inherited_priority); 84254721Semaste 85254721Semaste if (in_readyq) { 86254721Semaste if ((pthread->priority_mutex_count > 0) && 87254721Semaste (old_prio > pthread->active_priority)) { 88254721Semaste /* 89254721Semaste * POSIX states that if the priority is 90254721Semaste * being lowered, the thread must be 91254721Semaste * inserted at the head of the queue for 92254721Semaste * its priority if it owns any priority 93254721Semaste * protection or inheritence mutexes. 94254721Semaste */ 95254721Semaste PTHREAD_PRIOQ_INSERT_HEAD(pthread); 96254721Semaste } 97254721Semaste else 98254721Semaste PTHREAD_PRIOQ_INSERT_TAIL(pthread); 99254721Semaste } 100254721Semaste 101254721Semaste /* 102254721Semaste * Check for any mutex priority adjustments. This 103254721Semaste * includes checking for a priority mutex on which 104254721Semaste * this thread is waiting. 105254721Semaste */ 106254721Semaste _mutex_notify_priochange(pthread); 107254721Semaste } 108254721Semaste 109254721Semaste /* Set the scheduling policy: */ 110254721Semaste pthread->attr.sched_policy = policy; 111254721Semaste 112254721Semaste /* 113254721Semaste * Undefer and handle pending signals, yielding if 114254721Semaste * necessary: 115254721Semaste */ 116254721Semaste _thread_kern_sig_undefer(); 117254721Semaste } 118254721Semaste return(ret); 119254721Semaste} 120254721Semaste