1bb535300SJeff Roberson /* 2bb535300SJeff Roberson * Copyright (c) 1998 Daniel Eischen <eischen@vigrid.com>. 3bb535300SJeff Roberson * All rights reserved. 4bb535300SJeff Roberson * 5bb535300SJeff Roberson * Redistribution and use in source and binary forms, with or without 6bb535300SJeff Roberson * modification, are permitted provided that the following conditions 7bb535300SJeff Roberson * are met: 8bb535300SJeff Roberson * 1. Redistributions of source code must retain the above copyright 9bb535300SJeff Roberson * notice, this list of conditions and the following disclaimer. 10bb535300SJeff Roberson * 2. Redistributions in binary form must reproduce the above copyright 11bb535300SJeff Roberson * notice, this list of conditions and the following disclaimer in the 12bb535300SJeff Roberson * documentation and/or other materials provided with the distribution. 13bb535300SJeff Roberson * 3. All advertising materials mentioning features or use of this software 14bb535300SJeff Roberson * must display the following acknowledgement: 15bb535300SJeff Roberson * This product includes software developed by Daniel Eischen. 16bb535300SJeff Roberson * 4. Neither the name of the author nor the names of any co-contributors 17bb535300SJeff Roberson * may be used to endorse or promote products derived from this software 18bb535300SJeff Roberson * without specific prior written permission. 19bb535300SJeff Roberson * 20bb535300SJeff Roberson * THIS SOFTWARE IS PROVIDED BY DANIEL EISCHEN AND CONTRIBUTORS ``AS IS'' AND 21bb535300SJeff Roberson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22bb535300SJeff Roberson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23bb535300SJeff Roberson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 24bb535300SJeff Roberson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25bb535300SJeff Roberson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26bb535300SJeff Roberson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27bb535300SJeff Roberson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28bb535300SJeff Roberson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29bb535300SJeff Roberson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30bb535300SJeff Roberson * SUCH DAMAGE. 31bb535300SJeff Roberson * 32bb535300SJeff Roberson * $FreeBSD$ 33bb535300SJeff Roberson */ 34a091d823SDavid Xu 35bb535300SJeff Roberson #include <errno.h> 36bb535300SJeff Roberson #include <sys/param.h> 37bb535300SJeff Roberson #include <pthread.h> 38a091d823SDavid Xu 39bb535300SJeff Roberson #include "thr_private.h" 40bb535300SJeff Roberson 41bb535300SJeff Roberson __weak_reference(_pthread_setschedparam, pthread_setschedparam); 42bb535300SJeff Roberson 43bb535300SJeff Roberson int 44bb535300SJeff Roberson _pthread_setschedparam(pthread_t pthread, int policy, 45bb535300SJeff Roberson const struct sched_param *param) 46bb535300SJeff Roberson { 47a091d823SDavid Xu struct pthread *curthread = _get_curthread(); 48a091d823SDavid Xu int in_syncq; 49a091d823SDavid Xu int in_readyq = 0; 50b3d73b9bSMike Makonnen int old_prio; 51a091d823SDavid Xu int ret = 0; 52bb535300SJeff Roberson 53a091d823SDavid Xu if ((param == NULL) || (policy < SCHED_FIFO) || (policy > SCHED_RR)) { 54a091d823SDavid Xu /* Return an invalid argument error: */ 55a091d823SDavid Xu ret = EINVAL; 56a091d823SDavid Xu } else if ((param->sched_priority < THR_MIN_PRIORITY) || 57a091d823SDavid Xu (param->sched_priority > THR_MAX_PRIORITY)) { 58a091d823SDavid Xu /* Return an unsupported value error. */ 59a091d823SDavid Xu ret = ENOTSUP; 60a091d823SDavid Xu 61a091d823SDavid Xu /* Find the thread in the list of active threads: */ 62a091d823SDavid Xu } else if ((ret = _thr_ref_add(curthread, pthread, /*include dead*/0)) 63a091d823SDavid Xu == 0) { 64a091d823SDavid Xu /* 65a091d823SDavid Xu * Lock the threads scheduling queue while we change 66a091d823SDavid Xu * its priority: 67a091d823SDavid Xu */ 68a091d823SDavid Xu THR_THREAD_LOCK(curthread, pthread); 69a091d823SDavid Xu if (pthread->state == PS_DEAD) { 70a091d823SDavid Xu THR_THREAD_UNLOCK(curthread, pthread); 71a091d823SDavid Xu _thr_ref_delete(curthread, pthread); 72b3d73b9bSMike Makonnen return (ESRCH); 73bb535300SJeff Roberson } 74a091d823SDavid Xu in_syncq = pthread->sflags & THR_FLAGS_IN_SYNCQ; 75bb535300SJeff Roberson 76a091d823SDavid Xu /* Set the scheduling policy: */ 77bb535300SJeff Roberson pthread->attr.sched_policy = policy; 78a091d823SDavid Xu 79a091d823SDavid Xu if (param->sched_priority == 80a091d823SDavid Xu THR_BASE_PRIORITY(pthread->base_priority)) 81a091d823SDavid Xu /* 82a091d823SDavid Xu * There is nothing to do; unlock the threads 83a091d823SDavid Xu * scheduling queue. 84a091d823SDavid Xu */ 85a091d823SDavid Xu THR_THREAD_UNLOCK(curthread, pthread); 86a091d823SDavid Xu else { 87a091d823SDavid Xu /* 88a091d823SDavid Xu * Remove the thread from its current priority 89a091d823SDavid Xu * queue before any adjustments are made to its 90a091d823SDavid Xu * active priority: 91a091d823SDavid Xu */ 92a091d823SDavid Xu old_prio = pthread->active_priority; 93a091d823SDavid Xu /* if ((pthread->flags & THR_FLAGS_IN_RUNQ) != 0) */ { 94a091d823SDavid Xu in_readyq = 1; 95a091d823SDavid Xu /* THR_RUNQ_REMOVE(pthread); */ 96a091d823SDavid Xu } 97a091d823SDavid Xu 98a091d823SDavid Xu /* Set the thread base priority: */ 99a091d823SDavid Xu pthread->base_priority &= 100a091d823SDavid Xu (THR_SIGNAL_PRIORITY | THR_RT_PRIORITY); 101a091d823SDavid Xu pthread->base_priority = param->sched_priority; 102a091d823SDavid Xu 103a091d823SDavid Xu /* Recalculate the active priority: */ 104a091d823SDavid Xu pthread->active_priority = MAX(pthread->base_priority, 105a091d823SDavid Xu pthread->inherited_priority); 106a091d823SDavid Xu 107a091d823SDavid Xu if (in_readyq) { 108a091d823SDavid Xu if ((pthread->priority_mutex_count > 0) && 109a091d823SDavid Xu (old_prio > pthread->active_priority)) { 110a091d823SDavid Xu /* 111a091d823SDavid Xu * POSIX states that if the priority is 112a091d823SDavid Xu * being lowered, the thread must be 113a091d823SDavid Xu * inserted at the head of the queue for 114a091d823SDavid Xu * its priority if it owns any priority 115a091d823SDavid Xu * protection or inheritence mutexes. 116a091d823SDavid Xu */ 117a091d823SDavid Xu /* THR_RUNQ_INSERT_HEAD(pthread); */ 118a091d823SDavid Xu } 119a091d823SDavid Xu else 120a091d823SDavid Xu /* THR_RUNQ_INSERT_TAIL(pthread)*/ ; 121a091d823SDavid Xu } 122a091d823SDavid Xu 123a091d823SDavid Xu /* Unlock the threads scheduling queue: */ 124a091d823SDavid Xu THR_THREAD_UNLOCK(curthread, pthread); 125a091d823SDavid Xu 126a091d823SDavid Xu /* 127a091d823SDavid Xu * Check for any mutex priority adjustments. This 128a091d823SDavid Xu * includes checking for a priority mutex on which 129a091d823SDavid Xu * this thread is waiting. 130a091d823SDavid Xu */ 131a091d823SDavid Xu _mutex_notify_priochange(curthread, pthread, in_syncq); 132a091d823SDavid Xu } 133a091d823SDavid Xu _thr_ref_delete(curthread, pthread); 134a091d823SDavid Xu } 135a091d823SDavid Xu return (ret); 136bb535300SJeff Roberson } 137