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 * $DragonFly: src/lib/libthread_xu/thread/thr_attr.c,v 1.4 2006/03/12 11:32:21 davidxu Exp $ 97 */ 98 99 #include <machine/tls.h> 100 101 #include <errno.h> 102 #include <pthread.h> 103 #include <stdlib.h> 104 #include <string.h> 105 #include <pthread_np.h> 106 107 #include "thr_private.h" 108 109 __weak_reference(_pthread_attr_destroy, pthread_attr_destroy); 110 111 int 112 _pthread_attr_destroy(pthread_attr_t *attr) 113 { 114 int ret; 115 116 /* Check for invalid arguments: */ 117 if (attr == NULL || *attr == NULL) 118 /* Invalid argument: */ 119 ret = EINVAL; 120 else { 121 /* Free the memory allocated to the attribute object: */ 122 free(*attr); 123 124 /* 125 * Leave the attribute pointer NULL now that the memory 126 * has been freed: 127 */ 128 *attr = NULL; 129 ret = 0; 130 } 131 return(ret); 132 } 133 134 __weak_reference(_pthread_attr_get_np, pthread_attr_get_np); 135 136 int 137 _pthread_attr_get_np(pthread_t pid, pthread_attr_t *dst) 138 { 139 struct pthread *curthread; 140 struct pthread_attr attr; 141 int ret; 142 143 if (pid == NULL || dst == NULL || *dst == NULL) 144 return (EINVAL); 145 146 curthread = tls_get_curthread(); 147 if ((ret = _thr_ref_add(curthread, pid, /*include dead*/0)) != 0) 148 return (ret); 149 attr = pid->attr; 150 if (pid->tlflags & TLFLAGS_DETACHED) 151 attr.flags |= PTHREAD_DETACHED; 152 _thr_ref_delete(curthread, pid); 153 memcpy(*dst, &attr, sizeof(struct pthread_attr)); 154 155 return (0); 156 } 157 158 __weak_reference(_pthread_attr_getdetachstate, pthread_attr_getdetachstate); 159 160 int 161 _pthread_attr_getdetachstate(const pthread_attr_t *attr, int *detachstate) 162 { 163 int ret; 164 165 /* Check for invalid arguments: */ 166 if (attr == NULL || *attr == NULL || detachstate == NULL) 167 ret = EINVAL; 168 else { 169 /* Check if the detached flag is set: */ 170 if ((*attr)->flags & PTHREAD_DETACHED) 171 /* Return detached: */ 172 *detachstate = PTHREAD_CREATE_DETACHED; 173 else 174 /* Return joinable: */ 175 *detachstate = PTHREAD_CREATE_JOINABLE; 176 ret = 0; 177 } 178 return(ret); 179 } 180 181 __weak_reference(_pthread_attr_getguardsize, pthread_attr_getguardsize); 182 183 int 184 _pthread_attr_getguardsize(const pthread_attr_t *attr, size_t *guardsize) 185 { 186 int ret; 187 188 /* Check for invalid arguments: */ 189 if (attr == NULL || *attr == NULL || guardsize == NULL) 190 ret = EINVAL; 191 else { 192 /* Return the guard size: */ 193 *guardsize = (*attr)->guardsize_attr; 194 ret = 0; 195 } 196 return(ret); 197 } 198 199 __weak_reference(_pthread_attr_getinheritsched, pthread_attr_getinheritsched); 200 201 int 202 _pthread_attr_getinheritsched(const pthread_attr_t *attr, int *sched_inherit) 203 { 204 int ret = 0; 205 206 if ((attr == NULL) || (*attr == NULL)) 207 ret = EINVAL; 208 else 209 *sched_inherit = (*attr)->sched_inherit; 210 211 return(ret); 212 } 213 214 __weak_reference(_pthread_attr_getschedparam, pthread_attr_getschedparam); 215 216 int 217 _pthread_attr_getschedparam(const pthread_attr_t *attr, struct sched_param *param) 218 { 219 int ret = 0; 220 221 if ((attr == NULL) || (*attr == NULL) || (param == NULL)) 222 ret = EINVAL; 223 else 224 param->sched_priority = (*attr)->prio; 225 226 return(ret); 227 } 228 229 __weak_reference(_pthread_attr_getschedpolicy, pthread_attr_getschedpolicy); 230 231 int 232 _pthread_attr_getschedpolicy(const pthread_attr_t *attr, int *policy) 233 { 234 int ret = 0; 235 236 if ((attr == NULL) || (*attr == NULL) || (policy == NULL)) 237 ret = EINVAL; 238 else 239 *policy = (*attr)->sched_policy; 240 241 return(ret); 242 } 243 244 __weak_reference(_pthread_attr_getscope, pthread_attr_getscope); 245 246 int 247 _pthread_attr_getscope(const pthread_attr_t *attr, int *contentionscope) 248 { 249 int ret = 0; 250 251 if ((attr == NULL) || (*attr == NULL) || (contentionscope == NULL)) 252 /* Return an invalid argument: */ 253 ret = EINVAL; 254 255 else 256 *contentionscope = (*attr)->flags & PTHREAD_SCOPE_SYSTEM ? 257 PTHREAD_SCOPE_SYSTEM : PTHREAD_SCOPE_PROCESS; 258 259 return(ret); 260 } 261 262 __weak_reference(_pthread_attr_getstack, pthread_attr_getstack); 263 264 int 265 _pthread_attr_getstack(const pthread_attr_t * __restrict attr, 266 void ** __restrict stackaddr, 267 size_t * __restrict stacksize) 268 { 269 int ret; 270 271 /* Check for invalid arguments: */ 272 if (attr == NULL || *attr == NULL || stackaddr == NULL 273 || stacksize == NULL ) 274 ret = EINVAL; 275 else { 276 /* Return the stack address and size */ 277 *stackaddr = (*attr)->stackaddr_attr; 278 *stacksize = (*attr)->stacksize_attr; 279 ret = 0; 280 } 281 return(ret); 282 } 283 284 __weak_reference(_pthread_attr_getstackaddr, pthread_attr_getstackaddr); 285 286 int 287 _pthread_attr_getstackaddr(const pthread_attr_t *attr, void **stackaddr) 288 { 289 int ret; 290 291 /* Check for invalid arguments: */ 292 if (attr == NULL || *attr == NULL || stackaddr == NULL) 293 ret = EINVAL; 294 else { 295 /* Return the stack address: */ 296 *stackaddr = (*attr)->stackaddr_attr; 297 ret = 0; 298 } 299 return(ret); 300 } 301 302 __weak_reference(_pthread_attr_getstacksize, pthread_attr_getstacksize); 303 304 int 305 _pthread_attr_getstacksize(const pthread_attr_t *attr, size_t *stacksize) 306 { 307 int ret; 308 309 /* Check for invalid arguments: */ 310 if (attr == NULL || *attr == NULL || stacksize == NULL) 311 ret = EINVAL; 312 else { 313 /* Return the stack size: */ 314 *stacksize = (*attr)->stacksize_attr; 315 ret = 0; 316 } 317 return(ret); 318 } 319 320 __weak_reference(_pthread_attr_init, pthread_attr_init); 321 322 int 323 _pthread_attr_init(pthread_attr_t *attr) 324 { 325 int ret; 326 pthread_attr_t pattr; 327 328 _thr_check_init(); 329 330 /* Allocate memory for the attribute object: */ 331 if ((pattr = (pthread_attr_t) malloc(sizeof(struct pthread_attr))) == NULL) 332 /* Insufficient memory: */ 333 ret = ENOMEM; 334 else { 335 /* Initialise the attribute object with the defaults: */ 336 memcpy(pattr, &_pthread_attr_default, 337 sizeof(struct pthread_attr)); 338 339 /* Return a pointer to the attribute object: */ 340 *attr = pattr; 341 ret = 0; 342 } 343 return(ret); 344 } 345 346 __weak_reference(_pthread_attr_setcreatesuspend_np, pthread_attr_setcreatesuspend_np); 347 348 int 349 _pthread_attr_setcreatesuspend_np(pthread_attr_t *attr) 350 { 351 int ret; 352 353 if (attr == NULL || *attr == NULL) { 354 ret = EINVAL; 355 } else { 356 (*attr)->suspend = THR_CREATE_SUSPENDED; 357 ret = 0; 358 } 359 return(ret); 360 } 361 362 __weak_reference(_pthread_attr_setdetachstate, pthread_attr_setdetachstate); 363 364 int 365 _pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate) 366 { 367 int ret; 368 369 /* Check for invalid arguments: */ 370 if (attr == NULL || *attr == NULL || 371 (detachstate != PTHREAD_CREATE_DETACHED && 372 detachstate != PTHREAD_CREATE_JOINABLE)) 373 ret = EINVAL; 374 else { 375 /* Check if detached state: */ 376 if (detachstate == PTHREAD_CREATE_DETACHED) 377 /* Set the detached flag: */ 378 (*attr)->flags |= PTHREAD_DETACHED; 379 else 380 /* Reset the detached flag: */ 381 (*attr)->flags &= ~PTHREAD_DETACHED; 382 ret = 0; 383 } 384 return(ret); 385 } 386 387 __weak_reference(_pthread_attr_setguardsize, pthread_attr_setguardsize); 388 389 int 390 _pthread_attr_setguardsize(pthread_attr_t *attr, size_t guardsize) 391 { 392 int ret; 393 394 /* Check for invalid arguments. */ 395 if (attr == NULL || *attr == NULL) 396 ret = EINVAL; 397 else { 398 /* Save the stack size. */ 399 (*attr)->guardsize_attr = guardsize; 400 ret = 0; 401 } 402 return(ret); 403 } 404 405 __weak_reference(_pthread_attr_setinheritsched, pthread_attr_setinheritsched); 406 407 int 408 _pthread_attr_setinheritsched(pthread_attr_t *attr, int sched_inherit) 409 { 410 int ret = 0; 411 412 if ((attr == NULL) || (*attr == NULL)) 413 ret = EINVAL; 414 else if (sched_inherit != PTHREAD_INHERIT_SCHED && 415 sched_inherit != PTHREAD_EXPLICIT_SCHED) 416 ret = ENOTSUP; 417 else 418 (*attr)->sched_inherit = sched_inherit; 419 420 return(ret); 421 } 422 423 __weak_reference(_pthread_attr_setschedparam, pthread_attr_setschedparam); 424 425 int 426 _pthread_attr_setschedparam(pthread_attr_t *attr, const struct sched_param *param) 427 { 428 int ret = 0; 429 430 if ((attr == NULL) || (*attr == NULL)) 431 ret = EINVAL; 432 else if (param == NULL) { 433 ret = ENOTSUP; 434 } else if ((param->sched_priority < THR_MIN_PRIORITY) || 435 (param->sched_priority > THR_MAX_PRIORITY)) { 436 /* Return an unsupported value error. */ 437 ret = ENOTSUP; 438 } else 439 (*attr)->prio = param->sched_priority; 440 441 return(ret); 442 } 443 444 __weak_reference(_pthread_attr_setschedpolicy, pthread_attr_setschedpolicy); 445 446 int 447 _pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy) 448 { 449 int ret = 0; 450 451 if ((attr == NULL) || (*attr == NULL)) 452 ret = EINVAL; 453 else if ((policy < SCHED_FIFO) || (policy > SCHED_RR)) { 454 ret = ENOTSUP; 455 } else 456 (*attr)->sched_policy = policy; 457 458 return(ret); 459 } 460 461 __weak_reference(_pthread_attr_setscope, pthread_attr_setscope); 462 463 int 464 _pthread_attr_setscope(pthread_attr_t *attr, int contentionscope) 465 { 466 int ret = 0; 467 468 if ((attr == NULL) || (*attr == NULL)) { 469 /* Return an invalid argument: */ 470 ret = EINVAL; 471 } else if ((contentionscope != PTHREAD_SCOPE_PROCESS) && 472 (contentionscope != PTHREAD_SCOPE_SYSTEM)) { 473 ret = EINVAL; 474 } else if (contentionscope == PTHREAD_SCOPE_SYSTEM) { 475 (*attr)->flags |= contentionscope; 476 } else { 477 (*attr)->flags &= ~PTHREAD_SCOPE_SYSTEM; 478 } 479 return (ret); 480 } 481 482 __weak_reference(_pthread_attr_setstack, pthread_attr_setstack); 483 484 int 485 _pthread_attr_setstack(pthread_attr_t *attr, void *stackaddr, 486 size_t stacksize) 487 { 488 int ret; 489 490 /* Check for invalid arguments: */ 491 if (attr == NULL || *attr == NULL || stackaddr == NULL 492 || stacksize < PTHREAD_STACK_MIN) 493 ret = EINVAL; 494 else { 495 /* Save the stack address and stack size */ 496 (*attr)->stackaddr_attr = stackaddr; 497 (*attr)->stacksize_attr = stacksize; 498 ret = 0; 499 } 500 return(ret); 501 } 502 503 __weak_reference(_pthread_attr_setstackaddr, pthread_attr_setstackaddr); 504 505 int 506 _pthread_attr_setstackaddr(pthread_attr_t *attr, void *stackaddr) 507 { 508 int ret; 509 510 /* Check for invalid arguments: */ 511 if (attr == NULL || *attr == NULL || stackaddr == NULL) 512 ret = EINVAL; 513 else { 514 /* Save the stack address: */ 515 (*attr)->stackaddr_attr = stackaddr; 516 ret = 0; 517 } 518 return(ret); 519 } 520 521 __weak_reference(_pthread_attr_setstacksize, pthread_attr_setstacksize); 522 523 int 524 _pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize) 525 { 526 int ret; 527 528 /* Check for invalid arguments: */ 529 if (attr == NULL || *attr == NULL || stacksize < PTHREAD_STACK_MIN) 530 ret = EINVAL; 531 else { 532 /* Save the stack size: */ 533 (*attr)->stacksize_attr = stacksize; 534 ret = 0; 535 } 536 return(ret); 537 } 538