xref: /dflybsd-src/lib/libthread_xu/thread/thr_setschedparam.c (revision fc71f871bbdd9cb103854bd01bd88d3baef664ee)
171b3fa15SDavid Xu /*
271b3fa15SDavid Xu  * Copyright (c) 1998 Daniel Eischen <eischen@vigrid.com>.
371b3fa15SDavid Xu  * All rights reserved.
471b3fa15SDavid Xu  *
571b3fa15SDavid Xu  * Redistribution and use in source and binary forms, with or without
671b3fa15SDavid Xu  * modification, are permitted provided that the following conditions
771b3fa15SDavid Xu  * are met:
871b3fa15SDavid Xu  * 1. Redistributions of source code must retain the above copyright
971b3fa15SDavid Xu  *    notice, this list of conditions and the following disclaimer.
1071b3fa15SDavid Xu  * 2. Redistributions in binary form must reproduce the above copyright
1171b3fa15SDavid Xu  *    notice, this list of conditions and the following disclaimer in the
1271b3fa15SDavid Xu  *    documentation and/or other materials provided with the distribution.
1371b3fa15SDavid Xu  * 3. All advertising materials mentioning features or use of this software
1471b3fa15SDavid Xu  *    must display the following acknowledgement:
1571b3fa15SDavid Xu  *	This product includes software developed by Daniel Eischen.
1671b3fa15SDavid Xu  * 4. Neither the name of the author nor the names of any co-contributors
1771b3fa15SDavid Xu  *    may be used to endorse or promote products derived from this software
1871b3fa15SDavid Xu  *    without specific prior written permission.
1971b3fa15SDavid Xu  *
2071b3fa15SDavid Xu  * THIS SOFTWARE IS PROVIDED BY DANIEL EISCHEN AND CONTRIBUTORS ``AS IS'' AND
2171b3fa15SDavid Xu  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2271b3fa15SDavid Xu  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2371b3fa15SDavid Xu  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2471b3fa15SDavid Xu  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2571b3fa15SDavid Xu  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2671b3fa15SDavid Xu  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2771b3fa15SDavid Xu  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2871b3fa15SDavid Xu  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2971b3fa15SDavid Xu  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3071b3fa15SDavid Xu  * SUCH DAMAGE.
3171b3fa15SDavid Xu  *
32*fc71f871SDavid Xu  * $DragonFly: src/lib/libthread_xu/thread/thr_setschedparam.c,v 1.5 2006/04/06 13:03:09 davidxu Exp $
3371b3fa15SDavid Xu  */
349e2ee207SJoerg Sonnenberger 
35*fc71f871SDavid Xu #include "namespace.h"
369e2ee207SJoerg Sonnenberger #include <machine/tls.h>
379e2ee207SJoerg Sonnenberger 
3871b3fa15SDavid Xu #include <sys/param.h>
39*fc71f871SDavid Xu #include <errno.h>
4071b3fa15SDavid Xu #include <pthread.h>
41*fc71f871SDavid Xu #include "un-namespace.h"
42*fc71f871SDavid Xu 
4371b3fa15SDavid Xu #include "thr_private.h"
4471b3fa15SDavid Xu 
4571b3fa15SDavid Xu int
4671b3fa15SDavid Xu _pthread_setschedparam(pthread_t pthread, int policy,
4771b3fa15SDavid Xu 	const struct sched_param *param)
4871b3fa15SDavid Xu {
499e2ee207SJoerg Sonnenberger 	struct pthread *curthread = tls_get_curthread();
5071b3fa15SDavid Xu 	int	ret = 0;
5171b3fa15SDavid Xu 
5271b3fa15SDavid Xu 	if ((param == NULL) || (policy < SCHED_FIFO) || (policy > SCHED_RR)) {
5371b3fa15SDavid Xu 		/* Return an invalid argument error: */
5471b3fa15SDavid Xu 		ret = EINVAL;
5571b3fa15SDavid Xu 	} else if ((param->sched_priority < THR_MIN_PRIORITY) ||
5671b3fa15SDavid Xu 	    (param->sched_priority > THR_MAX_PRIORITY)) {
5771b3fa15SDavid Xu 		/* Return an unsupported value error. */
5871b3fa15SDavid Xu 		ret = ENOTSUP;
5971b3fa15SDavid Xu 
6071b3fa15SDavid Xu 	/* Find the thread in the list of active threads: */
6171b3fa15SDavid Xu 	} else if ((ret = _thr_ref_add(curthread, pthread, /*include dead*/0))
6271b3fa15SDavid Xu 	    == 0) {
6371b3fa15SDavid Xu 		/*
6471b3fa15SDavid Xu 		 * Lock the threads scheduling queue while we change
6571b3fa15SDavid Xu 		 * its priority:
6671b3fa15SDavid Xu 		 */
6771b3fa15SDavid Xu 		THR_THREAD_LOCK(curthread, pthread);
6871b3fa15SDavid Xu 		if (pthread->state == PS_DEAD) {
6971b3fa15SDavid Xu 			THR_THREAD_UNLOCK(curthread, pthread);
7071b3fa15SDavid Xu 			_thr_ref_delete(curthread, pthread);
7171b3fa15SDavid Xu 			return (ESRCH);
7271b3fa15SDavid Xu 		}
7371b3fa15SDavid Xu 
7471b3fa15SDavid Xu 		/* Set the scheduling policy: */
7571b3fa15SDavid Xu 		pthread->attr.sched_policy = policy;
7671b3fa15SDavid Xu 
7771b3fa15SDavid Xu 		if (param->sched_priority ==
7871b3fa15SDavid Xu 		    THR_BASE_PRIORITY(pthread->base_priority))
7971b3fa15SDavid Xu 			/*
8071b3fa15SDavid Xu 			 * There is nothing to do; unlock the threads
8171b3fa15SDavid Xu 			 * scheduling queue.
8271b3fa15SDavid Xu 			 */
8371b3fa15SDavid Xu 			THR_THREAD_UNLOCK(curthread, pthread);
8471b3fa15SDavid Xu 		else {
8571b3fa15SDavid Xu 			/* Set the thread base priority: */
8671b3fa15SDavid Xu 			pthread->base_priority = param->sched_priority;
8771b3fa15SDavid Xu 
8871b3fa15SDavid Xu 			/* Recalculate the active priority: */
8971b3fa15SDavid Xu 			pthread->active_priority = MAX(pthread->base_priority,
9071b3fa15SDavid Xu 			    pthread->inherited_priority);
9171b3fa15SDavid Xu 
9271b3fa15SDavid Xu 			/* Unlock the threads scheduling queue: */
9371b3fa15SDavid Xu 			THR_THREAD_UNLOCK(curthread, pthread);
9471b3fa15SDavid Xu 		}
9571b3fa15SDavid Xu 		_thr_ref_delete(curthread, pthread);
9671b3fa15SDavid Xu 	}
9771b3fa15SDavid Xu 	return (ret);
9871b3fa15SDavid Xu }
995a1048c8SDavid Xu 
1005a1048c8SDavid Xu __strong_reference(_pthread_setschedparam, pthread_setschedparam);
101