1112918Sjeff/*
2112918Sjeff * Copyright (c) 1998 Daniel Eischen <eischen@vigrid.com>.
3112918Sjeff * All rights reserved.
4112918Sjeff *
5112918Sjeff * Redistribution and use in source and binary forms, with or without
6112918Sjeff * modification, are permitted provided that the following conditions
7112918Sjeff * are met:
8112918Sjeff * 1. Redistributions of source code must retain the above copyright
9112918Sjeff *    notice, this list of conditions and the following disclaimer.
10112918Sjeff * 2. Redistributions in binary form must reproduce the above copyright
11112918Sjeff *    notice, this list of conditions and the following disclaimer in the
12112918Sjeff *    documentation and/or other materials provided with the distribution.
13112918Sjeff * 3. All advertising materials mentioning features or use of this software
14112918Sjeff *    must display the following acknowledgement:
15112918Sjeff *	This product includes software developed by Daniel Eischen.
16112918Sjeff * 4. Neither the name of the author nor the names of any co-contributors
17112918Sjeff *    may be used to endorse or promote products derived from this software
18112918Sjeff *    without specific prior written permission.
19112918Sjeff *
20112918Sjeff * THIS SOFTWARE IS PROVIDED BY DANIEL EISCHEN AND CONTRIBUTORS ``AS IS'' AND
21112918Sjeff * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22112918Sjeff * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23112918Sjeff * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24112918Sjeff * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25112918Sjeff * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26112918Sjeff * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27112918Sjeff * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28112918Sjeff * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29112918Sjeff * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30112918Sjeff * SUCH DAMAGE.
31112918Sjeff *
32112918Sjeff * $FreeBSD$
33112918Sjeff */
34144518Sdavidxu
35157457Sdavidxu#include "namespace.h"
36157457Sdavidxu#include <sys/param.h>
37112918Sjeff#include <errno.h>
38112918Sjeff#include <pthread.h>
39157457Sdavidxu#include "un-namespace.h"
40144518Sdavidxu
41112918Sjeff#include "thr_private.h"
42112918Sjeff
43112918Sjeff__weak_reference(_pthread_setschedparam, pthread_setschedparam);
44112918Sjeff
45157194Sdavidxu/*
46157194Sdavidxu * Set a thread's scheduling parameters, this should be done
47157194Sdavidxu * in kernel, doing it in userland is no-op.
48157194Sdavidxu */
49112918Sjeffint
50112918Sjeff_pthread_setschedparam(pthread_t pthread, int policy,
51112918Sjeff	const struct sched_param *param)
52112918Sjeff{
53160287Sdavidxu	struct pthread	*curthread = _get_curthread();
54160287Sdavidxu	int	ret;
55112918Sjeff
56238642Sdavidxu	if (pthread == curthread)
57160287Sdavidxu		THR_LOCK(curthread);
58238642Sdavidxu	else if ((ret = _thr_find_thread(curthread, pthread,
59238640Sdavidxu		 /*include dead*/0)) != 0)
60238640Sdavidxu		return (ret);
61238640Sdavidxu	if (pthread->attr.sched_policy == policy &&
62238640Sdavidxu	    (policy == SCHED_OTHER ||
63238640Sdavidxu	     pthread->attr.prio == param->sched_priority)) {
64238640Sdavidxu		pthread->attr.prio = param->sched_priority;
65160287Sdavidxu		THR_THREAD_UNLOCK(curthread, pthread);
66238640Sdavidxu		return (0);
67112918Sjeff	}
68238640Sdavidxu	ret = _thr_setscheduler(pthread->tid, policy, param);
69238640Sdavidxu	if (ret == -1)
70238640Sdavidxu		ret = errno;
71238640Sdavidxu	else {
72238640Sdavidxu		pthread->attr.sched_policy = policy;
73238640Sdavidxu		pthread->attr.prio = param->sched_priority;
74238640Sdavidxu	}
75238640Sdavidxu	THR_THREAD_UNLOCK(curthread, pthread);
76144518Sdavidxu	return (ret);
77112918Sjeff}
78