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