pthread_misc.c revision 1.14.6.1 1 /* $NetBSD: pthread_misc.c,v 1.14.6.1 2014/05/22 11:36:59 yamt 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.14.6.1 2014/05/22 11:36:59 yamt 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 #include "reentrant.h"
50
51 int pthread__sched_yield(void);
52
53 int _sys___sigprocmask14(int, const sigset_t *, sigset_t *);
54 int _sys_sched_yield(void);
55
56 __strong_alias(__libc_thr_sigsetmask,pthread_sigmask)
57 __strong_alias(__sigprocmask14,pthread_sigmask)
58 __strong_alias(__libc_thr_yield,pthread__sched_yield)
59
60 int
61 pthread_getschedparam(pthread_t thread, int *policy, struct sched_param *param)
62 {
63
64 if (pthread__find(thread) != 0)
65 return ESRCH;
66
67 if (_sched_getparam(getpid(), thread->pt_lid, policy, param) < 0)
68 return errno;
69
70 return 0;
71 }
72
73 int
74 pthread_setschedparam(pthread_t thread, int policy,
75 const struct sched_param *param)
76 {
77 struct sched_param sp;
78
79 if (pthread__find(thread) != 0)
80 return ESRCH;
81
82 memcpy(&sp, param, sizeof(struct sched_param));
83 if (_sched_setparam(getpid(), thread->pt_lid, policy, &sp) < 0)
84 return errno;
85
86 return 0;
87 }
88
89 int
90 pthread_getaffinity_np(pthread_t thread, size_t size, cpuset_t *cpuset)
91 {
92
93 if (pthread__find(thread) != 0)
94 return ESRCH;
95
96 if (_sched_getaffinity(getpid(), thread->pt_lid, size, cpuset) < 0)
97 return errno;
98
99 return 0;
100 }
101
102 int
103 pthread_setaffinity_np(pthread_t thread, size_t size, cpuset_t *cpuset)
104 {
105
106 if (pthread__find(thread) != 0)
107 return ESRCH;
108
109 if (_sched_setaffinity(getpid(), thread->pt_lid, size, cpuset) < 0)
110 return errno;
111
112 return 0;
113 }
114
115 int
116 pthread_setschedprio(pthread_t thread, int prio)
117 {
118 struct sched_param sp;
119
120 if (pthread__find(thread) != 0)
121 return ESRCH;
122
123 sp.sched_priority = prio;
124 if (_sched_setparam(getpid(), thread->pt_lid, SCHED_NONE, &sp) < 0)
125 return errno;
126
127 return 0;
128 }
129
130 int
131 pthread_kill(pthread_t thread, int sig)
132 {
133
134 if ((sig < 0) || (sig >= _NSIG))
135 return EINVAL;
136 if (pthread__find(thread) != 0)
137 return ESRCH;
138 if (_lwp_kill(thread->pt_lid, sig))
139 return errno;
140 return 0;
141 }
142
143 int
144 pthread_sigmask(int how, const sigset_t *set, sigset_t *oset)
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 if (__predict_false(__uselibcstub))
158 return __libc_thr_yield();
159
160 self = pthread__self();
161
162 /* Memory barrier for unlocked mutex release. */
163 membar_producer();
164 self->pt_blocking++;
165 error = _sys_sched_yield();
166 self->pt_blocking--;
167 membar_sync();
168
169 return error;
170 }
171