thr_setprio.c revision 256281
1101099Srwatson/*
2184308Srwatson * Copyright (c) 1995 John Birrell <jb@cimlogic.com.au>.
3140628Srwatson * All rights reserved.
4172930Srwatson *
5101099Srwatson * Redistribution and use in source and binary forms, with or without
6101099Srwatson * modification, are permitted provided that the following conditions
7101099Srwatson * are met:
8101099Srwatson * 1. Redistributions of source code must retain the above copyright
9140628Srwatson *    notice, this list of conditions and the following disclaimer.
10140628Srwatson * 2. Redistributions in binary form must reproduce the above copyright
11140628Srwatson *    notice, this list of conditions and the following disclaimer in the
12140628Srwatson *    documentation and/or other materials provided with the distribution.
13101099Srwatson * 3. Neither the name of the author nor the names of any co-contributors
14172930Srwatson *    may be used to endorse or promote products derived from this software
15172930Srwatson *    without specific prior written permission.
16172930Srwatson *
17101099Srwatson * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
18101099Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19101099Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20101099Srwatson * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21101099Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22101099Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23101099Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24101099Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25101099Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26101099Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27101099Srwatson * SUCH DAMAGE.
28101099Srwatson *
29101099Srwatson * $FreeBSD: stable/10/lib/libthr/thread/thr_setprio.c 238642 2012-07-20 03:22:17Z davidxu $
30101099Srwatson */
31101099Srwatson
32101099Srwatson#include "namespace.h"
33101099Srwatson#include <pthread.h>
34101099Srwatson#include "un-namespace.h"
35101099Srwatson
36101099Srwatson#include "thr_private.h"
37101099Srwatson
38101099Srwatson__weak_reference(_pthread_setprio, pthread_setprio);
39101099Srwatson
40101099Srwatsonint
41101099Srwatson_pthread_setprio(pthread_t pthread, int prio)
42101099Srwatson{
43168951Srwatson	struct pthread	*curthread = _get_curthread();
44101099Srwatson	struct sched_param	param;
45101099Srwatson	int	ret;
46101099Srwatson
47101099Srwatson	param.sched_priority = prio;
48101099Srwatson	if (pthread == curthread)
49105988Srwatson		THR_LOCK(curthread);
50101099Srwatson	else if ((ret = _thr_find_thread(curthread, pthread, /*include dead*/0)))
51164184Strhodes		return (ret);
52103183Sbde	if (pthread->attr.sched_policy == SCHED_OTHER ||
53145076Scsjp	    pthread->attr.prio == prio) {
54101099Srwatson		pthread->attr.prio = prio;
55168951Srwatson		ret = 0;
56101099Srwatson	} else {
57115497Srwatson		ret = _thr_setscheduler(pthread->tid,
58101099Srwatson			pthread->attr.sched_policy, &param);
59101099Srwatson		if (ret == -1)
60101099Srwatson			ret = errno;
61105696Srwatson		else
62101099Srwatson			pthread->attr.prio = prio;
63101099Srwatson	}
64101099Srwatson	THR_THREAD_UNLOCK(curthread, pthread);
65101099Srwatson	return (ret);
66101099Srwatson}
67150340Sphk