xref: /dflybsd-src/lib/libthread_xu/thread/thr_getschedparam.c (revision 940be950819fa932cd401a01f1182bf686a2e61e)
171b3fa15SDavid Xu /*
271b3fa15SDavid Xu  * Copyright (c) 1998 Daniel Eischen <eischen@vigrid.com>.
371b3fa15SDavid Xu  * All rights reserved.
471b3fa15SDavid Xu  *
571b3fa15SDavid Xu  * Redistribution and use in source and binary forms, with or without
671b3fa15SDavid Xu  * modification, are permitted provided that the following conditions
771b3fa15SDavid Xu  * are met:
871b3fa15SDavid Xu  * 1. Redistributions of source code must retain the above copyright
971b3fa15SDavid Xu  *    notice, this list of conditions and the following disclaimer.
1071b3fa15SDavid Xu  * 2. Redistributions in binary form must reproduce the above copyright
1171b3fa15SDavid Xu  *    notice, this list of conditions and the following disclaimer in the
1271b3fa15SDavid Xu  *    documentation and/or other materials provided with the distribution.
1371b3fa15SDavid Xu  * 3. All advertising materials mentioning features or use of this software
1471b3fa15SDavid Xu  *    must display the following acknowledgement:
1571b3fa15SDavid Xu  *	This product includes software developed by Daniel Eischen.
1671b3fa15SDavid Xu  * 4. Neither the name of the author nor the names of any co-contributors
1771b3fa15SDavid Xu  *    may be used to endorse or promote products derived from this software
1871b3fa15SDavid Xu  *    without specific prior written permission.
1971b3fa15SDavid Xu  *
2071b3fa15SDavid Xu  * THIS SOFTWARE IS PROVIDED BY DANIEL EISCHEN AND CONTRIBUTORS ``AS IS'' AND
2171b3fa15SDavid Xu  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2271b3fa15SDavid Xu  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2371b3fa15SDavid Xu  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2471b3fa15SDavid Xu  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2571b3fa15SDavid Xu  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2671b3fa15SDavid Xu  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2771b3fa15SDavid Xu  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2871b3fa15SDavid Xu  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2971b3fa15SDavid Xu  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3071b3fa15SDavid Xu  * SUCH DAMAGE.
3171b3fa15SDavid Xu  *
3271b3fa15SDavid Xu  */
339e2ee207SJoerg Sonnenberger 
34fc71f871SDavid Xu #include "namespace.h"
359e2ee207SJoerg Sonnenberger #include <machine/tls.h>
3671b3fa15SDavid Xu #include <errno.h>
3771b3fa15SDavid Xu #include <pthread.h>
38fc71f871SDavid Xu #include "un-namespace.h"
39fc71f871SDavid Xu 
4071b3fa15SDavid Xu #include "thr_private.h"
4171b3fa15SDavid Xu 
4271b3fa15SDavid Xu int
_pthread_getschedparam(pthread_t pthread,int * __restrict policy,struct sched_param * __restrict param)43d33005aaSSascha Wildner _pthread_getschedparam(pthread_t pthread, int * __restrict policy,
44d33005aaSSascha Wildner     struct sched_param * __restrict param)
4571b3fa15SDavid Xu {
46*940be950Szrj 	pthread_t curthread = tls_get_curthread();
478979fd9cSzrj 	int ret = 0;
4871b3fa15SDavid Xu 
49a31d0705SMatthew Dillon 	if (policy == NULL || param == NULL)
50a31d0705SMatthew Dillon 		return (EINVAL);
51a31d0705SMatthew Dillon 
52a31d0705SMatthew Dillon 	if (pthread == curthread) {
5371b3fa15SDavid Xu 		/*
5471b3fa15SDavid Xu 		 * Avoid searching the thread list when it is the current
5571b3fa15SDavid Xu 		 * thread.
5671b3fa15SDavid Xu 		 */
57a31d0705SMatthew Dillon 		THR_LOCK(curthread);
58a31d0705SMatthew Dillon 		*policy = curthread->attr.sched_policy;
59a31d0705SMatthew Dillon 		param->sched_priority = curthread->attr.prio;
60a31d0705SMatthew Dillon 		THR_UNLOCK(curthread);
6171b3fa15SDavid Xu 	}
6271b3fa15SDavid Xu 	/* Find the thread in the list of active threads. */
6371b3fa15SDavid Xu 	else if ((ret = _thr_ref_add(curthread, pthread, /*include dead*/0))
6471b3fa15SDavid Xu 	    == 0) {
6571b3fa15SDavid Xu 		THR_THREAD_LOCK(curthread, pthread);
66a31d0705SMatthew Dillon 		*policy = pthread->attr.sched_policy;
67a31d0705SMatthew Dillon 		param->sched_priority = pthread->attr.prio;
6871b3fa15SDavid Xu 		THR_THREAD_UNLOCK(curthread, pthread);
6971b3fa15SDavid Xu 		_thr_ref_delete(curthread, pthread);
7071b3fa15SDavid Xu 	}
7171b3fa15SDavid Xu 	return (ret);
7271b3fa15SDavid Xu }
735a1048c8SDavid Xu 
745a1048c8SDavid Xu __strong_reference(_pthread_getschedparam, pthread_getschedparam);
75