thr_setschedparam.c revision 59892
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 59892 2000-05-02 06:51:40Z jasone $ 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 4659892Sjasone if ((param == NULL) || (policy < SCHED_FIFO) || (policy > SCHED_RR)) { 4744963Sjb /* Return an invalid argument error: */ 4844963Sjb ret = EINVAL; 4959892Sjasone } else if ((param->sched_priority < PTHREAD_MIN_PRIORITY) || 5059892Sjasone (param->sched_priority > PTHREAD_MAX_PRIORITY)) { 5159892Sjasone /* Return an unsupported value error. */ 5259892Sjasone ret = ENOTSUP; 5344963Sjb 5444963Sjb /* Find the thread in the list of active threads: */ 5559892Sjasone } else if ((ret = _find_thread(pthread)) == 0) { 5644963Sjb /* 5748046Sjb * Defer signals to protect the scheduling queues from 5848046Sjb * access by the signal handler: 5944963Sjb */ 6048046Sjb _thread_kern_sig_defer(); 6144963Sjb 6244963Sjb if (param->sched_priority != pthread->base_priority) { 6344963Sjb /* 6444963Sjb * Remove the thread from its current priority 6544963Sjb * queue before any adjustments are made to its 6644963Sjb * active priority: 6744963Sjb */ 6855194Sdeischen old_prio = pthread->active_priority; 6948046Sjb if ((pthread->flags & PTHREAD_FLAGS_IN_PRIOQ) != 0) { 7044963Sjb in_readyq = 1; 7144963Sjb PTHREAD_PRIOQ_REMOVE(pthread); 7244963Sjb } 7344963Sjb 7444963Sjb /* Set the thread base priority: */ 7544963Sjb pthread->base_priority = param->sched_priority; 7644963Sjb 7744963Sjb /* Recalculate the active priority: */ 7844963Sjb pthread->active_priority = MAX(pthread->base_priority, 7944963Sjb pthread->inherited_priority); 8044963Sjb 8144963Sjb if (in_readyq) { 8244963Sjb if ((pthread->priority_mutex_count > 0) && 8344963Sjb (old_prio > pthread->active_priority)) { 8444963Sjb /* 8544963Sjb * POSIX states that if the priority is 8644963Sjb * being lowered, the thread must be 8744963Sjb * inserted at the head of the queue for 8844963Sjb * its priority if it owns any priority 8944963Sjb * protection or inheritence mutexes. 9044963Sjb */ 9144963Sjb PTHREAD_PRIOQ_INSERT_HEAD(pthread); 9244963Sjb } 9344963Sjb else 9444963Sjb PTHREAD_PRIOQ_INSERT_TAIL(pthread); 9544963Sjb } 9644963Sjb 9744963Sjb /* 9844963Sjb * Check for any mutex priority adjustments. This 9944963Sjb * includes checking for a priority mutex on which 10044963Sjb * this thread is waiting. 10144963Sjb */ 10244963Sjb _mutex_notify_priochange(pthread); 10344963Sjb } 10444963Sjb 10544963Sjb /* Set the scheduling policy: */ 10644963Sjb pthread->attr.sched_policy = policy; 10744963Sjb 10844963Sjb /* 10948046Sjb * Undefer and handle pending signals, yielding if 11048046Sjb * necessary: 11144963Sjb */ 11248046Sjb _thread_kern_sig_undefer(); 11344963Sjb } 11444963Sjb return(ret); 11544963Sjb} 11644963Sjb#endif 117