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