xref: /netbsd-src/lib/libpthread/pthread_misc.c (revision 568eb77ef2a2b1fbe2457d69c7aa6e1dd3d8de8b)
1 /*	$NetBSD: pthread_misc.c,v 1.19 2022/04/10 10:38:33 riastradh 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.19 2022/04/10 10:38:33 riastradh Exp $");
34 
35 /* Need to use libc-private names for atomic operations. */
36 #include "../../common/lib/libc/atomic/atomic_op_namespace.h"
37 
38 #include <errno.h>
39 #include <string.h>
40 #include <unistd.h>
41 
42 #include <sys/types.h>
43 #include <sys/pset.h>
44 #include <sys/signal.h>
45 #include <sys/time.h>
46 
47 #include <lwp.h>
48 #include <sched.h>
49 
50 #include "pthread.h"
51 #include "pthread_int.h"
52 #include "reentrant.h"
53 
54 int	pthread__sched_yield(void);
55 
56 int	_sys___sigprocmask14(int, const sigset_t *, sigset_t *);
57 int	_sys_sched_yield(void);
58 
__strong_alias(__libc_thr_sigsetmask,pthread_sigmask)59 __strong_alias(__libc_thr_sigsetmask,pthread_sigmask)
60 __strong_alias(__sigprocmask14,pthread_sigmask)
61 __strong_alias(__libc_thr_yield,pthread__sched_yield)
62 
63 int
64 pthread_getschedparam(pthread_t thread, int *policy, struct sched_param *param)
65 {
66 
67 	pthread__error(EINVAL, "Invalid thread",
68 	    thread->pt_magic == PT_MAGIC);
69 
70 	if (pthread__find(thread) != 0)
71 		return ESRCH;
72 
73 	if (_sched_getparam(getpid(), thread->pt_lid, policy, param) < 0)
74 		return errno;
75 
76 	return 0;
77 }
78 
79 int
pthread_setschedparam(pthread_t thread,int policy,const struct sched_param * param)80 pthread_setschedparam(pthread_t thread, int policy,
81     const struct sched_param *param)
82 {
83 	struct sched_param sp;
84 
85 	pthread__error(EINVAL, "Invalid thread",
86 	    thread->pt_magic == PT_MAGIC);
87 
88 	if (pthread__find(thread) != 0)
89 		return ESRCH;
90 
91 	memcpy(&sp, param, sizeof(struct sched_param));
92 	if (_sched_setparam(getpid(), thread->pt_lid, policy, &sp) < 0)
93 		return errno;
94 
95 	return 0;
96 }
97 
98 int
pthread_getaffinity_np(pthread_t thread,size_t size,cpuset_t * cpuset)99 pthread_getaffinity_np(pthread_t thread, size_t size, cpuset_t *cpuset)
100 {
101 
102 	pthread__error(EINVAL, "Invalid thread",
103 	    thread->pt_magic == PT_MAGIC);
104 
105 	if (pthread__find(thread) != 0)
106 		return ESRCH;
107 
108 	if (_sched_getaffinity(getpid(), thread->pt_lid, size, cpuset) < 0)
109 		return errno;
110 
111 	return 0;
112 }
113 
114 int
pthread_setaffinity_np(pthread_t thread,size_t size,cpuset_t * cpuset)115 pthread_setaffinity_np(pthread_t thread, size_t size, cpuset_t *cpuset)
116 {
117 
118 	pthread__error(EINVAL, "Invalid thread",
119 	    thread->pt_magic == PT_MAGIC);
120 
121 	if (pthread__find(thread) != 0)
122 		return ESRCH;
123 
124 	if (_sched_setaffinity(getpid(), thread->pt_lid, size, cpuset) < 0)
125 		return errno;
126 
127 	return 0;
128 }
129 
130 int
pthread_setschedprio(pthread_t thread,int prio)131 pthread_setschedprio(pthread_t thread, int prio)
132 {
133 	struct sched_param sp;
134 
135 	pthread__error(EINVAL, "Invalid thread",
136 	    thread->pt_magic == PT_MAGIC);
137 
138 	if (pthread__find(thread) != 0)
139 		return ESRCH;
140 
141 	sp.sched_priority = prio;
142 	if (_sched_setparam(getpid(), thread->pt_lid, SCHED_NONE, &sp) < 0)
143 		return errno;
144 
145 	return 0;
146 }
147 
148 int
pthread_kill(pthread_t thread,int sig)149 pthread_kill(pthread_t thread, int sig)
150 {
151 
152 	pthread__error(EINVAL, "Invalid thread",
153 	    thread->pt_magic == PT_MAGIC);
154 
155 	if ((sig < 0) || (sig >= _NSIG))
156 		return EINVAL;
157 	if (pthread__find(thread) != 0)
158 		return ESRCH;
159 	if (_lwp_kill(thread->pt_lid, sig))
160 		return errno;
161 	return 0;
162 }
163 
164 int
pthread_sigmask(int how,const sigset_t * set,sigset_t * oset)165 pthread_sigmask(int how, const sigset_t *set, sigset_t *oset)
166 {
167 	if (_sys___sigprocmask14(how, set, oset))
168 		return errno;
169 	return 0;
170 }
171 
172 int
pthread__sched_yield(void)173 pthread__sched_yield(void)
174 {
175 
176 	if (__predict_false(__uselibcstub))
177 		return __libc_thr_yield();
178 
179 	return _sys_sched_yield();
180 }
181