xref: /freebsd/lib/libthr/thread/thr_setprio.c (revision f8bbbce458194ff4312c610d32a64ff4a3a71d45)
1*8a16b7a1SPedro F. Giffuni /*-
2*8a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3*8a16b7a1SPedro F. Giffuni  *
4bb535300SJeff Roberson  * Copyright (c) 1995 John Birrell <jb@cimlogic.com.au>.
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.
15fed32d75SWarner Losh  * 3. Neither the name of the author nor the names of any co-contributors
16bb535300SJeff Roberson  *    may be used to endorse or promote products derived from this software
17bb535300SJeff Roberson  *    without specific prior written permission.
18bb535300SJeff Roberson  *
19bb535300SJeff Roberson  * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
20bb535300SJeff Roberson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21bb535300SJeff Roberson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22bb535300SJeff Roberson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23bb535300SJeff Roberson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24bb535300SJeff Roberson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25bb535300SJeff Roberson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26bb535300SJeff Roberson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27bb535300SJeff Roberson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28bb535300SJeff Roberson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29bb535300SJeff Roberson  * SUCH DAMAGE.
30bb535300SJeff Roberson  */
3137a6356bSDavid Xu 
3237a6356bSDavid Xu #include "namespace.h"
33bb535300SJeff Roberson #include <pthread.h>
3437a6356bSDavid Xu #include "un-namespace.h"
3537a6356bSDavid Xu 
36bb535300SJeff Roberson #include "thr_private.h"
37bb535300SJeff Roberson 
38bb535300SJeff Roberson __weak_reference(_pthread_setprio, pthread_setprio);
39bb535300SJeff Roberson 
40bb535300SJeff Roberson int
_pthread_setprio(pthread_t pthread,int prio)41bb535300SJeff Roberson _pthread_setprio(pthread_t pthread, int prio)
42bb535300SJeff Roberson {
437b4f8f03SDavid Xu 	struct pthread	*curthread = _get_curthread();
44bb535300SJeff Roberson 	struct sched_param	param;
457b4f8f03SDavid Xu 	int	ret;
46bb535300SJeff Roberson 
47bb535300SJeff Roberson 	param.sched_priority = prio;
4830dd4f44SDavid Xu 	if (pthread == curthread)
497b4f8f03SDavid Xu 		THR_LOCK(curthread);
5030dd4f44SDavid Xu 	else if ((ret = _thr_find_thread(curthread, pthread, /*include dead*/0)))
51670bc18dSDavid Xu 		return (ret);
52e2dc286cSDavid Xu 	if (pthread->attr.sched_policy == SCHED_OTHER ||
53e2dc286cSDavid Xu 	    pthread->attr.prio == prio) {
54e2dc286cSDavid Xu 		pthread->attr.prio = prio;
55e2dc286cSDavid Xu 		ret = 0;
56e2dc286cSDavid Xu 	} else {
57e6747c7cSDavid Xu 		ret = _thr_setscheduler(pthread->tid,
58670bc18dSDavid Xu 			pthread->attr.sched_policy, &param);
597b4f8f03SDavid Xu 		if (ret == -1)
607b4f8f03SDavid Xu 			ret = errno;
617b4f8f03SDavid Xu 		else
627b4f8f03SDavid Xu 			pthread->attr.prio = prio;
63e2dc286cSDavid Xu 	}
647b4f8f03SDavid Xu 	THR_THREAD_UNLOCK(curthread, pthread);
65bb535300SJeff Roberson 	return (ret);
66bb535300SJeff Roberson }
67