1 /* 2 * Copyright (c) 2003 Craig Rodrigues <rodrigc@attbi.com>. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by Craig Rodrigues. 16 * 4. Neither the name of the author nor the names of any co-contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY CRAIG RODRIGUES AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 * 32 */ 33 34 /* 35 * Copyright (c) 1998 Daniel Eischen <eischen@vigrid.com>. 36 * Copyright (C) 2001 Jason Evans <jasone@freebsd.org>. 37 * Copyright (c) 2002,2003 Alexey Zelkin <phantom@FreeBSD.org> 38 * All rights reserved. 39 * 40 * Redistribution and use in source and binary forms, with or without 41 * modification, are permitted provided that the following conditions 42 * are met: 43 * 1. Redistributions of source code must retain the above copyright 44 * notice(s), this list of conditions and the following disclaimer 45 * unmodified other than the allowable addition of one or more 46 * copyright notices. 47 * 2. Redistributions in binary form must reproduce the above copyright 48 * notice(s), this list of conditions and the following disclaimer in 49 * the documentation and/or other materials provided with the 50 * distribution. 51 * 52 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY 53 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 55 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE 56 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 57 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 58 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 59 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 60 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 61 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 62 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 63 */ 64 65 /* 66 * Copyright (c) 1996 John Birrell <jb@cimlogic.com.au>. 67 * All rights reserved. 68 * 69 * Redistribution and use in source and binary forms, with or without 70 * modification, are permitted provided that the following conditions 71 * are met: 72 * 1. Redistributions of source code must retain the above copyright 73 * notice, this list of conditions and the following disclaimer. 74 * 2. Redistributions in binary form must reproduce the above copyright 75 * notice, this list of conditions and the following disclaimer in the 76 * documentation and/or other materials provided with the distribution. 77 * 3. All advertising materials mentioning features or use of this software 78 * must display the following acknowledgement: 79 * This product includes software developed by John Birrell. 80 * 4. Neither the name of the author nor the names of any co-contributors 81 * may be used to endorse or promote products derived from this software 82 * without specific prior written permission. 83 * 84 * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND 85 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 86 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 87 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 88 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 89 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 90 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 91 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 92 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 93 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 94 * SUCH DAMAGE. 95 * 96 * $FreeBSD$ 97 */ 98 99 #include <errno.h> 100 #include <pthread.h> 101 #include <stdlib.h> 102 #include <string.h> 103 #include <pthread_np.h> 104 105 #include "thr_private.h" 106 107 __weak_reference(_pthread_attr_destroy, pthread_attr_destroy); 108 109 int 110 _pthread_attr_destroy(pthread_attr_t *attr) 111 { 112 int ret; 113 114 /* Check for invalid arguments: */ 115 if (attr == NULL || *attr == NULL) 116 /* Invalid argument: */ 117 ret = EINVAL; 118 else { 119 /* Free the memory allocated to the attribute object: */ 120 free(*attr); 121 122 /* 123 * Leave the attribute pointer NULL now that the memory 124 * has been freed: 125 */ 126 *attr = NULL; 127 ret = 0; 128 } 129 return(ret); 130 } 131 132 __weak_reference(_pthread_attr_get_np, pthread_attr_get_np); 133 134 int 135 _pthread_attr_get_np(pthread_t pid, pthread_attr_t *dst) 136 { 137 struct pthread *curthread; 138 struct pthread_attr attr; 139 int ret; 140 141 if (pid == NULL || dst == NULL || *dst == NULL) 142 return (EINVAL); 143 144 curthread = _get_curthread(); 145 if ((ret = _thr_ref_add(curthread, pid, /*include dead*/0)) != 0) 146 return (ret); 147 attr = pid->attr; 148 _thr_ref_delete(curthread, pid); 149 memcpy(*dst, &attr, sizeof(struct pthread_attr)); 150 151 return (0); 152 } 153 154 __weak_reference(_pthread_attr_getdetachstate, pthread_attr_getdetachstate); 155 156 int 157 _pthread_attr_getdetachstate(const pthread_attr_t *attr, int *detachstate) 158 { 159 int ret; 160 161 /* Check for invalid arguments: */ 162 if (attr == NULL || *attr == NULL || detachstate == NULL) 163 ret = EINVAL; 164 else { 165 /* Check if the detached flag is set: */ 166 if ((*attr)->flags & PTHREAD_DETACHED) 167 /* Return detached: */ 168 *detachstate = PTHREAD_CREATE_DETACHED; 169 else 170 /* Return joinable: */ 171 *detachstate = PTHREAD_CREATE_JOINABLE; 172 ret = 0; 173 } 174 return(ret); 175 } 176 177 __weak_reference(_pthread_attr_getguardsize, pthread_attr_getguardsize); 178 179 int 180 _pthread_attr_getguardsize(const pthread_attr_t *attr, size_t *guardsize) 181 { 182 int ret; 183 184 /* Check for invalid arguments: */ 185 if (attr == NULL || *attr == NULL || guardsize == NULL) 186 ret = EINVAL; 187 else { 188 /* Return the guard size: */ 189 *guardsize = (*attr)->guardsize_attr; 190 ret = 0; 191 } 192 return(ret); 193 } 194 195 __weak_reference(_pthread_attr_getinheritsched, pthread_attr_getinheritsched); 196 197 int 198 _pthread_attr_getinheritsched(const pthread_attr_t *attr, int *sched_inherit) 199 { 200 int ret = 0; 201 202 if ((attr == NULL) || (*attr == NULL)) 203 ret = EINVAL; 204 else 205 *sched_inherit = (*attr)->sched_inherit; 206 207 return(ret); 208 } 209 210 __weak_reference(_pthread_attr_getschedparam, pthread_attr_getschedparam); 211 212 int 213 _pthread_attr_getschedparam(const pthread_attr_t *attr, struct sched_param *param) 214 { 215 int ret = 0; 216 217 if ((attr == NULL) || (*attr == NULL) || (param == NULL)) 218 ret = EINVAL; 219 else 220 param->sched_priority = (*attr)->prio; 221 222 return(ret); 223 } 224 225 __weak_reference(_pthread_attr_getschedpolicy, pthread_attr_getschedpolicy); 226 227 int 228 _pthread_attr_getschedpolicy(const pthread_attr_t *attr, int *policy) 229 { 230 int ret = 0; 231 232 if ((attr == NULL) || (*attr == NULL) || (policy == NULL)) 233 ret = EINVAL; 234 else 235 *policy = (*attr)->sched_policy; 236 237 return(ret); 238 } 239 240 __weak_reference(_pthread_attr_getscope, pthread_attr_getscope); 241 242 int 243 _pthread_attr_getscope(const pthread_attr_t *attr, int *contentionscope) 244 { 245 int ret = 0; 246 247 if ((attr == NULL) || (*attr == NULL) || (contentionscope == NULL)) 248 /* Return an invalid argument: */ 249 ret = EINVAL; 250 251 else 252 *contentionscope = (*attr)->flags & PTHREAD_SCOPE_SYSTEM ? 253 PTHREAD_SCOPE_SYSTEM : PTHREAD_SCOPE_PROCESS; 254 255 return(ret); 256 } 257 258 __weak_reference(_pthread_attr_getstack, pthread_attr_getstack); 259 260 int 261 _pthread_attr_getstack(const pthread_attr_t * __restrict attr, 262 void ** __restrict stackaddr, 263 size_t * __restrict stacksize) 264 { 265 int ret; 266 267 /* Check for invalid arguments: */ 268 if (attr == NULL || *attr == NULL || stackaddr == NULL 269 || stacksize == NULL ) 270 ret = EINVAL; 271 else { 272 /* Return the stack address and size */ 273 *stackaddr = (*attr)->stackaddr_attr; 274 *stacksize = (*attr)->stacksize_attr; 275 ret = 0; 276 } 277 return(ret); 278 } 279 280 __weak_reference(_pthread_attr_getstackaddr, pthread_attr_getstackaddr); 281 282 int 283 _pthread_attr_getstackaddr(const pthread_attr_t *attr, void **stackaddr) 284 { 285 int ret; 286 287 /* Check for invalid arguments: */ 288 if (attr == NULL || *attr == NULL || stackaddr == NULL) 289 ret = EINVAL; 290 else { 291 /* Return the stack address: */ 292 *stackaddr = (*attr)->stackaddr_attr; 293 ret = 0; 294 } 295 return(ret); 296 } 297 298 __weak_reference(_pthread_attr_getstacksize, pthread_attr_getstacksize); 299 300 int 301 _pthread_attr_getstacksize(const pthread_attr_t *attr, size_t *stacksize) 302 { 303 int ret; 304 305 /* Check for invalid arguments: */ 306 if (attr == NULL || *attr == NULL || stacksize == NULL) 307 ret = EINVAL; 308 else { 309 /* Return the stack size: */ 310 *stacksize = (*attr)->stacksize_attr; 311 ret = 0; 312 } 313 return(ret); 314 } 315 316 __weak_reference(_pthread_attr_init, pthread_attr_init); 317 318 int 319 _pthread_attr_init(pthread_attr_t *attr) 320 { 321 int ret; 322 pthread_attr_t pattr; 323 324 _thr_check_init(); 325 326 /* Allocate memory for the attribute object: */ 327 if ((pattr = (pthread_attr_t) malloc(sizeof(struct pthread_attr))) == NULL) 328 /* Insufficient memory: */ 329 ret = ENOMEM; 330 else { 331 /* Initialise the attribute object with the defaults: */ 332 memcpy(pattr, &_pthread_attr_default, sizeof(struct pthread_attr)); 333 334 /* Return a pointer to the attribute object: */ 335 *attr = pattr; 336 ret = 0; 337 } 338 return(ret); 339 } 340 341 __weak_reference(_pthread_attr_setcreatesuspend_np, pthread_attr_setcreatesuspend_np); 342 343 int 344 _pthread_attr_setcreatesuspend_np(pthread_attr_t *attr) 345 { 346 int ret; 347 348 if (attr == NULL || *attr == NULL) { 349 ret = EINVAL; 350 } else { 351 (*attr)->suspend = THR_CREATE_SUSPENDED; 352 ret = 0; 353 } 354 return(ret); 355 } 356 357 __weak_reference(_pthread_attr_setdetachstate, pthread_attr_setdetachstate); 358 359 int 360 _pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate) 361 { 362 int ret; 363 364 /* Check for invalid arguments: */ 365 if (attr == NULL || *attr == NULL || 366 (detachstate != PTHREAD_CREATE_DETACHED && 367 detachstate != PTHREAD_CREATE_JOINABLE)) 368 ret = EINVAL; 369 else { 370 /* Check if detached state: */ 371 if (detachstate == PTHREAD_CREATE_DETACHED) 372 /* Set the detached flag: */ 373 (*attr)->flags |= PTHREAD_DETACHED; 374 else 375 /* Reset the detached flag: */ 376 (*attr)->flags &= ~PTHREAD_DETACHED; 377 ret = 0; 378 } 379 return(ret); 380 } 381 382 __weak_reference(_pthread_attr_setguardsize, pthread_attr_setguardsize); 383 384 int 385 _pthread_attr_setguardsize(pthread_attr_t *attr, size_t guardsize) 386 { 387 int ret; 388 389 /* Check for invalid arguments. */ 390 if (attr == NULL || *attr == NULL) 391 ret = EINVAL; 392 else { 393 /* Save the stack size. */ 394 (*attr)->guardsize_attr = guardsize; 395 ret = 0; 396 } 397 return(ret); 398 } 399 400 __weak_reference(_pthread_attr_setinheritsched, pthread_attr_setinheritsched); 401 402 int 403 _pthread_attr_setinheritsched(pthread_attr_t *attr, int sched_inherit) 404 { 405 int ret = 0; 406 407 if ((attr == NULL) || (*attr == NULL)) 408 ret = EINVAL; 409 else if (sched_inherit != PTHREAD_INHERIT_SCHED && 410 sched_inherit != PTHREAD_EXPLICIT_SCHED) 411 ret = ENOTSUP; 412 else 413 (*attr)->sched_inherit = sched_inherit; 414 415 return(ret); 416 } 417 418 __weak_reference(_pthread_attr_setschedparam, pthread_attr_setschedparam); 419 420 int 421 _pthread_attr_setschedparam(pthread_attr_t *attr, const struct sched_param *param) 422 { 423 int ret = 0; 424 425 if ((attr == NULL) || (*attr == NULL)) 426 ret = EINVAL; 427 else if (param == NULL) { 428 ret = ENOTSUP; 429 } else if ((param->sched_priority < THR_MIN_PRIORITY) || 430 (param->sched_priority > THR_MAX_PRIORITY)) { 431 /* Return an unsupported value error. */ 432 ret = ENOTSUP; 433 } else 434 (*attr)->prio = param->sched_priority; 435 436 return(ret); 437 } 438 439 __weak_reference(_pthread_attr_setschedpolicy, pthread_attr_setschedpolicy); 440 441 int 442 _pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy) 443 { 444 int ret = 0; 445 446 if ((attr == NULL) || (*attr == NULL)) 447 ret = EINVAL; 448 else if ((policy < SCHED_FIFO) || (policy > SCHED_RR)) { 449 ret = ENOTSUP; 450 } else 451 (*attr)->sched_policy = policy; 452 453 return(ret); 454 } 455 456 __weak_reference(_pthread_attr_setscope, pthread_attr_setscope); 457 458 int 459 _pthread_attr_setscope(pthread_attr_t *attr, int contentionscope) 460 { 461 int ret = 0; 462 463 if ((attr == NULL) || (*attr == NULL)) { 464 /* Return an invalid argument: */ 465 ret = EINVAL; 466 } else if ((contentionscope != PTHREAD_SCOPE_PROCESS) && 467 (contentionscope != PTHREAD_SCOPE_SYSTEM)) { 468 ret = EINVAL; 469 } else if (contentionscope == PTHREAD_SCOPE_SYSTEM) { 470 (*attr)->flags |= contentionscope; 471 } else { 472 (*attr)->flags &= ~PTHREAD_SCOPE_SYSTEM; 473 } 474 return (ret); 475 } 476 477 __weak_reference(_pthread_attr_setstack, pthread_attr_setstack); 478 479 int 480 _pthread_attr_setstack(pthread_attr_t *attr, void *stackaddr, 481 size_t stacksize) 482 { 483 int ret; 484 485 /* Check for invalid arguments: */ 486 if (attr == NULL || *attr == NULL || stackaddr == NULL 487 || stacksize < PTHREAD_STACK_MIN) 488 ret = EINVAL; 489 else { 490 /* Save the stack address and stack size */ 491 (*attr)->stackaddr_attr = stackaddr; 492 (*attr)->stacksize_attr = stacksize; 493 ret = 0; 494 } 495 return(ret); 496 } 497 498 __weak_reference(_pthread_attr_setstackaddr, pthread_attr_setstackaddr); 499 500 int 501 _pthread_attr_setstackaddr(pthread_attr_t *attr, void *stackaddr) 502 { 503 int ret; 504 505 /* Check for invalid arguments: */ 506 if (attr == NULL || *attr == NULL || stackaddr == NULL) 507 ret = EINVAL; 508 else { 509 /* Save the stack address: */ 510 (*attr)->stackaddr_attr = stackaddr; 511 ret = 0; 512 } 513 return(ret); 514 } 515 516 __weak_reference(_pthread_attr_setstacksize, pthread_attr_setstacksize); 517 518 int 519 _pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize) 520 { 521 int ret; 522 523 /* Check for invalid arguments: */ 524 if (attr == NULL || *attr == NULL || stacksize < PTHREAD_STACK_MIN) 525 ret = EINVAL; 526 else { 527 /* Save the stack size: */ 528 (*attr)->stacksize_attr = stacksize; 529 ret = 0; 530 } 531 return(ret); 532 } 533