xref: /freebsd/lib/libthr/thread/thr_setschedparam.c (revision f8bbbce458194ff4312c610d32a64ff4a3a71d45)
1*df57947fSPedro F. Giffuni /*-
2*df57947fSPedro F. Giffuni  * SPDX-License-Identifier: BSD-4-Clause
3*df57947fSPedro F. Giffuni  *
4bb535300SJeff Roberson  * Copyright (c) 1998 Daniel Eischen <eischen@vigrid.com>.
5bb535300SJeff Roberson  * All rights reserved.
6bb535300SJeff Roberson  *
7bb535300SJeff Roberson  * Redistribution and use in source and binary forms, with or without
8bb535300SJeff Roberson  * modification, are permitted provided that the following conditions
9bb535300SJeff Roberson  * are met:
10bb535300SJeff Roberson  * 1. Redistributions of source code must retain the above copyright
11bb535300SJeff Roberson  *    notice, this list of conditions and the following disclaimer.
12bb535300SJeff Roberson  * 2. Redistributions in binary form must reproduce the above copyright
13bb535300SJeff Roberson  *    notice, this list of conditions and the following disclaimer in the
14bb535300SJeff Roberson  *    documentation and/or other materials provided with the distribution.
15bb535300SJeff Roberson  * 3. All advertising materials mentioning features or use of this software
16bb535300SJeff Roberson  *    must display the following acknowledgement:
17bb535300SJeff Roberson  *	This product includes software developed by Daniel Eischen.
18bb535300SJeff Roberson  * 4. Neither the name of the author nor the names of any co-contributors
19bb535300SJeff Roberson  *    may be used to endorse or promote products derived from this software
20bb535300SJeff Roberson  *    without specific prior written permission.
21bb535300SJeff Roberson  *
22bb535300SJeff Roberson  * THIS SOFTWARE IS PROVIDED BY DANIEL EISCHEN AND CONTRIBUTORS ``AS IS'' AND
23bb535300SJeff Roberson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24bb535300SJeff Roberson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25bb535300SJeff Roberson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26bb535300SJeff Roberson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27bb535300SJeff Roberson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28bb535300SJeff Roberson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29bb535300SJeff Roberson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30bb535300SJeff Roberson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31bb535300SJeff Roberson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32bb535300SJeff Roberson  * SUCH DAMAGE.
33bb535300SJeff Roberson  */
34a091d823SDavid Xu 
3537a6356bSDavid Xu #include "namespace.h"
36bb535300SJeff Roberson #include <sys/param.h>
3737a6356bSDavid Xu #include <errno.h>
38bb535300SJeff Roberson #include <pthread.h>
3937a6356bSDavid Xu #include "un-namespace.h"
40a091d823SDavid Xu 
41bb535300SJeff Roberson #include "thr_private.h"
42bb535300SJeff Roberson 
43bb535300SJeff Roberson __weak_reference(_pthread_setschedparam, pthread_setschedparam);
44bb535300SJeff Roberson 
459ad4b644SDavid Xu /*
469ad4b644SDavid Xu  * Set a thread's scheduling parameters, this should be done
479ad4b644SDavid Xu  * in kernel, doing it in userland is no-op.
489ad4b644SDavid Xu  */
49bb535300SJeff Roberson int
_pthread_setschedparam(pthread_t pthread,int policy,const struct sched_param * param)50bb535300SJeff Roberson _pthread_setschedparam(pthread_t pthread, int policy,
51bb535300SJeff Roberson 	const struct sched_param *param)
52bb535300SJeff Roberson {
53a091d823SDavid Xu 	struct pthread	*curthread = _get_curthread();
547b4f8f03SDavid Xu 	int	ret;
55bb535300SJeff Roberson 
5630dd4f44SDavid Xu 	if (pthread == curthread)
577b4f8f03SDavid Xu 		THR_LOCK(curthread);
5830dd4f44SDavid Xu 	else if ((ret = _thr_find_thread(curthread, pthread,
597e0cf81bSDavid Xu 		 /*include dead*/0)) != 0)
607e0cf81bSDavid Xu 		return (ret);
61e2dc286cSDavid Xu 	if (pthread->attr.sched_policy == policy &&
62e2dc286cSDavid Xu 	    (policy == SCHED_OTHER ||
63e2dc286cSDavid Xu 	     pthread->attr.prio == param->sched_priority)) {
64e2dc286cSDavid Xu 		pthread->attr.prio = param->sched_priority;
65e2dc286cSDavid Xu 		THR_THREAD_UNLOCK(curthread, pthread);
66e2dc286cSDavid Xu 		return (0);
67e2dc286cSDavid Xu 	}
68e6747c7cSDavid Xu 	ret = _thr_setscheduler(pthread->tid, policy, param);
697b4f8f03SDavid Xu 	if (ret == -1)
707b4f8f03SDavid Xu 		ret = errno;
71a091d823SDavid Xu 	else {
727b4f8f03SDavid Xu 		pthread->attr.sched_policy = policy;
737b4f8f03SDavid Xu 		pthread->attr.prio = param->sched_priority;
74a091d823SDavid Xu 	}
757b4f8f03SDavid Xu 	THR_THREAD_UNLOCK(curthread, pthread);
76a091d823SDavid Xu 	return (ret);
77bb535300SJeff Roberson }
78