144963Sjb/*
244963Sjb * Copyright (c) 1998 Daniel Eischen <eischen@vigrid.com>.
344963Sjb * All rights reserved.
444963Sjb *
544963Sjb * Redistribution and use in source and binary forms, with or without
644963Sjb * modification, are permitted provided that the following conditions
744963Sjb * are met:
844963Sjb * 1. Redistributions of source code must retain the above copyright
944963Sjb *    notice, this list of conditions and the following disclaimer.
1044963Sjb * 2. Redistributions in binary form must reproduce the above copyright
1144963Sjb *    notice, this list of conditions and the following disclaimer in the
1244963Sjb *    documentation and/or other materials provided with the distribution.
1344963Sjb * 3. All advertising materials mentioning features or use of this software
1444963Sjb *    must display the following acknowledgement:
1544963Sjb *	This product includes software developed by Daniel Eischen.
1644963Sjb * 4. Neither the name of the author nor the names of any co-contributors
1744963Sjb *    may be used to endorse or promote products derived from this software
1844963Sjb *    without specific prior written permission.
1944963Sjb *
2044963Sjb * THIS SOFTWARE IS PROVIDED BY DANIEL EISCHEN AND CONTRIBUTORS ``AS IS'' AND
2144963Sjb * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2244963Sjb * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2344963Sjb * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2444963Sjb * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2544963Sjb * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2644963Sjb * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2744963Sjb * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2844963Sjb * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2944963Sjb * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3044963Sjb * SUCH DAMAGE.
3144963Sjb *
3250476Speter * $FreeBSD$
3344963Sjb */
34174112Sdeischen
35174112Sdeischen#include "namespace.h"
3644963Sjb#include <errno.h>
3744963Sjb#include <pthread.h>
38174112Sdeischen#include "un-namespace.h"
39103388Smini#include "thr_private.h"
4044963Sjb
4175369Sdeischen__weak_reference(_pthread_getschedparam, pthread_getschedparam);
4271581Sdeischen
4344963Sjbint
4471581Sdeischen_pthread_getschedparam(pthread_t pthread, int *policy,
4553812Salfred	struct sched_param *param)
4644963Sjb{
47113658Sdeischen	struct pthread *curthread = _get_curthread();
48117300Sdavidxu	int ret, tmp;
4944963Sjb
5044963Sjb	if ((param == NULL) || (policy == NULL))
5144963Sjb		/* Return an invalid argument error: */
5244963Sjb		ret = EINVAL;
53113658Sdeischen	else if (pthread == curthread) {
54113658Sdeischen		/*
55113658Sdeischen		 * Avoid searching the thread list when it is the current
56113658Sdeischen		 * thread.
57113658Sdeischen		 */
58113658Sdeischen		THR_SCHED_LOCK(curthread, curthread);
5967097Sdeischen		param->sched_priority =
60113658Sdeischen		    THR_BASE_PRIORITY(pthread->base_priority);
61117300Sdavidxu		tmp = pthread->attr.sched_policy;
62113658Sdeischen		THR_SCHED_UNLOCK(curthread, curthread);
63117300Sdavidxu		*policy = tmp;
64113658Sdeischen		ret = 0;
6544963Sjb	}
66113658Sdeischen	/* Find the thread in the list of active threads. */
67113658Sdeischen	else if ((ret = _thr_ref_add(curthread, pthread, /*include dead*/0))
68113658Sdeischen	    == 0) {
69113658Sdeischen		THR_SCHED_LOCK(curthread, pthread);
70113658Sdeischen		param->sched_priority =
71113658Sdeischen		    THR_BASE_PRIORITY(pthread->base_priority);
72117300Sdavidxu		tmp = pthread->attr.sched_policy;
73113658Sdeischen		THR_SCHED_UNLOCK(curthread, pthread);
74113658Sdeischen		_thr_ref_delete(curthread, pthread);
75117300Sdavidxu		*policy = tmp;
76113658Sdeischen	}
77113658Sdeischen	return (ret);
7844963Sjb}
79