thr_setprio.c revision 160321
158310Sache/*
258310Sache * Copyright (c) 1995 John Birrell <jb@cimlogic.com.au>.
358310Sache * All rights reserved.
458310Sache *
558310Sache * Redistribution and use in source and binary forms, with or without
658310Sache * modification, are permitted provided that the following conditions
758310Sache * are met:
858310Sache * 1. Redistributions of source code must retain the above copyright
958310Sache *    notice, this list of conditions and the following disclaimer.
1058310Sache * 2. Redistributions in binary form must reproduce the above copyright
1158310Sache *    notice, this list of conditions and the following disclaimer in the
1258310Sache *    documentation and/or other materials provided with the distribution.
1358310Sache * 3. All advertising materials mentioning features or use of this software
1458310Sache *    must display the following acknowledgement:
1558310Sache *	This product includes software developed by John Birrell.
1658310Sache * 4. Neither the name of the author nor the names of any co-contributors
1758310Sache *    may be used to endorse or promote products derived from this software
1858310Sache *    without specific prior written permission.
1958310Sache *
2058310Sache * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
2158310Sache * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2258310Sache * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2358310Sache * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2458310Sache * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2558310Sache * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2658310Sache * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2758310Sache * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2858310Sache * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2958310Sache * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3058310Sache * SUCH DAMAGE.
3158310Sache *
3258310Sache * $FreeBSD: head/lib/libthr/thread/thr_setprio.c 160321 2006-07-13 06:35:43Z davidxu $
3358310Sache */
3458310Sache
3558310Sache#include "namespace.h"
3658310Sache#include <pthread.h>
3758310Sache#include "un-namespace.h"
3858310Sache
3958310Sache#include "thr_private.h"
4058310Sache
4158310Sache__weak_reference(_pthread_setprio, pthread_setprio);
42119610Sache
43119610Sacheint
44119610Sache_pthread_setprio(pthread_t pthread, int prio)
4558310Sache{
4658310Sache	struct pthread	*curthread = _get_curthread();
47	struct sched_param	param;
48	int	ret;
49
50	param.sched_priority = prio;
51	if (pthread == curthread) {
52		THR_LOCK(curthread);
53		ret = thr_setschedparam(curthread->tid, &param, sizeof(param));
54		if (ret == -1)
55			ret = errno;
56		else
57			curthread->attr.prio = prio;
58		THR_UNLOCK(curthread);
59	} else if ((ret = _thr_ref_add(curthread, pthread, /*include dead*/0))
60		== 0) {
61		THR_THREAD_LOCK(curthread, pthread);
62		ret = thr_setschedparam(pthread->tid, &param, sizeof(param));
63		if (ret == -1)
64			ret = errno;
65		else
66			pthread->attr.prio = prio;
67		THR_THREAD_UNLOCK(curthread, pthread);
68		_thr_ref_delete(curthread, pthread);
69	}
70	return (ret);
71}
72