1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1996 Jeffrey Hsu <hsu@freebsd.org>. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the author nor the names of any co-contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 /* 33 * Copyright (c) 1997 John Birrell <jb@cimlogic.com.au>. 34 * All rights reserved. 35 * 36 * Redistribution and use in source and binary forms, with or without 37 * modification, are permitted provided that the following conditions 38 * are met: 39 * 1. Redistributions of source code must retain the above copyright 40 * notice, this list of conditions and the following disclaimer. 41 * 2. Redistributions in binary form must reproduce the above copyright 42 * notice, this list of conditions and the following disclaimer in the 43 * documentation and/or other materials provided with the distribution. 44 * 3. Neither the name of the author nor the names of any co-contributors 45 * may be used to endorse or promote products derived from this software 46 * without specific prior written permission. 47 * 48 * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND 49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 51 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 58 * SUCH DAMAGE. 59 * 60 */ 61 62 #include "namespace.h" 63 #include <string.h> 64 #include <stdlib.h> 65 #include <errno.h> 66 #include <pthread.h> 67 #include <pthread_np.h> 68 #include "un-namespace.h" 69 70 #include "thr_private.h" 71 72 __weak_reference(_thr_mutexattr_init, pthread_mutexattr_init); 73 __weak_reference(_thr_mutexattr_init, _pthread_mutexattr_init); 74 __weak_reference(_pthread_mutexattr_setkind_np, pthread_mutexattr_setkind_np); 75 __weak_reference(_pthread_mutexattr_getkind_np, pthread_mutexattr_getkind_np); 76 __weak_reference(_pthread_mutexattr_gettype, pthread_mutexattr_gettype); 77 __weak_reference(_thr_mutexattr_settype, pthread_mutexattr_settype); 78 __weak_reference(_thr_mutexattr_settype, _pthread_mutexattr_settype); 79 __weak_reference(_thr_mutexattr_destroy, pthread_mutexattr_destroy); 80 __weak_reference(_thr_mutexattr_destroy, _pthread_mutexattr_destroy); 81 __weak_reference(_pthread_mutexattr_getpshared, pthread_mutexattr_getpshared); 82 __weak_reference(_pthread_mutexattr_setpshared, pthread_mutexattr_setpshared); 83 __weak_reference(_pthread_mutexattr_getprotocol, pthread_mutexattr_getprotocol); 84 __weak_reference(_pthread_mutexattr_setprotocol, pthread_mutexattr_setprotocol); 85 __weak_reference(_pthread_mutexattr_getprioceiling, 86 pthread_mutexattr_getprioceiling); 87 __weak_reference(_pthread_mutexattr_setprioceiling, 88 pthread_mutexattr_setprioceiling); 89 __weak_reference(_thr_mutexattr_getrobust, pthread_mutexattr_getrobust); 90 __weak_reference(_thr_mutexattr_getrobust, _pthread_mutexattr_getrobust); 91 __weak_reference(_thr_mutexattr_setrobust, pthread_mutexattr_setrobust); 92 __weak_reference(_thr_mutexattr_setrobust, _pthread_mutexattr_setrobust); 93 94 int 95 _thr_mutexattr_init(pthread_mutexattr_t *attr) 96 { 97 int ret; 98 pthread_mutexattr_t pattr; 99 100 if ((pattr = (pthread_mutexattr_t) 101 malloc(sizeof(struct pthread_mutex_attr))) == NULL) { 102 ret = ENOMEM; 103 } else { 104 memcpy(pattr, &_pthread_mutexattr_default, 105 sizeof(struct pthread_mutex_attr)); 106 *attr = pattr; 107 ret = 0; 108 } 109 return (ret); 110 } 111 112 int 113 _pthread_mutexattr_setkind_np(pthread_mutexattr_t *attr, int kind) 114 { 115 int ret; 116 if (attr == NULL || *attr == NULL) { 117 errno = EINVAL; 118 ret = -1; 119 } else { 120 (*attr)->m_type = kind; 121 ret = 0; 122 } 123 return(ret); 124 } 125 126 int 127 _pthread_mutexattr_getkind_np(pthread_mutexattr_t attr) 128 { 129 int ret; 130 131 if (attr == NULL) { 132 errno = EINVAL; 133 ret = -1; 134 } else { 135 ret = attr->m_type; 136 } 137 return (ret); 138 } 139 140 int 141 _thr_mutexattr_settype(pthread_mutexattr_t *attr, int type) 142 { 143 int ret; 144 145 if (attr == NULL || *attr == NULL || type >= PTHREAD_MUTEX_TYPE_MAX) { 146 ret = EINVAL; 147 } else { 148 (*attr)->m_type = type; 149 ret = 0; 150 } 151 return (ret); 152 } 153 154 int 155 _pthread_mutexattr_gettype(const pthread_mutexattr_t * __restrict attr, 156 int * __restrict type) 157 { 158 int ret; 159 160 if (attr == NULL || *attr == NULL || (*attr)->m_type >= 161 PTHREAD_MUTEX_TYPE_MAX) { 162 ret = EINVAL; 163 } else { 164 *type = (*attr)->m_type; 165 ret = 0; 166 } 167 return (ret); 168 } 169 170 int 171 _thr_mutexattr_destroy(pthread_mutexattr_t *attr) 172 { 173 int ret; 174 if (attr == NULL || *attr == NULL) { 175 ret = EINVAL; 176 } else { 177 free(*attr); 178 *attr = NULL; 179 ret = 0; 180 } 181 return (ret); 182 } 183 184 int 185 _pthread_mutexattr_getpshared(const pthread_mutexattr_t *attr, 186 int *pshared) 187 { 188 189 if (attr == NULL || *attr == NULL) 190 return (EINVAL); 191 *pshared = (*attr)->m_pshared; 192 return (0); 193 } 194 195 int 196 _pthread_mutexattr_setpshared(pthread_mutexattr_t *attr, int pshared) 197 { 198 199 if (attr == NULL || *attr == NULL || 200 (pshared != PTHREAD_PROCESS_PRIVATE && 201 pshared != PTHREAD_PROCESS_SHARED)) 202 return (EINVAL); 203 (*attr)->m_pshared = pshared; 204 return (0); 205 } 206 207 int 208 _pthread_mutexattr_getprotocol(const pthread_mutexattr_t * __restrict mattr, 209 int * __restrict protocol) 210 { 211 int ret = 0; 212 213 if (mattr == NULL || *mattr == NULL) 214 ret = EINVAL; 215 else 216 *protocol = (*mattr)->m_protocol; 217 218 return (ret); 219 } 220 221 int 222 _pthread_mutexattr_setprotocol(pthread_mutexattr_t *mattr, int protocol) 223 { 224 int ret = 0; 225 226 if (mattr == NULL || *mattr == NULL || 227 protocol < PTHREAD_PRIO_NONE || protocol > PTHREAD_PRIO_PROTECT) 228 ret = EINVAL; 229 else { 230 (*mattr)->m_protocol = protocol; 231 (*mattr)->m_ceiling = THR_MAX_RR_PRIORITY; 232 } 233 return (ret); 234 } 235 236 int 237 _pthread_mutexattr_getprioceiling(const pthread_mutexattr_t * __restrict mattr, 238 int * __restrict prioceiling) 239 { 240 int ret = 0; 241 242 if (mattr == NULL || *mattr == NULL) 243 ret = EINVAL; 244 else if ((*mattr)->m_protocol != PTHREAD_PRIO_PROTECT) 245 ret = EINVAL; 246 else 247 *prioceiling = (*mattr)->m_ceiling; 248 249 return (ret); 250 } 251 252 int 253 _pthread_mutexattr_setprioceiling(pthread_mutexattr_t *mattr, int prioceiling) 254 { 255 int ret = 0; 256 257 if (mattr == NULL || *mattr == NULL) 258 ret = EINVAL; 259 else if ((*mattr)->m_protocol != PTHREAD_PRIO_PROTECT) 260 ret = EINVAL; 261 else 262 (*mattr)->m_ceiling = prioceiling; 263 264 return (ret); 265 } 266 267 int 268 _thr_mutexattr_getrobust(pthread_mutexattr_t *mattr, int *robust) 269 { 270 int ret; 271 272 if (mattr == NULL || *mattr == NULL) { 273 ret = EINVAL; 274 } else { 275 ret = 0; 276 *robust = (*mattr)->m_robust; 277 } 278 return (ret); 279 } 280 281 int 282 _thr_mutexattr_setrobust(pthread_mutexattr_t *mattr, int robust) 283 { 284 int ret; 285 286 if (mattr == NULL || *mattr == NULL) { 287 ret = EINVAL; 288 } else if (robust != PTHREAD_MUTEX_STALLED && 289 robust != PTHREAD_MUTEX_ROBUST) { 290 ret = EINVAL; 291 } else { 292 ret = 0; 293 (*mattr)->m_robust = robust; 294 } 295 return (ret); 296 } 297 298