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