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