pthread_misc.c revision 1.5 1 /* $NetBSD: pthread_misc.c,v 1.5 2008/01/26 17:55:30 rmind 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.5 2008/01/26 17:55:30 rmind 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, param) < 0)
76 return errno;
77
78 *policy = param->sched_class;
79 return 0;
80 }
81
82 int
83 pthread_setschedparam(pthread_t thread, int policy,
84 const struct sched_param *param)
85 {
86 struct sched_param sp;
87
88 if (pthread__find(thread) != 0)
89 return ESRCH;
90
91 memcpy(&sp, param, sizeof(struct sched_param));
92 sp.sched_class = policy;
93 if (_sched_setparam(getpid(), thread->pt_lid, &sp) < 0)
94 return errno;
95
96 return 0;
97 }
98
99 int
100 pthread_getaffinity_np(pthread_t thread, size_t size, cpuset_t *cpuset)
101 {
102
103 if (pthread__find(thread) != 0)
104 return ESRCH;
105
106 if (_sched_getaffinity(getpid(), thread->pt_lid, size, cpuset) < 0)
107 return errno;
108
109 return 0;
110 }
111
112 int
113 pthread_setaffinity_np(pthread_t thread, size_t size, cpuset_t *cpuset)
114 {
115
116 if (pthread__find(thread) != 0)
117 return ESRCH;
118
119 if (_sched_setaffinity(getpid(), thread->pt_lid, size, cpuset) < 0)
120 return errno;
121
122 return 0;
123 }
124
125 int
126 pthread_setschedprio(pthread_t thread, int prio)
127 {
128 struct sched_param sp;
129
130 if (pthread__find(thread) != 0)
131 return ESRCH;
132
133 sp.sched_class = SCHED_NONE;
134 sp.sched_priority = prio;
135 if (_sched_setparam(getpid(), thread->pt_lid, &sp) < 0)
136 return errno;
137
138 return 0;
139 }
140
141 int
142 pthread_kill(pthread_t thread, int sig)
143 {
144
145 if ((sig < 0) || (sig >= _NSIG))
146 return EINVAL;
147 if (pthread__find(thread) != 0)
148 return ESRCH;
149
150 return _lwp_kill(thread->pt_lid, sig);
151 }
152
153 int
154 pthread_sigmask(int how, const sigset_t *set, sigset_t *oset)
155 {
156
157 return _sys___sigprocmask14(how, set, oset);
158 }
159
160 int
161 nanosleep(const struct timespec *rqtp, struct timespec *rmtp)
162 {
163
164 /*
165 * For now, just nanosleep. In the future, maybe pass a ucontext_t
166 * to _lwp_nanosleep() and allow it to recycle our kernel stack.
167 */
168 return _sys_nanosleep(rqtp, rmtp);
169 }
170
171 int
172 pthread__sched_yield(void)
173 {
174 pthread_t self;
175 int error;
176
177 self = pthread__self();
178
179 #ifdef PTHREAD__HAVE_ATOMIC
180 /* Memory barrier for unlocked mutex release. */
181 pthread__membar_producer();
182 #endif
183 self->pt_blocking++;
184 error = _sys_sched_yield();
185 self->pt_blocking--;
186
187 return error;
188 }
189