xref: /netbsd-src/lib/libpthread/pthread_misc.c (revision 267197ec1eebfcb9810ea27a89625b6ddf68e3e7)
1 /*	$NetBSD: pthread_misc.c,v 1.7 2008/02/10 18:50:54 ad Exp $	*/
2 
3 /*-
4  * Copyright (c) 2001, 2006, 2007, 2008 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Nathan J. Williams.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *        This product includes software developed by the NetBSD
21  *        Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 #include <sys/cdefs.h>
40 __RCSID("$NetBSD: pthread_misc.c,v 1.7 2008/02/10 18:50:54 ad Exp $");
41 
42 #include <errno.h>
43 #include <string.h>
44 #include <unistd.h>
45 
46 #include <sys/types.h>
47 #include <sys/pset.h>
48 #include <sys/signal.h>
49 #include <sys/time.h>
50 
51 #include <lwp.h>
52 #include <sched.h>
53 
54 #include "pthread.h"
55 #include "pthread_int.h"
56 
57 int	pthread__sched_yield(void);
58 
59 int	_sys___sigprocmask14(int, const sigset_t *, sigset_t *);
60 int	_sys_nanosleep(const struct timespec *, struct timespec *);
61 int	_sys_sched_yield(void);
62 
63 __strong_alias(_nanosleep, nanosleep)
64 __strong_alias(__libc_thr_sigsetmask,pthread_sigmask)
65 __strong_alias(__sigprocmask14,pthread_sigmask)
66 __strong_alias(__libc_thr_yield,pthread__sched_yield)
67 
68 int
69 pthread_getschedparam(pthread_t thread, int *policy, struct sched_param *param)
70 {
71 
72 	if (pthread__find(thread) != 0)
73 		return ESRCH;
74 
75 	if (_sched_getparam(getpid(), thread->pt_lid, policy, param) < 0)
76 		return errno;
77 
78 	return 0;
79 }
80 
81 int
82 pthread_setschedparam(pthread_t thread, int policy,
83     const struct sched_param *param)
84 {
85 	struct sched_param sp;
86 
87 	if (pthread__find(thread) != 0)
88 		return ESRCH;
89 
90 	memcpy(&sp, param, sizeof(struct sched_param));
91 	if (_sched_setparam(getpid(), thread->pt_lid, policy, &sp) < 0)
92 		return errno;
93 
94 	return 0;
95 }
96 
97 int
98 pthread_getaffinity_np(pthread_t thread, size_t size, cpuset_t *cpuset)
99 {
100 
101 	if (pthread__find(thread) != 0)
102 		return ESRCH;
103 
104 	if (_sched_getaffinity(getpid(), thread->pt_lid, size, cpuset) < 0)
105 		return errno;
106 
107 	return 0;
108 }
109 
110 int
111 pthread_setaffinity_np(pthread_t thread, size_t size, cpuset_t *cpuset)
112 {
113 
114 	if (pthread__find(thread) != 0)
115 		return ESRCH;
116 
117 	if (_sched_setaffinity(getpid(), thread->pt_lid, size, cpuset) < 0)
118 		return errno;
119 
120 	return 0;
121 }
122 
123 int
124 pthread_setschedprio(pthread_t thread, int prio)
125 {
126 	struct sched_param sp;
127 
128 	if (pthread__find(thread) != 0)
129 		return ESRCH;
130 
131 	sp.sched_priority = prio;
132 	if (_sched_setparam(getpid(), thread->pt_lid, SCHED_NONE, &sp) < 0)
133 		return errno;
134 
135 	return 0;
136 }
137 
138 int
139 pthread_kill(pthread_t thread, int sig)
140 {
141 
142 	if ((sig < 0) || (sig >= _NSIG))
143 		return EINVAL;
144 	if (pthread__find(thread) != 0)
145 		return ESRCH;
146 
147 	return _lwp_kill(thread->pt_lid, sig);
148 }
149 
150 int
151 pthread_sigmask(int how, const sigset_t *set, sigset_t *oset)
152 {
153 
154 	return _sys___sigprocmask14(how, set, oset);
155 }
156 
157 int
158 nanosleep(const struct timespec *rqtp, struct timespec *rmtp)
159 {
160 
161 	/*
162 	 * For now, just nanosleep.  In the future, maybe pass a ucontext_t
163 	 * to _lwp_nanosleep() and allow it to recycle our kernel stack.
164 	 */
165 	return  _sys_nanosleep(rqtp, rmtp);
166 }
167 
168 int
169 pthread__sched_yield(void)
170 {
171 	pthread_t self;
172 	int error;
173 
174 	self = pthread__self();
175 
176 	/* Memory barrier for unlocked mutex release. */
177 	membar_producer();
178 	self->pt_blocking++;
179 	error = _sys_sched_yield();
180 	self->pt_blocking--;
181 	membar_sync();
182 
183 	return error;
184 }
185