pthread_misc.c revision 1.10.4.1 1 /* $NetBSD: pthread_misc.c,v 1.10.4.1 2009/09/05 12:51:09 bouyer 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 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 __RCSID("$NetBSD: pthread_misc.c,v 1.10.4.1 2009/09/05 12:51:09 bouyer Exp $");
34
35 #include <errno.h>
36 #include <string.h>
37 #include <unistd.h>
38
39 #include <sys/types.h>
40 #include <sys/pset.h>
41 #include <sys/signal.h>
42 #include <sys/time.h>
43
44 #include <lwp.h>
45 #include <sched.h>
46
47 #include "pthread.h"
48 #include "pthread_int.h"
49
50 int pthread__sched_yield(void);
51
52 int _sys___sigprocmask14(int, const sigset_t *, sigset_t *);
53 int _sys_sched_yield(void);
54
55 __strong_alias(__libc_thr_sigsetmask,pthread_sigmask)
56 __strong_alias(__sigprocmask14,pthread_sigmask)
57 __strong_alias(__libc_thr_yield,pthread__sched_yield)
58
59 int
60 pthread_getschedparam(pthread_t thread, int *policy, struct sched_param *param)
61 {
62
63 if (pthread__find(thread) != 0)
64 return ESRCH;
65
66 if (_sched_getparam(getpid(), thread->pt_lid, policy, param) < 0)
67 return errno;
68
69 return 0;
70 }
71
72 int
73 pthread_setschedparam(pthread_t thread, int policy,
74 const struct sched_param *param)
75 {
76 struct sched_param sp;
77
78 if (pthread__find(thread) != 0)
79 return ESRCH;
80
81 memcpy(&sp, param, sizeof(struct sched_param));
82 if (_sched_setparam(getpid(), thread->pt_lid, policy, &sp) < 0)
83 return errno;
84
85 return 0;
86 }
87
88 int
89 pthread_getaffinity_np(pthread_t thread, size_t size, cpuset_t *cpuset)
90 {
91
92 if (pthread__find(thread) != 0)
93 return ESRCH;
94
95 if (_sched_getaffinity(getpid(), thread->pt_lid, size, cpuset) < 0)
96 return errno;
97
98 return 0;
99 }
100
101 int
102 pthread_setaffinity_np(pthread_t thread, size_t size, cpuset_t *cpuset)
103 {
104
105 if (pthread__find(thread) != 0)
106 return ESRCH;
107
108 if (_sched_setaffinity(getpid(), thread->pt_lid, size, cpuset) < 0)
109 return errno;
110
111 return 0;
112 }
113
114 int
115 pthread_setschedprio(pthread_t thread, int prio)
116 {
117 struct sched_param sp;
118
119 if (pthread__find(thread) != 0)
120 return ESRCH;
121
122 sp.sched_priority = prio;
123 if (_sched_setparam(getpid(), thread->pt_lid, SCHED_NONE, &sp) < 0)
124 return errno;
125
126 return 0;
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 if (_lwp_kill(thread->pt_lid, sig))
138 return errno;
139 return 0;
140 }
141
142 int
143 pthread_sigmask(int how, const sigset_t *set, sigset_t *oset)
144 {
145
146 if (_sys___sigprocmask14(how, set, oset))
147 return errno;
148 return 0;
149 }
150
151 int
152 pthread__sched_yield(void)
153 {
154 pthread_t self;
155 int error;
156
157 self = pthread__self();
158
159 /* Memory barrier for unlocked mutex release. */
160 membar_producer();
161 self->pt_blocking++;
162 error = _sys_sched_yield();
163 self->pt_blocking--;
164 membar_sync();
165
166 return error;
167 }
168