thr_setschedparam.c revision 144518
1235288Sadrian/* 2235288Sadrian * Copyright (c) 1998 Daniel Eischen <eischen@vigrid.com>. 3235288Sadrian * All rights reserved. 4235288Sadrian * 5235288Sadrian * Redistribution and use in source and binary forms, with or without 6235288Sadrian * modification, are permitted provided that the following conditions 7235288Sadrian * are met: 8235288Sadrian * 1. Redistributions of source code must retain the above copyright 9235288Sadrian * notice, this list of conditions and the following disclaimer. 10235288Sadrian * 2. Redistributions in binary form must reproduce the above copyright 11235288Sadrian * notice, this list of conditions and the following disclaimer in the 12235288Sadrian * documentation and/or other materials provided with the distribution. 13235288Sadrian * 3. All advertising materials mentioning features or use of this software 14235288Sadrian * must display the following acknowledgement: 15235288Sadrian * This product includes software developed by Daniel Eischen. 16235288Sadrian * 4. Neither the name of the author nor the names of any co-contributors 17235288Sadrian * may be used to endorse or promote products derived from this software 18235288Sadrian * without specific prior written permission. 19235288Sadrian * 20235288Sadrian * THIS SOFTWARE IS PROVIDED BY DANIEL EISCHEN AND CONTRIBUTORS ``AS IS'' AND 21235288Sadrian * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22235288Sadrian * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23235288Sadrian * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 24235288Sadrian * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25235288Sadrian * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26235288Sadrian * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27235288Sadrian * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28235288Sadrian * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29235288Sadrian * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30235288Sadrian * SUCH DAMAGE. 31235288Sadrian * 32235288Sadrian * $FreeBSD: head/lib/libthr/thread/thr_setschedparam.c 144518 2005-04-02 01:20:00Z davidxu $ 33235288Sadrian */ 34235288Sadrian 35235288Sadrian#include <errno.h> 36235288Sadrian#include <sys/param.h> 37235288Sadrian#include <pthread.h> 38235288Sadrian 39235288Sadrian#include "thr_private.h" 40235288Sadrian 41235288Sadrian__weak_reference(_pthread_setschedparam, pthread_setschedparam); 42235288Sadrian 43235288Sadrianint 44235288Sadrian_pthread_setschedparam(pthread_t pthread, int policy, 45235288Sadrian const struct sched_param *param) 46235288Sadrian{ 47235288Sadrian struct pthread *curthread = _get_curthread(); 48235288Sadrian int in_syncq; 49235288Sadrian int in_readyq = 0; 50235288Sadrian int old_prio; 51235288Sadrian int ret = 0; 52235288Sadrian 53235288Sadrian if ((param == NULL) || (policy < SCHED_FIFO) || (policy > SCHED_RR)) { 54235288Sadrian /* Return an invalid argument error: */ 55235288Sadrian ret = EINVAL; 56235288Sadrian } else if ((param->sched_priority < THR_MIN_PRIORITY) || 57235288Sadrian (param->sched_priority > THR_MAX_PRIORITY)) { 58235288Sadrian /* Return an unsupported value error. */ 59235288Sadrian ret = ENOTSUP; 60235288Sadrian 61235288Sadrian /* Find the thread in the list of active threads: */ 62235288Sadrian } else if ((ret = _thr_ref_add(curthread, pthread, /*include dead*/0)) 63235288Sadrian == 0) { 64235288Sadrian /* 65235288Sadrian * Lock the threads scheduling queue while we change 66235288Sadrian * its priority: 67235288Sadrian */ 68235288Sadrian THR_THREAD_LOCK(curthread, pthread); 69235288Sadrian if (pthread->state == PS_DEAD) { 70235288Sadrian THR_THREAD_UNLOCK(curthread, pthread); 71235288Sadrian _thr_ref_delete(curthread, pthread); 72235288Sadrian return (ESRCH); 73235288Sadrian } 74235288Sadrian in_syncq = pthread->sflags & THR_FLAGS_IN_SYNCQ; 75235288Sadrian 76235288Sadrian /* Set the scheduling policy: */ 77235288Sadrian pthread->attr.sched_policy = policy; 78235288Sadrian 79235288Sadrian if (param->sched_priority == 80235288Sadrian THR_BASE_PRIORITY(pthread->base_priority)) 81235288Sadrian /* 82235288Sadrian * There is nothing to do; unlock the threads 83235288Sadrian * scheduling queue. 84235288Sadrian */ 85235288Sadrian THR_THREAD_UNLOCK(curthread, pthread); 86235288Sadrian else { 87235288Sadrian /* 88235288Sadrian * Remove the thread from its current priority 89235288Sadrian * queue before any adjustments are made to its 90235288Sadrian * active priority: 91235288Sadrian */ 92235288Sadrian old_prio = pthread->active_priority; 93 /* if ((pthread->flags & THR_FLAGS_IN_RUNQ) != 0) */ { 94 in_readyq = 1; 95 /* THR_RUNQ_REMOVE(pthread); */ 96 } 97 98 /* Set the thread base priority: */ 99 pthread->base_priority &= 100 (THR_SIGNAL_PRIORITY | THR_RT_PRIORITY); 101 pthread->base_priority = param->sched_priority; 102 103 /* Recalculate the active priority: */ 104 pthread->active_priority = MAX(pthread->base_priority, 105 pthread->inherited_priority); 106 107 if (in_readyq) { 108 if ((pthread->priority_mutex_count > 0) && 109 (old_prio > pthread->active_priority)) { 110 /* 111 * POSIX states that if the priority is 112 * being lowered, the thread must be 113 * inserted at the head of the queue for 114 * its priority if it owns any priority 115 * protection or inheritence mutexes. 116 */ 117 /* THR_RUNQ_INSERT_HEAD(pthread); */ 118 } 119 else 120 /* THR_RUNQ_INSERT_TAIL(pthread)*/ ; 121 } 122 123 /* Unlock the threads scheduling queue: */ 124 THR_THREAD_UNLOCK(curthread, pthread); 125 126 /* 127 * Check for any mutex priority adjustments. This 128 * includes checking for a priority mutex on which 129 * this thread is waiting. 130 */ 131 _mutex_notify_priochange(curthread, pthread, in_syncq); 132 } 133 _thr_ref_delete(curthread, pthread); 134 } 135 return (ret); 136} 137